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)
31using NTSTATUS = LONG;
32# endif
33#endif
34
35namespace itk
36{
46class ITKCommon_EXPORT MemoryUsageObserverBase
47{
48public:
51
54
56 virtual MemoryLoadType
58};
59
60#if defined(WIN32) || defined(_WIN32)
61class ITKCommon_EXPORT WindowsMemoryUsageObserver : public MemoryUsageObserverBase
62{
63public:
64 WindowsMemoryUsageObserver();
66 virtual ~WindowsMemoryUsageObserver();
67
69 virtual MemoryLoadType
70 GetMemoryUsage();
71
72protected:
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__
85class ITKCommon_EXPORT LinuxMemoryUsageObserver : public MemoryUsageObserverBase
86{
87public:
89 ~LinuxMemoryUsageObserver() override;
90 MemoryLoadType
91 GetMemoryUsage() override;
92};
93#endif // __linux__
96#if defined(__APPLE__)
97class ITKCommon_EXPORT MacOSXMemoryUsageObserver : public MemoryUsageObserverBase
98{
99public:
101 ~MacOSXMemoryUsageObserver() override;
102 MemoryLoadType
103 GetMemoryUsage() override;
104};
105#endif // Mac OS X
108#if defined(__SUNPRO_CC) || defined(__sun__)
109class ITKCommon_EXPORT SunSolarisMemoryUsageObserver : public MemoryUsageObserverBase
110{
111public:
113 virtual ~SunSolarisMemoryUsageObserver();
114 virtual MemoryLoadType
115 GetMemoryUsage();
116};
117#endif // Sun Solaris
120#if !defined(WIN32) && !defined(_WIN32)
122{
123public:
127 GetMemoryUsage() override;
128};
131# if defined(ITK_HAS_MALLINFO) || defined(ITK_HAS_MALLINFO2)
136class ITKCommon_EXPORT MallinfoMemoryUsageObserver : public MemoryUsageObserverBase
137{
138public:
140 ~MallinfoMemoryUsageObserver() override;
141 MemoryLoadType
142 GetMemoryUsage() override;
143};
144# endif // Mallinfo
145#endif // !defined(WIN32) && !defined(_WIN32)
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 */
155class 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{
171public:
174};
175} // end of namespace itk
176
177#endif // itkMemoryUsageObserver_h
virtual MemoryLoadType GetMemoryUsage()=0
Provides the memory usage of the process.
~MemoryUsageObserver() override
MemoryLoadType GetMemoryUsage() override
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:86