Main MRPT website > C++ reference for MRPT 1.9.9
memory.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <cstdio> // for size_t, fclose, fopen, fscanf, FILE
13 #include <cstdlib> // for free, realloc
14 #include <exception> // for exception
15 #include <mrpt/config.h> // for MRPT_OS_LINUX
16 #include <mrpt/core/exceptions.h> // for MRPT_END, MRPT_START, MRPT_UNUSE...
17 #include <mrpt/system/memory.h>
18 
19 #ifdef __APPLE__
20 #include <mach/mach_init.h>
21 #include <mach/task.h>
22 #endif
23 
24 #ifdef _WIN32
25 #include <windows.h>
26 // Windows:
28 {
29  DWORD cb;
37  SIZE_T PagefileUsage;
40 
41 using namespace mrpt;
42 using namespace mrpt::system;
43 using namespace std;
44 
45 namespace mrpt
46 {
47 namespace system
48 {
49 /** This is an auxiliary class for mrpt::system::getMemoryUsage() under Windows.
50  * It loads in runtime PSAPI.DLL. This is to avoid problems in some platforms,
51  * i.e Windows 2000,
52  * where this DLL must not be present.
53  */
55 {
56  protected:
57  typedef BOOL(WINAPI* TGetProcessMemoryInfo)(
58  HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb);
59 
60  TGetProcessMemoryInfo m_ptr;
61 
62  public:
63  HMODULE m_dll;
64 
66  {
67  m_ptr = nullptr;
68 
69  m_dll = LoadLibraryA("PSAPI.DLL");
70  if (m_dll)
71  {
72  m_ptr = (TGetProcessMemoryInfo)GetProcAddress(
73  m_dll, "GetProcessMemoryInfo");
74  }
75  }
77  {
78  if (m_dll)
79  {
80  FreeLibrary(m_dll);
81  m_dll = nullptr;
82  m_ptr = nullptr;
83  }
84  }
85 
87  HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb)
88  {
89  try
90  {
91  if (!m_ptr)
92  return false;
93  else
94  return (*m_ptr)(Process, ppsmemCounters, cb);
95  }
96  catch (...)
97  {
98  return false;
99  }
100  }
101 };
102 } // namespace system
103 } // namespace mrpt
104 
105 #endif
106 
107 /*---------------------------------------------------------------
108  getMemoryUsage
109  ---------------------------------------------------------------*/
111 {
112  MRPT_START
113  unsigned long MEM = 0;
114 
115 #ifdef _WIN32
116  // Windows:
117  static CAuxPSAPI_Loader PSAPI_LOADER;
118 
120  pmc.cb = sizeof(pmc);
121 
122  if (PSAPI_LOADER.GetProcessMemoryInfo(
123  GetCurrentProcess(), &pmc, sizeof(pmc)))
124  {
125  MEM = (long)pmc.PagefileUsage;
126  }
127 #endif
128 
129 #ifdef MRPT_OS_LINUX
130  // Linux:
131  // int page_size = getpagesize();
132 
133  FILE* f = ::fopen("/proc/self/stat", "r");
134  if (!f) return 0;
135 
136  // Note: some of these scanf specifiers would normally be 'long' versions if
137  // not for the fact that we are using suppression (gcc warns). see 'man
138  // proc' for scanf specifiers and meanings.
139  if (!::fscanf(
140  f,
141  "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d "
142  "%*d %*d %*d %*d %*d %*u %lu",
143  &MEM))
144  {
145  // Error parsing:
146  MEM = 0;
147  }
148  ::fclose(f);
149 //::system("cat /proc/self/statm");
150 #endif
151 
152 #ifdef __APPLE__
153  mach_task_basic_info info;
154  mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
155  if (task_info(
156  mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info,
157  &count) == 0)
158  {
159  MEM = info.virtual_size;
160  }
161 #endif
162  return MEM;
163  MRPT_END
164 }
mrpt::system::getMemoryUsage
unsigned long getMemoryUsage()
Returns the memory occupied by this process, in bytes.
Definition: memory.cpp:110
exceptions.h
mrpt::system::os::fclose
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:273
_PROCESS_MEMORY_COUNTERS::PageFaultCount
DWORD PageFaultCount
Definition: memory.cpp:30
mrpt::system::CAuxPSAPI_Loader::CAuxPSAPI_Loader
CAuxPSAPI_Loader()
Definition: memory.cpp:65
system-precomp.h
_PROCESS_MEMORY_COUNTERS::QuotaPagedPoolUsage
SIZE_T QuotaPagedPoolUsage
Definition: memory.cpp:34
_PROCESS_MEMORY_COUNTERS::WorkingSetSize
SIZE_T WorkingSetSize
Definition: memory.cpp:32
_PROCESS_MEMORY_COUNTERS::PagefileUsage
SIZE_T PagefileUsage
Definition: memory.cpp:37
MEM
@ MEM
Definition: inflate.h:53
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
_PROCESS_MEMORY_COUNTERS::QuotaPeakPagedPoolUsage
SIZE_T QuotaPeakPagedPoolUsage
Definition: memory.cpp:33
_PROCESS_MEMORY_COUNTERS::QuotaNonPagedPoolUsage
SIZE_T QuotaNonPagedPoolUsage
Definition: memory.cpp:36
_PROCESS_MEMORY_COUNTERS::PeakWorkingSetSize
SIZE_T PeakWorkingSetSize
Definition: memory.cpp:31
mrpt::system::CAuxPSAPI_Loader::GetProcessMemoryInfo
BOOL WINAPI GetProcessMemoryInfo(HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb)
Definition: memory.cpp:86
mrpt::system::CAuxPSAPI_Loader
This is an auxiliary class for mrpt::system::getMemoryUsage() under Windows.
Definition: memory.cpp:54
_PROCESS_MEMORY_COUNTERS::cb
DWORD cb
Definition: memory.cpp:29
count
GLuint GLuint GLsizei count
Definition: glext.h:3528
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::system::CAuxPSAPI_Loader::m_ptr
TGetProcessMemoryInfo m_ptr
Definition: memory.cpp:60
_PROCESS_MEMORY_COUNTERS::PeakPagefileUsage
SIZE_T PeakPagefileUsage
Definition: memory.cpp:38
PROCESS_MEMORY_COUNTERS
struct _PROCESS_MEMORY_COUNTERS PROCESS_MEMORY_COUNTERS
_PROCESS_MEMORY_COUNTERS::QuotaPeakNonPagedPoolUsage
SIZE_T QuotaPeakNonPagedPoolUsage
Definition: memory.cpp:35
BOOL
int BOOL
Definition: xstypedefs.h:76
_PROCESS_MEMORY_COUNTERS
Definition: memory.cpp:27
PPROCESS_MEMORY_COUNTERS
struct _PROCESS_MEMORY_COUNTERS * PPROCESS_MEMORY_COUNTERS
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::system::CAuxPSAPI_Loader::~CAuxPSAPI_Loader
~CAuxPSAPI_Loader()
Definition: memory.cpp:76
mrpt::system::CAuxPSAPI_Loader::m_dll
HMODULE m_dll
Definition: memory.cpp:63
mrpt::system::os::fopen
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:255
memory.h
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST