18 #ifdef MRPT_OS_WINDOWS 23 #include <sys/utime.h> 30 #include <sys/select.h> 41 #include <sys/types.h> 44 #include <sys/sysctl.h> 45 #include <mach/mach_init.h> 46 #include <mach/thread_act.h> 59 #ifdef MRPT_OS_WINDOWS 65 int timeLeft_ms = time_ms - (int)(tictac.
Tac()*1000);
66 while ( timeLeft_ms>0 )
68 usleep( timeLeft_ms * 1000 );
69 timeLeft_ms = time_ms - (int)(tictac.
Tac()*1000);
85 #ifdef MRPT_OS_WINDOWS 86 return GetCurrentThreadId();
87 #elif defined(MRPT_OS_APPLE) 88 return reinterpret_cast<unsigned long>(pthread_self());
90 return pthread_self();
96 #ifdef MRPT_OS_WINDOWS 98 SetThreadPriority(GetCurrentThread(), priority);
100 const pthread_t tid =
102 reinterpret_cast<long unsigned int>(pthread_self());
108 struct sched_param
param;
110 if (0 != (ret = pthread_getschedparam(tid, &policy, &
param))) {
111 cerr <<
"[mrpt::system::changeThreadPriority] Warning: Failed call to pthread_getschedparam (error: `" << strerror(ret) <<
"`)" << endl;
116 int min_prio = sched_get_priority_min(policy), max_prio = sched_get_priority_max(policy);
117 if (min_prio<0) min_prio = 1;
118 if (max_prio<0) max_prio = 99;
124 case tpLower: prio = (max_prio + 3 * min_prio) / 4;
break;
125 case tpLow: prio = (max_prio + 2 * min_prio) / 3;
break;
126 case tpNormal: prio = (max_prio + min_prio) / 2;
break;
127 case tpHigh: prio = (2 * max_prio + min_prio) / 3;
break;
128 case tpHigher: prio = (3 * max_prio + min_prio) / 4;
break;
132 param.sched_priority = prio;
133 if (0 != (ret = pthread_setschedparam(tid, policy, &
param))) {
134 cerr <<
"[mrpt::system::changeThreadPriority] Warning: Failed call to pthread_setschedparam (error: `" << strerror(ret) <<
"`)" << endl;
146 #ifdef MRPT_OS_WINDOWS 150 case ppIdle: dwPri = IDLE_PRIORITY_CLASS;
break;
151 case ppNormal: dwPri = NORMAL_PRIORITY_CLASS;
break;
152 case ppHigh: dwPri = HIGH_PRIORITY_CLASS;
break;
153 case ppVeryHigh: dwPri= REALTIME_PRIORITY_CLASS;
break;
157 SetPriorityClass( GetCurrentProcess(), dwPri );
162 case ppIdle: nice_val =+19;
break;
164 case ppHigh: nice_val =-10;
break;
170 const int ret = nice(nice_val);
171 if (ret==-1 && errno==EPERM) {
172 std::cerr <<
"[mrpt::system::changeCurrentProcessPriority] Error calling nice(): Not enough permissions.\n";
181 time_t &creationTime,
187 #ifdef MRPT_OS_WINDOWS 188 FILETIME timCreat,timExit, timKernel, timUser;
193 #if !defined(HAVE_OPENTHREAD) // defined(_MSC_VER) && (_MSC_VER<1300) 198 threadHandle = OpenThread( READ_CONTROL | THREAD_QUERY_INFORMATION,
FALSE, GetCurrentThreadId() );
199 if (!threadHandle)
THROW_EXCEPTION(
"Cannot open the thread with the given 'threadId'");
202 if (!GetThreadTimes( threadHandle , &timCreat, &timExit, &timKernel, &timUser ))
204 CloseHandle(threadHandle);
208 #if defined(HAVE_OPENTHREAD) // _MSC_VER) && (_MSC_VER>=1300) 210 CloseHandle(threadHandle);
215 t = (((
uint64_t)timCreat.dwHighDateTime) << 32) | timCreat.dwLowDateTime;
216 creationTime = (
t - 116444736000000000ULL)/10000000;
218 t = (((
uint64_t)timExit.dwHighDateTime) << 32) | timExit.dwLowDateTime;
219 exitTime = (
t - 116444736000000000ULL)/10000000;
222 int64_t t1 = (((
uint64_t)timKernel.dwHighDateTime) << 32) | timKernel.dwLowDateTime;
223 int64_t t2 = (((
uint64_t)timUser.dwHighDateTime) << 32) | timUser.dwLowDateTime;
225 cpuTime = ((double)(t1+t2)) * 100e-9;
239 #if MRPT_WORD_SIZE==64 240 pid_t
id = (
long int)syscall(186);
241 #elif MRPT_WORD_SIZE==32 242 pid_t
id = (
long int)syscall(224);
244 #error MRPT_WORD_SIZE must be 32 or 64. 249 long unsigned tms_utime=0, tms_stime=0;
250 ifstream is(
format(
"/proc/self/task/%i/stat",
id).c_str() );
257 size_t idx =
s.find(
")");
259 if (idx!=string::npos)
264 if (tokens.size()>=13)
266 sscanf(tokens[11].c_str(),
"%lu" ,&tms_utime);
267 sscanf(tokens[12].c_str(),
"%lu", &tms_stime);
273 double clockTicksPerSecond = (double)sysconf(_SC_CLK_TCK);
274 if (clockTicksPerSecond>0)
275 cpuTime = (tms_utime + tms_stime) / clockTicksPerSecond;
279 thread_basic_info
info;
280 mach_msg_type_number_t
count = THREAD_BASIC_INFO_COUNT;
281 if(thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&
info, &
count)==0)
283 double utime =
info.user_time.seconds +
info.user_time.microseconds * 1e-6;
284 double stime =
info.system_time.seconds +
info.system_time.microseconds * 1e-6;
285 cpuTime = utime + stime;
297 #ifdef MRPT_OS_WINDOWS 299 PROCESS_INFORMATION PI;
301 memset(&SI,0,
sizeof(STARTUPINFOA) );
302 SI.cb =
sizeof(STARTUPINFOA);
304 if (CreateProcessA( NULL, (LPSTR)command.c_str(), NULL, NULL,
true, 0, NULL, NULL, &SI, &PI) )
307 WaitForSingleObject( PI.hProcess, INFINITE );
313 DWORD e = GetLastError();
315 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,0,e,0,str,
sizeof(str), NULL);
318 std::cerr <<
"[launchProcess] Couldn't spawn process. Error msg: " << str << std::endl;
322 return 0== ::system(command.c_str());
329 static unsigned int ret = 0;
333 #ifdef MRPT_OS_WINDOWS 336 ret=si.dwNumberOfProcessors;
338 #elif defined(MRPT_OS_APPLE) 339 size_t len=
sizeof(int);
340 if(sysctlbyname(
"hw.logicalcpu", &ret, &
len, NULL, 0) != 0)
345 f.open(
"/proc/cpuinfo");
350 unsigned int nProc = 0;
351 while (!f.fail() && !f.eof())
354 if (!f.fail() && !f.eof())
355 if (lin.find(
"processor")!=std::string::npos)
358 ret = nProc ? nProc : 1;
369 #ifdef MRPT_OS_WINDOWS 381 if (threadHandle.isClear())
return;
383 #ifdef MRPT_OS_WINDOWS 384 TerminateThread(threadHandle.m_thread->native_handle(), DWORD(-1));
385 #elif defined(MRPT_OS_APPLE) 386 pthread_cancel(reinterpret_cast<pthread_t>(threadHandle.m_thread->native_handle()));
388 pthread_cancel(threadHandle.m_thread->native_handle());
390 threadHandle.m_thread->join();
GLuint GLuint GLsizei count
TThreadPriority
The type for cross-platform thread priorities.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
unsigned long BASE_IMPEXP getCurrentThreadId() MRPT_NO_THROWS
Returns the ID of the current thread.
void BASE_IMPEXP changeCurrentProcessPriority(TProcessPriority priority)
Change the priority of the given process (it applies to all the threads, plus independent modifiers f...
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
#define THROW_EXCEPTION(msg)
unsigned int BASE_IMPEXP getNumberOfProcessors()
Return the number of processors ("cores"), or 1 if it cannot be determined.
#define MRPT_NO_THROWS
C++11 noexcept: Used after member declarations.
TProcessPriority
The type for cross-platform process (application) priorities.
void Tic()
Starts the stopwatch.
bool BASE_IMPEXP launchProcess(const std::string &command)
Executes the given command (which may contain a program + arguments), and waits until it finishes...
void BASE_IMPEXP getCurrentThreadTimes(time_t &creationTime, time_t &exitTime, double &cpuTime)
Returns the creation and exit times of the current thread and its CPU time consumed.
std::vector< std::string > vector_string
A type for passing a vector of strings.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
This class implements a high-performance stopwatch.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
void BASE_IMPEXP joinThread(TThreadHandle &threadHandle)
Waits until the given thread ends.
unsigned __int64 uint64_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::shared_ptr< std::thread > m_thread
double Tac()
Stops the stopwatch.
void BASE_IMPEXP terminateThread(TThreadHandle &threadHandle) MRPT_NO_THROWS
Terminate a thread, giving it no choice to delete objects, etc (use only as a last resource) ...
void BASE_IMPEXP changeCurrentThreadPriority(TThreadPriority priority)
Change the priority of the current thread - for Windows, see also changeCurrentProcessPriority() ...
void BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.
void BASE_IMPEXP exitThread() MRPT_NO_THROWS
Explicit close of the current (running) thread.