FML
SystemMemory.h
Go to the documentation of this file.
1#ifndef SYSTEMMEMORY_HEADER
2#define SYSTEMMEMORY_HEADER
3
4/*
5 * Author: David Robert Nadeau
6 * Site: http://NadeauSoftware.com/
7 * License: Creative Commons Attribution 3.0 Unported License
8 * http://creativecommons.org/licenses/by/3.0/deed.en_US
9 */
10
11#if defined(_WIN32)
12#include <psapi.h>
13#include <windows.h>
14
15#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
16#include <sys/resource.h>
17#include <unistd.h>
18
19#if defined(__APPLE__) && defined(__MACH__)
20#include <mach/mach.h>
21
22#elif (defined(_AIX) || defined(__TOS__AIX__)) || \
23 (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
24#include <fcntl.h>
25#include <procfs.h>
26
27#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
28#include <stdio.h>
29
30#endif
31
32#else
33#error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
34#endif
35
36/**
37 * Returns the peak (maximum so far) resident set size (physical
38 * memory use) measured in bytes, or zero if the value cannot be
39 * determined on this OS.
40 */
41size_t getPeakRSS() {
42#if defined(_WIN32)
43 /* Windows -------------------------------------------------- */
44 PROCESS_MEMORY_COUNTERS info;
45 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
46 return (size_t)info.PeakWorkingSetSize;
47
48#elif (defined(_AIX) || defined(__TOS__AIX__)) || \
49 (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
50 /* AIX and Solaris ------------------------------------------ */
51 struct psinfo psinfo;
52 int fd = -1;
53 if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
54 return (size_t)0L; /* Can't open? */
55 if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
56 close(fd);
57 return (size_t)0L; /* Can't read? */
58 }
59 close(fd);
60 return (size_t)(psinfo.pr_rssize * 1024L);
61
62#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
63 /* BSD, Linux, and OSX -------------------------------------- */
64 struct rusage rusage;
65 getrusage(RUSAGE_SELF, &rusage);
66#if defined(__APPLE__) && defined(__MACH__)
67 return (size_t)rusage.ru_maxrss;
68#else
69 return (size_t)(rusage.ru_maxrss * 1024L);
70#endif
71
72#else
73 /* Unknown OS ----------------------------------------------- */
74 return (size_t)0L; /* Unsupported. */
75#endif
76}
77
78/**
79 * Returns the current resident set size (physical memory use) measured
80 * in bytes, or zero if the value cannot be determined on this OS.
81 */
82size_t getCurrentRSS() {
83#if defined(_WIN32)
84 /* Windows -------------------------------------------------- */
85 PROCESS_MEMORY_COUNTERS info;
86 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
87 return (size_t)info.WorkingSetSize;
88
89#elif defined(__APPLE__) && defined(__MACH__)
90 /* OSX ------------------------------------------------------ */
91 struct mach_task_basic_info info;
92 mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
93 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &infoCount) != KERN_SUCCESS)
94 return (size_t)0L; /* Can't access? */
95 return (size_t)info.resident_size;
96
97#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
98 /* Linux ---------------------------------------------------- */
99 long rss = 0L;
100 FILE * fp = NULL;
101 if ((fp = fopen("/proc/self/statm", "r")) == NULL)
102 return (size_t)0L; /* Can't open? */
103 if (fscanf(fp, "%*s%ld", &rss) != 1) {
104 fclose(fp);
105 return (size_t)0L; /* Can't read? */
106 }
107 fclose(fp);
108 return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
109
110#else
111 /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
112 return (size_t)0L; /* Unsupported. */
113#endif
114}
115
116#endif
size_t getCurrentRSS()
Returns the current resident set size (physical memory use) measured in bytes, or zero if the value c...
Definition: SystemMemory.h:82
size_t getPeakRSS()
Returns the peak (maximum so far) resident set size (physical memory use) measured in bytes,...
Definition: SystemMemory.h:41
void info(HaloModel **hmp)
Definition: Wrapper.cpp:83