The Gaudi Framework  v40r0 (475e45c1)
Memory.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #include "ProcessDescriptor.h"
12 #include <GaudiKernel/Memory.h>
13 #include <limits.h>
14 
15 #ifdef _WIN32
16 # include <process.h>
17 # define getpid _getpid
18 #else
19 # include <libgen.h>
20 # include <sys/times.h>
21 # include <unistd.h>
22 #endif
23 
25 long System::adjustMemory( MemoryUnit unit, long value ) {
26  if ( value != -1 ) {
27  switch ( unit ) {
28  case Byte:
29  break;
30  case kByte:
31  value = value / 1024;
32  break;
33  case MByte:
34  value = ( value / 1024 ) / 1024;
35  break;
36  case GByte:
37  value = ( ( value / 1024 ) / 1024 ) / 1024;
38  break;
39  case TByte:
40  value = ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024;
41  break;
42  case PByte:
43  value = ( ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024;
44  break;
45  case EByte:
46  value = ( ( ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024;
47  break;
48  default:
49  value = -1;
50  break;
51  }
52  }
53  return value;
54 }
55 
57 long System::basePriority( InfoType fetch, long pid ) {
59  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.BasePriority;
60  return 0;
61 }
62 
65  static const long s_pid = ::getpid();
66  return s_pid;
67 }
68 
70 long System::parentID( InfoType fetch, long pid ) {
72  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.InheritedFromUniqueProcessId;
73  return 0;
74 }
75 
77 long System::affinityMask( InfoType fetch, long pid ) {
79  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.AffinityMask;
80  return 0;
81 }
82 
84 long System::exitStatus( InfoType fetch, long pid ) {
86  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.ExitStatus;
87  return -2;
88 }
89 
92  long info;
93  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return info;
94  return -2;
95 }
96 
99  VM_COUNTERS info;
100  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
101  return adjustMemory( unit, info.QuotaPeakNonPagedPoolUsage );
102  return -2;
103 }
104 
107  VM_COUNTERS info;
108  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
109  return adjustMemory( unit, info.QuotaNonPagedPoolUsage );
110  return -2;
111 }
112 
116  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
117  return adjustMemory( unit, quota.NonPagedPoolLimit );
118  return 0;
119 }
120 
122 long System::pagedMemory( MemoryUnit unit, InfoType fetch, long pid ) {
123  VM_COUNTERS info;
124  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
125  return adjustMemory( unit, info.QuotaPagedPoolUsage );
126  return -2;
127 }
128 
131  VM_COUNTERS info;
132  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
133  return adjustMemory( unit, info.QuotaPeakPagedPoolUsage );
134  return -2;
135 }
136 
140  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
141  return adjustMemory( unit, quota.PagedPoolLimit );
142  return 0;
143 }
144 
147  VM_COUNTERS info;
148  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return info.PageFaultCount;
149  return -2;
150 }
151 
154  VM_COUNTERS info;
155  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.PagefileUsage );
156  return -2;
157 }
158 
161  VM_COUNTERS info;
162  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
163  return adjustMemory( unit, info.PeakPagefileUsage );
164  return -2;
165 }
166 
170  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
171  if ( long( quota.PagefileLimit ) < 0 ) return -1; // LONG_MAX;
172  return adjustMemory( unit, quota.PagefileLimit );
173  }
174  return -2;
175 }
176 
179  VM_COUNTERS info;
180  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.WorkingSetSize );
181  return -2;
182 }
183 
186  VM_COUNTERS info;
187  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
188  return adjustMemory( unit, info.PeakWorkingSetSize );
189  return -2;
190 }
191 
194  QUOTA_LIMITS quota;
195  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
196  return adjustMemory( unit, quota.MinimumWorkingSetSize );
197  return 0;
198 }
199 
202  QUOTA_LIMITS quota;
203  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
204  return adjustMemory( unit, quota.MaximumWorkingSetSize );
205  return 0;
206 }
207 
210  VM_COUNTERS info;
211  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.VirtualSize );
212  return -2;
213 }
214 
217  VM_COUNTERS info;
218  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.PeakVirtualSize );
219  return -2;
220 }
221 
224  QUOTA_LIMITS quota;
225  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
226  if ( long( quota.PagefileLimit ) == -1 ) return -1; // LONG_MAX;
227  return adjustMemory( unit, quota.PagefileLimit );
228  }
229  return 0;
230 }
System::GByte
@ GByte
Definition: Memory.h:50
System::VM_COUNTERS::QuotaPeakPagedPoolUsage
unsigned long QuotaPeakPagedPoolUsage
Definition: ProcessDescriptor.h:74
System::exitStatus
GAUDI_API long exitStatus(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Exit status (does not really make sense for the running process,...
Definition: Memory.cpp:84
System::VM_COUNTERS::PeakPagefileUsage
unsigned long PeakPagefileUsage
Definition: ProcessDescriptor.h:79
System::pagefileUsage
GAUDI_API long pagefileUsage(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: Current page file usage.
Definition: Memory.cpp:153
System::VM_COUNTERS::QuotaPagedPoolUsage
unsigned long QuotaPagedPoolUsage
Definition: ProcessDescriptor.h:75
System::VM_COUNTERS::WorkingSetSize
unsigned long WorkingSetSize
Definition: ProcessDescriptor.h:73
System::InfoType
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:15
System::VM_COUNTERS::PagefileUsage
unsigned long PagefileUsage
Definition: ProcessDescriptor.h:78
System::POOLED_USAGE_AND_LIMITS::PagefileLimit
unsigned long PagefileLimit
Definition: ProcessDescriptor.h:94
System::MByte
@ MByte
Definition: Memory.h:50
System::POOLED_USAGE_AND_LIMITS::NonPagedPoolLimit
unsigned long NonPagedPoolLimit
Definition: ProcessDescriptor.h:91
System::mappedMemoryPeak
GAUDI_API long mappedMemoryPeak(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:185
System::pagedMemoryPeak
GAUDI_API long pagedMemoryPeak(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: Maximum of paged memory occupied by the process 'pid'.
Definition: Memory.cpp:130
System::parentID
GAUDI_API long parentID(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Parent's process ID.
Definition: Memory.cpp:70
System::PROCESS_BASIC_INFORMATION::AffinityMask
unsigned long AffinityMask
Definition: ProcessDescriptor.h:30
System::VM_COUNTERS::PeakWorkingSetSize
unsigned long PeakWorkingSetSize
Definition: ProcessDescriptor.h:72
Memory.h
System::EByte
@ EByte
Definition: Memory.h:50
System::VM_COUNTERS::VirtualSize
unsigned long VirtualSize
Definition: ProcessDescriptor.h:70
NewInputWrite.fetch
fetch
Definition: NewInputWrite.py:45
System::adjustMemory
GAUDI_API long adjustMemory(MemoryUnit typ, long value)
Convert time from kByte to requested representation (Experts only)
Definition: Memory.cpp:25
System::PROCESS_BASIC_INFORMATION::BasePriority
long BasePriority
Definition: ProcessDescriptor.h:31
System::PROCESS_BASIC_INFORMATION::InheritedFromUniqueProcessId
unsigned long InheritedFromUniqueProcessId
Definition: ProcessDescriptor.h:33
System::nonPagedMemory
GAUDI_API long nonPagedMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: Current usage of non paged memory.
Definition: Memory.cpp:106
System::nonPagedMemoryPeak
GAUDI_API long nonPagedMemoryPeak(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: Peak usage of non paged memory.
Definition: Memory.cpp:98
System::VM_COUNTERS::PageFaultCount
unsigned long PageFaultCount
Definition: ProcessDescriptor.h:71
System::priorityBoost
GAUDI_API long priorityBoost(InfoType fetch=PriorityBoost, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:91
System::virtualMemoryLimit
GAUDI_API long virtualMemoryLimit(MemoryUnit unit=kByte, InfoType fetch=Quota, long pid=-1)
System Process Limits: Maximum amount of the page file this process is allowed to use.
Definition: Memory.cpp:223
System::NoFetch
@ NoFetch
Definition: SystemBase.h:15
System::VM_COUNTERS::QuotaNonPagedPoolUsage
unsigned long QuotaNonPagedPoolUsage
Definition: ProcessDescriptor.h:77
System::VM_COUNTERS::PeakVirtualSize
unsigned long PeakVirtualSize
Definition: ProcessDescriptor.h:69
System::PROCESS_BASIC_INFORMATION
Definition: ProcessDescriptor.h:27
System::pagefileUsageLimit
GAUDI_API long pagefileUsageLimit(MemoryUnit unit=kByte, InfoType fetch=Quota, long pid=-1)
Basic Process Information: Peak usage of page file.
Definition: Memory.cpp:168
System::POOLED_USAGE_AND_LIMITS
Process Pooled Quota Usage and Limits NtQueryInformationProcess using ProcessPooledUsageAndLimits.
Definition: ProcessDescriptor.h:85
System::PROCESS_BASIC_INFORMATION::ExitStatus
long ExitStatus
Definition: ProcessDescriptor.h:28
System::pagedMemoryLimit
GAUDI_API long pagedMemoryLimit(MemoryUnit unit=kByte, InfoType fetch=Quota, long pid=-1)
Basic Process Information: Amount of paged memory that can be occupied by the process 'pid'.
Definition: Memory.cpp:138
System::QUOTA_LIMITS
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
Definition: ProcessDescriptor.h:41
System::Byte
@ Byte
Definition: Memory.h:50
System::pagefileUsagePeak
GAUDI_API long pagefileUsagePeak(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: Peak usage of page file.
Definition: Memory.cpp:160
System::nonPagedMemoryLimit
GAUDI_API long nonPagedMemoryLimit(MemoryUnit unit=kByte, InfoType fetch=Quota, long pid=-1)
System Process Limits: Maximum amount of non-paged memory this process is allowed to use.
Definition: Memory.cpp:114
System::minMemoryLimit
GAUDI_API long minMemoryLimit(MemoryUnit unit=kByte, InfoType fetch=Quota, long pid=-1)
System Process Limits: Minimum amount of virtual memory this process may use.
Definition: Memory.cpp:193
System::affinityMask
GAUDI_API long affinityMask(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Affinity mask.
Definition: Memory.cpp:77
System::basePriority
GAUDI_API long basePriority(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Base priority.
Definition: Memory.cpp:57
System::ProcessBasics
@ ProcessBasics
Definition: SystemBase.h:15
System::virtualMemoryPeak
GAUDI_API long virtualMemoryPeak(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:216
System::QUOTA_LIMITS::MaximumWorkingSetSize
unsigned long MaximumWorkingSetSize
Definition: ProcessDescriptor.h:45
System::getProcess
ProcessDescriptor * getProcess()
Retrieve Process structure.
Definition: ProcessDescriptor.h:141
System::kByte
@ kByte
Definition: Memory.h:50
System::VM_COUNTERS
Process Virtual Memory Counters NtQueryInformationProcess using ProcessVmCounters.
Definition: ProcessDescriptor.h:68
System::TByte
@ TByte
Definition: Memory.h:50
System::mappedMemory
GAUDI_API long mappedMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:178
System::MemoryUnit
MemoryUnit
Unit of memory.
Definition: Memory.h:50
System::POOLED_USAGE_AND_LIMITS::PagedPoolLimit
unsigned long PagedPoolLimit
Definition: ProcessDescriptor.h:88
System::maxMemoryLimit
GAUDI_API long maxMemoryLimit(MemoryUnit unit=kByte, InfoType fetch=Quota, long pid=-1)
System Process Limits: Maximum amount of virtual memory this process is allowed to use.
Definition: Memory.cpp:201
ProcessDescriptor.h
System::QUOTA_LIMITS::MinimumWorkingSetSize
unsigned long MinimumWorkingSetSize
Definition: ProcessDescriptor.h:44
System::QUOTA_LIMITS::PagefileLimit
unsigned long PagefileLimit
Definition: ProcessDescriptor.h:46
System::pagedMemory
GAUDI_API long pagedMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: Amount of paged memory currently occupied by the process 'pid'.
Definition: Memory.cpp:122
System::numPageFault
GAUDI_API long numPageFault(InfoType fetch=Memory, long pid=-1)
Basic Process Information: Number of page faults.
Definition: Memory.cpp:146
System::virtualMemory
GAUDI_API long virtualMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:209
System::PByte
@ PByte
Definition: Memory.h:50
System::VM_COUNTERS::QuotaPeakNonPagedPoolUsage
unsigned long QuotaPeakNonPagedPoolUsage
Definition: ProcessDescriptor.h:76
System::procID
GAUDI_API long procID()
Basic Process Information: Process ID.
Definition: Memory.cpp:64