12static const long TICK_TO_100NSEC = 100000;
52#include <sys/signal.h>
53#include <sys/syscall.h>
58# include <sys/procfs.h>
60# include <mach/mach.h>
61# include <mach/task_info.h>
62# include <sys/resource.h>
66#include <sys/resource.h>
277static long pg_size = sysconf( _SC_PAGESIZE );
285 std::ostringstream ost;
287 ost <<
"/proc/" << pid <<
"/stat";
288 std::string fname = ost.str();
289 if ( ( fd = open( fname.c_str(), O_RDONLY ) ) < 0 ) {
290 std::cerr <<
"Failed to open " << ost.str() << std::endl;
294 lseek( fd, 0, SEEK_SET );
295 if ( ( cnt = read( fd, buf,
sizeof( buf ) - 1 ) ) < 0 ) {
296 std::cout <<
"LINUX Read of Proc file failed:" << std::endl;
308 "%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 "
309 "%lu %lu %lu %lu %lu %lu %lu",
324static inline long processID(
long pid ) {
return ( pid > 0 ) ? pid : ( ::getpid() ); }
333 if ( pid != ::getpid() ) {
335 m_handle =
reinterpret_cast<void*
>(
static_cast<long>( ::getpid() ) );
349 if ( info == 0 )
return 0;
353#if defined( __linux )
357 getrusage( RUSAGE_SELF, &
usage );
372 if ( info == 0 )
return 0;
375 if ( fetch ==
Quota ) {
376#if defined( __linux )
381 getrlimit( RLIMIT_DATA, &lim );
382 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
387 getrlimit( RLIMIT_STACK, &lim );
388 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
398#elif defined( __APPLE__ )
410 if ( info == 0 )
return 0;
418 status = ( status == 0 ) ? 1 : 0;
429 if ( info == 0 )
return 0;
433#if defined( __linux )
434 const ssize_t bufsize = 1024;
436 pid = processID( pid );
437 snprintf( buf,
sizeof( buf ),
"/proc/%ld/statm", pid );
438 long size, resident, share, trs, lrs, drs, dt;
439 int fd = open( buf, O_RDONLY );
440 ssize_t nread = read( fd, buf, bufsize );
442 if ( nread < bufsize && nread >= 0 ) buf[nread] =
'\0';
443 fd = sscanf( buf,
"%ld %ld %ld %ld %ld %ld %ld", &size, &resident, &share, &trs, &drs, &lrs, &dt );
457#elif defined( __APPLE__ )
460 mach_task_basic_info_data_t taskInfo;
461 mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
463 kern_return_t kr = task_info( mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount );
464 if ( kr == KERN_SUCCESS ) {
473 info->
PagefileUsage = taskInfo.virtual_size - taskInfo.resident_size;
478 if ( getrusage( RUSAGE_SELF, &
usage ) == 0 ) {
508 if ( info == 0 )
return 0;
511 if ( fetch ==
Quota ) {
512#if defined( __linux )
516 if ( pid > 0 && pid != ::getpid() )
return 0;
519 getrlimit( RLIMIT_DATA, &lim );
520 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
523 getrlimit( RLIMIT_STACK, &lim );
524 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
528 getrlimit( RLIMIT_RSS, &lim );
529 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
532 getrlimit( RLIMIT_AS, &lim );
533 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
536 getrlimit( RLIMIT_CPU, &lim );
537 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
539#elif defined( __APPLE__ )
549 if ( info == 0 )
return 0;
553#if defined( __linux )
555 pid = processID( pid );
575 if ( info == 0 )
return 0;
578 if ( fetch ==
Times ) {
579#if defined( __linux )
580 static long long prc_start = 0;
581 bool myself = pid <= 0 || pid == ::getpid();
582 if ( myself && prc_start == 0 ) {
587 static long long offset =
588 100 *
static_cast<long long>( time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
589 prc_start = ( prc.
starttime + offset ) * TICK_TO_100NSEC;
595 info->
UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
596 info->
KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
602 static long long offset =
603 100 *
static_cast<long long>( time(
nullptr ) ) -
static_cast<long long>( times( &tmsb ) );
607 info->
UserTime = t.tms_utime * TICK_TO_100NSEC;
608 info->
KernelTime = t.tms_stime * TICK_TO_100NSEC;
615#elif defined( __APPLE__ )
618 static long long prc_start = 0;
619 if ( prc_start == 0 ) {
621 gettimeofday( &tv,
nullptr );
623 prc_start =
static_cast<long long>( tv.tv_sec ) * 10000000LL +
static_cast<long long>( tv.tv_usec ) * 10LL;
627 if ( getrusage( RUSAGE_SELF, &
usage ) == 0 ) {
629 info->
UserTime =
static_cast<long long>(
usage.ru_utime.tv_sec ) * 10000000LL +
630 static_cast<long long>(
usage.ru_utime.tv_usec ) * 10LL;
631 info->
KernelTime =
static_cast<long long>(
usage.ru_stime.tv_sec ) * 10000000LL +
632 static_cast<long long>(
usage.ru_stime.tv_usec ) * 10LL;
642 static clock_t sys_start = times( 0 );
643 static long long offset = 100 *
long long( time( 0 ) ) - sys_start;
644 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