You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.0 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "heapDebug.h"
  2. #if !defined( DEBUG ) && defined( SKIP_MEM_CHECK )
  3. size_t getPeakRSS( ){ return 0; };
  4. size_t getCurrentRSS( ){ return 0; };
  5. #else
  6. /**
  7. * Returns the peak (maximum so far) resident set size (physical
  8. * memory use) measured in bytes, or zero if the value cannot be
  9. * determined on this OS.
  10. */
  11. size_t getPeakRSS( )
  12. {
  13. #if defined(_WIN32)
  14. /* Windows -------------------------------------------------- */
  15. PROCESS_MEMORY_COUNTERS info;
  16. GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
  17. return (size_t)info.PeakWorkingSetSize;
  18. #elif (defined(_AIX) || defined(__TOS__AIX__)) || (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
  19. /* AIX and Solaris ------------------------------------------ */
  20. struct psinfo psinfo;
  21. int fd = -1;
  22. if ( (fd = open( "/proc/self/psinfo", O_RDONLY )) == -1 )
  23. return (size_t)0L; /* Can't open? */
  24. if ( read( fd, &psinfo, sizeof(psinfo) ) != sizeof(psinfo) )
  25. {
  26. close( fd );
  27. return (size_t)0L; /* Can't read? */
  28. }
  29. close( fd );
  30. return (size_t)(psinfo.pr_rssize * 1024L);
  31. #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
  32. /* BSD, Linux, and OSX -------------------------------------- */
  33. struct rusage rusage;
  34. getrusage( RUSAGE_SELF, &rusage );
  35. #if defined(__APPLE__) && defined(__MACH__)
  36. return (size_t)rusage.ru_maxrss;
  37. #else
  38. return (size_t)(rusage.ru_maxrss * 1024L);
  39. #endif
  40. #else
  41. /* Unknown OS ----------------------------------------------- */
  42. return (size_t)0L; /* Unsupported. */
  43. #endif
  44. }
  45. /**
  46. * Returns the current resident set size (physical memory use) measured
  47. * in bytes, or zero if the value cannot be determined on this OS.
  48. */
  49. size_t getCurrentRSS( )
  50. {
  51. #if defined(_WIN32)
  52. /* Windows -------------------------------------------------- */
  53. PROCESS_MEMORY_COUNTERS info;
  54. GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
  55. return (size_t)info.WorkingSetSize;
  56. #elif defined(__APPLE__) && defined(__MACH__)
  57. /* OSX ------------------------------------------------------ */
  58. struct mach_task_basic_info info;
  59. mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
  60. if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
  61. (task_info_t)&info, &infoCount ) != KERN_SUCCESS )
  62. return (size_t)0L; /* Can't access? */
  63. return (size_t)info.resident_size;
  64. #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
  65. /* Linux ---------------------------------------------------- */
  66. long rss = 0L;
  67. FILE* fp = NULL;
  68. if ( (fp = fopen( "/proc/self/statm", "r" )) == NULL )
  69. return (size_t)0L; /* Can't open? */
  70. if ( fscanf( fp, "%*s%ld", &rss ) != 1 )
  71. {
  72. fclose( fp );
  73. return (size_t)0L; /* Can't read? */
  74. }
  75. fclose( fp );
  76. return (size_t)rss * (size_t)sysconf( _SC_PAGESIZE);
  77. #else
  78. /* AIX, BSD, Solaris, and Unknown OS ------------------------ */
  79. return (size_t)0L; /* Unsupported. */
  80. #endif
  81. }
  82. #endif // DEBUG