The Gaudi Framework  master (594c33fa)
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 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 118 of file ProcessDescriptor.h.

Constructor & Destructor Documentation

◆ ProcessDescriptor()

System::ProcessDescriptor::ProcessDescriptor ( )

Definition at line 391 of file ProcessDescriptor.cpp.

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

◆ ~ProcessDescriptor()

System::ProcessDescriptor::~ProcessDescriptor ( )
virtual

Definition at line 405 of file ProcessDescriptor.cpp.

405 {}

Member Function Documentation

◆ query() [1/7]

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

Definition at line 407 of file ProcessDescriptor.cpp.

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

◆ query() [2/7]

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

Definition at line 627 of file ProcessDescriptor.cpp.

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

◆ query() [3/7]

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

Definition at line 478 of file ProcessDescriptor.cpp.

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

◆ query() [4/7]

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

Definition at line 435 of file ProcessDescriptor.cpp.

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

◆ query() [5/7]

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

Definition at line 595 of file ProcessDescriptor.cpp.

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

◆ query() [6/7]

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

Definition at line 549 of file ProcessDescriptor.cpp.

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

◆ query() [7/7]

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

Definition at line 506 of file ProcessDescriptor.cpp.

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

The documentation for this class was generated from the following files:
linux_proc::vsize
unsigned long vsize
Definition: ProcessDescriptor.cpp:291
linux_proc::rss
long rss
Definition: ProcessDescriptor.cpp:292
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:290
gaudirun.fd
fd
Definition: gaudirun.py:628
linux_proc::startcode
unsigned long startcode
Definition: ProcessDescriptor.cpp:294
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:272
readProcStat
void readProcStat(long pid, linux_proc &pinfo)
Definition: ProcessDescriptor.cpp:311
linux_proc::priority
long priority
Definition: ProcessDescriptor.cpp:286
linux_proc::majflt
unsigned long majflt
Definition: ProcessDescriptor.cpp:280
linux_proc::flags
unsigned long flags
Definition: ProcessDescriptor.cpp:277
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:268
linux_proc::minflt
unsigned long minflt
Definition: ProcessDescriptor.cpp:278
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