ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkMemoryUsageObserver.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  * http://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 GetMemoryUsage() = 0;
57 };
58 
59 #if defined( WIN32 ) || defined( _WIN32 )
60 class ITKCommon_EXPORT WindowsMemoryUsageObserver:public MemoryUsageObserverBase
61 {
62 public:
63  WindowsMemoryUsageObserver();
65  virtual ~WindowsMemoryUsageObserver();
66 
68  virtual MemoryLoadType GetMemoryUsage();
69 
70 protected:
71 #if defined( SUPPORT_TOOLHELP32 )
72  typedef NTSTATUS ( WINAPI * PZwQuerySystemInformation )(UINT, PVOID, ULONG, PULONG);
73 
74  // handle ntdll.dll library
75  HMODULE m_hNTLib;
76  // Windows native API function to query system information
77  PZwQuerySystemInformation ZwQuerySystemInformation;
78 #endif // defined(SUPPORT_TOOLHELP32)
79 };
80 #endif // defined(WIN32) || defined(_WIN32)
81 
82 #ifdef __linux__
83 class ITKCommon_EXPORT LinuxMemoryUsageObserver:public MemoryUsageObserverBase
84 {
85 public:
87  virtual ~LinuxMemoryUsageObserver();
88  virtual MemoryLoadType GetMemoryUsage();
89 };
90 #endif // __linux__
91 
92 
93 #if defined( __APPLE__ )
94 class ITKCommon_EXPORT MacOSXMemoryUsageObserver:public MemoryUsageObserverBase
95 {
96 public:
98  ~MacOSXMemoryUsageObserver() override;
99  MemoryLoadType GetMemoryUsage() override;
100 };
101 #endif // Mac OS X
102 
103 
104 #if defined( __SUNPRO_CC ) || defined ( __sun__ )
105 class ITKCommon_EXPORT SunSolarisMemoryUsageObserver:public MemoryUsageObserverBase
106 {
107 public:
109  virtual ~SunSolarisMemoryUsageObserver();
110  virtual MemoryLoadType GetMemoryUsage();
111 };
112 #endif // Sun Solaris
113 
114 
115 #if !defined( WIN32 ) && !defined( _WIN32 )
117 {
118 public:
120  ~SysResourceMemoryUsageObserver() override;
121  MemoryLoadType GetMemoryUsage() override;
122 };
124 
125 #if defined( ITK_HAS_MALLINFO )
126 
130 class ITKCommon_EXPORT MallinfoMemoryUsageObserver:public MemoryUsageObserverBase
131 {
132 public:
134  virtual ~MallinfoMemoryUsageObserver();
135  virtual MemoryLoadType GetMemoryUsage();
136 };
137 #endif // Mallinfo
138 #endif // !defined(WIN32) && !defined(_WIN32)
139 
140 
141 /* \class MemoryUsageObserver
142  * The best MemoryUsageObserver has been chosen for each OS.
143  * However, SysResourceMemoryUsageObserver is far from being accurate. Other
144  * way of getting the Memory Usage shall be used.
145  * For FreeBSD, some alternatives would be to parse the output of
146  * "sysctl vm.vmtotal" or "sysctl -a | grep -i memory"
147 */
148 class ITKCommon_EXPORT MemoryUsageObserver:
149 #if defined( WIN32 ) || defined( _WIN32 )
150  public WindowsMemoryUsageObserver
151 #elif defined( __linux__ )
152  public LinuxMemoryUsageObserver
153 #elif defined( __SUNPRO_CC ) || defined ( __sun__ )
154  public SunSolarisMemoryUsageObserver
155 #elif defined( __APPLE__ )
156  public MacOSXMemoryUsageObserver
157 #elif defined( __FreeBSD__ ) || defined( __OpenBSD__ )
159 #else
160  public MallinfoMemoryUsageObserver
161 #endif
162 {
163 public:
165  ~MemoryUsageObserver() override;
166 };
167 } // end of namespace itk
168 
169 #endif // itkMemoryUsageObserver_h
unsigned long SizeValueType
Definition: itkIntTypes.h:83
Provides the memory usage of the process.