blob: 41b27ededaf22c9a86ba4a620cdc2848b892f5bc [file] [log] [blame]
ilnikab210c82017-02-28 02:18:27 -08001/*
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 06:47:31 +020011#include "rtc_base/memory_usage.h"
ilnikab210c82017-02-28 02:18:27 -080012
13#if defined(WEBRTC_LINUX)
14#include <unistd.h>
15#include <cstdlib>
16#include <cstdio>
17#include <cstring>
18#elif defined(WEBRTC_MAC)
19#include <mach/mach.h>
20#elif defined(WEBRTC_WIN)
21#include <windows.h>
22#include <psapi.h>
23#endif
24
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/logging.h"
ilnikab210c82017-02-28 02:18:27 -080026
27namespace rtc {
28
29int64_t GetProcessResidentSizeBytes() {
30#if defined(WEBRTC_LINUX)
31 FILE* file = fopen("/proc/self/statm", "r");
32 if (file == nullptr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010033 RTC_LOG(LS_ERROR) << "Failed to open /proc/self/statm";
ilnikab210c82017-02-28 02:18:27 -080034 return -1;
35 }
36 int result = -1;
37 if (fscanf(file, "%*s%d", &result) != 1) {
38 fclose(file);
Mirko Bonadei675513b2017-11-09 11:09:25 +010039 RTC_LOG(LS_ERROR) << "Failed to parse /proc/self/statm";
ilnikab210c82017-02-28 02:18:27 -080040 return -1;
41 }
42 fclose(file);
43 return static_cast<int64_t>(result) * sysconf(_SC_PAGESIZE);
44#elif defined(WEBRTC_MAC)
45 task_basic_info_64 info;
46 mach_msg_type_number_t info_count = TASK_BASIC_INFO_64_COUNT;
47 if (task_info(mach_task_self(), TASK_BASIC_INFO_64,
48 reinterpret_cast<task_info_t>(&info),
49 &info_count) != KERN_SUCCESS) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010050 RTC_LOG_ERR(LS_ERROR) << "task_info() failed";
ilnikab210c82017-02-28 02:18:27 -080051 return -1;
52 }
53 return info.resident_size;
54#elif defined(WEBRTC_WIN)
55 PROCESS_MEMORY_COUNTERS pmc;
56 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)) == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010057 RTC_LOG_ERR(LS_ERROR) << "GetProcessMemoryInfo() failed";
ilnikab210c82017-02-28 02:18:27 -080058 return -1;
59 }
60 return pmc.WorkingSetSize;
61#else
62 // Not implemented yet.
63 static_assert(false,
64 "GetProcessVirtualMemoryUsageBytes() platform support not yet "
65 "implemented.");
66#endif
67}
68
69} // namespace rtc