OpenGM  2.3.x
Discrete Graphical Model Library
meminfo.hxx
Go to the documentation of this file.
1 //******************************************************
2 //** Author: Joerg Hendrik Kappes
3 //**
4 //** The MemoryInfo class provides a interface to access memory informations
5 //** for the current process or system-wide across diffrent platforms.
6 //** It provides interfaces to get the:
7 //** - current physically used memory by the process
8 //** - current virtual used memory by the process
9 //** - maximal physically used memory by the process so far
10 //** - maximal virtual used memory by the process so far
11 //**
12 //** - used physical memory of the system
13 //** - free physical memory of the system
14 //** - total physical memory of the system
15 //**
16 //******************************************************
17 
18 #pragma once
19 #ifndef SYS_MEMORYINFO_HXX
20 #define SYS_MEMORYINFO_HXX
21 
22 #include <iostream>
23 #include <stdexcept>
24 #include <limits>
25 #include "stdlib.h"
26 #include "stdio.h"
27 #include "string.h"
28 
29 // uncomment this line if U have problems with memorylogging -> this will disable it.
30 #define SYS_MEMORYINFO_ON
31 
32 #if ( defined(__APPLE__) && defined(SYS_MEMORYINFO_ON) )
33 # define SYS_MEMORYINFO_MAC
34 # include <mach/vm_statistics.h>
35 # include <mach/mach_types.h>
36 # include <mach/mach_init.h>
37 # include <mach/mach_host.h>
38 # include <mach/mach.h>
39 # include <limits>
40 #elif (defined(SYS_MEMORYINFO_ON) && (defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(_WIN64)) )
41 # define SYS_MEMORYINFO_WINDOWS
42 # include <windows.h>
43 # include <psapi.h>
44 # undef min
45 # undef max
46 #elif (defined(SYS_MEMORYINFO_ON))
47 # define SYS_MEMORYINFO_LINUX
48 # include <unistd.h>
49 # include "sys/types.h"
50 # include "sys/sysinfo.h"
51 #else
52 #endif
53 namespace sys {
54 
55  class MemoryInfo{
56  public:
57  inline static double usedVirtualMem();
58  inline static double usedPhysicalMem();
59  inline static double usedVirtualMemMax();
60  inline static double usedPhysicalMemMax();
61 
62  inline static double usedSystemMem();
63  inline static double freeSystemMem();
64  inline static double systemMem();
65  private:
66 
67  inline static int parseLine(char* line){
68  int i = strlen(line);
69  while (*line < '0' || *line > '9') line++;
70  line[i-3] = '\0';
71  i = atoi(line);
72  return i;
73  }
74  };
75 
76 //**************************************
77 //**
78 //** Implementation for Linux
79 //**
80 //*************************************
81 #ifdef SYS_MEMORYINFO_LINUX
83  FILE* file = fopen("/proc/self/status", "r");
84  int result = -1;
85  char line[128];
86  while (fgets(line, 128, file) != NULL){
87  if (strncmp(line, "VmSize:", 7) == 0){
88  result = parseLine(line);
89  break;
90  }
91  }
92  fclose(file);
93  return static_cast<double>(result);
94  }
96  FILE* file = fopen("/proc/self/status", "r");
97  int result = -1;
98  char line[128];
99  while (fgets(line, 128, file) != NULL){
100  if (strncmp(line, "VmRSS:", 6) == 0){
101  result = parseLine(line);
102  break;
103  }
104  }
105  fclose(file);
106  return static_cast<double>(result);
107  }
109  FILE* file = fopen("/proc/self/status", "r");
110  int result = -1;
111  char line[128];
112  while (fgets(line, 128, file) != NULL){
113  if (strncmp(line, "VmPeak:", 7) == 0){
114  result = parseLine(line);
115  break;
116  }
117  }
118  fclose(file);
119  return static_cast<double>(result);
120  }
122  FILE* file = fopen("/proc/self/status", "r");
123  int result = -1;
124  char line[128];
125  while (fgets(line, 128, file) != NULL){
126  if (strncmp(line, "VmHWM:", 6) == 0){
127  result = parseLine(line);
128  break;
129  }
130  }
131  fclose(file);
132  return static_cast<double>(result);
133  }
135  struct sysinfo memInfo;
136  sysinfo(&memInfo);
137  return static_cast<double>(memInfo.totalram - memInfo.freeram)*memInfo.mem_unit/1024.0;
138  }
140  struct sysinfo memInfo;
141  sysinfo(&memInfo);
142  return static_cast<double>(memInfo.freeram)*memInfo.mem_unit/1024.0;
143  }
145  struct sysinfo memInfo;
146  sysinfo(&memInfo);
147  return static_cast<double>(memInfo.totalram)*memInfo.mem_unit/1024.0;
148  //long pages = sysconf(_SC_PHYS_PAGES);
149  //long page_size = sysconf(_SC_PAGE_SIZE);
150  //return static_cast<double>(pages * page_size)/1024.0;
151  }
152 
153 //**************************************************
154 //**
155 //** Implementation for Windows
156 //**
157 //**************************************************
158 # elif defined( SYS_MEMORYINFO_WINDOWS )
160  PROCESS_MEMORY_COUNTERS_EX pmc;
161  GetProcessMemoryInfo(GetCurrentProcess(),(PROCESS_MEMORY_COUNTERS*) &pmc, sizeof(pmc));
162  return static_cast<double>(pmc.WorkingSetSize)/1024.0;
163  }
165  PROCESS_MEMORY_COUNTERS_EX pmc;
166  GetProcessMemoryInfo(GetCurrentProcess(),(PROCESS_MEMORY_COUNTERS*) &pmc, sizeof(pmc));
167  return static_cast<double>(pmc.PrivateUsage)/1024.0;
168  }
170  PROCESS_MEMORY_COUNTERS_EX pmc;
171  GetProcessMemoryInfo(GetCurrentProcess(),(PROCESS_MEMORY_COUNTERS*) &pmc, sizeof(pmc));
172  return static_cast<double>(pmc.PeakWorkingSetSize)/1024.0;
173  }
175  PROCESS_MEMORY_COUNTERS_EX pmc;
176  GetProcessMemoryInfo(GetCurrentProcess(),(PROCESS_MEMORY_COUNTERS*) &pmc, sizeof(pmc));
177  return static_cast<double>(pmc.PeakPagefileUsage)/1024.0;
178  }
179  double MemoryInfo::usedSystemMem(){
180  MEMORYSTATUSEX memInfo;
181  memInfo.dwLength = sizeof(MEMORYSTATUSEX);
182  GlobalMemoryStatusEx(&memInfo);
183  return static_cast<double>(memInfo.ullTotalPhys-memInfo.ullAvailPhys)/1024.0;
184  }
185  double MemoryInfo::freeSystemMem(){
186  MEMORYSTATUSEX memInfo;
187  memInfo.dwLength = sizeof(MEMORYSTATUSEX);
188  GlobalMemoryStatusEx(&memInfo);
189  return static_cast<double>(memInfo.ullAvailPhys)/1024.0;
190  }
191  double MemoryInfo::systemMem(){
192  MEMORYSTATUSEX memInfo;
193  memInfo.dwLength = sizeof(MEMORYSTATUSEX);
194  GlobalMemoryStatusEx(&memInfo);
195  return static_cast<double>(memInfo.ullTotalPhys)/1024.0;
196  }
197 
198 //**************************************************
199 //**
200 //** Implementation for MAC
201 //**
202 //**************************************************
203 # elif defined( SYS_MEMORYINFO_MAC )
205  struct mach_task_basic_info info;
206  mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
207  if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,(task_info_t)&info, &infoCount ) != KERN_SUCCESS )
208  return std::numeric_limits<double>::quiet_NaN();
209  else
210  return static_cast<double>(info.resident_size)/1024.0;
211  }
213  struct mach_task_basic_info info;
214  mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
215  if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,(task_info_t)&info, &infoCount ) != KERN_SUCCESS )
216  return std::numeric_limits<double>::quiet_NaN();
217  else
218  return static_cast<double>(info.virtual_size)/1024.0;
219  }
221  struct rusage rusage;
222  getrusage( RUSAGE_SELF, &rusage );
223  return static_cast<double>(rusage.ru_maxrss)/1024;
224  //return std::numeric_limits<double>::quiet_NaN();
225  }
227  return std::numeric_limits<double>::quiet_NaN();
228  }
229  double MemoryInfo::usedSystemMem(){
230  return std::numeric_limits<double>::quiet_NaN();
231  }
232  double MemoryInfo::freeSystemMem(){
233  return std::numeric_limits<double>::quiet_NaN();
234  }
235  double MemoryInfo::systemMem(){
236  return std::numeric_limits<double>::quiet_NaN();
237  }
238 #else
240  return std::numeric_limits<double>::quiet_NaN();
241  }
243  return std::numeric_limits<double>::quiet_NaN();
244  }
246  return std::numeric_limits<double>::quiet_NaN();
247  }
249  return std::numeric_limits<double>::quiet_NaN();
250  }
251  double MemoryInfo::usedSystemMem(){
252  return std::numeric_limits<double>::quiet_NaN();
253  }
254  double MemoryInfo::freeSystemMem(){
255  return std::numeric_limits<double>::quiet_NaN();
256  }
257  double MemoryInfo::systemMem(){
258  return std::numeric_limits<double>::quiet_NaN();
259  }
260 #endif
261 
262 } // namespace sys
263 
264 
265 #endif // SYS_MEMORYINFO_HXX
static double usedVirtualMemMax()
Definition: meminfo.hxx:108
static double usedPhysicalMemMax()
Definition: meminfo.hxx:121
static double usedVirtualMem()
Definition: meminfo.hxx:82
Definition: meminfo.hxx:53
static double freeSystemMem()
Definition: meminfo.hxx:139
static double usedPhysicalMem()
Definition: meminfo.hxx:95
static double usedSystemMem()
Definition: meminfo.hxx:134
static double systemMem()
Definition: meminfo.hxx:144