labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

getRSS.h
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 //http://nadeausoftware.com/articles/2012/07/c_c_tip_how_get_process_resident_set_size_physical_memory_use
9 
10 #if defined(_WIN32)
11 #include <windows.h>
12 #include <psapi.h>
13 
14 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
15 #include <unistd.h>
16 #include <sys/resource.h>
17 
18 #if defined(__APPLE__) && defined(__MACH__)
19 #include <mach/mach.h>
20 
21 #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
22 #include <fcntl.h>
23 #include <procfs.h>
24 
25 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
26 #include <stdio.h>
27 
28 #endif
29 
30 #else
31 #error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
32 #endif
33 
34 
35 
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 
119  if ((size_t)rss > std::numeric_limits<size_t>::max()/(size_t)sysconf( _SC_PAGESIZE))
120  return std::numeric_limits<size_t>::max();
121  else
122  return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
123 
124 #else
125  /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
126  return (size_t)0L; /* Unsupported. */
127 #endif
128 }
size_t getCurrentRSS()
Definition: getRSS.h:89
size_t getPeakRSS()
Definition: getRSS.h:43
T read(std::ifstream &is)
Read value from stream.
Definition: ROOTFilter.cpp:542