diff options
Diffstat (limited to 'tests/getrssusage.cpp')
-rw-r--r-- | tests/getrssusage.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/tests/getrssusage.cpp b/tests/getrssusage.cpp new file mode 100644 index 0000000..020c7d6 --- /dev/null +++ b/tests/getrssusage.cpp | |||
@@ -0,0 +1,124 @@ | |||
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 | * This file has been adapted to match the coding style of the rest of the codebase. | ||
8 | * | ||
9 | * On windows link against psapi.lib, for the rest the standard library is sufficient. | ||
10 | */ | ||
11 | #include "getrssusage.h" | ||
12 | |||
13 | #if defined(_WIN32) | ||
14 | #include <windows.h> | ||
15 | #include <psapi.h> | ||
16 | |||
17 | #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) | ||
18 | #include <unistd.h> | ||
19 | #include <sys/resource.h> | ||
20 | |||
21 | #if defined(__APPLE__) && defined(__MACH__) | ||
22 | #include <mach/mach.h> | ||
23 | |||
24 | #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__))) | ||
25 | #include <fcntl.h> | ||
26 | #include <procfs.h> | ||
27 | |||
28 | #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) | ||
29 | #include <stdio.h> | ||
30 | |||
31 | #endif | ||
32 | |||
33 | #else | ||
34 | #error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS." | ||
35 | #endif | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Returns the peak (maximum so far) resident set size (physical | ||
40 | * memory use) measured in bytes, or zero if the value cannot be | ||
41 | * determined on this OS. | ||
42 | */ | ||
43 | size_t getPeakRSS( ) | ||
44 | { | ||
45 | #if defined(_WIN32) | ||
46 | /* Windows -------------------------------------------------- */ | ||
47 | PROCESS_MEMORY_COUNTERS info; | ||
48 | GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) ); | ||
49 | return (size_t)info.PeakWorkingSetSize; | ||
50 | |||
51 | #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__))) | ||
52 | /* AIX and Solaris ------------------------------------------ */ | ||
53 | struct psinfo psinfo; | ||
54 | int fd = -1; | ||
55 | if ( (fd = open( "/proc/self/psinfo", O_RDONLY )) == -1 ) | ||
56 | return (size_t)0L; /* Can't open? */ | ||
57 | if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) ) | ||
58 | { | ||
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) || (defined(__APPLE__) && defined(__MACH__)) | ||
66 | /* BSD, Linux, and OSX -------------------------------------- */ | ||
67 | struct rusage rusage; | ||
68 | getrusage( RUSAGE_SELF, &rusage ); | ||
69 | #if defined(__APPLE__) && defined(__MACH__) | ||
70 | return (size_t)rusage.ru_maxrss; | ||
71 | #else | ||
72 | return (size_t)(rusage.ru_maxrss * 1024L); | ||
73 | #endif | ||
74 | |||
75 | #else | ||
76 | /* Unknown OS ----------------------------------------------- */ | ||
77 | return (size_t)0L; /* Unsupported. */ | ||
78 | #endif | ||
79 | } | ||
80 | |||
81 | |||
82 | |||
83 | |||
84 | |||
85 | /** | ||
86 | * Returns the current resident set size (physical memory use) measured | ||
87 | * in bytes, or zero if the value cannot be determined on this OS. | ||
88 | */ | ||
89 | size_t getCurrentRSS( ) | ||
90 | { | ||
91 | #if defined(_WIN32) | ||
92 | /* Windows -------------------------------------------------- */ | ||
93 | PROCESS_MEMORY_COUNTERS info; | ||
94 | GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) ); | ||
95 | return (size_t)info.WorkingSetSize; | ||
96 | |||
97 | #elif defined(__APPLE__) && defined(__MACH__) | ||
98 | /* OSX ------------------------------------------------------ */ | ||
99 | struct mach_task_basic_info info; | ||
100 | mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT; | ||
101 | if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO, | ||
102 | (task_info_t)&info, &infoCount ) != KERN_SUCCESS ) | ||
103 | return (size_t)0L; /* Can't access? */ | ||
104 | return (size_t)info.resident_size; | ||
105 | |||
106 | #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) | ||
107 | /* Linux ---------------------------------------------------- */ | ||
108 | long rss = 0L; | ||
109 | FILE* fp = NULL; | ||
110 | if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL ) | ||
111 | return (size_t)0L; /* Can't open? */ | ||
112 | if ( fscanf( fp, "%*s%ld", &rss ) != 1 ) | ||
113 | { | ||
114 | fclose( fp ); | ||
115 | return (size_t)0L; /* Can't read? */ | ||
116 | } | ||
117 | fclose( fp ); | ||
118 | return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE); | ||
119 | |||
120 | #else | ||
121 | /* AIX, BSD, Solaris, and Unknown OS ------------------------ */ | ||
122 | return (size_t)0L; /* Unsupported. */ | ||
123 | #endif | ||
124 | } | ||