The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ProcessDescriptor.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2021 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 //====================================================================
12 // ProcStat.cpp
13 //--------------------------------------------------------------------
14 //
15 // Package : System (The LHCb System service)
16 //
17 // Description: Invoke interactively the ProcStat from a
18 // running application
19 //
20 // Author : M.Frank
21 // Created : 13/11/00
22 // Changes :
23 //
24 //====================================================================
25 #define GAUDIKERNEL_PROCSTAT_CPP
26 
27 #ifndef __APPLE__
28 static const long TICK_TO_100NSEC = 100000;
29 #endif // not __APPLE__
30 
31 namespace System {
46  ProcessIoPortHandlers, // Note: this is kernel mode only
58  };
59 }
60 #ifdef _WIN32
61 # define strcasecmp _stricmp
62 # define strncasecmp _strnicmp
63 # define NOMSG
64 # define NOGDI
65 # include "process.h"
66 # include "windows.h"
67 # define getpid _getpid
68 namespace NtApi {
69  //__declspec(dllimport) long __stdcall NtQueryInformationProcess(
70  // typedef __declspec(dllimport) long __stdcall (*__NtQueryInformationProcess)(
71  // extern "C" long __cdecl NtQueryInformationProcess(
72 
73  typedef long( WINAPI* __NtQueryInformationProcess )(
74 
75  void* ProcessHandle, long ProcessInformationClass, void* ProcessInformation,
76  unsigned long ProcessInformationLength, unsigned long* ReturnLength );
77  __NtQueryInformationProcess NtQueryInformationProcess;
78 }; // namespace NtApi
79 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
80 # define WINVER 0
81 # include "libgen.h"
82 # include "sys/times.h"
83 # include "unistd.h"
84 # include <cstdio>
85 # include <errno.h>
86 # include <fcntl.h>
87 # include <iostream>
88 # include <sstream>
89 # include <string>
90 # include <sys/signal.h>
91 # include <sys/syscall.h>
92 # include <sys/types.h>
93 # include <unistd.h>
94 # ifndef __APPLE__
95 # include <sys/procfs.h>
96 # endif
97 # include <cstdio>
98 # include <sys/resource.h>
99 # include <sys/time.h>
100 
101 /* Format of the Linux proc/stat (man 5 proc, kernel 2.6.35):
102  pid %d The process ID.
103 
104  comm %s The filename of the executable, in parentheses. This is visible
105  whether or not the executable is swapped out.
106 
107  state %c One character from the string "RSDZTW" where R is running, S is
108  sleeping in an interruptible wait, D is waiting in uninterruptible
109  disk sleep, Z is zombie, T is traced or stopped (on a signal), and
110  W is paging.
111 
112  ppid %d The PID of the parent.
113 
114  pgrp %d The process group ID of the process.
115 
116  session %d The session ID of the process.
117 
118  tty_nr %d The controlling terminal of the process. (The minor device number
119  is contained in the combination of bits 31 to 20 and 7 to 0; the
120  major device number is in bits 15 t0 8.)
121 
122  tpgid %d The ID of the foreground process group of the controlling terminal
123  of the process.
124 
125  flags %u (%lu before Linux 2.6.22)
126  The kernel flags word of the process. For bit meanings, see the
127  PF_* defines in <linux/sched.h>. Details depend on the kernel
128  version.
129 
130  minflt %lu The number of minor faults the process has made which have not
131  required loading a memory page from disk.
132 
133  cminflt %lu The number of minor faults that the process's waited-for children
134  have made.
135 
136  majflt %lu The number of major faults the process has made which have
137  required loading a memory page from disk.
138 
139  cmajflt %lu The number of major faults that the process's waited-for children
140  have made.
141 
142  utime %lu Amount of time that this process has been scheduled in user mode,
143  measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This
144  includes guest time, guest_time (time spent running a virtual CPU,
145  see below), so that applications that are not aware of the guest
146  time field do not lose that time from their calculations.
147 
148  stime %lu Amount of time that this process has been scheduled in kernel
149  mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
150 
151  cutime %ld Amount of time that this process's waited-for children have been
152  scheduled in user mode, measured in clock ticks (divide by
153  sysconf(_SC_CLK_TCK). (See also times(2).) This includes guest
154  time, cguest_time (time spent running a virtual CPU, see below).
155 
156  cstime %ld Amount of time that this process's waited-for children have been
157  scheduled in kernel mode, measured in clock ticks (divide by
158  sysconf(_SC_CLK_TCK).
159 
160  priority %ld
161  (Explanation for Linux 2.6) For processes running a real-time
162  scheduling policy (policy below; see sched_setscheduler(2)), this
163  is the negated scheduling priority, minus one; that is, a number
164  in the range -2 to -100, corresponding to real-time priorities 1
165  to 99. For processes running under a non-real-time scheduling
166  policy, this is the raw nice value (setpriority(2)) as represented
167  in the kernel. The kernel stores nice values as numbers in the
168  range 0 (high) to 39 (low), corresponding to the user-visible nice
169  range of -20 to 19.
170 
171  Before Linux 2.6, this was a scaled value based on the scheduler
172  weighting given to this process.
173 
174  nice %ld The nice value (see setpriority(2)), a value in the range 19 (low
175  priority) to -20 (high priority).
176 
177  num_threads %ld
178  Number of threads in this process (since Linux 2.6). Before ker‐
179  nel 2.6, this field was hard coded to 0 as a placeholder for an
180  earlier removed field.
181 
182  itrealvalue %ld
183  The time in jiffies before the next SIGALRM is sent to the process
184  due to an interval timer. Since kernel 2.6.17, this field is no
185  longer maintained, and is hard coded as 0.
186 
187  starttime %llu (was %lu before Linux 2.6)
188  The time in jiffies the process started after system boot.
189 
190  vsize %lu Virtual memory size in bytes.
191 
192  rss %ld Resident Set Size: number of pages the process has in real memory.
193  This is just the pages which count towards text, data, or stack
194  space. This does not include pages which have not been demand-
195  loaded in, or which are swapped out.
196 
197  rsslim %lu Current soft limit in bytes on the rss of the process; see the
198  description of RLIMIT_RSS in getpriority(2).
199 
200  startcode %lu
201  The address above which program text can run.
202 
203  endcode %lu The address below which program text can run.
204 
205  startstack %lu
206  The address of the start (i.e., bottom) of the stack.
207 
208  kstkesp %lu The current value of ESP (stack pointer), as found in the kernel
209  stack page for the process.
210 
211  kstkeip %lu The current EIP (instruction pointer).
212 
213  signal %lu The bitmap of pending signals, displayed as a decimal number.
214  Obsolete, because it does not provide information on real-time
215  signals; use /proc/[pid]/status instead.
216 
217  blocked %lu The bitmap of blocked signals, displayed as a decimal number.
218  Obsolete, because it does not provide information on real-time
219  signals; use /proc/[pid]/status instead.
220 
221  sigignore %lu
222  The bitmap of ignored signals, displayed as a decimal number.
223  Obsolete, because it does not provide information on real-time
224  signals; use /proc/[pid]/status instead.
225 
226  sigcatch %lu
227  The bitmap of caught signals, displayed as a decimal number.
228  Obsolete, because it does not provide information on real-time
229  signals; use /proc/[pid]/status instead.
230 
231  wchan %lu This is the "channel" in which the process is waiting. It is the
232  address of a system call, and can be looked up in a namelist if
233  you need a textual name. (If you have an up-to-date
234  /etc/psdatabase, then try ps -l to see the WCHAN field in action.)
235 
236  nswap %lu Number of pages swapped (not maintained).
237 
238  cnswap %lu Cumulative nswap for child processes (not maintained).
239 
240  exit_signal %d (since Linux 2.1.22)
241  Signal to be sent to parent when we die.
242 
243  processor %d (since Linux 2.2.8)
244  CPU number last executed on.
245 
246  rt_priority %u (since Linux 2.5.19; was %lu before Linux 2.6.22)
247  Real-time scheduling priority, a number in the range 1 to 99 for
248  processes scheduled under a real-time policy, or 0, for non-real-
249  time processes (see sched_setscheduler(2)).
250 
251  policy %u (since Linux 2.5.19; was %lu before Linux 2.6.22)
252  Scheduling policy (see sched_setscheduler(2)). Decode using the
253  SCHED_* constants in linux/sched.h.
254 
255  delayacct_blkio_ticks %llu (since Linux 2.6.18)
256  Aggregated block I/O delays, measured in clock ticks (centisec‐
257  onds).
258 
259  guest_time %lu (since Linux 2.6.24)
260  Guest time of the process (time spent running a virtual CPU for a
261  guest operating system), measured in clock ticks (divide by
262  sysconf(_SC_CLK_TCK).
263 
264  cguest_time %ld (since Linux 2.6.24)
265  Guest time of the process's children, measured in clock ticks
266  (divide by sysconf(_SC_CLK_TCK).
267 */
268 struct linux_proc {
269  int pid;
270  char comm[400];
271  char state;
272  int ppid;
273  int pgrp;
274  int session;
275  int tty;
276  int tpgid;
277  unsigned long flags;
278  unsigned long minflt;
279  unsigned long cminflt;
280  unsigned long majflt;
281  unsigned long cmajflt;
282  unsigned long utime;
283  unsigned long stime;
284  long cutime;
285  long cstime;
286  long priority;
287  long nice;
290  unsigned long long starttime;
291  unsigned long vsize;
292  long rss;
293  unsigned long rlim;
294  unsigned long startcode;
295  unsigned long endcode;
296  unsigned long startstack;
297  unsigned long kstkesp;
298  unsigned long kstkeip;
299  unsigned long signal;
300  unsigned long blocked;
301  unsigned long sigignore;
302  unsigned long sigcatch;
303  unsigned long wchan;
304 };
305 
306 # ifdef __APPLE__
307 // static long pg_size = 0;
308 # else
309 static long pg_size = sysconf( _SC_PAGESIZE ); // getpagesize();
310 # endif
311 void readProcStat( long pid, linux_proc& pinfo ) {
312 
313  ssize_t cnt;
314  int fd;
315  char buf[512];
316 
317  std::ostringstream ost;
318 
319  ost << "/proc/" << pid << "/stat";
320  std::string fname = ost.str();
321  if ( ( fd = open( fname.c_str(), O_RDONLY ) ) < 0 ) {
322  std::cerr << "Failed to open " << ost.str() << std::endl;
323  return;
324  }
325 
326  lseek( fd, 0, SEEK_SET );
327  if ( ( cnt = read( fd, buf, sizeof( buf ) - 1 ) ) < 0 ) {
328  std::cout << "LINUX Read of Proc file failed:" << std::endl;
329  close( fd );
330  return;
331  }
332 
333  // Format
334  if ( cnt > 0 ) {
335  buf[cnt] = '\0';
336  sscanf( buf,
337  // 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 20 1 2 3 4 5 6 7 8 9
338  // 30 1 2 3 4 5
339  "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu %lu %ld %lu %lu %lu %lu "
340  "%lu %lu %lu %lu %lu %lu %lu",
341  &pinfo.pid, pinfo.comm, &pinfo.state, &pinfo.ppid, &pinfo.pgrp, &pinfo.session, &pinfo.tty, &pinfo.tpgid,
342  &pinfo.flags, &pinfo.minflt, &pinfo.cminflt, &pinfo.majflt, &pinfo.cmajflt, &pinfo.utime, &pinfo.stime,
343  &pinfo.cutime, &pinfo.cstime, &pinfo.priority, &pinfo.nice, &pinfo.num_threads, &pinfo.itrealvalue,
344  &pinfo.starttime, &pinfo.vsize, &pinfo.rss, &pinfo.rlim, &pinfo.startcode, &pinfo.endcode,
345  &pinfo.startstack, &pinfo.kstkesp, &pinfo.kstkeip, &pinfo.signal, &pinfo.blocked, &pinfo.sigignore,
346  &pinfo.sigcatch, &pinfo.wchan );
347  }
348  close( fd );
349 }
350 #endif
351 
352 // static long s_myPid = ::getpid();
353 // In order to properly support e.g. fork() calls, we cannot keep a copy of the pid!
354 
355 #ifndef __APPLE__
356 static inline long processID( long pid ) { return ( pid > 0 ) ? pid : ( ::getpid() ); }
357 #endif // not __APPLE__
358 
359 // Framework include files
360 #include "GaudiKernel/ModuleInfo.h"
361 #include "GaudiKernel/System.h"
362 #include "ProcessDescriptor.h"
363 
365  if ( pid > 0 ) {
366  if ( pid != ::getpid() ) {
367 #ifdef _WIN32
368  m_handle = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid );
369 #else
370  // Note: the return type of getpid is pid_t, which is int on 64bit machines too
371  m_handle = reinterpret_cast<void*>( static_cast<long>( ::getpid() ) );
372 #endif
373  m_needRelease = true;
374  return;
375  }
376  }
378  m_needRelease = false;
379 }
380 
382  if ( m_needRelease ) {
383 #ifdef _WIN32
384  ::CloseHandle( m_handle );
385 #else
386  m_handle = nullptr;
387 #endif
388  }
389 }
390 
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 }
404 
406 
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 }
434 
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 }
477 
478 long System::ProcessDescriptor::query( long pid, InfoType fetch, long* info ) {
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 }
505 
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 }
548 
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 }
594 
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;
620 #else // All Other
621  if ( pid ) {}
622 #endif // End ALL OS
623  }
624  return status;
625 }
626 
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 }
System::ProcessLdtInformation
@ ProcessLdtInformation
Definition: ProcessDescriptor.cpp:43
System::VM_COUNTERS::QuotaPeakPagedPoolUsage
unsigned long QuotaPeakPagedPoolUsage
Definition: ProcessDescriptor.h:75
linux_proc::blocked
unsigned long blocked
Definition: ProcessDescriptor.cpp:300
linux_proc::vsize
unsigned long vsize
Definition: ProcessDescriptor.cpp:291
System::ProcessInfoCommand
ProcessInfoCommand
Definition: ProcessDescriptor.cpp:32
System::VM_COUNTERS::PeakPagefileUsage
unsigned long PeakPagefileUsage
Definition: ProcessDescriptor.h:80
linux_proc::rss
long rss
Definition: ProcessDescriptor.cpp:292
System::VM_COUNTERS::QuotaPagedPoolUsage
unsigned long QuotaPagedPoolUsage
Definition: ProcessDescriptor.h:76
System::KERNEL_USER_TIMES
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
Definition: ProcessDescriptor.h:102
std::string
STL class.
System::VM_COUNTERS::WorkingSetSize
unsigned long WorkingSetSize
Definition: ProcessDescriptor.h:74
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:22
System::InfoType
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:28
linux_proc::endcode
unsigned long endcode
Definition: ProcessDescriptor.cpp:295
System::ProcessDescriptor::ProcessHandle::m_needRelease
bool m_needRelease
Definition: ProcessDescriptor.h:121
System::VM_COUNTERS::PagefileUsage
unsigned long PagefileUsage
Definition: ProcessDescriptor.h:79
System::POOLED_USAGE_AND_LIMITS::PagefileLimit
unsigned long PagefileLimit
Definition: ProcessDescriptor.h:95
System::ProcessBasicInformation
@ ProcessBasicInformation
Definition: ProcessDescriptor.cpp:33
linux_proc::comm
char comm[400]
Definition: ProcessDescriptor.cpp:270
System::POOLED_USAGE_AND_LIMITS::PeakNonPagedPoolUsage
unsigned long PeakNonPagedPoolUsage
Definition: ProcessDescriptor.h:90
linux_proc::starttime
unsigned long long starttime
Definition: ProcessDescriptor.cpp:290
System::POOLED_USAGE_AND_LIMITS::PagedPoolUsage
unsigned long PagedPoolUsage
Definition: ProcessDescriptor.h:88
System::ProcessAffinityMask
@ ProcessAffinityMask
Definition: ProcessDescriptor.cpp:54
System::POOLED_USAGE_AND_LIMITS::NonPagedPoolLimit
unsigned long NonPagedPoolLimit
Definition: ProcessDescriptor.h:92
System::QUOTA_LIMITS::TimeLimit
long long TimeLimit
Definition: ProcessDescriptor.h:48
linux_proc::tty
int tty
Definition: ProcessDescriptor.cpp:275
System.h
gaudirun.fd
fd
Definition: gaudirun.py:627
linux_proc::startcode
unsigned long startcode
Definition: ProcessDescriptor.cpp:294
System::ProcessWx86Information
@ ProcessWx86Information
Definition: ProcessDescriptor.cpp:52
linux_proc::itrealvalue
long itrealvalue
Definition: ProcessDescriptor.cpp:289
System::KERNEL_USER_TIMES::UserTime
long long UserTime
Definition: ProcessDescriptor.h:106
System::PROCESS_BASIC_INFORMATION::AffinityMask
unsigned long AffinityMask
Definition: ProcessDescriptor.h:31
System::VM_COUNTERS::PeakWorkingSetSize
unsigned long PeakWorkingSetSize
Definition: ProcessDescriptor.h:73
System::VM_COUNTERS::VirtualSize
unsigned long VirtualSize
Definition: ProcessDescriptor.h:71
System::ProcessVmCounters
@ ProcessVmCounters
Definition: ProcessDescriptor.cpp:36
NewInputWrite.fetch
fetch
Definition: NewInputWrite.py:45
linux_proc::wchan
unsigned long wchan
Definition: ProcessDescriptor.cpp:303
System::PROCESS_BASIC_INFORMATION::BasePriority
long BasePriority
Definition: ProcessDescriptor.h:32
System::processHandle
GAUDI_API ProcessHandle processHandle()
Handle to running process.
Definition: ModuleInfo.cpp:135
System::PROCESS_BASIC_INFORMATION::InheritedFromUniqueProcessId
unsigned long InheritedFromUniqueProcessId
Definition: ProcessDescriptor.h:34
linux_proc::ppid
int ppid
Definition: ProcessDescriptor.cpp:272
System::QUOTA_LIMITS::PagedPoolLimit
unsigned long PagedPoolLimit
Definition: ProcessDescriptor.h:43
System::VM_COUNTERS::PageFaultCount
unsigned long PageFaultCount
Definition: ProcessDescriptor.h:72
readProcStat
void readProcStat(long pid, linux_proc &pinfo)
Definition: ProcessDescriptor.cpp:311
linux_proc::tpgid
int tpgid
Definition: ProcessDescriptor.cpp:276
linux_proc::priority
long priority
Definition: ProcessDescriptor.cpp:286
System::ProcessAccessToken
@ ProcessAccessToken
Definition: ProcessDescriptor.cpp:42
linux_proc::majflt
unsigned long majflt
Definition: ProcessDescriptor.cpp:280
linux_proc::flags
unsigned long flags
Definition: ProcessDescriptor.cpp:277
linux_proc::stime
unsigned long stime
Definition: ProcessDescriptor.cpp:283
System::PriorityBoost
@ PriorityBoost
Definition: SystemBase.h:28
System::MaxProcessInfoClass
@ MaxProcessInfoClass
Definition: ProcessDescriptor.cpp:56
System::ProcessDescriptor::ProcessHandle::ProcessHandle
ProcessHandle(long pid)
Definition: ProcessDescriptor.cpp:364
System::ProcessDescriptor::ProcessDescriptor
ProcessDescriptor()
Definition: ProcessDescriptor.cpp:391
System::IO_COUNTERS::WriteOperationCount
unsigned long WriteOperationCount
Definition: ProcessDescriptor.h:56
linux_proc::num_threads
long num_threads
Definition: ProcessDescriptor.cpp:288
ModuleInfo.h
System::ProcessEllapsedTime
@ ProcessEllapsedTime
Definition: ProcessDescriptor.cpp:57
System::PROCESS_BASIC_INFORMATION::UniqueProcessId
unsigned long UniqueProcessId
Definition: ProcessDescriptor.h:33
bug_34121.t
t
Definition: bug_34121.py:30
System::ProcessDescriptor::~ProcessDescriptor
virtual ~ProcessDescriptor()
Definition: ProcessDescriptor.cpp:405
System::POOLED_USAGE_AND_LIMITS::PagefileUsage
unsigned long PagefileUsage
Definition: ProcessDescriptor.h:94
System::ProcessEnableAlignmentFaultFixup
@ ProcessEnableAlignmentFaultFixup
Definition: ProcessDescriptor.cpp:50
System::ProcessTimes
@ ProcessTimes
Definition: ProcessDescriptor.cpp:37
System::VM_COUNTERS::QuotaNonPagedPoolUsage
unsigned long QuotaNonPagedPoolUsage
Definition: ProcessDescriptor.h:78
System::ProcessDescriptor::ProcessHandle
Definition: ProcessDescriptor.h:119
System::VM_COUNTERS::PeakVirtualSize
unsigned long PeakVirtualSize
Definition: ProcessDescriptor.h:70
linux_proc::rlim
unsigned long rlim
Definition: ProcessDescriptor.cpp:293
System::Memory
@ Memory
Definition: SystemBase.h:28
System::ProcessDescriptor::ProcessHandle::m_handle
void * m_handle
Definition: ProcessDescriptor.h:120
System::PROCESS_BASIC_INFORMATION
Definition: ProcessDescriptor.h:28
std::cerr
System::POOLED_USAGE_AND_LIMITS
Process Pooled Quota Usage and Limits NtQueryInformationProcess using ProcessPooledUsageAndLimits.
Definition: ProcessDescriptor.h:86
System::PROCESS_BASIC_INFORMATION::ExitStatus
long ExitStatus
Definition: ProcessDescriptor.h:29
System::ProcessRaisePriority
@ ProcessRaisePriority
Definition: ProcessDescriptor.cpp:39
std::string::c_str
T c_str(T... args)
AlgSequencer.h
h
Definition: AlgSequencer.py:31
linux_proc::startstack
unsigned long startstack
Definition: ProcessDescriptor.cpp:296
linux_proc
Definition: ProcessDescriptor.cpp:268
System::QUOTA_LIMITS
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
Definition: ProcessDescriptor.h:42
System::ProcessDefaultHardErrorMode
@ ProcessDefaultHardErrorMode
Definition: ProcessDescriptor.cpp:45
System::IO_COUNTERS::ReadTransferCount
long long ReadTransferCount
Definition: ProcessDescriptor.h:61
System::ProcessUserModeIOPL
@ ProcessUserModeIOPL
Definition: ProcessDescriptor.cpp:49
System::ProcessDebugPort
@ ProcessDebugPort
Definition: ProcessDescriptor.cpp:40
linux_proc::cminflt
unsigned long cminflt
Definition: ProcessDescriptor.cpp:279
System::ProcessLdtSize
@ ProcessLdtSize
Definition: ProcessDescriptor.cpp:44
System::ProcessPriorityClass
@ ProcessPriorityClass
Definition: ProcessDescriptor.cpp:51
linux_proc::signal
unsigned long signal
Definition: ProcessDescriptor.cpp:299
linux_proc::minflt
unsigned long minflt
Definition: ProcessDescriptor.cpp:278
linux_proc::pgrp
int pgrp
Definition: ProcessDescriptor.cpp:273
linux_proc::kstkesp
unsigned long kstkesp
Definition: ProcessDescriptor.cpp:297
linux_proc::sigcatch
unsigned long sigcatch
Definition: ProcessDescriptor.cpp:302
System::IO_COUNTERS
Process I/O Counters NtQueryInformationProcess using ProcessIoCounters.
Definition: ProcessDescriptor.h:54
plotSpeedupsPyRoot.time
time
Definition: plotSpeedupsPyRoot.py:180
linux_proc::pid
int pid
Definition: ProcessDescriptor.cpp:269
System::KERNEL_USER_TIMES::KernelTime
long long KernelTime
Definition: ProcessDescriptor.h:105
System::ProcessDescriptor::ProcessHandle::~ProcessHandle
virtual ~ProcessHandle()
Definition: ProcessDescriptor.cpp:381
std::ostringstream
STL class.
hivetimeline.read
def read(f, regex=".*", skipevents=0)
Definition: hivetimeline.py:32
System::KERNEL_USER_TIMES::CreateTime
long long CreateTime
Definition: ProcessDescriptor.h:103
System::ProcessBasics
@ ProcessBasics
Definition: SystemBase.h:28
System::Times
@ Times
Definition: SystemBase.h:28
System::Quota
@ Quota
Definition: SystemBase.h:28
System::POOLED_USAGE_AND_LIMITS::NonPagedPoolUsage
unsigned long NonPagedPoolUsage
Definition: ProcessDescriptor.h:91
std::endl
T endl(T... args)
System::ProcessPooledUsageAndLimits
@ ProcessPooledUsageAndLimits
Definition: ProcessDescriptor.cpp:47
System::ProcessWorkingSetWatch
@ ProcessWorkingSetWatch
Definition: ProcessDescriptor.cpp:48
linux_proc::state
char state
Definition: ProcessDescriptor.cpp:271
System::POOLED_USAGE_AND_LIMITS::PeakPagedPoolUsage
unsigned long PeakPagedPoolUsage
Definition: ProcessDescriptor.h:87
System::QUOTA_LIMITS::MaximumWorkingSetSize
unsigned long MaximumWorkingSetSize
Definition: ProcessDescriptor.h:46
linux_proc::sigignore
unsigned long sigignore
Definition: ProcessDescriptor.cpp:301
System
Note: OS specific details for environment resolution.
Definition: Debugger.h:29
System::ProcessQuotaLimits
@ ProcessQuotaLimits
Definition: ProcessDescriptor.cpp:34
System::IO_COUNTERS::OtherTransferCount
long long OtherTransferCount
Definition: ProcessDescriptor.h:63
System::ProcessIoPortHandlers
@ ProcessIoPortHandlers
Definition: ProcessDescriptor.cpp:46
System::VM_COUNTERS
Process Virtual Memory Counters NtQueryInformationProcess using ProcessVmCounters.
Definition: ProcessDescriptor.h:69
System::KERNEL_USER_TIMES::ExitTime
long long ExitTime
Definition: ProcessDescriptor.h:104
System::ProcessExceptionPort
@ ProcessExceptionPort
Definition: ProcessDescriptor.cpp:41
System::PROCESS_BASIC_INFORMATION::PebBaseAddress
PPEB PebBaseAddress
Definition: ProcessDescriptor.h:30
linux_proc::session
int session
Definition: ProcessDescriptor.cpp:274
std::ostringstream::str
T str(T... args)
System::POOLED_USAGE_AND_LIMITS::PagedPoolLimit
unsigned long PagedPoolLimit
Definition: ProcessDescriptor.h:89
System::ProcessHandle
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:42
System::IO_COUNTERS::OtherOperationCount
unsigned long OtherOperationCount
Definition: ProcessDescriptor.h:57
linux_proc::cmajflt
unsigned long cmajflt
Definition: ProcessDescriptor.cpp:281
ProcessDescriptor.h
System::QUOTA_LIMITS::MinimumWorkingSetSize
unsigned long MinimumWorkingSetSize
Definition: ProcessDescriptor.h:45
System::QUOTA_LIMITS::PagefileLimit
unsigned long PagefileLimit
Definition: ProcessDescriptor.h:47
linux_proc::kstkeip
unsigned long kstkeip
Definition: ProcessDescriptor.cpp:298
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:41
System::IO_COUNTERS::ReadOperationCount
unsigned long ReadOperationCount
Definition: ProcessDescriptor.h:55
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
linux_proc::utime
unsigned long utime
Definition: ProcessDescriptor.cpp:282
linux_proc::nice
long nice
Definition: ProcessDescriptor.cpp:287
System::ProcessDescriptor::query
long query(long pid, InfoType info, PROCESS_BASIC_INFORMATION *buffer)
Definition: ProcessDescriptor.cpp:595
System::IO_COUNTERS::WriteTransferCount
long long WriteTransferCount
Definition: ProcessDescriptor.h:62
linux_proc::cstime
long cstime
Definition: ProcessDescriptor.cpp:285
System::VM_COUNTERS::QuotaPeakNonPagedPoolUsage
unsigned long QuotaPeakNonPagedPoolUsage
Definition: ProcessDescriptor.h:77
GaudiKernel.Constants.FALSE
FALSE
Definition: Constants.py:38
System::ProcessBasePriority
@ ProcessBasePriority
Definition: ProcessDescriptor.cpp:38
System::ProcessHandleCount
@ ProcessHandleCount
Definition: ProcessDescriptor.cpp:53
System::QUOTA_LIMITS::NonPagedPoolLimit
unsigned long NonPagedPoolLimit
Definition: ProcessDescriptor.h:44
linux_proc::cutime
long cutime
Definition: ProcessDescriptor.cpp:284
System::POOLED_USAGE_AND_LIMITS::PeakPagefileUsage
unsigned long PeakPagefileUsage
Definition: ProcessDescriptor.h:93
System::IO
@ IO
Definition: SystemBase.h:28