12 static const long TICK_TO_100NSEC = 100000;
13 #endif // not __APPLE__
45 # define strcasecmp _stricmp
46 # define strncasecmp _strnicmp
51 # define getpid _getpid
57 typedef long( WINAPI* __NtQueryInformationProcess )(
59 void*
ProcessHandle,
long ProcessInformationClass,
void* ProcessInformation,
60 unsigned long ProcessInformationLength,
unsigned long* ReturnLength );
61 __NtQueryInformationProcess NtQueryInformationProcess;
63 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
72 # include <sys/signal.h>
73 # include <sys/syscall.h>
74 # include <sys/times.h>
75 # include <sys/types.h>
78 # include <sys/procfs.h>
81 # include <sys/resource.h>
82 # include <sys/time.h>
292 static long pg_size = sysconf( _SC_PAGESIZE );
300 std::ostringstream ost;
302 ost <<
"/proc/" << pid <<
"/stat";
303 std::string fname = ost.str();
304 if ( (
fd = open( fname.c_str(), O_RDONLY ) ) < 0 ) {
305 std::cerr <<
"Failed to open " << ost.str() << std::endl;
309 lseek(
fd, 0, SEEK_SET );
310 if ( ( cnt =
read(
fd, buf,
sizeof( buf ) - 1 ) ) < 0 ) {
311 std::cout <<
"LINUX Read of Proc file failed:" << std::endl;
323 "%d %400s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu "
324 "%lu %lu %lu %lu %lu %lu %lu",
340 static inline long processID(
long pid ) {
return ( pid > 0 ) ? pid : ( ::getpid() ); }
341 #endif // not __APPLE__
349 if ( pid != ::getpid() ) {
351 m_handle = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid );
354 m_handle =
reinterpret_cast<void*
>(
static_cast<long>( ::getpid() ) );
365 if ( m_needRelease ) {
367 ::CloseHandle( m_handle );
376 static bool first =
true;
379 void* mh = ::LoadLibrary(
"NTDll.dll" );
381 NtApi::NtQueryInformationProcess =
382 ( NtApi::__NtQueryInformationProcess )::GetProcAddress( (HINSTANCE)mh,
"NtQueryInformationProcess" );
391 if ( info == 0 )
return 0;
395 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
398 status = ( status == 0 ) ? 1 : 0;
399 #elif defined( _WIN32 ) // Windows 95,98...
400 #elif defined( __linux )
404 getrusage( RUSAGE_SELF, &
usage );
419 if ( info == 0 )
return 0;
423 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
427 status = ( status == 0 ) ? 1 : 0;
428 #elif defined( _WIN32 ) // Windows 95,98...
429 #elif defined( __linux ) // Linux
434 getrlimit( RLIMIT_DATA, &lim );
435 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
440 getrlimit( RLIMIT_STACK, &lim );
441 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
451 #elif defined( __APPLE__ )
463 if ( info == 0 )
return 0;
468 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
470 status = NtApi::NtQueryInformationProcess(
h.handle(),
ProcessPriorityBoost, info,
sizeof(
long ), 0 );
471 #elif defined( _WIN32 ) // Windows 95,98...
475 if ( pid > 0 ) status = 0;
479 status = ( status == 0 ) ? 1 : 0;
490 if ( info == 0 )
return 0;
494 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
497 status = ( status == 0 ) ? 1 : 0;
498 #elif defined( _WIN32 ) // Windows 95,98...
499 #elif defined( __linux ) // Linux
500 const ssize_t bufsize = 1024;
502 pid = processID( pid );
503 sprintf( buf,
"/proc/%ld/statm", pid );
504 long size, resident, share, trs, lrs, drs, dt;
505 int fd = open( buf, O_RDONLY );
506 ssize_t nread =
read(
fd, buf, bufsize );
508 if ( nread < bufsize && nread >= 0 ) buf[nread] =
'\0';
509 fd = sscanf( buf,
"%ld %ld %ld %ld %ld %ld %ld", &
size, &resident, &share, &trs, &drs, &lrs, &dt );
523 #elif defined( __APPLE__ )
533 if ( info == 0 )
return 0;
537 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
540 status = ( status == 0 ) ? 1 : 0;
541 #elif defined( _WIN32 ) // Windows 95,98...
542 #elif defined( __linux ) // Linux
546 if ( pid > 0 && pid != ::getpid() )
return 0;
549 getrlimit( RLIMIT_DATA, &lim );
550 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
553 getrlimit( RLIMIT_STACK, &lim );
554 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
558 getrlimit( RLIMIT_RSS, &lim );
559 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
562 getrlimit( RLIMIT_AS, &lim );
563 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
566 getrlimit( RLIMIT_CPU, &lim );
567 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
569 #elif defined( __APPLE__ )
579 if ( info == 0 )
return 0;
583 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
587 status = ( status == 0 ) ? 1 : 0;
588 #elif defined( _WIN32 ) // Windows 95,98...
589 #elif defined( __linux ) // Linux
591 pid = processID( pid );
611 if ( info == 0 )
return 0;
615 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
618 status = ( status == 0 ) ? 1 : 0;
619 #elif defined( _WIN32 ) // Windows 95,98...
620 #elif defined( __linux ) // Linux
621 static long long prc_start = 0;
622 bool myself = pid <= 0 || pid == ::getpid();
623 if ( myself && prc_start == 0 ) {
628 static long long offset =
629 100 *
static_cast<long long>(
time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
630 prc_start = ( prc.
starttime + offset ) * TICK_TO_100NSEC;
636 info->
UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
637 info->
KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
643 static long long offset =
644 100 *
static_cast<long long>(
time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
648 info->
UserTime =
t.tms_utime * TICK_TO_100NSEC;
656 #elif defined( __APPLE__ )
660 #else // no /proc file system: assume sys_start for the first call
662 static clock_t sys_start = times( 0 );
663 static long long offset = 100 *
long long(
time( 0 ) ) - sys_start;
664 clock_t now = times( &tmsb );