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