PolyFEM
Loading...
Searching...
No Matches
getRSS.c
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#include <windows.h>
10#include <psapi.h>
11
12#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
13#include <unistd.h>
14#include <sys/resource.h>
15
16#if defined(__APPLE__) && defined(__MACH__)
17#include <mach/mach.h>
18
19#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
20#include <fcntl.h>
21#include <procfs.h>
22
23#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
24#include <stdio.h>
25
26#endif
27
28#else
29#error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
30#endif
31
37size_t getPeakRSS(void)
38{
39#if defined(_WIN32)
40 /* Windows -------------------------------------------------- */
41 PROCESS_MEMORY_COUNTERS info;
42 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
43 return (size_t)info.PeakWorkingSetSize;
44
45#elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
46 /* AIX and Solaris ------------------------------------------ */
47 struct psinfo psinfo;
48 int fd = -1;
49 if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
50 return (size_t)0L; /* Can't open? */
51 if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo))
52 {
53 close(fd);
54 return (size_t)0L; /* Can't read? */
55 }
56 close(fd);
57 return (size_t)(psinfo.pr_rssize * 1024L);
58
59#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
60 /* BSD, Linux, and OSX -------------------------------------- */
61 struct rusage rusage;
62 getrusage(RUSAGE_SELF, &rusage);
63#if defined(__APPLE__) && defined(__MACH__)
64 return (size_t)rusage.ru_maxrss;
65#else
66 return (size_t)(rusage.ru_maxrss * 1024L);
67#endif
68
69#else
70 /* Unknown OS ----------------------------------------------- */
71 return (size_t)0L; /* Unsupported. */
72#endif
73}
74
79size_t getCurrentRSS(void)
80{
81#if defined(_WIN32)
82 /* Windows -------------------------------------------------- */
83 PROCESS_MEMORY_COUNTERS info;
84 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
85 return (size_t)info.WorkingSetSize;
86
87#elif defined(__APPLE__) && defined(__MACH__)
88 /* OSX ------------------------------------------------------ */
89 struct mach_task_basic_info info;
90 mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
91 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
92 (task_info_t)&info, &infoCount)
93 != 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 {
105 fclose(fp);
106 return (size_t)0L; /* Can't read? */
107 }
108 fclose(fp);
109 return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
110
111#else
112 /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
113 return (size_t)0L; /* Unsupported. */
114#endif
115}
size_t getPeakRSS(void)
Returns the peak (maximum so far) resident set size (physical memory use) measured in bytes,...
Definition getRSS.c:37
size_t getCurrentRSS(void)
Returns the current resident set size (physical memory use) measured in bytes, or zero if the value c...
Definition getRSS.c:79