memorymanagment.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "memorymanagment.h"
  2. /*
  3. * Author: David Robert Nadeau
  4. * Site: http://NadeauSoftware.com/
  5. * License: Creative Commons Attribution 3.0 Unported License
  6. * http://creativecommons.org/licenses/by/3.0/deed.en_US
  7. */
  8. #if defined(_WIN32)
  9. #include <windows.h>
  10. #include <psapi.h>
  11. #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
  12. #include <unistd.h>
  13. #include <sys/resource.h>
  14. #if defined(__APPLE__) && defined(__MACH__)
  15. #include <mach/mach.h>
  16. #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
  17. #include <fcntl.h>
  18. #include <procfs.h>
  19. #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
  20. #include <stdio.h>
  21. #endif
  22. #else
  23. #error "Cannot define getPeakRSS( ) or getCurrentRSS( ) for an unknown OS."
  24. #endif
  25. /**
  26. * Returns the peak (maximum so far) resident set size (physical
  27. * memory use) measured in bytes, or zero if the value cannot be
  28. * determined on this OS.
  29. */
  30. size_t getPeakRSS( )
  31. {
  32. #if defined(_WIN32)
  33. /* Windows -------------------------------------------------- */
  34. PROCESS_MEMORY_COUNTERS info;
  35. GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
  36. return (size_t)info.PeakWorkingSetSize;
  37. #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
  38. /* AIX and Solaris ------------------------------------------ */
  39. struct psinfo psinfo;
  40. int fd = -1;
  41. if ( (fd = open( "/proc/self/psinfo", O_RDONLY )) == -1 )
  42. return (size_t)0L; /* Can't open? */
  43. if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) )
  44. {
  45. close( fd );
  46. return (size_t)0L; /* Can't read? */
  47. }
  48. close( fd );
  49. return (size_t)(psinfo.pr_rssize * 1024L);
  50. #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
  51. /* BSD, Linux, and OSX -------------------------------------- */
  52. struct rusage rusage;
  53. getrusage( RUSAGE_SELF, &rusage );
  54. #if defined(__APPLE__) && defined(__MACH__)
  55. return (size_t)rusage.ru_maxrss;
  56. #else
  57. return (size_t)(rusage.ru_maxrss * 1024L);
  58. #endif
  59. #else
  60. /* Unknown OS ----------------------------------------------- */
  61. return (size_t)0L; /* Unsupported. */
  62. #endif
  63. }
  64. /**
  65. * Returns the current resident set size (physical memory use) measured
  66. * in bytes, or zero if the value cannot be determined on this OS.
  67. */
  68. size_t getCurrentRSS( )
  69. {
  70. #if defined(_WIN32)
  71. /* Windows -------------------------------------------------- */
  72. PROCESS_MEMORY_COUNTERS info;
  73. GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
  74. return (size_t)info.WorkingSetSize;
  75. #elif defined(__APPLE__) && defined(__MACH__)
  76. /* OSX ------------------------------------------------------ */
  77. struct mach_task_basic_info info;
  78. mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
  79. if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
  80. (task_info_t)&info, &infoCount ) != KERN_SUCCESS )
  81. return (size_t)0L; /* Can't access? */
  82. return (size_t)info.resident_size;
  83. #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
  84. /* Linux ---------------------------------------------------- */
  85. long rss = 0L;
  86. FILE* fp = NULL;
  87. if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL )
  88. return (size_t)0L; /* Can't open? */
  89. if ( fscanf( fp, "%*s%ld", &rss ) != 1 )
  90. {
  91. fclose( fp );
  92. return (size_t)0L; /* Can't read? */
  93. }
  94. fclose( fp );
  95. return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
  96. #else
  97. /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
  98. return (size_t)0L; /* Unsupported. */
  99. #endif
  100. }