The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <libgen.h>
14 #include <limits.h>
15 #include <sys/times.h>
16 #include <unistd.h>
17 
19 long System::adjustMemory( MemoryUnit unit, long value ) {
20  if ( value != -1 ) {
21  switch ( unit ) {
22  case Byte:
23  break;
24  case kByte:
25  value = value / 1024;
26  break;
27  case MByte:
28  value = ( value / 1024 ) / 1024;
29  break;
30  case GByte:
31  value = ( ( value / 1024 ) / 1024 ) / 1024;
32  break;
33  case TByte:
34  value = ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024;
35  break;
36  case PByte:
37  value = ( ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024;
38  break;
39  case EByte:
40  value = ( ( ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024;
41  break;
42  default:
43  value = -1;
44  break;
45  }
46  }
47  return value;
48 }
49 
51 long System::basePriority( InfoType fetch, long pid ) {
53  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.BasePriority;
54  return 0;
55 }
56 
59  static const long s_pid = ::getpid();
60  return s_pid;
61 }
62 
64 long System::parentID( InfoType fetch, long pid ) {
66  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.InheritedFromUniqueProcessId;
67  return 0;
68 }
69 
71 long System::affinityMask( InfoType fetch, long pid ) {
73  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.AffinityMask;
74  return 0;
75 }
76 
78 long System::exitStatus( InfoType fetch, long pid ) {
80  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.ExitStatus;
81  return -2;
82 }
83 
86  long info;
87  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return info;
88  return -2;
89 }
90 
93  VM_COUNTERS info;
94  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
95  return adjustMemory( unit, info.QuotaPeakNonPagedPoolUsage );
96  return -2;
97 }
98 
101  VM_COUNTERS info;
102  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
103  return adjustMemory( unit, info.QuotaNonPagedPoolUsage );
104  return -2;
105 }
106 
110  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
111  return adjustMemory( unit, quota.NonPagedPoolLimit );
112  return 0;
113 }
114 
116 long System::pagedMemory( MemoryUnit unit, InfoType fetch, long pid ) {
117  VM_COUNTERS info;
118  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
119  return adjustMemory( unit, info.QuotaPagedPoolUsage );
120  return -2;
121 }
122 
125  VM_COUNTERS info;
126  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
127  return adjustMemory( unit, info.QuotaPeakPagedPoolUsage );
128  return -2;
129 }
130 
134  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
135  return adjustMemory( unit, quota.PagedPoolLimit );
136  return 0;
137 }
138 
141  VM_COUNTERS info;
142  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return info.PageFaultCount;
143  return -2;
144 }
145 
148  VM_COUNTERS info;
149  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.PagefileUsage );
150  return -2;
151 }
152 
155  VM_COUNTERS info;
156  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
157  return adjustMemory( unit, info.PeakPagefileUsage );
158  return -2;
159 }
160 
164  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
165  if ( long( quota.PagefileLimit ) < 0 ) return -1; // LONG_MAX;
166  return adjustMemory( unit, quota.PagefileLimit );
167  }
168  return -2;
169 }
170 
173  VM_COUNTERS info;
174  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.WorkingSetSize );
175  return -2;
176 }
177 
180  VM_COUNTERS info;
181  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
182  return adjustMemory( unit, info.PeakWorkingSetSize );
183  return -2;
184 }
185 
188  QUOTA_LIMITS quota;
189  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
190  return adjustMemory( unit, quota.MinimumWorkingSetSize );
191  return 0;
192 }
193 
196  QUOTA_LIMITS quota;
197  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
198  return adjustMemory( unit, quota.MaximumWorkingSetSize );
199  return 0;
200 }
201 
204  VM_COUNTERS info;
205  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.VirtualSize );
206  return -2;
207 }
208 
211  VM_COUNTERS info;
212  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.PeakVirtualSize );
213  return -2;
214 }
215 
218  QUOTA_LIMITS quota;
219  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
220  if ( long( quota.PagefileLimit ) == -1 ) return -1; // LONG_MAX;
221  return adjustMemory( unit, quota.PagefileLimit );
222  }
223  return 0;
224 }
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:78
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:147
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:179
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:124
System::parentID
GAUDI_API long parentID(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Parent's process ID.
Definition: Memory.cpp:64
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:19
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:100
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:92
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:85
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:217
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:162
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:132
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:154
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:108
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:187
System::affinityMask
GAUDI_API long affinityMask(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Affinity mask.
Definition: Memory.cpp:71
System::basePriority
GAUDI_API long basePriority(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Base priority.
Definition: Memory.cpp:51
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:210
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:172
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:195
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:116
System::numPageFault
GAUDI_API long numPageFault(InfoType fetch=Memory, long pid=-1)
Basic Process Information: Number of page faults.
Definition: Memory.cpp:140
System::virtualMemory
GAUDI_API long virtualMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:203
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:58