The Gaudi Framework  v39r1 (adb068b2)
System::ProcessDescriptor Class Reference

#include </builds/gaudi/Gaudi/GaudiKernel/src/Lib/ProcessDescriptor.h>

Classes

class  ProcessHandle
 

Public Member Functions

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

Detailed Description

Provides access to process information

Author
M.Frank
Sebastien Ponce

Definition at line 118 of file ProcessDescriptor.h.

Constructor & Destructor Documentation

◆ ProcessDescriptor()

System::ProcessDescriptor::ProcessDescriptor ( )

Definition at line 390 of file ProcessDescriptor.cpp.

390  {
391 #ifdef _WIN32
392  static bool first = true;
393  if ( first ) {
394  first = false;
395  void* mh = ::LoadLibrary( "NTDll.dll" );
396  if ( mh ) {
397  NtApi::NtQueryInformationProcess =
398  ( NtApi::__NtQueryInformationProcess )::GetProcAddress( (HINSTANCE)mh, "NtQueryInformationProcess" );
399  }
400  }
401 #endif
402 }

◆ ~ProcessDescriptor()

System::ProcessDescriptor::~ProcessDescriptor ( )
virtual

Definition at line 404 of file ProcessDescriptor.cpp.

404 {}

Member Function Documentation

◆ query() [1/7]

long System::ProcessDescriptor::query ( long  pid,
InfoType  fetch,
IO_COUNTERS info 
)

Definition at line 406 of file ProcessDescriptor.cpp.

406  {
407  if ( info == 0 ) return 0;
408  long status = 1;
409 
410  if ( fetch == IO ) {
411 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
412  ProcessHandle h( pid );
413  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessIoCounters, info, sizeof( IO_COUNTERS ), 0 );
414  status = ( status == 0 ) ? 1 : 0;
415 #elif defined( _WIN32 ) // Windows 95,98...
416 #elif defined( __linux )
417  linux_proc prc;
418  readProcStat( processID( pid ), prc );
419  rusage usage;
420  getrusage( RUSAGE_SELF, &usage );
421  info->ReadOperationCount = usage.ru_inblock;
422  info->WriteOperationCount = usage.ru_oublock;
423  info->OtherOperationCount = 0;
424  info->ReadTransferCount = usage.ru_inblock;
425  info->WriteTransferCount = usage.ru_oublock;
426  info->OtherTransferCount = 0;
427 #else // All Other
428  if ( pid ) {}
429 #endif // End ALL OS
430  }
431  return status;
432 }

◆ query() [2/7]

long System::ProcessDescriptor::query ( long  pid,
InfoType  fetch,
KERNEL_USER_TIMES info 
)

Definition at line 626 of file ProcessDescriptor.cpp.

626  {
627  if ( info == 0 ) return 0;
628  long status = 1;
629 
630  if ( fetch == Times ) {
631 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
632  ProcessHandle h( pid );
633  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessTimes, info, sizeof( KERNEL_USER_TIMES ), 0 );
634  status = ( status == 0 ) ? 1 : 0;
635 #elif defined( _WIN32 ) // Windows 95,98...
636 #elif defined( __linux ) // Linux
637  static long long prc_start = 0;
638  bool myself = pid <= 0 || pid == ::getpid(); // avoid unnecessary calls to getpid if pid<0
639  if ( myself && prc_start == 0 ) { // called only once to set prc_start
640  linux_proc prc;
641  readProcStat( processID( pid ), prc );
642  // prc.startup is in ticks since system start, need to offset for absolute time
643  tms tmsb;
644  static long long offset =
645  100 * static_cast<long long>( time( nullptr ) ) - static_cast<long long>( times( &tmsb ) );
646  prc_start = ( prc.starttime + offset ) * TICK_TO_100NSEC;
647  }
648 
649  if ( myself ) { // myself
650  tms tmsb;
651  times( &tmsb );
652  info->UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
653  info->KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
654  info->CreateTime = prc_start;
655  } else { // other process
656  linux_proc prc;
657  readProcStat( processID( pid ), prc );
658  tms tmsb;
659  static long long offset =
660  100 * static_cast<long long>( time( nullptr ) ) - static_cast<long long>( times( &tmsb ) );
661 
662  tms t;
663  times( &t );
664  info->UserTime = t.tms_utime * TICK_TO_100NSEC;
665  info->KernelTime = t.tms_stime * TICK_TO_100NSEC;
666  info->CreateTime = ( prc.starttime + offset ) * TICK_TO_100NSEC;
667  }
668  info->ExitTime = 0;
669 
670  status = 1;
671 
672 #elif defined( __APPLE__ )
673  if ( pid ) {}
674 // FIXME (MCl): Make an alternative function get timing on OSX
675 // times() seems to cause a segmentation fault
676 #else // no /proc file system: assume sys_start for the first call
677  tms tmsb;
678  static clock_t sys_start = times( 0 );
679  static long long offset = 100 * long long( time( 0 ) ) - sys_start;
680  clock_t now = times( &tmsb );
681  info->CreateTime = offset + now;
682  info->UserTime = tmsb.tms_utime;
683  info->KernelTime = tmsb.tms_stime;
684  info->CreateTime *= TICK_TO_100NSEC;
685  info->UserTime *= TICK_TO_100NSEC;
686  info->KernelTime *= TICK_TO_100NSEC;
687  info->ExitTime = 0;
688  status = 1;
689 #endif
690  }
691 
692  return status;
693 }

◆ query() [3/7]

long System::ProcessDescriptor::query ( long  pid,
InfoType  fetch,
long *  info 
)

Definition at line 477 of file ProcessDescriptor.cpp.

477  {
478 
479  if ( info == 0 ) return 0;
480  long status = 1;
481 
482  switch ( fetch ) {
483  case PriorityBoost:
484 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
485  ProcessHandle h( pid );
486  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessPriorityBoost, info, sizeof( long ), 0 );
487 #elif defined( _WIN32 ) // Windows 95,98...
488  if ( pid ) {}
489 #else
490  // Not applicable
491  if ( pid > 0 ) status = 0; // to avoid compiler warning
492  status = 0;
493  *info = 0;
494 #endif // End ALL OS
495  status = ( status == 0 ) ? 1 : 0;
496  break;
497  default:
498  status = -1;
499  *info = -1;
500  break;
501  }
502  return status;
503 }

◆ query() [4/7]

long System::ProcessDescriptor::query ( long  pid,
InfoType  fetch,
POOLED_USAGE_AND_LIMITS info 
)

Definition at line 434 of file ProcessDescriptor.cpp.

434  {
435  if ( info == 0 ) return 0;
436  long status = 1;
437 
438  if ( fetch == Quota ) {
439 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
440  ProcessHandle h( pid );
441  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessPooledUsageAndLimits, info,
442  sizeof( POOLED_USAGE_AND_LIMITS ), 0 );
443  status = ( status == 0 ) ? 1 : 0;
444 #elif defined( _WIN32 ) // Windows 95,98...
445 #elif defined( __linux ) // Linux
446  // rusage usage;
447  // getrusage(RUSAGE_SELF, &usage);
448  rlimit lim;
449 
450  getrlimit( RLIMIT_DATA, &lim );
451  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
452  info->PeakPagedPoolUsage = lim.rlim_cur;
453  info->PagedPoolUsage = lim.rlim_cur;
454  info->PagedPoolLimit = lim.rlim_max;
455 
456  getrlimit( RLIMIT_STACK, &lim );
457  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
458  info->PeakNonPagedPoolUsage = lim.rlim_cur;
459  info->NonPagedPoolUsage = lim.rlim_cur;
460  info->NonPagedPoolLimit = lim.rlim_max;
461 
462  linux_proc prc;
463  readProcStat( processID( pid ), prc );
464  info->PeakPagefileUsage = prc.rss * pg_size;
465  info->PagefileUsage = prc.rss * pg_size;
466  info->PagefileLimit = 0xFFFFFFFF;
467 #elif defined( __APPLE__ )
468  if ( pid ) {}
469 #else // All Other
470  if ( pid ) {}
471 #endif // End ALL OS
472  }
473 
474  return status;
475 }

◆ query() [5/7]

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

Definition at line 594 of file ProcessDescriptor.cpp.

594  {
595  if ( info == 0 ) return 0;
596  long status = 1;
597 
598  if ( fetch == ProcessBasics ) {
599 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
600  ProcessHandle h( pid );
601  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessBasicInformation, info,
602  sizeof( PROCESS_BASIC_INFORMATION ), 0 );
603  status = ( status == 0 ) ? 1 : 0;
604 #elif defined( _WIN32 ) // Windows 95,98...
605 #elif defined( __linux ) // Linux
606  linux_proc prc;
607  pid = processID( pid );
608  readProcStat( pid, prc );
609  info->ExitStatus = 0;
610  info->PebBaseAddress = (PPEB)prc.startcode;
611  info->BasePriority = 2 * 15 - prc.priority;
612  // std::cout << "Base Priority=" << info->BasePriority << "|"
613  // << prc.priority << std::endl;
614  info->AffinityMask = prc.flags;
615  // std::cout << "Flags =" << info->AffinityMask << "|"
616  // << prc.flags << std::endl;
617  info->UniqueProcessId = pid;
618  info->InheritedFromUniqueProcessId = prc.ppid;
619 #else // All Other
620  if ( pid ) {}
621 #endif // End ALL OS
622  }
623  return status;
624 }

◆ query() [6/7]

long System::ProcessDescriptor::query ( long  pid,
InfoType  fetch,
QUOTA_LIMITS info 
)

Definition at line 548 of file ProcessDescriptor.cpp.

548  {
549  if ( info == 0 ) return 0;
550  long status = 1;
551 
552  if ( fetch == Quota ) {
553 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
554  ProcessHandle h( pid );
555  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessQuotaLimits, info, sizeof( QUOTA_LIMITS ), 0 );
556  status = ( status == 0 ) ? 1 : 0;
557 #elif defined( _WIN32 ) // Windows 95,98...
558 #elif defined( __linux ) // Linux
559  // On linux all this stuff typically is not set
560  // (ie. rlim_max=RLIM_INFINITY...)
561 
562  if ( pid > 0 && pid != ::getpid() ) return 0; // only possible for myself
563 
564  rlimit lim;
565  getrlimit( RLIMIT_DATA, &lim );
566  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
567  info->PagedPoolLimit = lim.rlim_max;
568 
569  getrlimit( RLIMIT_STACK, &lim );
570  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
571  info->NonPagedPoolLimit = lim.rlim_max;
572  info->MinimumWorkingSetSize = 0;
573 
574  getrlimit( RLIMIT_RSS, &lim );
575  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
576  info->MaximumWorkingSetSize = lim.rlim_max;
577 
578  getrlimit( RLIMIT_AS, &lim );
579  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
580  info->PagefileLimit = lim.rlim_max;
581 
582  getrlimit( RLIMIT_CPU, &lim );
583  if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
584  info->TimeLimit = lim.rlim_max;
585 #elif defined( __APPLE__ )
586  if ( pid ) {}
587 #else // All Other
588  if ( pid ) {}
589 #endif // End ALL OS
590  }
591  return status;
592 }

◆ query() [7/7]

long System::ProcessDescriptor::query ( long  pid,
InfoType  fetch,
VM_COUNTERS info 
)

Definition at line 505 of file ProcessDescriptor.cpp.

505  {
506  if ( info == 0 ) return 0;
507  long status = 1;
508 
509  if ( fetch == Memory ) {
510 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
511  ProcessHandle h( pid );
512  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessVmCounters, info, sizeof( VM_COUNTERS ), 0 );
513  status = ( status == 0 ) ? 1 : 0;
514 #elif defined( _WIN32 ) // Windows 95,98...
515 #elif defined( __linux ) // Linux
516  const ssize_t bufsize = 1024;
517  char buf[bufsize];
518  pid = processID( pid );
519  sprintf( buf, "/proc/%ld/statm", pid );
520  long size, resident, share, trs, lrs, drs, dt;
521  int fd = open( buf, O_RDONLY );
522  ssize_t nread = read( fd, buf, bufsize );
523  close( fd );
524  if ( nread < bufsize && nread >= 0 ) buf[nread] = '\0';
525  fd = sscanf( buf, "%ld %ld %ld %ld %ld %ld %ld", &size, &resident, &share, &trs, &drs, &lrs, &dt );
526  linux_proc prc;
527  readProcStat( pid, prc );
528  info->PeakVirtualSize = prc.vsize;
529  info->VirtualSize = prc.vsize;
530  info->PeakWorkingSetSize = resident * pg_size;
531  info->WorkingSetSize = resident * pg_size;
532  info->QuotaPeakPagedPoolUsage = share * pg_size;
533  info->QuotaPagedPoolUsage = share * pg_size;
534  info->QuotaNonPagedPoolUsage = ( trs + drs ) * pg_size; // drs = data/stack size
535  info->QuotaPeakNonPagedPoolUsage = ( trs + drs ) * pg_size; // trs = VmExe size
536  info->PageFaultCount = prc.majflt + prc.minflt;
537  info->PagefileUsage = prc.vsize - resident * pg_size;
538  info->PeakPagefileUsage = prc.vsize - resident * pg_size;
539 #elif defined( __APPLE__ )
540  if ( pid ) {}
541 #else // All Other
542  if ( pid ) {}
543 #endif // End ALL OS
544  }
545  return status;
546 }

The documentation for this class was generated from the following files:
linux_proc::vsize
unsigned long vsize
Definition: ProcessDescriptor.cpp:290
linux_proc::rss
long rss
Definition: ProcessDescriptor.cpp:291
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
System::ProcessBasicInformation
@ ProcessBasicInformation
Definition: ProcessDescriptor.cpp:33
linux_proc::starttime
unsigned long long starttime
Definition: ProcessDescriptor.cpp:289
gaudirun.fd
fd
Definition: gaudirun.py:630
linux_proc::startcode
unsigned long startcode
Definition: ProcessDescriptor.cpp:293
System::ProcessVmCounters
@ ProcessVmCounters
Definition: ProcessDescriptor.cpp:36
NewInputWrite.fetch
fetch
Definition: NewInputWrite.py:45
std::sscanf
T sscanf(T... args)
linux_proc::ppid
int ppid
Definition: ProcessDescriptor.cpp:271
readProcStat
void readProcStat(long pid, linux_proc &pinfo)
Definition: ProcessDescriptor.cpp:310
linux_proc::priority
long priority
Definition: ProcessDescriptor.cpp:285
linux_proc::majflt
unsigned long majflt
Definition: ProcessDescriptor.cpp:279
linux_proc::flags
unsigned long flags
Definition: ProcessDescriptor.cpp:276
System::PriorityBoost
@ PriorityBoost
Definition: SystemBase.h:28
bug_34121.t
t
Definition: bug_34121.py:31
System::ProcessTimes
@ ProcessTimes
Definition: ProcessDescriptor.cpp:37
std::sprintf
T sprintf(T... args)
System::Memory
@ Memory
Definition: SystemBase.h:28
AlgSequencer.h
h
Definition: AlgSequencer.py:31
linux_proc
Definition: ProcessDescriptor.cpp:267
linux_proc::minflt
unsigned long minflt
Definition: ProcessDescriptor.cpp:277
plotSpeedupsPyRoot.time
time
Definition: plotSpeedupsPyRoot.py:180
hivetimeline.read
def read(f, regex=".*", skipevents=0)
Definition: hivetimeline.py:32
System::ProcessBasics
@ ProcessBasics
Definition: SystemBase.h:28
System::Times
@ Times
Definition: SystemBase.h:28
System::Quota
@ Quota
Definition: SystemBase.h:28
System::ProcessPooledUsageAndLimits
@ ProcessPooledUsageAndLimits
Definition: ProcessDescriptor.cpp:47
System::ProcessQuotaLimits
@ ProcessQuotaLimits
Definition: ProcessDescriptor.cpp:34
System::ProcessHandle
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:42
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:41
System::ProcessIoCounters
@ ProcessIoCounters
Definition: ProcessDescriptor.cpp:35
System::ProcessPriorityBoost
@ ProcessPriorityBoost
Definition: ProcessDescriptor.cpp:55
System::PPEB
struct _PEB * PPEB
Basic Process Information NtQueryInformationProcess using ProcessBasicInfo.
Definition: ProcessDescriptor.h:27
System::IO
@ IO
Definition: SystemBase.h:28