1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #ifndef memtrack_hpp
- #define memtrack_hpp
- /*
- * Author: David Robert Nadeau
- * Site: http://NadeauSoftware.com/
- * License: Creative Commons Attribution 3.0 Unported License
- * http://creativecommons.org/licenses/by/3.0/deed.en_US
- */
- #include <unistd.h>
- #include <sys/resource.h>
- #include <stdio.h>
- namespace fv_util {
- /**
- * Returns the peak (maximum so far) resident set size (physical
- * memory use) measured in bytes.
- */
- size_t getPeakRSS() {
- struct rusage rusage;
- getrusage(RUSAGE_SELF, &rusage);
- return (size_t) (rusage.ru_maxrss * 1024L);
- }
- /**
- * Returns the current resident set size (physical memory use) measured
- * in bytes.
- */
- size_t getCurrentRSS() {
- long rss = 0L;
- FILE *fp = NULL;
- if ((fp = fopen("/proc/self/statm", "r")) == NULL)
- return (size_t) 0L; /* Can't open? */
- if (fscanf(fp, "%*s%ld", &rss) != 1) {
- fclose(fp);
- return (size_t) 0L; /* Can't read? */
- }
- fclose(fp);
- return (size_t) rss * (size_t) sysconf(_SC_PAGESIZE);
- }
- } // fv
- #endif // memtrack_hpp
|