The Gaudi Framework  v40r0 (475e45c1)
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 117 of file ProcessDescriptor.h.

Constructor & Destructor Documentation

◆ ProcessDescriptor()

System::ProcessDescriptor::ProcessDescriptor ( )

Definition at line 374 of file ProcessDescriptor.cpp.

374  {
375 #ifdef _WIN32
376  static bool first = true;
377  if ( first ) {
378  first = false;
379  void* mh = ::LoadLibrary( "NTDll.dll" );
380  if ( mh ) {
381  NtApi::NtQueryInformationProcess =
382  ( NtApi::__NtQueryInformationProcess )::GetProcAddress( (HINSTANCE)mh, "NtQueryInformationProcess" );
383  }
384  }
385 #endif
386 }

◆ ~ProcessDescriptor()

System::ProcessDescriptor::~ProcessDescriptor ( )
virtual

Definition at line 388 of file ProcessDescriptor.cpp.

388 {}

Member Function Documentation

◆ query() [1/7]

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

Definition at line 390 of file ProcessDescriptor.cpp.

390  {
391  if ( info == 0 ) return 0;
392  long status = 1;
393 
394  if ( fetch == IO ) {
395 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
396  ProcessHandle h( pid );
397  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessIoCounters, info, sizeof( IO_COUNTERS ), 0 );
398  status = ( status == 0 ) ? 1 : 0;
399 #elif defined( _WIN32 ) // Windows 95,98...
400 #elif defined( __linux )
401  linux_proc prc;
402  readProcStat( processID( pid ), prc );
403  rusage usage;
404  getrusage( RUSAGE_SELF, &usage );
405  info->ReadOperationCount = usage.ru_inblock;
406  info->WriteOperationCount = usage.ru_oublock;
407  info->OtherOperationCount = 0;
408  info->ReadTransferCount = usage.ru_inblock;
409  info->WriteTransferCount = usage.ru_oublock;
410  info->OtherTransferCount = 0;
411 #else // All Other
412  if ( pid ) {}
413 #endif // End ALL OS
414  }
415  return status;
416 }

◆ query() [2/7]

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

Definition at line 610 of file ProcessDescriptor.cpp.

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

◆ query() [3/7]

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

Definition at line 461 of file ProcessDescriptor.cpp.

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

◆ query() [4/7]

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

Definition at line 418 of file ProcessDescriptor.cpp.

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

◆ query() [5/7]

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

Definition at line 578 of file ProcessDescriptor.cpp.

578  {
579  if ( info == 0 ) return 0;
580  long status = 1;
581 
582  if ( fetch == ProcessBasics ) {
583 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
584  ProcessHandle h( pid );
585  status = NtApi::NtQueryInformationProcess( h.handle(), ProcessBasicInformation, info,
586  sizeof( PROCESS_BASIC_INFORMATION ), 0 );
587  status = ( status == 0 ) ? 1 : 0;
588 #elif defined( _WIN32 ) // Windows 95,98...
589 #elif defined( __linux ) // Linux
590  linux_proc prc;
591  pid = processID( pid );
592  readProcStat( pid, prc );
593  info->ExitStatus = 0;
594  info->PebBaseAddress = (PPEB)prc.startcode;
595  info->BasePriority = 2 * 15 - prc.priority;
596  // std::cout << "Base Priority=" << info->BasePriority << "|"
597  // << prc.priority << std::endl;
598  info->AffinityMask = prc.flags;
599  // std::cout << "Flags =" << info->AffinityMask << "|"
600  // << prc.flags << std::endl;
601  info->UniqueProcessId = pid;
602  info->InheritedFromUniqueProcessId = prc.ppid;
603 #else // All Other
604  if ( pid ) {}
605 #endif // End ALL OS
606  }
607  return status;
608 }

◆ query() [6/7]

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

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  if ( pid ) {}
571 #else // All Other
572  if ( pid ) {}
573 #endif // End ALL OS
574  }
575  return status;
576 }

◆ query() [7/7]

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

Definition at line 489 of file ProcessDescriptor.cpp.

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

The documentation for this class was generated from the following files:
linux_proc::vsize
unsigned long vsize
Definition: ProcessDescriptor.cpp:274
linux_proc::rss
long rss
Definition: ProcessDescriptor.cpp:275
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
System::ProcessBasicInformation
@ ProcessBasicInformation
Definition: ProcessDescriptor.cpp:17
linux_proc::starttime
unsigned long long starttime
Definition: ProcessDescriptor.cpp:273
gaudirun.fd
fd
Definition: gaudirun.py:630
linux_proc::startcode
unsigned long startcode
Definition: ProcessDescriptor.cpp:277
System::ProcessVmCounters
@ ProcessVmCounters
Definition: ProcessDescriptor.cpp:20
NewInputWrite.fetch
fetch
Definition: NewInputWrite.py:45
linux_proc::ppid
int ppid
Definition: ProcessDescriptor.cpp:255
readProcStat
void readProcStat(long pid, linux_proc &pinfo)
Definition: ProcessDescriptor.cpp:294
linux_proc::priority
long priority
Definition: ProcessDescriptor.cpp:269
linux_proc::majflt
unsigned long majflt
Definition: ProcessDescriptor.cpp:263
linux_proc::flags
unsigned long flags
Definition: ProcessDescriptor.cpp:260
System::PriorityBoost
@ PriorityBoost
Definition: SystemBase.h:15
bug_34121.t
t
Definition: bug_34121.py:31
System::ProcessTimes
@ ProcessTimes
Definition: ProcessDescriptor.cpp:21
System::Memory
@ Memory
Definition: SystemBase.h:15
AlgSequencer.h
h
Definition: AlgSequencer.py:31
linux_proc
Definition: ProcessDescriptor.cpp:251
linux_proc::minflt
unsigned long minflt
Definition: ProcessDescriptor.cpp:261
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:15
System::Times
@ Times
Definition: SystemBase.h:15
System::Quota
@ Quota
Definition: SystemBase.h:15
System::ProcessPooledUsageAndLimits
@ ProcessPooledUsageAndLimits
Definition: ProcessDescriptor.cpp:31
usage
void usage(const std::string &argv0)
Definition: listcomponents.cpp:40
System::ProcessQuotaLimits
@ ProcessQuotaLimits
Definition: ProcessDescriptor.cpp:18
System::ProcessHandle
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:27
System::ProcessIoCounters
@ ProcessIoCounters
Definition: ProcessDescriptor.cpp:19
System::ProcessPriorityBoost
@ ProcessPriorityBoost
Definition: ProcessDescriptor.cpp:39
System::PPEB
struct _PEB * PPEB
Basic Process Information NtQueryInformationProcess using ProcessBasicInfo.
Definition: ProcessDescriptor.h:26
System::IO
@ IO
Definition: SystemBase.h:15