The Gaudi Framework  v31r0 (aeb156f0)
System::ProcessDescriptor Class Reference

Provides access to process information. More...

#include <src/Lib/ProcessDescriptor.h>

Classes

class  ProcessHandle
 

Public Member Functions

 ProcessDescriptor ()
 
virtual ~ProcessDescriptor ()
 
long query (long pid, InfoType info, PROCESS_BASIC_INFORMATION *buffer)
 
long query (long pid, InfoType info, POOLED_USAGE_AND_LIMITS *buffer)
 
long query (long pid, InfoType info, KERNEL_USER_TIMES *buffer)
 
long query (long pid, InfoType info, QUOTA_LIMITS *buffer)
 
long query (long pid, InfoType info, VM_COUNTERS *buffer)
 
long query (long pid, InfoType info, IO_COUNTERS *buffer)
 
long query (long pid, InfoType info, long *buffer)
 

Detailed Description

Provides access to process information.

Author
M.Frank
Sebastien Ponce

Definition at line 108 of file ProcessDescriptor.h.

Constructor & Destructor Documentation

System::ProcessDescriptor::ProcessDescriptor ( )

Definition at line 380 of file ProcessDescriptor.cpp.

380  {
381 #ifdef _WIN32
382  static bool first = true;
383  if ( first ) {
384  first = false;
385  void* mh = ::LoadLibrary( "NTDll.dll" );
386  if ( mh ) {
387  NtApi::NtQueryInformationProcess =
388  ( NtApi::__NtQueryInformationProcess )::GetProcAddress( (HINSTANCE)mh, "NtQueryInformationProcess" );
389  }
390  }
391 #endif
392 }
System::ProcessDescriptor::~ProcessDescriptor ( )
virtual

Definition at line 394 of file ProcessDescriptor.cpp.

394 {}

Member Function Documentation

long System::ProcessDescriptor::query ( long  pid,
InfoType  info,
PROCESS_BASIC_INFORMATION buffer 
)

Definition at line 576 of file ProcessDescriptor.cpp.

576  {
577  if ( info == 0 ) return 0;
578  long status = 1;
579 
580  if ( fetch == ProcessBasics ) {
581 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
582  ProcessHandle h( pid );
583  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessBasicInformation, info,
584  sizeof( PROCESS_BASIC_INFORMATION ), 0 );
585  status = ( status == 0 ) ? 1 : 0;
586 #elif defined( _WIN32 ) // Windows 95,98...
587 #elif defined( __linux ) // Linux
588  linux_proc prc;
589  pid = processID( pid );
590  readProcStat( pid, prc );
591  info->ExitStatus = 0;
592  info->PebBaseAddress = (PPEB)prc.startcode;
593  info->BasePriority = 2 * 15 - prc.priority;
594  // std::cout << "Base Priority=" << info->BasePriority << "|"
595  // << prc.priority << std::endl;
596  info->AffinityMask = prc.flags;
597  // std::cout << "Flags =" << info->AffinityMask << "|"
598  // << prc.flags << std::endl;
599  info->UniqueProcessId = pid;
600  info->InheritedFromUniqueProcessId = prc.ppid;
601 #else // All Other
602 #endif // End ALL OS
603  }
604  return status;
605 }
unsigned long flags
void readProcStat(long pid, linux_proc &pinfo)
unsigned long startcode
struct _PEB * PPEB
Basic Process Information NtQueryInformationProcess using ProcessBasicInfo.
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32
long System::ProcessDescriptor::query ( long  pid,
InfoType  info,
POOLED_USAGE_AND_LIMITS buffer 
)

Definition at line 423 of file ProcessDescriptor.cpp.

423  {
424  if ( info == 0 ) return 0;
425  long status = 1;
426 
427  if ( fetch == Quota ) {
428 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
429  ProcessHandle h( pid );
430  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessPooledUsageAndLimits, info,
431  sizeof( POOLED_USAGE_AND_LIMITS ), 0 );
432  status = ( status == 0 ) ? 1 : 0;
433 #elif defined( _WIN32 ) // Windows 95,98...
434 #elif defined( __linux ) // Linux
435  // rusage usage;
436  // getrusage(RUSAGE_SELF, &usage);
437  rlimit lim;
438 
439  getrlimit( RLIMIT_DATA, &lim );
440  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
441  info->PeakPagedPoolUsage = lim.rlim_cur;
442  info->PagedPoolUsage = lim.rlim_cur;
443  info->PagedPoolLimit = lim.rlim_max;
444 
445  getrlimit( RLIMIT_STACK, &lim );
446  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
447  info->PeakNonPagedPoolUsage = lim.rlim_cur;
448  info->NonPagedPoolUsage = lim.rlim_cur;
449  info->NonPagedPoolLimit = lim.rlim_max;
450 
451  linux_proc prc;
452  readProcStat( processID( pid ), prc );
453  info->PeakPagefileUsage = prc.rss * pg_size;
454  info->PagefileUsage = prc.rss * pg_size;
455  info->PagefileLimit = 0xFFFFFFFF;
456 #elif defined( __APPLE__ )
457 #else // All Other
458 #endif // End ALL OS
459  }
460 
461  return status;
462 }
void readProcStat(long pid, linux_proc &pinfo)
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32
long System::ProcessDescriptor::query ( long  pid,
InfoType  info,
KERNEL_USER_TIMES buffer 
)

Definition at line 607 of file ProcessDescriptor.cpp.

607  {
608  if ( info == 0 ) return 0;
609  long status = 1;
610 
611  if ( fetch == Times ) {
612 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
613  ProcessHandle h( pid );
614  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessTimes, info, sizeof( KERNEL_USER_TIMES ), 0 );
615  status = ( status == 0 ) ? 1 : 0;
616 #elif defined( _WIN32 ) // Windows 95,98...
617 #elif defined( __linux ) // Linux
618  static long long prc_start = 0;
619  bool myself = pid <= 0 || pid == ::getpid(); // avoid unnecessary calls to getpid if pid<0
620  if ( myself && prc_start == 0 ) { // called only once to set prc_start
621  linux_proc prc;
622  readProcStat( processID( pid ), prc );
623  // prc.startup is in ticks since system start, need to offset for absolute time
624  tms tmsb;
625  static long long offset =
626  100 * static_cast<long long>( time( nullptr ) ) - static_cast<long long>( times( &tmsb ) );
627  prc_start = ( prc.starttime + offset ) * TICK_TO_100NSEC;
628  }
629 
630  if ( myself ) { // myself
631  tms tmsb;
632  times( &tmsb );
633  info->UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
634  info->KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
635  info->CreateTime = prc_start;
636  } else { // other process
637  linux_proc prc;
638  readProcStat( processID( pid ), prc );
639  tms tmsb;
640  static long long offset =
641  100 * static_cast<long long>( time( nullptr ) ) - static_cast<long long>( times( &tmsb ) );
642 
643  tms t;
644  times( &t );
645  info->UserTime = t.tms_utime * TICK_TO_100NSEC;
646  info->KernelTime = t.tms_stime * TICK_TO_100NSEC;
647  info->CreateTime = ( prc.starttime + offset ) * TICK_TO_100NSEC;
648  }
649  info->ExitTime = 0;
650 
651  status = 1;
652 
653 #elif defined( __APPLE__ )
654 // FIXME (MCl): Make an alternative function get timing on OSX
655 // times() seems to cause a segmentation fault
656 #else // no /proc file system: assume sys_start for the first call
657  tms tmsb;
658  static clock_t sys_start = times( 0 );
659  static long long offset = 100 * long long( time( 0 ) ) - sys_start;
660  clock_t now = times( &tmsb );
661  info->CreateTime = offset + now;
662  info->UserTime = tmsb.tms_utime;
663  info->KernelTime = tmsb.tms_stime;
664  info->CreateTime *= TICK_TO_100NSEC;
665  info->UserTime *= TICK_TO_100NSEC;
666  info->KernelTime *= TICK_TO_100NSEC;
667  info->ExitTime = 0;
668  status = 1;
669 #endif
670  }
671 
672  return status;
673 }
void readProcStat(long pid, linux_proc &pinfo)
unsigned long long starttime
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32
long System::ProcessDescriptor::query ( long  pid,
InfoType  info,
QUOTA_LIMITS buffer 
)

Definition at line 532 of file ProcessDescriptor.cpp.

532  {
533  if ( info == 0 ) return 0;
534  long status = 1;
535 
536  if ( fetch == Quota ) {
537 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
538  ProcessHandle h( pid );
539  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessQuotaLimits, info, sizeof( QUOTA_LIMITS ), 0 );
540  status = ( status == 0 ) ? 1 : 0;
541 #elif defined( _WIN32 ) // Windows 95,98...
542 #elif defined( __linux ) // Linux
543  // On linux all this stuff typically is not set
544  // (ie. rlim_max=RLIM_INFINITY...)
545 
546  if ( pid > 0 && pid != ::getpid() ) return 0; // only possible for myself
547 
548  rlimit lim;
549  getrlimit( RLIMIT_DATA, &lim );
550  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
551  info->PagedPoolLimit = lim.rlim_max;
552 
553  getrlimit( RLIMIT_STACK, &lim );
554  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
555  info->NonPagedPoolLimit = lim.rlim_max;
556  info->MinimumWorkingSetSize = 0;
557 
558  getrlimit( RLIMIT_RSS, &lim );
559  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
560  info->MaximumWorkingSetSize = lim.rlim_max;
561 
562  getrlimit( RLIMIT_AS, &lim );
563  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
564  info->PagefileLimit = lim.rlim_max;
565 
566  getrlimit( RLIMIT_CPU, &lim );
567  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
568  info->TimeLimit = lim.rlim_max;
569 #elif defined( __APPLE__ )
570 #else // All Other
571 #endif // End ALL OS
572  }
573  return status;
574 }
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32
long System::ProcessDescriptor::query ( long  pid,
InfoType  info,
VM_COUNTERS buffer 
)

Definition at line 491 of file ProcessDescriptor.cpp.

491  {
492  if ( info == 0 ) return 0;
493  long status = 1;
494 
495  if ( fetch == Memory ) {
496 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
497  ProcessHandle h( pid );
498  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessVmCounters, info, sizeof( VM_COUNTERS ), 0 );
499  status = ( status == 0 ) ? 1 : 0;
500 #elif defined( _WIN32 ) // Windows 95,98...
501 #elif defined( __linux ) // Linux
502  const ssize_t bufsize = 1024;
503  char buf[bufsize];
504  pid = processID( pid );
505  sprintf( buf, "/proc/%ld/statm", pid );
506  long size, resident, share, trs, lrs, drs, dt;
507  int fd = open( buf, O_RDONLY );
508  ssize_t nread = read( fd, buf, bufsize );
509  close( fd );
510  if ( nread < bufsize && nread >= 0 ) buf[nread] = '\0';
511  fd = sscanf( buf, "%ld %ld %ld %ld %ld %ld %ld", &size, &resident, &share, &trs, &drs, &lrs, &dt );
512  linux_proc prc;
513  readProcStat( pid, prc );
514  info->PeakVirtualSize = prc.vsize;
515  info->VirtualSize = prc.vsize;
516  info->PeakWorkingSetSize = resident * pg_size;
517  info->WorkingSetSize = resident * pg_size;
518  info->QuotaPeakPagedPoolUsage = share * pg_size;
519  info->QuotaPagedPoolUsage = share * pg_size;
520  info->QuotaNonPagedPoolUsage = ( trs + drs ) * pg_size; // drs = data/stack size
521  info->QuotaPeakNonPagedPoolUsage = ( trs + drs ) * pg_size; // trs = VmExe size
522  info->PageFaultCount = prc.majflt + prc.minflt;
523  info->PagefileUsage = prc.vsize - resident * pg_size;
524  info->PeakPagefileUsage = prc.vsize - resident * pg_size;
525 #elif defined( __APPLE__ )
526 #else // All Other
527 #endif // End ALL OS
528  }
529  return status;
530 }
unsigned long minflt
T sscanf(T...args)
def read(f, regex='.*', skipevents=0)
Definition: hivetimeline.py:22
void readProcStat(long pid, linux_proc &pinfo)
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
unsigned long vsize
unsigned long majflt
T sprintf(T...args)
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32
long System::ProcessDescriptor::query ( long  pid,
InfoType  info,
IO_COUNTERS buffer 
)

Definition at line 396 of file ProcessDescriptor.cpp.

396  {
397  if ( info == 0 ) return 0;
398  long status = 1;
399 
400  if ( fetch == IO ) {
401 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
402  ProcessHandle h( pid );
403  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessIoCounters, info, sizeof( IO_COUNTERS ), 0 );
404  status = ( status == 0 ) ? 1 : 0;
405 #elif defined( _WIN32 ) // Windows 95,98...
406 #elif defined( __linux )
407  linux_proc prc;
408  readProcStat( processID( pid ), prc );
409  rusage usage;
410  getrusage( RUSAGE_SELF, &usage );
411  info->ReadOperationCount = usage.ru_inblock;
412  info->WriteOperationCount = usage.ru_oublock;
413  info->OtherOperationCount = 0;
414  info->ReadTransferCount = usage.ru_inblock;
415  info->WriteTransferCount = usage.ru_oublock;
416  info->OtherTransferCount = 0;
417 #else // All Other
418 #endif // End ALL OS
419  }
420  return status;
421 }
void usage(std::string argv0)
void readProcStat(long pid, linux_proc &pinfo)
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32
long System::ProcessDescriptor::query ( long  pid,
InfoType  info,
long *  buffer 
)

Definition at line 464 of file ProcessDescriptor.cpp.

464  {
465 
466  if ( info == 0 ) return 0;
467  long status = 1;
468 
469  switch ( fetch ) {
470  case PriorityBoost:
471 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
472  ProcessHandle h( pid );
473  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessPriorityBoost, info, sizeof( long ), 0 );
474 #elif defined( _WIN32 ) // Windows 95,98...
475 #else
476  // Not applicable
477  if ( pid > 0 ) status = 0; // to avoid compiler warning
478  status = 0;
479  *info = 0;
480 #endif // End ALL OS
481  status = ( status == 0 ) ? 1 : 0;
482  break;
483  default:
484  status = -1;
485  info = &status;
486  break;
487  }
488  return status;
489 }
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32

The documentation for this class was generated from the following files: