The Gaudi Framework  master (37c0b60a)
Memory.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 // Memory.cpp
13 //--------------------------------------------------------------------
14 //
15 // Package : System (The LHCb System service)
16 //
17 // Description: Information of memory usage from a given process
18 //
19 // Author : M.Frank
20 // Created : 13/11/00
21 // Changes :
22 //
23 //====================================================================
24 #define GAUDIKERNEL_MEMORY_CPP
25 
26 #ifdef _WIN32
27 # include <process.h>
28 # define getpid _getpid
29 #else
30 # include <cstdio>
31 # include <errno.h>
32 # include <libgen.h>
33 # include <string.h>
34 # include <sys/times.h>
35 # include <unistd.h>
36 #endif
37 
38 // Framework include files
39 #include "ProcessDescriptor.h"
40 #include <GaudiKernel/Memory.h>
41 #include <limits.h>
42 
44 long System::adjustMemory( MemoryUnit unit, long value ) {
45  if ( value != -1 ) {
46  switch ( unit ) {
47  case Byte:
48  break;
49  case kByte:
50  value = value / 1024;
51  break;
52  case MByte:
53  value = ( value / 1024 ) / 1024;
54  break;
55  case GByte:
56  value = ( ( value / 1024 ) / 1024 ) / 1024;
57  break;
58  case TByte:
59  value = ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024;
60  break;
61  case PByte:
62  value = ( ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024;
63  break;
64  case EByte:
65  value = ( ( ( ( ( value / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024 ) / 1024;
66  break;
67  default:
68  value = -1;
69  break;
70  }
71  }
72  return value;
73 }
74 
76 long System::basePriority( InfoType fetch, long pid ) {
78  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.BasePriority;
79  return 0;
80 }
81 
84  static const long s_pid = ::getpid();
85  return s_pid;
86 }
87 
89 long System::parentID( InfoType fetch, long pid ) {
91  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.InheritedFromUniqueProcessId;
92  return 0;
93 }
94 
96 long System::affinityMask( InfoType fetch, long pid ) {
98  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.AffinityMask;
99  return 0;
100 }
101 
103 long System::exitStatus( InfoType fetch, long pid ) {
105  if ( fetch != NoFetch && getProcess()->query( pid, ProcessBasics, &info ) ) return info.ExitStatus;
106  return -2;
107 }
108 
111  long info;
112  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return info;
113  return -2;
114 }
115 
118  VM_COUNTERS info;
119  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
120  return adjustMemory( unit, info.QuotaPeakNonPagedPoolUsage );
121  return -2;
122 }
123 
126  VM_COUNTERS info;
127  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
128  return adjustMemory( unit, info.QuotaNonPagedPoolUsage );
129  return -2;
130 }
131 
135  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
136  return adjustMemory( unit, quota.NonPagedPoolLimit );
137  return 0;
138 }
139 
141 long System::pagedMemory( MemoryUnit unit, InfoType fetch, long pid ) {
142  VM_COUNTERS info;
143  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
144  return adjustMemory( unit, info.QuotaPagedPoolUsage );
145  return -2;
146 }
147 
150  VM_COUNTERS info;
151  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
152  return adjustMemory( unit, info.QuotaPeakPagedPoolUsage );
153  return -2;
154 }
155 
159  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
160  return adjustMemory( unit, quota.PagedPoolLimit );
161  return 0;
162 }
163 
166  VM_COUNTERS info;
167  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return info.PageFaultCount;
168  return -2;
169 }
170 
173  VM_COUNTERS info;
174  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.PagefileUsage );
175  return -2;
176 }
177 
180  VM_COUNTERS info;
181  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
182  return adjustMemory( unit, info.PeakPagefileUsage );
183  return -2;
184 }
185 
189  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
190  if ( long( quota.PagefileLimit ) < 0 ) return -1; // LONG_MAX;
191  return adjustMemory( unit, quota.PagefileLimit );
192  }
193  return -2;
194 }
195 
198  VM_COUNTERS info;
199  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.WorkingSetSize );
200  return -2;
201 }
202 
205  VM_COUNTERS info;
206  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) )
207  return adjustMemory( unit, info.PeakWorkingSetSize );
208  return -2;
209 }
210 
213  QUOTA_LIMITS quota;
214  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
215  return adjustMemory( unit, quota.MinimumWorkingSetSize );
216  return 0;
217 }
218 
221  QUOTA_LIMITS quota;
222  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) )
223  return adjustMemory( unit, quota.MaximumWorkingSetSize );
224  return 0;
225 }
226 
229  VM_COUNTERS info;
230  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.VirtualSize );
231  return -2;
232 }
233 
236  VM_COUNTERS info;
237  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) return adjustMemory( unit, info.PeakVirtualSize );
238  return -2;
239 }
240 
243  QUOTA_LIMITS quota;
244  if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
245  if ( long( quota.PagefileLimit ) == -1 ) return -1; // LONG_MAX;
246  return adjustMemory( unit, quota.PagefileLimit );
247  }
248  return 0;
249 }
System::GByte
@ GByte
Definition: Memory.h:65
System::VM_COUNTERS::QuotaPeakPagedPoolUsage
unsigned long QuotaPeakPagedPoolUsage
Definition: ProcessDescriptor.h:75
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:103
System::VM_COUNTERS::PeakPagefileUsage
unsigned long PeakPagefileUsage
Definition: ProcessDescriptor.h:80
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:172
System::VM_COUNTERS::QuotaPagedPoolUsage
unsigned long QuotaPagedPoolUsage
Definition: ProcessDescriptor.h:76
System::VM_COUNTERS::WorkingSetSize
unsigned long WorkingSetSize
Definition: ProcessDescriptor.h:74
System::InfoType
InfoType
Enumeration for fetching information.
Definition: SystemBase.h:28
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::MByte
@ MByte
Definition: Memory.h:65
System::POOLED_USAGE_AND_LIMITS::NonPagedPoolLimit
unsigned long NonPagedPoolLimit
Definition: ProcessDescriptor.h:92
System::mappedMemoryPeak
GAUDI_API long mappedMemoryPeak(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:204
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:149
System::parentID
GAUDI_API long parentID(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Parent's process ID.
Definition: Memory.cpp:89
System::PROCESS_BASIC_INFORMATION::AffinityMask
unsigned long AffinityMask
Definition: ProcessDescriptor.h:31
System::VM_COUNTERS::PeakWorkingSetSize
unsigned long PeakWorkingSetSize
Definition: ProcessDescriptor.h:73
Memory.h
System::EByte
@ EByte
Definition: Memory.h:65
System::VM_COUNTERS::VirtualSize
unsigned long VirtualSize
Definition: ProcessDescriptor.h:71
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:44
System::PROCESS_BASIC_INFORMATION::BasePriority
long BasePriority
Definition: ProcessDescriptor.h:32
System::PROCESS_BASIC_INFORMATION::InheritedFromUniqueProcessId
unsigned long InheritedFromUniqueProcessId
Definition: ProcessDescriptor.h:34
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:125
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:117
System::VM_COUNTERS::PageFaultCount
unsigned long PageFaultCount
Definition: ProcessDescriptor.h:72
System::priorityBoost
GAUDI_API long priorityBoost(InfoType fetch=PriorityBoost, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:110
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:242
System::NoFetch
@ NoFetch
Definition: SystemBase.h:28
System::VM_COUNTERS::QuotaNonPagedPoolUsage
unsigned long QuotaNonPagedPoolUsage
Definition: ProcessDescriptor.h:78
System::VM_COUNTERS::PeakVirtualSize
unsigned long PeakVirtualSize
Definition: ProcessDescriptor.h:70
System::PROCESS_BASIC_INFORMATION
Definition: ProcessDescriptor.h:28
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:187
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::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:157
System::QUOTA_LIMITS
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...
Definition: ProcessDescriptor.h:42
System::Byte
@ Byte
Definition: Memory.h:65
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:179
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:133
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:212
System::affinityMask
GAUDI_API long affinityMask(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Affinity mask.
Definition: Memory.cpp:96
System::basePriority
GAUDI_API long basePriority(InfoType fetch=ProcessBasics, long pid=-1)
Basic Process Information: Base priority.
Definition: Memory.cpp:76
System::ProcessBasics
@ ProcessBasics
Definition: SystemBase.h:28
System::virtualMemoryPeak
GAUDI_API long virtualMemoryPeak(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:235
System::QUOTA_LIMITS::MaximumWorkingSetSize
unsigned long MaximumWorkingSetSize
Definition: ProcessDescriptor.h:46
System::getProcess
ProcessDescriptor * getProcess()
Retrieve Process structure.
Definition: ProcessDescriptor.h:142
System::kByte
@ kByte
Definition: Memory.h:65
System::VM_COUNTERS
Process Virtual Memory Counters NtQueryInformationProcess using ProcessVmCounters.
Definition: ProcessDescriptor.h:69
System::TByte
@ TByte
Definition: Memory.h:65
System::mappedMemory
GAUDI_API long mappedMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:197
System::MemoryUnit
MemoryUnit
Unit of memory.
Definition: Memory.h:65
System::POOLED_USAGE_AND_LIMITS::PagedPoolLimit
unsigned long PagedPoolLimit
Definition: ProcessDescriptor.h:89
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:220
ProcessDescriptor.h
System::QUOTA_LIMITS::MinimumWorkingSetSize
unsigned long MinimumWorkingSetSize
Definition: ProcessDescriptor.h:45
System::QUOTA_LIMITS::PagefileLimit
unsigned long PagefileLimit
Definition: ProcessDescriptor.h:47
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:141
System::numPageFault
GAUDI_API long numPageFault(InfoType fetch=Memory, long pid=-1)
Basic Process Information: Number of page faults.
Definition: Memory.cpp:165
System::virtualMemory
GAUDI_API long virtualMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:228
System::PByte
@ PByte
Definition: Memory.h:65
System::VM_COUNTERS::QuotaPeakNonPagedPoolUsage
unsigned long QuotaPeakNonPagedPoolUsage
Definition: ProcessDescriptor.h:77
System::procID
GAUDI_API long procID()
Basic Process Information: Process ID.
Definition: Memory.cpp:83