| 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 |
| 11 | #include "rtc_base/memory_usage.h" |
| 12 |
| 13 | #if defined(WEBRTC_LINUX) |
| 14 | #include <unistd.h> |
| 15 |
| 16 | #include <cstdio> |
| 17 | #elif defined(WEBRTC_MAC) |
| 18 | #include <mach/mach.h> |
| 19 | #elif defined(WEBRTC_WIN) |
| 20 | // clang-format off |
| 21 | // clang formating would change include order. |
| 22 | #include <windows.h> |
| 23 | #include <psapi.h> // must come after windows.h |
| 24 | // clang-format on |
| 25 | #endif |
| 26 |
| 27 | #include "rtc_base/logging.h" |
| 28 |
| 29 | namespace rtc { |
| 30 |
| 31 | int64_t GetProcessResidentSizeBytes() { |
| 32 | #if defined(WEBRTC_LINUX) |
| 33 | FILE* file = fopen("/proc/self/statm", "r"); |
| 34 | if (file == nullptr) { |
| 35 | RTC_LOG(LS_ERROR) << "Failed to open /proc/self/statm"; |
| 36 | return -1; |
| 37 | } |
| 38 | int result = -1; |
| 39 | if (fscanf(file, "%*s%d", &result) != 1) { |
| 40 | fclose(file); |
| 41 | RTC_LOG(LS_ERROR) << "Failed to parse /proc/self/statm"; |
| 42 | return -1; |
| 43 | } |
| 44 | fclose(file); |
| 45 | return static_cast<int64_t>(result) * sysconf(_SC_PAGESIZE); |
| 46 | #elif defined(WEBRTC_MAC) |
| 47 | task_basic_info_64 info; |
| 48 | mach_msg_type_number_t info_count = TASK_BASIC_INFO_64_COUNT; |
| 49 | if (task_info(mach_task_self(), TASK_BASIC_INFO_64, |
| 50 | reinterpret_cast<task_info_t>(&info), |
| 51 | &info_count) != KERN_SUCCESS) { |
| 52 | RTC_LOG_ERR(LS_ERROR) << "task_info() failed"; |
| 53 | return -1; |
| 54 | } |
| 55 | return info.resident_size; |
| 56 | #elif defined(WEBRTC_WIN) |
| 57 | PROCESS_MEMORY_COUNTERS pmc; |
| 58 | if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)) == 0) { |
| 59 | RTC_LOG_ERR(LS_ERROR) << "GetProcessMemoryInfo() failed"; |
| 60 | return -1; |
| 61 | } |
| 62 | return pmc.WorkingSetSize; |
| 63 | #elif defined(WEBRTC_FUCHSIA) |
| 64 | RTC_LOG_ERR(LS_ERROR) << "GetProcessResidentSizeBytes() not implemented"; |
| 65 | return 0; |
| 66 | #else |
| 67 | // Not implemented yet. |
| 68 | static_assert(false, |
| 69 | "GetProcessVirtualMemoryUsageBytes() platform support not yet " |
| 70 | "implemented."); |
| 71 | #endif |
| 72 | } |
| 73 |
| 74 | } // namespace rtc |
| 75 |