12static const long TICK_TO_100NSEC = 100000;
52#include <sys/signal.h>
53#include <sys/syscall.h>
58# include <sys/procfs.h>
61#include <sys/resource.h>
272static long pg_size = sysconf( _SC_PAGESIZE );
280 std::ostringstream ost;
282 ost <<
"/proc/" << pid <<
"/stat";
283 std::string fname = ost.str();
284 if ( ( fd = open( fname.c_str(), O_RDONLY ) ) < 0 ) {
285 std::cerr <<
"Failed to open " << ost.str() << std::endl;
289 lseek( fd, 0, SEEK_SET );
290 if ( ( cnt = read( fd, buf,
sizeof( buf ) - 1 ) ) < 0 ) {
291 std::cout <<
"LINUX Read of Proc file failed:" << std::endl;
303 "%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 "
304 "%lu %lu %lu %lu %lu %lu %lu",
319static inline long processID(
long pid ) {
return ( pid > 0 ) ? pid : ( ::getpid() ); }
328 if ( pid != ::getpid() ) {
330 m_handle =
reinterpret_cast<void*
>(
static_cast<long>( ::getpid() ) );
344 if ( info == 0 )
return 0;
348#if defined( __linux )
352 getrusage( RUSAGE_SELF, &
usage );
367 if ( info == 0 )
return 0;
370 if ( fetch ==
Quota ) {
371#if defined( __linux )
376 getrlimit( RLIMIT_DATA, &lim );
377 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
382 getrlimit( RLIMIT_STACK, &lim );
383 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
393#elif defined( __APPLE__ )
405 if ( info == 0 )
return 0;
413 status = ( status == 0 ) ? 1 : 0;
424 if ( info == 0 )
return 0;
428#if defined( __linux )
429 const ssize_t bufsize = 1024;
431 pid = processID( pid );
432 sprintf( buf,
"/proc/%ld/statm", pid );
433 long size, resident, share, trs, lrs, drs, dt;
434 int fd = open( buf, O_RDONLY );
435 ssize_t nread = read( fd, buf, bufsize );
437 if ( nread < bufsize && nread >= 0 ) buf[nread] =
'\0';
438 fd = sscanf( buf,
"%ld %ld %ld %ld %ld %ld %ld", &size, &resident, &share, &trs, &drs, &lrs, &dt );
452#elif defined( __APPLE__ )
462 if ( info == 0 )
return 0;
465 if ( fetch ==
Quota ) {
466#if defined( __linux )
470 if ( pid > 0 && pid != ::getpid() )
return 0;
473 getrlimit( RLIMIT_DATA, &lim );
474 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
477 getrlimit( RLIMIT_STACK, &lim );
478 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
482 getrlimit( RLIMIT_RSS, &lim );
483 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
486 getrlimit( RLIMIT_AS, &lim );
487 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
490 getrlimit( RLIMIT_CPU, &lim );
491 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
493#elif defined( __APPLE__ )
503 if ( info == 0 )
return 0;
507#if defined( __linux )
509 pid = processID( pid );
529 if ( info == 0 )
return 0;
532 if ( fetch ==
Times ) {
533#if defined( __linux )
534 static long long prc_start = 0;
535 bool myself = pid <= 0 || pid == ::getpid();
536 if ( myself && prc_start == 0 ) {
541 static long long offset =
542 100 *
static_cast<long long>( time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
543 prc_start = ( prc.
starttime + offset ) * TICK_TO_100NSEC;
549 info->
UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
550 info->
KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
556 static long long offset =
557 100 *
static_cast<long long>( time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
561 info->
UserTime = t.tms_utime * TICK_TO_100NSEC;
562 info->
KernelTime = t.tms_stime * TICK_TO_100NSEC;
569#elif defined( __APPLE__ )
575 static clock_t sys_start = times( 0 );
576 static long long offset = 100 *
long long( time( 0 ) ) - sys_start;
577 clock_t now = times( &tmsb );
void readProcStat(long pid, linux_proc &pinfo)
long query(long pid, InfoType fetch, PROCESS_BASIC_INFORMATION *info)
void usage(const std::string &argv0)
Note: OS specific details for environment resolution.
@ ProcessBasicInformation
@ ProcessDefaultHardErrorMode
@ ProcessEnableAlignmentFaultFixup
@ ProcessPooledUsageAndLimits
GAUDI_API ProcessHandle processHandle()
Handle to running process.
InfoType
Enumeration for fetching information.
struct _PEB * PPEB
Basic Process Information NtQueryInformationProcess using ProcessBasicInfo.
Process I/O Counters NtQueryInformationProcess using ProcessIoCounters.
unsigned long WriteOperationCount
unsigned long OtherOperationCount
long long WriteTransferCount
long long OtherTransferCount
unsigned long ReadOperationCount
long long ReadTransferCount
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
Process Pooled Quota Usage and Limits NtQueryInformationProcess using ProcessPooledUsageAndLimits.
unsigned long PagefileUsage
unsigned long PeakNonPagedPoolUsage
unsigned long NonPagedPoolLimit
unsigned long PagedPoolUsage
unsigned long PeakPagefileUsage
unsigned long PagefileLimit
unsigned long PeakPagedPoolUsage
unsigned long NonPagedPoolUsage
unsigned long PagedPoolLimit
unsigned long AffinityMask
unsigned long InheritedFromUniqueProcessId
unsigned long UniqueProcessId
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
unsigned long PagedPoolLimit
unsigned long MaximumWorkingSetSize
unsigned long MinimumWorkingSetSize
unsigned long NonPagedPoolLimit
unsigned long PagefileLimit
Process Virtual Memory Counters NtQueryInformationProcess using ProcessVmCounters.
unsigned long PeakPagefileUsage
unsigned long QuotaPeakNonPagedPoolUsage
unsigned long QuotaNonPagedPoolUsage
unsigned long PeakWorkingSetSize
unsigned long QuotaPagedPoolUsage
unsigned long VirtualSize
unsigned long PagefileUsage
unsigned long WorkingSetSize
unsigned long PageFaultCount
unsigned long PeakVirtualSize
unsigned long QuotaPeakPagedPoolUsage
unsigned long long starttime