The Gaudi Framework  v30r3 (a5ef0a68)
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 109 of file ProcessDescriptor.h.

Constructor & Destructor Documentation

System::ProcessDescriptor::ProcessDescriptor ( )

Definition at line 385 of file ProcessDescriptor.cpp.

386 {
387 #ifdef _WIN32
388  static bool first = true;
389  if ( first ) {
390  first = false;
391  void* mh = ::LoadLibrary( "NTDll.dll" );
392  if ( mh ) {
393  NtApi::NtQueryInformationProcess =
394  ( NtApi::__NtQueryInformationProcess )::GetProcAddress( (HINSTANCE)mh, "NtQueryInformationProcess" );
395  }
396  }
397 #endif
398 }
System::ProcessDescriptor::~ProcessDescriptor ( )
virtual

Definition at line 400 of file ProcessDescriptor.cpp.

400 {}

Member Function Documentation

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

Definition at line 587 of file ProcessDescriptor.cpp.

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

Definition at line 430 of file ProcessDescriptor.cpp.

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

Definition at line 619 of file ProcessDescriptor.cpp.

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

Definition at line 542 of file ProcessDescriptor.cpp.

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

Definition at line 500 of file ProcessDescriptor.cpp.

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

Definition at line 402 of file ProcessDescriptor.cpp.

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

Definition at line 472 of file ProcessDescriptor.cpp.

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

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