ITK  6.0.0
Insight Toolkit
itkMemoryUsageObserver.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkMemoryUsageObserver_h
19 #define itkMemoryUsageObserver_h
20 
21 #include "itkConfigure.h"
22 #include "itkMacro.h"
23 #include "itkObject.h"
24 #include "itkObjectFactory.h"
25 #include "itkIntTypes.h"
26 
27 #if defined(WIN32) || defined(_WIN32)
28 # include <windows.h>
29 # define SUPPORT_TOOLHELP32
30 # if defined(SUPPORT_TOOLHELP32)
31 using NTSTATUS = LONG;
32 # endif
33 #endif
34 
35 namespace itk
36 {
46 class ITKCommon_EXPORT MemoryUsageObserverBase
47 {
48 public:
51 
53  virtual ~MemoryUsageObserverBase();
54 
56  virtual MemoryLoadType
57  GetMemoryUsage() = 0;
58 };
59 
60 #if defined(WIN32) || defined(_WIN32)
61 class ITKCommon_EXPORT WindowsMemoryUsageObserver : public MemoryUsageObserverBase
62 {
63 public:
64  WindowsMemoryUsageObserver();
66  virtual ~WindowsMemoryUsageObserver();
67 
69  virtual MemoryLoadType
70  GetMemoryUsage();
71 
72 protected:
73 # if defined(SUPPORT_TOOLHELP32)
74  using PZwQuerySystemInformation = NTSTATUS(WINAPI *)(UINT, PVOID, ULONG, PULONG);
75 
76  // handle ntdll.dll library
77  HMODULE m_hNTLib{ nullptr };
78  // Windows native API function to query system information
79  PZwQuerySystemInformation ZwQuerySystemInformation{ nullptr };
80 # endif // defined(SUPPORT_TOOLHELP32)
81 };
82 #endif // defined(WIN32) || defined(_WIN32)
83 
84 #ifdef __linux__
85 class ITKCommon_EXPORT LinuxMemoryUsageObserver : public MemoryUsageObserverBase
86 {
87 public:
89  ~LinuxMemoryUsageObserver() override;
90  MemoryLoadType
91  GetMemoryUsage() override;
92 };
93 #endif // __linux__
94 
96 #if defined(__APPLE__)
97 class ITKCommon_EXPORT MacOSXMemoryUsageObserver : public MemoryUsageObserverBase
98 {
99 public:
101  ~MacOSXMemoryUsageObserver() override;
102  MemoryLoadType
103  GetMemoryUsage() override;
104 };
105 #endif // Mac OS X
106 
108 #if defined(__SUNPRO_CC) || defined(__sun__)
109 class ITKCommon_EXPORT SunSolarisMemoryUsageObserver : public MemoryUsageObserverBase
110 {
111 public:
113  virtual ~SunSolarisMemoryUsageObserver();
114  virtual MemoryLoadType
115  GetMemoryUsage();
116 };
117 #endif // Sun Solaris
118 
120 #if !defined(WIN32) && !defined(_WIN32)
122 {
123 public:
125  ~SysResourceMemoryUsageObserver() override;
127  GetMemoryUsage() override;
128 };
131 # if defined(ITK_HAS_MALLINFO) || defined(ITK_HAS_MALLINFO2)
132 
136 class ITKCommon_EXPORT MallinfoMemoryUsageObserver : public MemoryUsageObserverBase
137 {
138 public:
140  ~MallinfoMemoryUsageObserver() override;
141  MemoryLoadType
142  GetMemoryUsage() override;
143 };
144 # endif // Mallinfo
145 #endif // !defined(WIN32) && !defined(_WIN32)
146 
148 /* \class MemoryUsageObserver
149  * The best MemoryUsageObserver has been chosen for each OS.
150  * However, SysResourceMemoryUsageObserver is far from being accurate. Other
151  * way of getting the Memory Usage shall be used.
152  * For FreeBSD, some alternatives would be to parse the output of
153  * "sysctl vm.vmtotal" or "sysctl -a | grep -i memory"
154  */
155 class ITKCommon_EXPORT MemoryUsageObserver
156  :
157 #if defined(WIN32) || defined(_WIN32)
158  public WindowsMemoryUsageObserver
159 #elif defined(__linux__)
160  public LinuxMemoryUsageObserver
161 #elif defined(__SUNPRO_CC) || defined(__sun__)
162  public SunSolarisMemoryUsageObserver
163 #elif defined(__APPLE__)
164  public MacOSXMemoryUsageObserver
165 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
167 #else
168  public MallinfoMemoryUsageObserver
169 #endif
170 {
171 public:
173  ~MemoryUsageObserver() override;
174 };
175 } // end of namespace itk
176 
177 #endif // itkMemoryUsageObserver_h
itkObjectFactory.h
itk::MemoryUsageObserverBase
Definition: itkMemoryUsageObserver.h:46
itk::MemoryUsageObserverBase::MemoryLoadType
SizeValueType MemoryLoadType
Definition: itkMemoryUsageObserver.h:50
itkMacro.h
itk::MemoryUsageObserver
Provides the memory usage of the process.
Definition: itkMemoryUsageObserver.h:155
itkIntTypes.h
itkObject.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::SysResourceMemoryUsageObserver
Definition: itkMemoryUsageObserver.h:121
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86