| The Gaudi Framework v32r1 (f65d50dc)

ProcessDescriptor.cpp
Go to the documentation of this file.
1 //====================================================================
2 // ProcStat.cpp
3 //--------------------------------------------------------------------
4 //
5 // Package : System (The LHCb System service)
6 //
7 // Description: Invoke interactively the ProcStat from a
8 // running application
9 //
10 // Author : M.Frank
11 // Created : 13/11/00
12 // Changes :
13 //
14 //====================================================================
15 #define GAUDIKERNEL_PROCSTAT_CPP
16
17 #ifndef __APPLE__
18 static const long TICK_TO_100NSEC = 100000;
19 #endif // not __APPLE__
20
21 namespace System {
22 enum ProcessInfoCommand {
23 ProcessBasicInformation,
24 ProcessQuotaLimits,
25 ProcessIoCounters,
26 ProcessVmCounters,
27 ProcessTimes,
28 ProcessBasePriority,
29 ProcessRaisePriority,
30 ProcessDebugPort,
31 ProcessExceptionPort,
32 ProcessAccessToken,
33 ProcessLdtInformation,
34 ProcessLdtSize,
35 ProcessDefaultHardErrorMode,
36 ProcessIoPortHandlers, // Note: this is kernel mode only
37 ProcessPooledUsageAndLimits,
38 ProcessWorkingSetWatch,
39 ProcessUserModeIOPL,
40 ProcessEnableAlignmentFaultFixup,
41 ProcessPriorityClass,
42 ProcessWx86Information,
43 ProcessHandleCount,
44 ProcessAffinityMask,
45 ProcessPriorityBoost,
46 MaxProcessInfoClass, //
47 ProcessEllapsedTime
48 };
49 }
50 #ifdef _WIN32
51 # define strcasecmp _stricmp
52 # define strncasecmp _strnicmp
53 # define NOMSG
54 # define NOGDI
55 # include "process.h"
56 # include "windows.h"
57 # define getpid _getpid
58 namespace NtApi {
59 //__declspec(dllimport) long __stdcall NtQueryInformationProcess(
60 // typedef __declspec(dllimport) long __stdcall (*__NtQueryInformationProcess)(
61 // extern "C" long __cdecl NtQueryInformationProcess(
62
63 typedef long( WINAPI* __NtQueryInformationProcess )(
64
65 void* ProcessHandle, long ProcessInformationClass, void* ProcessInformation,
66 unsigned long ProcessInformationLength, unsigned long* ReturnLength );
67 __NtQueryInformationProcess NtQueryInformationProcess;
68 }; // namespace NtApi
69 #else // UNIX...: first the EGCS stuff, then the OS dependent includes
70 # define WINVER 0
71 # include "libgen.h"
72 # include "sys/times.h"
73 # include "unistd.h"
74 # include <cstdio>
75 # include <errno.h>
76 # include <fcntl.h>
77 # include <iostream>
78 # include <sstream>
79 # include <string>
80 # include <sys/signal.h>
81 # include <sys/syscall.h>
82 # include <sys/types.h>
83 # include <unistd.h>
84 # ifndef __APPLE__
85 # include <sys/procfs.h>
86 # endif
87 # include <cstdio>
88 # include <sys/resource.h>
89 # include <sys/time.h>
90
91 /* Format of the Linux proc/stat (man 5 proc, kernel 2.6.35):
92 pid %d The process ID.
93
94 comm %s The filename of the executable, in parentheses. This is visible
95 whether or not the executable is swapped out.
96
97 state %c One character from the string "RSDZTW" where R is running, S is
98 sleeping in an interruptible wait, D is waiting in uninterruptible
99 disk sleep, Z is zombie, T is traced or stopped (on a signal), and
100 W is paging.
101
102 ppid %d The PID of the parent.
103
104 pgrp %d The process group ID of the process.
105
106 session %d The session ID of the process.
107
108 tty_nr %d The controlling terminal of the process. (The minor device number
109 is contained in the combination of bits 31 to 20 and 7 to 0; the
110 major device number is in bits 15 t0 8.)
111
112 tpgid %d The ID of the foreground process group of the controlling terminal
113 of the process.
114
115 flags %u (%lu before Linux 2.6.22)
116 The kernel flags word of the process. For bit meanings, see the
117 PF_* defines in <linux/sched.h>. Details depend on the kernel
118 version.
119
120 minflt %lu The number of minor faults the process has made which have not
121 required loading a memory page from disk.
122
123 cminflt %lu The number of minor faults that the process's waited-for children
124 have made.
125
126 majflt %lu The number of major faults the process has made which have
127 required loading a memory page from disk.
128
129 cmajflt %lu The number of major faults that the process's waited-for children
130 have made.
131
132 utime %lu Amount of time that this process has been scheduled in user mode,
133 measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This
134 includes guest time, guest_time (time spent running a virtual CPU,
135 see below), so that applications that are not aware of the guest
136 time field do not lose that time from their calculations.
137
138 stime %lu Amount of time that this process has been scheduled in kernel
139 mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
140
141 cutime %ld Amount of time that this process's waited-for children have been
142 scheduled in user mode, measured in clock ticks (divide by
143 sysconf(_SC_CLK_TCK). (See also times(2).) This includes guest
144 time, cguest_time (time spent running a virtual CPU, see below).
145
146 cstime %ld Amount of time that this process's waited-for children have been
147 scheduled in kernel mode, measured in clock ticks (divide by
148 sysconf(_SC_CLK_TCK).
149
150 priority %ld
151 (Explanation for Linux 2.6) For processes running a real-time
152 scheduling policy (policy below; see sched_setscheduler(2)), this
153 is the negated scheduling priority, minus one; that is, a number
154 in the range -2 to -100, corresponding to real-time priorities 1
155 to 99. For processes running under a non-real-time scheduling
156 policy, this is the raw nice value (setpriority(2)) as represented
157 in the kernel. The kernel stores nice values as numbers in the
158 range 0 (high) to 39 (low), corresponding to the user-visible nice
159 range of -20 to 19.
160
161 Before Linux 2.6, this was a scaled value based on the scheduler
162 weighting given to this process.
163
164 nice %ld The nice value (see setpriority(2)), a value in the range 19 (low
165 priority) to -20 (high priority).
166
167 num_threads %ld
168 Number of threads in this process (since Linux 2.6). Before ker‐
169 nel 2.6, this field was hard coded to 0 as a placeholder for an
170 earlier removed field.
171
172 itrealvalue %ld
173 The time in jiffies before the next SIGALRM is sent to the process
174 due to an interval timer. Since kernel 2.6.17, this field is no
175 longer maintained, and is hard coded as 0.
176
177 starttime %llu (was %lu before Linux 2.6)
178 The time in jiffies the process started after system boot.
179
180 vsize %lu Virtual memory size in bytes.
181
182 rss %ld Resident Set Size: number of pages the process has in real memory.
183 This is just the pages which count towards text, data, or stack
184 space. This does not include pages which have not been demand-
185 loaded in, or which are swapped out.
186
187 rsslim %lu Current soft limit in bytes on the rss of the process; see the
188 description of RLIMIT_RSS in getpriority(2).
189
190 startcode %lu
191 The address above which program text can run.
192
193 endcode %lu The address below which program text can run.
194
195 startstack %lu
196 The address of the start (i.e., bottom) of the stack.
197
198 kstkesp %lu The current value of ESP (stack pointer), as found in the kernel
199 stack page for the process.
200
201 kstkeip %lu The current EIP (instruction pointer).
202
203 signal %lu The bitmap of pending signals, displayed as a decimal number.
204 Obsolete, because it does not provide information on real-time
205 signals; use /proc/[pid]/status instead.
206
207 blocked %lu The bitmap of blocked signals, displayed as a decimal number.
208 Obsolete, because it does not provide information on real-time
209 signals; use /proc/[pid]/status instead.
210
211 sigignore %lu
212 The bitmap of ignored signals, displayed as a decimal number.
213 Obsolete, because it does not provide information on real-time
214 signals; use /proc/[pid]/status instead.
215
216 sigcatch %lu
217 The bitmap of caught 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 wchan %lu This is the "channel" in which the process is waiting. It is the
222 address of a system call, and can be looked up in a namelist if
223 you need a textual name. (If you have an up-to-date
224 /etc/psdatabase, then try ps -l to see the WCHAN field in action.)
225
226 nswap %lu Number of pages swapped (not maintained).
227
228 cnswap %lu Cumulative nswap for child processes (not maintained).
229
230 exit_signal %d (since Linux 2.1.22)
231 Signal to be sent to parent when we die.
232
233 processor %d (since Linux 2.2.8)
234 CPU number last executed on.
235
236 rt_priority %u (since Linux 2.5.19; was %lu before Linux 2.6.22)
237 Real-time scheduling priority, a number in the range 1 to 99 for
238 processes scheduled under a real-time policy, or 0, for non-real-
239 time processes (see sched_setscheduler(2)).
240
241 policy %u (since Linux 2.5.19; was %lu before Linux 2.6.22)
242 Scheduling policy (see sched_setscheduler(2)). Decode using the
243 SCHED_* constants in linux/sched.h.
244
245 delayacct_blkio_ticks %llu (since Linux 2.6.18)
246 Aggregated block I/O delays, measured in clock ticks (centisec‐
247 onds).
248
249 guest_time %lu (since Linux 2.6.24)
250 Guest time of the process (time spent running a virtual CPU for a
251 guest operating system), measured in clock ticks (divide by
252 sysconf(_SC_CLK_TCK).
253
254 cguest_time %ld (since Linux 2.6.24)
255 Guest time of the process's children, measured in clock ticks
256 (divide by sysconf(_SC_CLK_TCK).
257 */
258 struct linux_proc {
259 int pid;
260 char comm[400];
261 char state;
262 int ppid;
263 int pgrp;
264 int session;
265 int tty;
266 int tpgid;
267 unsigned long flags;
268 unsigned long minflt;
269 unsigned long cminflt;
270 unsigned long majflt;
271 unsigned long cmajflt;
272 unsigned long utime;
273 unsigned long stime;
274 long cutime;
275 long cstime;
276 long priority;
277 long nice;
278 long num_threads;
279 long itrealvalue;
280 unsigned long long starttime;
281 unsigned long vsize;
282 long rss;
283 unsigned long rlim;
284 unsigned long startcode;
285 unsigned long endcode;
286 unsigned long startstack;
287 unsigned long kstkesp;
288 unsigned long kstkeip;
289 unsigned long signal;
290 unsigned long blocked;
291 unsigned long sigignore;
292 unsigned long sigcatch;
293 unsigned long wchan;
294 };
295
296 # ifdef __APPLE__
297 // static long pg_size = 0;
298 # else
299 static long pg_size = sysconf( _SC_PAGESIZE ); // getpagesize();
300 # endif
301 void readProcStat( long pid, linux_proc& pinfo ) {
302
303 int cnt, fd;
304 char buf[512];
305
306 std::ostringstream ost;
307
308 ost << "/proc/" << pid << "/stat";
309 std::string fname = ost.str();
310 if ( ( fd = open( fname.c_str(), O_RDONLY ) ) < 0 ) {
311 std::cerr << "Failed to open " << ost.str() << std::endl;
312 return;
313 }
314
315 lseek( fd, 0, SEEK_SET );
316 if ( ( cnt = read( fd, buf, sizeof( buf ) - 1 ) ) < 0 ) {
317 std::cout << "LINUX Read of Proc file failed:" << std::endl;
318 close( fd );
319 return;
320 }
321
322 // Format
323 if ( cnt > 0 ) {
324 buf[cnt] = '\0';
325 sscanf( buf,
326 // 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
327 // 30 1 2 3 4 5
328 "%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 "
329 "%lu %lu %lu %lu %lu %lu %lu",
330 &pinfo.pid, pinfo.comm, &pinfo.state, &pinfo.ppid, &pinfo.pgrp, &pinfo.session, &pinfo.tty, &pinfo.tpgid,
331 &pinfo.flags, &pinfo.minflt, &pinfo.cminflt, &pinfo.majflt, &pinfo.cmajflt, &pinfo.utime, &pinfo.stime,
332 &pinfo.cutime, &pinfo.cstime, &pinfo.priority, &pinfo.nice, &pinfo.num_threads, &pinfo.itrealvalue,
333 &pinfo.starttime, &pinfo.vsize, &pinfo.rss, &pinfo.rlim, &pinfo.startcode, &pinfo.endcode,
334 &pinfo.startstack, &pinfo.kstkesp, &pinfo.kstkeip, &pinfo.signal, &pinfo.blocked, &pinfo.sigignore,
335 &pinfo.sigcatch, &pinfo.wchan );
336 }
337 close( fd );
338 }
339 #endif
340
341 // static long s_myPid = ::getpid();
342 // In order to properly support e.g. fork() calls, we cannot keep a copy of the pid!
343
344 #ifndef __APPLE__
345 static inline long processID( long pid ) { return ( pid > 0 ) ? pid : ( ::getpid() ); }
346 #endif // not __APPLE__
347
348 // Framework include files
349 #include "GaudiKernel/ModuleInfo.h"
350 #include "GaudiKernel/System.h"
351 #include "ProcessDescriptor.h"
352
353 System::ProcessDescriptor::ProcessHandle::ProcessHandle( long pid ) {
354 if ( pid > 0 ) {
355 if ( pid != ::getpid() ) {
356 #ifdef _WIN32
357 m_handle = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid );
358 #else
359 // Note: the return type of getpid is pid_t, which is int on 64bit machines too
360 m_handle = reinterpret_cast<void*>( static_cast<long>( ::getpid() ) );
361 #endif
362 m_needRelease = true;
363 return;
364 }
365 }
366 m_handle = processHandle();
367 m_needRelease = false;
368 }
369
370 System::ProcessDescriptor::ProcessHandle::~ProcessHandle() {
371 if ( m_needRelease ) {
372 #ifdef _WIN32
373 ::CloseHandle( m_handle );
374 #else
375 m_handle = nullptr;
376 #endif
377 }
378 }
379
380 System::ProcessDescriptor::ProcessDescriptor() {
381 #ifdef _WIN32
382 static bool first = true;
383 if ( first ) {
384 first = false;
385 void* mh = ::LoadLibrary( "NTDll.dll" );
386 if ( mh ) {
387 NtApi::NtQueryInformationProcess =
388 ( NtApi::__NtQueryInformationProcess )::GetProcAddress( (HINSTANCE)mh, "NtQueryInformationProcess" );
389 }
390 }
391 #endif
392 }
393
394 System::ProcessDescriptor::~ProcessDescriptor() {}
395
396 long System::ProcessDescriptor::query( long pid, InfoType fetch, IO_COUNTERS* info ) {
397 if ( info == 0 ) return 0;
398 long status = 1;
399
400 if ( fetch == IO ) {
401 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
402 ProcessHandle h( pid );
403 status = NtApi::NtQueryInformationProcess( h.handle(), ProcessIoCounters, info, sizeof( IO_COUNTERS ), 0 );
404 status = ( status == 0 ) ? 1 : 0;
405 #elif defined( _WIN32 ) // Windows 95,98...
406 #elif defined( __linux )
407 linux_proc prc;
408 readProcStat( processID( pid ), prc );
409 rusage usage;
410 getrusage( RUSAGE_SELF, &usage );
411 info->ReadOperationCount = usage.ru_inblock;
412 info->WriteOperationCount = usage.ru_oublock;
413 info->OtherOperationCount = 0;
414 info->ReadTransferCount = usage.ru_inblock;
415 info->WriteTransferCount = usage.ru_oublock;
416 info->OtherTransferCount = 0;
417 #else // All Other
418 if ( pid ) {}
419 #endif // End ALL OS
420 }
421 return status;
422 }
423
424 long System::ProcessDescriptor::query( long pid, InfoType fetch, POOLED_USAGE_AND_LIMITS* info ) {
425 if ( info == 0 ) return 0;
426 long status = 1;
427
428 if ( fetch == Quota ) {
429 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
430 ProcessHandle h( pid );
431 status = NtApi::NtQueryInformationProcess( h.handle(), ProcessPooledUsageAndLimits, info,
432 sizeof( POOLED_USAGE_AND_LIMITS ), 0 );
433 status = ( status == 0 ) ? 1 : 0;
434 #elif defined( _WIN32 ) // Windows 95,98...
435 #elif defined( __linux ) // Linux
436 // rusage usage;
437 // getrusage(RUSAGE_SELF, &usage);
438 rlimit lim;
439
440 getrlimit( RLIMIT_DATA, &lim );
441 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
442 info->PeakPagedPoolUsage = lim.rlim_cur;
443 info->PagedPoolUsage = lim.rlim_cur;
444 info->PagedPoolLimit = lim.rlim_max;
445
446 getrlimit( RLIMIT_STACK, &lim );
447 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
448 info->PeakNonPagedPoolUsage = lim.rlim_cur;
449 info->NonPagedPoolUsage = lim.rlim_cur;
450 info->NonPagedPoolLimit = lim.rlim_max;
451
452 linux_proc prc;
453 readProcStat( processID( pid ), prc );
454 info->PeakPagefileUsage = prc.rss * pg_size;
455 info->PagefileUsage = prc.rss * pg_size;
456 info->PagefileLimit = 0xFFFFFFFF;
457 #elif defined( __APPLE__ )
458 if ( pid ) {}
459 #else // All Other
460 if ( pid ) {}
461 #endif // End ALL OS
462 }
463
464 return status;
465 }
466
467 long System::ProcessDescriptor::query( long pid, InfoType fetch, long* info ) {
468
469 if ( info == 0 ) return 0;
470 long status = 1;
471
472 switch ( fetch ) {
473 case PriorityBoost:
474 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
475 ProcessHandle h( pid );
476 status = NtApi::NtQueryInformationProcess( h.handle(), ProcessPriorityBoost, info, sizeof( long ), 0 );
477 #elif defined( _WIN32 ) // Windows 95,98...
478 if ( pid ) {}
479 #else
480 // Not applicable
481 if ( pid > 0 ) status = 0; // to avoid compiler warning
482 status = 0;
483 *info = 0;
484 #endif // End ALL OS
485 status = ( status == 0 ) ? 1 : 0;
486 break;
487 default:
488 status = -1;
489 info = &status;
490 break;
491 }
492 return status;
493 }
494
495 long System::ProcessDescriptor::query( long pid, InfoType fetch, VM_COUNTERS* info ) {
496 if ( info == 0 ) return 0;
497 long status = 1;
498
499 if ( fetch == Memory ) {
500 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
501 ProcessHandle h( pid );
502 status = NtApi::NtQueryInformationProcess( h.handle(), ProcessVmCounters, info, sizeof( VM_COUNTERS ), 0 );
503 status = ( status == 0 ) ? 1 : 0;
504 #elif defined( _WIN32 ) // Windows 95,98...
505 #elif defined( __linux ) // Linux
506 const ssize_t bufsize = 1024;
507 char buf[bufsize];
508 pid = processID( pid );
509 sprintf( buf, "/proc/%ld/statm", pid );
510 long size, resident, share, trs, lrs, drs, dt;
511 int fd = open( buf, O_RDONLY );
512 ssize_t nread = read( fd, buf, bufsize );
513 close( fd );
514 if ( nread < bufsize && nread >= 0 ) buf[nread] = '\0';
515 fd = sscanf( buf, "%ld %ld %ld %ld %ld %ld %ld", &size, &resident, &share, &trs, &drs, &lrs, &dt );
516 linux_proc prc;
517 readProcStat( pid, prc );
518 info->PeakVirtualSize = prc.vsize;
519 info->VirtualSize = prc.vsize;
520 info->PeakWorkingSetSize = resident * pg_size;
521 info->WorkingSetSize = resident * pg_size;
522 info->QuotaPeakPagedPoolUsage = share * pg_size;
523 info->QuotaPagedPoolUsage = share * pg_size;
524 info->QuotaNonPagedPoolUsage = ( trs + drs ) * pg_size; // drs = data/stack size
525 info->QuotaPeakNonPagedPoolUsage = ( trs + drs ) * pg_size; // trs = VmExe size
526 info->PageFaultCount = prc.majflt + prc.minflt;
527 info->PagefileUsage = prc.vsize - resident * pg_size;
528 info->PeakPagefileUsage = prc.vsize - resident * pg_size;
529 #elif defined( __APPLE__ )
530 if ( pid ) {}
531 #else // All Other
532 if ( pid ) {}
533 #endif // End ALL OS
534 }
535 return status;
536 }
537
538 long System::ProcessDescriptor::query( long pid, InfoType fetch, QUOTA_LIMITS* info ) {
539 if ( info == 0 ) return 0;
540 long status = 1;
541
542 if ( fetch == Quota ) {
543 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
544 ProcessHandle h( pid );
545 status = NtApi::NtQueryInformationProcess( h.handle(), ProcessQuotaLimits, info, sizeof( QUOTA_LIMITS ), 0 );
546 status = ( status == 0 ) ? 1 : 0;
547 #elif defined( _WIN32 ) // Windows 95,98...
548 #elif defined( __linux ) // Linux
549 // On linux all this stuff typically is not set
550 // (ie. rlim_max=RLIM_INFINITY...)
551
552 if ( pid > 0 && pid != ::getpid() ) return 0; // only possible for myself
553
554 rlimit lim;
555 getrlimit( RLIMIT_DATA, &lim );
556 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
557 info->PagedPoolLimit = lim.rlim_max;
558
559 getrlimit( RLIMIT_STACK, &lim );
560 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
561 info->NonPagedPoolLimit = lim.rlim_max;
562 info->MinimumWorkingSetSize = 0;
563
564 getrlimit( RLIMIT_RSS, &lim );
565 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
566 info->MaximumWorkingSetSize = lim.rlim_max;
567
568 getrlimit( RLIMIT_AS, &lim );
569 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
570 info->PagefileLimit = lim.rlim_max;
571
572 getrlimit( RLIMIT_CPU, &lim );
573 if ( lim.rlim_max == RLIM_INFINITY ) lim.rlim_max = 0xFFFFFFFF;
574 info->TimeLimit = lim.rlim_max;
575 #elif defined( __APPLE__ )
576 if ( pid ) {}
577 #else // All Other
578 if ( pid ) {}
579 #endif // End ALL OS
580 }
581 return status;
582 }
583
584 long System::ProcessDescriptor::query( long pid, InfoType fetch, PROCESS_BASIC_INFORMATION* info ) {
585 if ( info == 0 ) return 0;
586 long status = 1;
587
588 if ( fetch == ProcessBasics ) {
589 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
590 ProcessHandle h( pid );
591 status = NtApi::NtQueryInformationProcess( h.handle(), ProcessBasicInformation, info,
592 sizeof( PROCESS_BASIC_INFORMATION ), 0 );
593 status = ( status == 0 ) ? 1 : 0;
594 #elif defined( _WIN32 ) // Windows 95,98...
595 #elif defined( __linux ) // Linux
596 linux_proc prc;
597 pid = processID( pid );
598 readProcStat( pid, prc );
599 info->ExitStatus = 0;
600 info->PebBaseAddress = (PPEB)prc.startcode;
601 info->BasePriority = 2 * 15 - prc.priority;
602 // std::cout << "Base Priority=" << info->BasePriority << "|"
603 // << prc.priority << std::endl;
604 info->AffinityMask = prc.flags;
605 // std::cout << "Flags =" << info->AffinityMask << "|"
606 // << prc.flags << std::endl;
607 info->UniqueProcessId = pid;
608 info->InheritedFromUniqueProcessId = prc.ppid;
609 #else // All Other
610 if ( pid ) {}
611 #endif // End ALL OS
612 }
613 return status;
614 }
615
616 long System::ProcessDescriptor::query( long pid, InfoType fetch, KERNEL_USER_TIMES* info ) {
617 if ( info == 0 ) return 0;
618 long status = 1;
619
620 if ( fetch == Times ) {
621 #if defined( _WIN32 ) && WINVER >= 0x0400 // Windows NT
622 ProcessHandle h( pid );
623 status = NtApi::NtQueryInformationProcess( h.handle(), ProcessTimes, info, sizeof( KERNEL_USER_TIMES ), 0 );
624 status = ( status == 0 ) ? 1 : 0;
625 #elif defined( _WIN32 ) // Windows 95,98...
626 #elif defined( __linux ) // Linux
627 static long long prc_start = 0;
628 bool myself = pid <= 0 || pid == ::getpid(); // avoid unnecessary calls to getpid if pid<0
629 if ( myself && prc_start == 0 ) { // called only once to set prc_start
630 linux_proc prc;
631 readProcStat( processID( pid ), prc );
632 // prc.startup is in ticks since system start, need to offset for absolute time
633 tms tmsb;
634 static long long offset =
635 100 * static_cast<long long>( time( nullptr ) ) - static_cast<long long>( times( &tmsb ) );
636 prc_start = ( prc.starttime + offset ) * TICK_TO_100NSEC;
637 }
638
639 if ( myself ) { // myself
640 tms tmsb;
641 times( &tmsb );
642 info->UserTime = tmsb.tms_utime * TICK_TO_100NSEC;
643 info->KernelTime = tmsb.tms_stime * TICK_TO_100NSEC;
644 info->CreateTime = prc_start;
645 } else { // other process
646 linux_proc prc;
647 readProcStat( processID( pid ), prc );
648 tms tmsb;
649 static long long offset =
650 100 * static_cast<long long>( time( nullptr ) ) - static_cast<long long>( times( &tmsb ) );
651
652 tms t;
653 times( &t );
654 info->UserTime = t.tms_utime * TICK_TO_100NSEC;
655 info->KernelTime = t.tms_stime * TICK_TO_100NSEC;
656 info->CreateTime = ( prc.starttime + offset ) * TICK_TO_100NSEC;
657 }
658 info->ExitTime = 0;
659
660 status = 1;
661
662 #elif defined( __APPLE__ )
663 if ( pid ) {}
664 // FIXME (MCl): Make an alternative function get timing on OSX
665 // times() seems to cause a segmentation fault
666 #else // no /proc file system: assume sys_start for the first call
667 tms tmsb;
668 static clock_t sys_start = times( 0 );
669 static long long offset = 100 * long long( time( 0 ) ) - sys_start;
670 clock_t now = times( &tmsb );
671 info->CreateTime = offset + now;
672 info->UserTime = tmsb.tms_utime;
673 info->KernelTime = tmsb.tms_stime;
674 info->CreateTime *= TICK_TO_100NSEC;
675 info->UserTime *= TICK_TO_100NSEC;
676 info->KernelTime *= TICK_TO_100NSEC;
677 info->ExitTime = 0;
678 status = 1;
679 #endif
680 }
681
682 return status;
683 }
linux_proc::minflt
unsigned long minflt
Definition: ProcessDescriptor.cpp:268
plotSpeedupsPyRoot.time
def time
Definition: plotSpeedupsPyRoot.py:152
System::ProcessDebugPort
Definition: ProcessDescriptor.cpp:30
linux_proc::kstkesp
unsigned long kstkesp
Definition: ProcessDescriptor.cpp:287
System.h
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:14
System::ProcessIoPortHandlers
Definition: ProcessDescriptor.cpp:36
System::ProcessWx86Information
Definition: ProcessDescriptor.cpp:42
System::ProcessUserModeIOPL
Definition: ProcessDescriptor.cpp:39
System::KERNEL_USER_TIMES
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
Definition: ProcessDescriptor.h:92
System::Memory
Definition: SystemBase.h:18
linux_proc::signal
unsigned long signal
Definition: ProcessDescriptor.cpp:289
linux_proc::wchan
unsigned long wchan
Definition: ProcessDescriptor.cpp:293
gaudirun.fd
fd
Definition: gaudirun.py:559
linux_proc::sigcatch
unsigned long sigcatch
Definition: ProcessDescriptor.cpp:292
linux_proc::state
char state
Definition: ProcessDescriptor.cpp:261
linux_proc::tty
int tty
Definition: ProcessDescriptor.cpp:265
System::ProcessBasicInformation
Definition: ProcessDescriptor.cpp:23
ProcessDescriptor.h
linux_proc::flags
unsigned long flags
Definition: ProcessDescriptor.cpp:267
linux_proc::pid
int pid
Definition: ProcessDescriptor.cpp:259
System
Note: OS specific details for environment resolution.
Definition: Debugger.h:19
System::Times
Definition: SystemBase.h:18
System::ProcessDescriptor::ProcessHandle::ProcessHandle
ProcessHandle(long pid)
Definition: ProcessDescriptor.cpp:353
System::ProcessEllapsedTime
Definition: ProcessDescriptor.cpp:47
linux_proc
Definition: ProcessDescriptor.cpp:258
std::endl
T endl(T... args)
System::IO_COUNTERS
Process I/O Counters NtQueryInformationProcess using ProcessIoCounters.
Definition: ProcessDescriptor.h:44
System::ProcessAccessToken
Definition: ProcessDescriptor.cpp:32
linux_proc::comm
char comm[400]
Definition: ProcessDescriptor.cpp:260
System::ProcessPriorityClass
Definition: ProcessDescriptor.cpp:41
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:41
linux_proc::num_threads
long num_threads
Definition: ProcessDescriptor.cpp:278
System::ProcessDescriptor::ProcessDescriptor
ProcessDescriptor()
Definition: ProcessDescriptor.cpp:380
System::ProcessHandleCount
Definition: ProcessDescriptor.cpp:43
hivetimeline.read
def read(f, regex='.*', skipevents=0)
Definition: hivetimeline.py:22
ModuleInfo.h
System::ProcessLdtSize
Definition: ProcessDescriptor.cpp:34
System::ProcessDescriptor::ProcessHandle::~ProcessHandle
virtual ~ProcessHandle()
Definition: ProcessDescriptor.cpp:370
System::processHandle
GAUDI_API ProcessHandle processHandle()
Handle to running process.
Definition: ModuleInfo.cpp:125
System::ProcessBasePriority
Definition: ProcessDescriptor.cpp:28
System::ProcessLdtInformation
Definition: ProcessDescriptor.cpp:33
System::ProcessIoCounters
Definition: ProcessDescriptor.cpp:25
linux_proc::session
int session
Definition: ProcessDescriptor.cpp:264
readProcStat
void readProcStat(long pid, linux_proc &pinfo)
Definition: ProcessDescriptor.cpp:301
std::string
STL class.
System::MaxProcessInfoClass
Definition: ProcessDescriptor.cpp:46
linux_proc::cstime
long cstime
Definition: ProcessDescriptor.cpp:275
System::PROCESS_BASIC_INFORMATION
Definition: ProcessDescriptor.h:18
linux_proc::pgrp
int pgrp
Definition: ProcessDescriptor.cpp:263
linux_proc::cutime
long cutime
Definition: ProcessDescriptor.cpp:274
linux_proc::nice
long nice
Definition: ProcessDescriptor.cpp:277
std::ostringstream
STL class.
System::ProcessQuotaLimits
Definition: ProcessDescriptor.cpp:24
linux_proc::itrealvalue
long itrealvalue
Definition: ProcessDescriptor.cpp:279
System::ProcessDescriptor::ProcessHandle
Definition: ProcessDescriptor.h:109
System::Quota
Definition: SystemBase.h:18
System::ProcessPriorityBoost
Definition: ProcessDescriptor.cpp:45
linux_proc::vsize
unsigned long vsize
Definition: ProcessDescriptor.cpp:281
std::ostringstream::str
T str(T... args)
System::ProcessExceptionPort
Definition: ProcessDescriptor.cpp:31
linux_proc::startstack
unsigned long startstack
Definition: ProcessDescriptor.cpp:286
linux_proc::rss
long rss
Definition: ProcessDescriptor.cpp:282
linux_proc::cminflt
unsigned long cminflt
Definition: ProcessDescriptor.cpp:269
GaudiKernel.Constants.FALSE
bool FALSE
Definition: Constants.py:21
System::ProcessWorkingSetWatch
Definition: ProcessDescriptor.cpp:38
linux_proc::tpgid
int tpgid
Definition: ProcessDescriptor.cpp:266
System::ProcessAffinityMask
Definition: ProcessDescriptor.cpp:44
linux_proc::majflt
unsigned long majflt
Definition: ProcessDescriptor.cpp:270
System::ProcessEnableAlignmentFaultFixup
Definition: ProcessDescriptor.cpp:40
linux_proc::starttime
unsigned long long starttime
Definition: ProcessDescriptor.cpp:280
linux_proc::cmajflt
unsigned long cmajflt
Definition: ProcessDescriptor.cpp:271
linux_proc::kstkeip
unsigned long kstkeip
Definition: ProcessDescriptor.cpp:288
System::QUOTA_LIMITS
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
Definition: ProcessDescriptor.h:32
linux_proc::priority
long priority
Definition: ProcessDescriptor.cpp:276
System::ProcessDescriptor::query
long query(long pid, InfoType info, PROCESS_BASIC_INFORMATION *buffer)
Definition: ProcessDescriptor.cpp:584
linux_proc::startcode
unsigned long startcode
Definition: ProcessDescriptor.cpp:284
System::PPEB
struct _PEB * PPEB
Basic Process Information NtQueryInformationProcess using ProcessBasicInfo.
Definition: ProcessDescriptor.h:17
System::IO
Definition: SystemBase.h:18
System::ProcessRaisePriority
Definition: ProcessDescriptor.cpp:29
std::string::c_str
T c_str(T... args)
System::ProcessDescriptor::ProcessHandle::m_needRelease
bool m_needRelease
Definition: ProcessDescriptor.h:111
System::ProcessDefaultHardErrorMode
Definition: ProcessDescriptor.cpp:35
linux_proc::utime
unsigned long utime
Definition: ProcessDescriptor.cpp:272
System::VM_COUNTERS
Process Virtual Memory Counters NtQueryInformationProcess using ProcessVmCounters.
Definition: ProcessDescriptor.h:59
linux_proc::ppid
int ppid
Definition: ProcessDescriptor.cpp:262
System::POOLED_USAGE_AND_LIMITS
Process Pooled Quota Usage and Limits NtQueryInformationProcess using ProcessPooledUsageAndLimits.
Definition: ProcessDescriptor.h:76
System::ProcessBasics
Definition: SystemBase.h:18
linux_proc::blocked
unsigned long blocked
Definition: ProcessDescriptor.cpp:290
System::ProcessDescriptor::ProcessHandle::m_handle
void * m_handle
Definition: ProcessDescriptor.h:110
System::PriorityBoost
Definition: SystemBase.h:18
System::ProcessDescriptor::~ProcessDescriptor
virtual ~ProcessDescriptor()
Definition: ProcessDescriptor.cpp:394
System::ProcessTimes
Definition: ProcessDescriptor.cpp:27
System::ProcessInfoCommand
ProcessInfoCommand
Definition: ProcessDescriptor.cpp:22
std::cerr
linux_proc::endcode
unsigned long endcode
Definition: ProcessDescriptor.cpp:285
linux_proc::stime
unsigned long stime
Definition: ProcessDescriptor.cpp:273
linux_proc::sigignore
unsigned long sigignore
Definition: ProcessDescriptor.cpp:291
System::ProcessPooledUsageAndLimits
Definition: ProcessDescriptor.cpp:37
System::InfoType
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:18
System::ProcessDescriptor::ProcessHandle::handle
void * handle()
Definition: ProcessDescriptor.h:117
System::ProcessHandle
void * ProcessHandle
Definition of the process handle.
Definition: ModuleInfo.h:32
linux_proc::rlim
unsigned long rlim
Definition: ProcessDescriptor.cpp:283
System::ProcessVmCounters
Definition: ProcessDescriptor.cpp:26