ProcStats.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkTypes.h"
  8. #include "tools/ProcStats.h"
  9. #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) || defined(SK_BUILD_FOR_ANDROID)
  10. #include <sys/resource.h>
  11. int sk_tools::getMaxResidentSetSizeMB() {
  12. struct rusage ru;
  13. getrusage(RUSAGE_SELF, &ru);
  14. #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
  15. return static_cast<int>(ru.ru_maxrss / 1024 / 1024); // Darwin reports bytes.
  16. #else
  17. return static_cast<int>(ru.ru_maxrss / 1024); // Linux reports kilobytes.
  18. #endif
  19. }
  20. #elif defined(SK_BUILD_FOR_WIN)
  21. #include <windows.h>
  22. #include <psapi.h>
  23. int sk_tools::getMaxResidentSetSizeMB() {
  24. PROCESS_MEMORY_COUNTERS info;
  25. GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
  26. return static_cast<int>(info.PeakWorkingSetSize / 1024 / 1024); // Windows reports bytes.
  27. }
  28. #else
  29. int sk_tools::getMaxResidentSetSizeMB() { return -1; }
  30. #endif
  31. #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
  32. #include <mach/mach.h>
  33. int sk_tools::getCurrResidentSetSizeMB() {
  34. mach_task_basic_info info;
  35. mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
  36. if (KERN_SUCCESS !=
  37. task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count)) {
  38. return -1;
  39. }
  40. return info.resident_size / 1024 / 1024; // Darwin reports bytes.
  41. }
  42. #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_ANDROID) // N.B. /proc is Linux-only.
  43. #include <unistd.h>
  44. #include <stdio.h>
  45. int sk_tools::getCurrResidentSetSizeMB() {
  46. const long pageSize = sysconf(_SC_PAGESIZE);
  47. long long rssPages = 0;
  48. if (FILE* statm = fopen("/proc/self/statm", "r")) {
  49. // statm contains: program-size rss shared text lib data dirty, all in page counts.
  50. int rc = fscanf(statm, "%*d %lld", &rssPages);
  51. fclose(statm);
  52. if (rc != 1) {
  53. return -1;
  54. }
  55. }
  56. return rssPages * pageSize / 1024 / 1024;
  57. }
  58. #elif defined(SK_BUILD_FOR_WIN)
  59. int sk_tools::getCurrResidentSetSizeMB() {
  60. PROCESS_MEMORY_COUNTERS info;
  61. GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
  62. return static_cast<int>(info.WorkingSetSize / 1024 / 1024); // Windows reports bytes.
  63. }
  64. #else
  65. int sk_tools::getCurrResidentSetSizeMB() { return -1; }
  66. #endif