25 #define GAUDIKERNEL_PROCSTAT_CPP
28 static const long TICK_TO_100NSEC = 100000;
29 #endif // not __APPLE__
61 # define strcasecmp _stricmp
62 # define strncasecmp _strnicmp
67 # define getpid _getpid
73 typedef long( WINAPI* __NtQueryInformationProcess )(
75 void*
ProcessHandle,
long ProcessInformationClass,
void* ProcessInformation,
76 unsigned long ProcessInformationLength,
unsigned long* ReturnLength );
77 __NtQueryInformationProcess NtQueryInformationProcess;
79 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
88 # include <sys/signal.h>
89 # include <sys/syscall.h>
90 # include <sys/times.h>
91 # include <sys/types.h>
94 # include <sys/procfs.h>
97 # include <sys/resource.h>
98 # include <sys/time.h>
308 static long pg_size = sysconf( _SC_PAGESIZE );
318 ost <<
"/proc/" << pid <<
"/stat";
320 if ( (
fd = open( fname.
c_str(), O_RDONLY ) ) < 0 ) {
325 lseek(
fd, 0, SEEK_SET );
326 if ( ( cnt =
read(
fd, buf,
sizeof( buf ) - 1 ) ) < 0 ) {
339 "%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 "
340 "%lu %lu %lu %lu %lu %lu %lu",
356 static inline long processID(
long pid ) {
return ( pid > 0 ) ? pid : ( ::getpid() ); }
357 #endif // not __APPLE__
366 if ( pid != ::getpid() ) {
368 m_handle = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid );
371 m_handle =
reinterpret_cast<void*
>(
static_cast<long>( ::getpid() ) );
382 if ( m_needRelease ) {
384 ::CloseHandle( m_handle );
393 static bool first =
true;
396 void* mh = ::LoadLibrary(
"NTDll.dll" );
398 NtApi::NtQueryInformationProcess =
399 ( NtApi::__NtQueryInformationProcess )::GetProcAddress( (HINSTANCE)mh,
"NtQueryInformationProcess" );
408 if ( info == 0 )
return 0;
412 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
415 status = ( status == 0 ) ? 1 : 0;
416 #elif defined( _WIN32 ) // Windows 95,98...
417 #elif defined( __linux )
421 getrusage( RUSAGE_SELF, &
usage );
436 if ( info == 0 )
return 0;
440 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
444 status = ( status == 0 ) ? 1 : 0;
445 #elif defined( _WIN32 ) // Windows 95,98...
446 #elif defined( __linux ) // Linux
451 getrlimit( RLIMIT_DATA, &lim );
452 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
457 getrlimit( RLIMIT_STACK, &lim );
458 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
468 #elif defined( __APPLE__ )
480 if ( info == 0 )
return 0;
485 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
487 status = NtApi::NtQueryInformationProcess(
h.handle(),
ProcessPriorityBoost, info,
sizeof(
long ), 0 );
488 #elif defined( _WIN32 ) // Windows 95,98...
492 if ( pid > 0 ) status = 0;
496 status = ( status == 0 ) ? 1 : 0;
507 if ( info == 0 )
return 0;
511 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
514 status = ( status == 0 ) ? 1 : 0;
515 #elif defined( _WIN32 ) // Windows 95,98...
516 #elif defined( __linux ) // Linux
517 const ssize_t bufsize = 1024;
519 pid = processID( pid );
520 sprintf( buf,
"/proc/%ld/statm", pid );
521 long size, resident, share, trs, lrs, drs, dt;
522 int fd = open( buf, O_RDONLY );
523 ssize_t nread =
read(
fd, buf, bufsize );
525 if ( nread < bufsize && nread >= 0 ) buf[nread] =
'\0';
526 fd = sscanf( buf,
"%ld %ld %ld %ld %ld %ld %ld", &
size, &resident, &share, &trs, &drs, &lrs, &dt );
540 #elif defined( __APPLE__ )
550 if ( info == 0 )
return 0;
554 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
557 status = ( status == 0 ) ? 1 : 0;
558 #elif defined( _WIN32 ) // Windows 95,98...
559 #elif defined( __linux ) // Linux
563 if ( pid > 0 && pid != ::getpid() )
return 0;
566 getrlimit( RLIMIT_DATA, &lim );
567 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
570 getrlimit( RLIMIT_STACK, &lim );
571 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
575 getrlimit( RLIMIT_RSS, &lim );
576 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
579 getrlimit( RLIMIT_AS, &lim );
580 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
583 getrlimit( RLIMIT_CPU, &lim );
584 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
586 #elif defined( __APPLE__ )
596 if ( info == 0 )
return 0;
600 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
604 status = ( status == 0 ) ? 1 : 0;
605 #elif defined( _WIN32 ) // Windows 95,98...
606 #elif defined( __linux ) // Linux
608 pid = processID( pid );
628 if ( info == 0 )
return 0;
632 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
635 status = ( status == 0 ) ? 1 : 0;
636 #elif defined( _WIN32 ) // Windows 95,98...
637 #elif defined( __linux ) // Linux
638 static long long prc_start = 0;
639 bool myself = pid <= 0 || pid == ::getpid();
640 if ( myself && prc_start == 0 ) {
645 static long long offset =
646 100 *
static_cast<long long>(
time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
647 prc_start = ( prc.
starttime + offset ) * TICK_TO_100NSEC;
653 info->
UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
654 info->
KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
660 static long long offset =
661 100 *
static_cast<long long>(
time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
665 info->
UserTime =
t.tms_utime * TICK_TO_100NSEC;
673 #elif defined( __APPLE__ )
677 #else // no /proc file system: assume sys_start for the first call
679 static clock_t sys_start = times( 0 );
680 static long long offset = 100 *
long long(
time( 0 ) ) - sys_start;
681 clock_t now = times( &tmsb );