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 { 37 /** \class MemoryUsageObserver 38 * \brief Provides the memory usage of the process. 39 * 40 * This class represents a memory load analyser object 41 * and provides a memory usage in platform-independent format. 42 * 43 * \ingroup ITKCommon 44 */ 45 46 class ITKCommon_EXPORT MemoryUsageObserverBase 47 { 48 public: 49 /** Define the type for the memory usage */ 50 using MemoryLoadType = SizeValueType; 51 52 /** destructor */ 53 virtual ~MemoryUsageObserverBase(); 54 55 /** Returns the memory load in kO */ 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(); 64 /** destructor */ 65 virtual ~WindowsMemoryUsageObserver(); 66 67 /** Returns the memory load in kO */ 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: 86 /** destructor */ 87 virtual ~LinuxMemoryUsageObserver(); 88 virtual MemoryLoadType GetMemoryUsage(); 89 }; 90 #endif // __linux__ 91 92 #if defined( __APPLE__ ) 93 class ITKCommon_EXPORT MacOSXMemoryUsageObserver:public MemoryUsageObserverBase 94 { 95 public: 96 /** destructor */ 97 ~MacOSXMemoryUsageObserver() override; 98 MemoryLoadType GetMemoryUsage() override; 99 }; 100 #endif // Mac OS X 101 102 #if defined( __SUNPRO_CC ) || defined ( __sun__ ) 103 class ITKCommon_EXPORT SunSolarisMemoryUsageObserver:public MemoryUsageObserverBase 104 { 105 public: 106 /** destructor */ 107 virtual ~SunSolarisMemoryUsageObserver(); 108 virtual MemoryLoadType GetMemoryUsage(); 109 }; 110 #endif // Sun Solaris 111 112 #if !defined( WIN32 ) && !defined( _WIN32 ) 113 class ITKCommon_EXPORT SysResourceMemoryUsageObserver:public MemoryUsageObserverBase 114 { 115 public: 116 /** destructor */ 117 ~SysResourceMemoryUsageObserver() override; 118 MemoryLoadType GetMemoryUsage() override; 119 }; 120 121 #if defined( ITK_HAS_MALLINFO ) 122 /** \class MallinfoMemoryUsageObserver 123 * \brief The MallinfoMemoryUsageObserver 124 * \ingroup ITKCommon 125 */ 126 class ITKCommon_EXPORT MallinfoMemoryUsageObserver:public MemoryUsageObserverBase 127 { 128 public: 129 /** destructor */ 130 virtual ~MallinfoMemoryUsageObserver(); 131 virtual MemoryLoadType GetMemoryUsage(); 132 }; 133 #endif // Mallinfo 134 #endif // !defined(WIN32) && !defined(_WIN32) 135 136 /* \class MemoryUsageObserver 137 * The best MemoryUsageObserver has been chosen for each OS. 138 * However, SysResourceMemoryUsageObserver is far from being accurate. Other 139 * way of getting the Memory Usage shall be used. 140 * For FreeBSD, some alternatives would be to parse the output of 141 * "sysctl vm.vmtotal" or "sysctl -a | grep -i memory" 142 */ 143 class ITKCommon_EXPORT MemoryUsageObserver: 144 #if defined( WIN32 ) || defined( _WIN32 ) 145 public WindowsMemoryUsageObserver 146 #elif defined( __linux__ ) 147 public LinuxMemoryUsageObserver 148 #elif defined( __SUNPRO_CC ) || defined ( __sun__ ) 149 public SunSolarisMemoryUsageObserver 150 #elif defined( __APPLE__ ) 151 public MacOSXMemoryUsageObserver 152 #elif (defined( __FreeBSD__ )||defined(__DragonFly__)) || defined( __OpenBSD__ ) 153 public SysResourceMemoryUsageObserver 154 #else 155 public MallinfoMemoryUsageObserver 156 #endif 157 { 158 public: 159 /** destructor */ 160 ~MemoryUsageObserver() override; 161 }; 162 } // end of namespace itk 163 164 #endif // itkMemoryUsageObserver_h 165