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