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