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.

74 lines
2.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/memory_usage.h"
  11. #if defined(WEBRTC_LINUX)
  12. #include <unistd.h>
  13. #include <cstdio>
  14. #elif defined(WEBRTC_MAC)
  15. #include <mach/mach.h>
  16. #elif defined(WEBRTC_WIN)
  17. // clang-format off
  18. // clang formating would change include order.
  19. #include <windows.h>
  20. #include <psapi.h> // must come after windows.h
  21. // clang-format on
  22. #endif
  23. #include "rtc_base/logging.h"
  24. namespace rtc {
  25. int64_t GetProcessResidentSizeBytes() {
  26. #if defined(WEBRTC_LINUX)
  27. FILE* file = fopen("/proc/self/statm", "r");
  28. if (file == nullptr) {
  29. RTC_LOG(LS_ERROR) << "Failed to open /proc/self/statm";
  30. return -1;
  31. }
  32. int result = -1;
  33. if (fscanf(file, "%*s%d", &result) != 1) {
  34. fclose(file);
  35. RTC_LOG(LS_ERROR) << "Failed to parse /proc/self/statm";
  36. return -1;
  37. }
  38. fclose(file);
  39. return static_cast<int64_t>(result) * sysconf(_SC_PAGESIZE);
  40. #elif defined(WEBRTC_MAC)
  41. task_basic_info_64 info;
  42. mach_msg_type_number_t info_count = TASK_BASIC_INFO_64_COUNT;
  43. if (task_info(mach_task_self(), TASK_BASIC_INFO_64,
  44. reinterpret_cast<task_info_t>(&info),
  45. &info_count) != KERN_SUCCESS) {
  46. RTC_LOG_ERR(LS_ERROR) << "task_info() failed";
  47. return -1;
  48. }
  49. return info.resident_size;
  50. #elif defined(WEBRTC_WIN)
  51. PROCESS_MEMORY_COUNTERS pmc;
  52. if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)) == 0) {
  53. RTC_LOG_ERR(LS_ERROR) << "GetProcessMemoryInfo() failed";
  54. return -1;
  55. }
  56. return pmc.WorkingSetSize;
  57. #elif defined(WEBRTC_FUCHSIA)
  58. RTC_LOG_ERR(LS_ERROR) << "GetProcessResidentSizeBytes() not implemented";
  59. return 0;
  60. #else
  61. // Not implemented yet.
  62. static_assert(false,
  63. "GetProcessVirtualMemoryUsageBytes() platform support not yet "
  64. "implemented.");
  65. #endif
  66. }
  67. } // namespace rtc