blob: 2d33d98eeda6311b8a2ae67d8f5ad43b7fc8f265 [file] [log] [blame]
ilnikab210c82017-02-28 10:18:271/*
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 Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/memory_usage.h"
ilnikab210c82017-02-28 10:18:2712
13#if defined(WEBRTC_LINUX)
14#include <unistd.h>
Jonas Olssona4d87372019-07-05 17:08:3315
ilnikab210c82017-02-28 10:18:2716#include <cstdio>
ilnikab210c82017-02-28 10:18:2717#elif defined(WEBRTC_MAC)
18#include <mach/mach.h>
19#elif defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 13:03:0520// clang-format off
21// clang formating would change include order.
ilnikab210c82017-02-28 10:18:2722#include <windows.h>
Yves Gerey3e707812018-11-28 15:47:4923#include <psapi.h> // must come after windows.h
Yves Gerey665174f2018-06-19 13:03:0524// clang-format on
Sarah Pham74f108a2023-01-19 23:18:1925#elif defined(WEBRTC_FUCHSIA)
26#include <lib/zx/process.h>
27#include <zircon/status.h>
ilnikab210c82017-02-28 10:18:2728#endif
29
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "rtc_base/logging.h"
ilnikab210c82017-02-28 10:18:2731
32namespace rtc {
33
34int64_t GetProcessResidentSizeBytes() {
35#if defined(WEBRTC_LINUX)
36 FILE* file = fopen("/proc/self/statm", "r");
37 if (file == nullptr) {
Mirko Bonadei675513b2017-11-09 10:09:2538 RTC_LOG(LS_ERROR) << "Failed to open /proc/self/statm";
ilnikab210c82017-02-28 10:18:2739 return -1;
40 }
41 int result = -1;
42 if (fscanf(file, "%*s%d", &result) != 1) {
43 fclose(file);
Mirko Bonadei675513b2017-11-09 10:09:2544 RTC_LOG(LS_ERROR) << "Failed to parse /proc/self/statm";
ilnikab210c82017-02-28 10:18:2745 return -1;
46 }
47 fclose(file);
48 return static_cast<int64_t>(result) * sysconf(_SC_PAGESIZE);
49#elif defined(WEBRTC_MAC)
50 task_basic_info_64 info;
51 mach_msg_type_number_t info_count = TASK_BASIC_INFO_64_COUNT;
52 if (task_info(mach_task_self(), TASK_BASIC_INFO_64,
53 reinterpret_cast<task_info_t>(&info),
54 &info_count) != KERN_SUCCESS) {
Mirko Bonadei675513b2017-11-09 10:09:2555 RTC_LOG_ERR(LS_ERROR) << "task_info() failed";
ilnikab210c82017-02-28 10:18:2756 return -1;
57 }
58 return info.resident_size;
59#elif defined(WEBRTC_WIN)
60 PROCESS_MEMORY_COUNTERS pmc;
61 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)) == 0) {
Mirko Bonadei675513b2017-11-09 10:09:2562 RTC_LOG_ERR(LS_ERROR) << "GetProcessMemoryInfo() failed";
ilnikab210c82017-02-28 10:18:2763 return -1;
64 }
65 return pmc.WorkingSetSize;
Scott Grahamf26e2902018-10-24 22:15:3666#elif defined(WEBRTC_FUCHSIA)
Sarah Pham74f108a2023-01-19 23:18:1967 zx_info_task_stats_t task_stats;
68 zx_status_t status = zx::process::self()->get_info(
69 ZX_INFO_TASK_STATS, &task_stats, sizeof(task_stats), nullptr, nullptr);
70 if (status == ZX_OK) {
71 return task_stats.mem_mapped_bytes;
72 } else {
73 RTC_LOG_ERR(LS_ERROR) << "get_info() failed: "
74 << zx_status_get_string(status);
75 return -1;
76 }
ilnikab210c82017-02-28 10:18:2777#else
78 // Not implemented yet.
79 static_assert(false,
80 "GetProcessVirtualMemoryUsageBytes() platform support not yet "
81 "implemented.");
82#endif
83}
84
85} // namespace rtc