Wildmeshing Toolkit
getRSS.cpp
Go to the documentation of this file.
1 /*
2  * Author: David Robert Nadeau
3  * Site: http://NadeauSoftware.com/
4  * License: Creative Commons Attribution 3.0 Unported License
5  * http://creativecommons.org/licenses/by/3.0/deed.en_US
6  */
7 
8 #if defined(_WIN32)
9 // clang-format off
10 #include <windows.h> // must be included BEFORE psapi.h
11 #include <psapi.h>
12 // clang-format on
13 
14 #elif defined(__unix__) || defined(__unix) || defined(unix) || \
15  (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 #include "getRSS.h"
37 
38 namespace wmtk {
44 size_t getPeakRSS()
45 {
46 #if defined(_WIN32)
47  /* Windows -------------------------------------------------- */
48  PROCESS_MEMORY_COUNTERS info;
49  GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
50  return (size_t)info.PeakWorkingSetSize;
51 
52 #elif (defined(_AIX) || defined(__TOS__AIX__)) || \
53  (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
54  /* AIX and Solaris ------------------------------------------ */
55  struct psinfo psinfo;
56  int fd = -1;
57  if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1) return (size_t)0L; /* Can't open? */
58  if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
59  close(fd);
60  return (size_t)0L; /* Can't read? */
61  }
62  close(fd);
63  return (size_t)(psinfo.pr_rssize * 1024L);
64 
65 #elif defined(__unix__) || defined(__unix) || defined(unix) || \
66  (defined(__APPLE__) && defined(__MACH__))
67  /* BSD, Linux, and OSX -------------------------------------- */
68  struct rusage rusage;
69  getrusage(RUSAGE_SELF, &rusage);
70 #if defined(__APPLE__) && defined(__MACH__)
71  return (size_t)rusage.ru_maxrss;
72 #else
73  return (size_t)(rusage.ru_maxrss * 1024L);
74 #endif
75 
76 #else
77  /* Unknown OS ----------------------------------------------- */
78  return (size_t)0L; /* Unsupported. */
79 #endif
80 }
81 
82 
87 size_t getCurrentRSS()
88 {
89 #if defined(_WIN32)
90  /* Windows -------------------------------------------------- */
91  PROCESS_MEMORY_COUNTERS info;
92  GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
93  return (size_t)info.WorkingSetSize;
94 
95 #elif defined(__APPLE__) && defined(__MACH__)
96  /* OSX ------------------------------------------------------ */
97  struct mach_task_basic_info info;
98  mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
99  if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &infoCount) !=
100  KERN_SUCCESS)
101  return (size_t)0L; /* Can't access? */
102  return (size_t)info.resident_size;
103 
104 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
105  /* Linux ---------------------------------------------------- */
106  long rss = 0L;
107  FILE* fp = NULL;
108  if ((fp = fopen("/proc/self/statm", "r")) == NULL) return (size_t)0L; /* Can't open? */
109  if (fscanf(fp, "%*s%ld", &rss) != 1) {
110  fclose(fp);
111  return (size_t)0L; /* Can't read? */
112  }
113  fclose(fp);
114  return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
115 
116 #else
117  /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
118  return (size_t)0L; /* Unsupported. */
119 #endif
120 }
121 } // namespace wmtk
Definition: Accessor.hpp:6
size_t getPeakRSS()
Returns the peak (maximum so far) resident set size (physical memory use) measured in bytes,...
Definition: getRSS.cpp:44
size_t getCurrentRSS()
Returns the current resident set size (physical memory use) measured in bytes, or zero if the value c...
Definition: getRSS.cpp:87