MRPT  2.0.1
memory.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/config.h> // for MRPT_OS_LINUX
13 #include <mrpt/core/exceptions.h> // for MRPT_END, MRPT_START, MRPT_UNUSE...
14 #include <mrpt/system/memory.h>
15 #include <cstdio> // for size_t, fclose, fopen, fscanf, FILE
16 #include <cstdlib> // for free, realloc
17 #include <exception> // for exception
18 
19 #ifdef __APPLE__
20 #include <mach/mach_init.h>
21 #include <mach/task.h>
22 #endif
23 
24 #ifdef MRPT_OS_LINUX
25 #include <unistd.h> // sysconf()
26 #endif
27 
28 #ifdef _WIN32
29 #include <windows.h>
30 // Windows:
32 {
33  DWORD cb;
41  SIZE_T PagefileUsage;
44 
45 using namespace mrpt;
46 using namespace mrpt::system;
47 using namespace std;
48 
49 namespace mrpt
50 {
51 namespace system
52 {
53 /** This is an auxiliary class for mrpt::system::getMemoryUsage() under Windows.
54  * It loads in runtime PSAPI.DLL. This is to avoid problems in some platforms,
55  * i.e Windows 2000,
56  * where this DLL must not be present.
57  */
59 {
60  protected:
61  typedef BOOL(WINAPI* TGetProcessMemoryInfo)(
62  HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb);
63 
64  TGetProcessMemoryInfo m_ptr;
65 
66  public:
67  HMODULE m_dll;
68 
70  {
71  m_ptr = nullptr;
72 
73  m_dll = LoadLibraryA("PSAPI.DLL");
74  if (m_dll)
75  {
76  m_ptr = (TGetProcessMemoryInfo)GetProcAddress(
77  m_dll, "GetProcessMemoryInfo");
78  }
79  }
81  {
82  if (m_dll)
83  {
84  FreeLibrary(m_dll);
85  m_dll = nullptr;
86  m_ptr = nullptr;
87  }
88  }
89 
90  BOOL WINAPI GetProcessMemoryInfo(
91  HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb)
92  {
93  try
94  {
95  if (!m_ptr)
96  return false;
97  else
98  return (*m_ptr)(Process, ppsmemCounters, cb);
99  }
100  catch (...)
101  {
102  return false;
103  }
104  }
105 };
106 } // namespace system
107 } // namespace mrpt
108 
109 #endif
110 
111 /*---------------------------------------------------------------
112  getMemoryUsage
113  ---------------------------------------------------------------*/
115 {
116  MRPT_START
117  unsigned long MEM = 0;
118 
119 #ifdef _WIN32
120  // Windows:
121  static CAuxPSAPI_Loader PSAPI_LOADER;
122 
124  pmc.cb = sizeof(pmc);
125 
126  if (PSAPI_LOADER.GetProcessMemoryInfo(
127  GetCurrentProcess(), &pmc, sizeof(pmc)))
128  {
129  MEM = (long)pmc.PagefileUsage;
130  }
131 #endif
132 
133 #ifdef MRPT_OS_LINUX
134  FILE* f = ::fopen("/proc/self/statm", "r");
135  if (!f) return 0;
136 
137  unsigned long mem_pages = 0;
138  // see 'man proc' for docs on this
139  if (!::fscanf(f, "%*d %*d %*d %*d %*d %lu %*d", &mem_pages))
140  {
141  // Error parsing.
142  MEM = 0;
143  }
144  ::fclose(f);
145 
146  // Multiply by the page size:
147  static const long pagesize = ::sysconf(_SC_PAGE_SIZE);
148  MEM = static_cast<decltype(MEM)>(pagesize) * mem_pages;
149 
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 }
#define MRPT_START
Definition: exceptions.h:241
struct _PROCESS_MEMORY_COUNTERS PROCESS_MEMORY_COUNTERS
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:275
STL namespace.
BOOL WINAPI GetProcessMemoryInfo(HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb)
Definition: memory.cpp:90
This is an auxiliary class for mrpt::system::getMemoryUsage() under Windows.
Definition: memory.cpp:58
SIZE_T QuotaNonPagedPoolUsage
Definition: memory.cpp:40
SIZE_T QuotaPeakNonPagedPoolUsage
Definition: memory.cpp:39
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned long getMemoryUsage()
Returns the memory occupied by this process, in bytes.
Definition: memory.cpp:114
struct _PROCESS_MEMORY_COUNTERS * PPROCESS_MEMORY_COUNTERS
SIZE_T QuotaPeakPagedPoolUsage
Definition: memory.cpp:37
#define MRPT_END
Definition: exceptions.h:245
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:257
TGetProcessMemoryInfo m_ptr
Definition: memory.cpp:64



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020