FMS  2025.04
Flexible Modeling System
mpp_memuse.c
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 /* Include headers common for Unix and Unix-like OSs. __unix__ is defined on Linux OSs */
9 #if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
10 #include <unistd.h>
11 #include <sys/resource.h>
12 
13 /* Include specific headers for specific OS types */
14 #if defined(__APPLE__) && defined(__MACH__)
15 #include <mach/mach.h>
16 
17 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
18 #include <stdio.h>
19 /* #endif for specific headers checks */
20 #endif
21 
22 #else
23 /* Not a Unix-like OS. Not supported */
24 #error "Cannot define getPeakRSS( ) for an unknown OS."
25 #endif
26 
27 /**
28  * Returns the peak (maximum so far) resident set size (physical
29  * memory use) measured in bytes, or zero if the value cannot be
30  * determined on this OS.
31  */
32 size_t getpeakrss( )
33 {
34 #if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
35  /* BSD, Linux, and OSX -------------------------------------- */
36  struct rusage rusage;
37  getrusage( RUSAGE_SELF, &rusage );
38 #if defined(__APPLE__) && defined(__MACH__)
39  return (size_t)rusage.ru_maxrss;
40 #else
41  return (size_t)(rusage.ru_maxrss * 1024L);
42 #endif
43 
44 #else
45  /* Unknown OS ----------------------------------------------- */
46  return (size_t)0L; /* Unsupported. */
47 #endif
48 }