MUQ  0.4.3
mymemory.h
Go to the documentation of this file.
1 #ifndef MYMEMORY_H
2 #define MYMEMORY_H
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 <windows.h>
13 #include <psapi.h>
14 
15 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
16 #include <unistd.h>
17 #include <sys/resource.h>
18 
19 #if defined(__APPLE__) && defined(__MACH__)
20 #include <mach/mach.h>
21 
22 #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
23 #include <fcntl.h>
24 #include <procfs.h>
25 
26 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
27 #include <stdio.h>
28 
29 #endif
30 
31 #else
32 #error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
33 #endif
34 
35 
40 inline size_t getCurrentRSS( )
41 {
42 #if defined(_WIN32)
43  /* Windows -------------------------------------------------- */
44  PROCESS_MEMORY_COUNTERS info;
45  GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
46  return (size_t)info.WorkingSetSize;
47 
48 #elif defined(__APPLE__) && defined(__MACH__)
49  /* OSX ------------------------------------------------------ */
50  struct mach_task_basic_info info;
51  mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
52  if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
53  (task_info_t)&info, &infoCount ) != KERN_SUCCESS )
54  return (size_t)0L; /* Can't access? */
55  return (size_t)info.resident_size;
56 
57 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
58  /* Linux ---------------------------------------------------- */
59  long rss = 0L;
60  FILE* fp = NULL;
61  if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL )
62  return (size_t)0L; /* Can't open? */
63  if ( fscanf( fp, "%*s%ld", &rss ) != 1 )
64  {
65  fclose( fp );
66  return (size_t)0L; /* Can't read? */
67  }
68  fclose( fp );
69  return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
70 
71 #else
72  /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
73  return (size_t)0L; /* Unsupported. */
74 #endif
75 }
76 
77 
78 #endif
size_t getCurrentRSS()
Definition: mymemory.h:40