1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/controller/memory_usage_monitor_posix.h"
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <utility>
#include "base/compiler_specific.h"
#include "build/build_config.h"
#include "third_party/blink/public/platform/platform.h"
namespace blink {
namespace {
bool ReadFileContents(int fd, base::span<char> contents) {
lseek(fd, 0, SEEK_SET);
ssize_t res = read(fd, contents.data(), contents.size() - 1);
if (res <= 0)
return false;
contents[res] = '\0';
return true;
}
static MemoryUsageMonitor* g_instance_for_testing = nullptr;
MemoryUsageMonitorPosix& GetMemoryUsageMonitor() {
DEFINE_STATIC_LOCAL(MemoryUsageMonitorPosix, monitor, ());
return monitor;
}
} // namespace
// static
MemoryUsageMonitor& MemoryUsageMonitor::Instance() {
return g_instance_for_testing ? *g_instance_for_testing
: GetMemoryUsageMonitor();
}
// static
void MemoryUsageMonitor::SetInstanceForTesting(MemoryUsageMonitor* instance) {
g_instance_for_testing = instance;
}
// Since the measurement is done every second in background, optimizations are
// in place to get just the metrics we need from the proc files. So, this
// calculation exists here instead of using the cross-process memory-infra code.
bool MemoryUsageMonitorPosix::CalculateProcessMemoryFootprint(
int statm_fd,
int status_fd,
uint64_t* private_footprint,
uint64_t* swap_footprint,
uint64_t* vm_size,
uint64_t* vm_hwm_size) {
// Get total resident and shared sizes from statm file.
static size_t page_size = getpagesize();
uint64_t resident_pages;
uint64_t shared_pages;
uint64_t vm_size_pages;
constexpr uint32_t kMaxLineSize = 4096;
char line[kMaxLineSize];
if (!ReadFileContents(statm_fd, line))
return false;
int num_scanned =
UNSAFE_TODO(sscanf(line, "%" SCNu64 " %" SCNu64 " %" SCNu64,
&vm_size_pages, &resident_pages, &shared_pages));
if (num_scanned != 3)
return false;
// Get swap size from status file. The format is: VmSwap : 10 kB.
if (!ReadFileContents(status_fd, line))
return false;
char* swap_line = UNSAFE_TODO(strstr(line, "VmSwap"));
if (!swap_line)
return false;
num_scanned =
UNSAFE_TODO(sscanf(swap_line, "VmSwap: %" SCNu64 " kB", swap_footprint));
if (num_scanned != 1)
return false;
char* hwm_line = UNSAFE_TODO(strstr(line, "VmHWM"));
if (!hwm_line)
return false;
num_scanned =
UNSAFE_TODO(sscanf(hwm_line, "VmHWM: %" SCNu64 " kB", vm_hwm_size));
if (num_scanned != 1)
return false;
*vm_hwm_size *= 1024;
*swap_footprint *= 1024;
*private_footprint =
(resident_pages - shared_pages) * page_size + *swap_footprint;
*vm_size = vm_size_pages * page_size;
return true;
}
void MemoryUsageMonitorPosix::GetProcessMemoryUsage(MemoryUsage& usage) {
#if BUILDFLAG(IS_ANDROID)
ResetFileDescriptors();
#endif
if (!statm_fd_.is_valid() || !status_fd_.is_valid())
return;
uint64_t private_footprint, swap, vm_size, vm_hwm_size;
if (CalculateProcessMemoryFootprint(statm_fd_.get(), status_fd_.get(),
&private_footprint, &swap, &vm_size,
&vm_hwm_size)) {
usage.private_footprint_bytes = static_cast<double>(private_footprint);
usage.swap_bytes = static_cast<double>(swap);
usage.vm_size_bytes = static_cast<double>(vm_size);
usage.peak_resident_bytes = static_cast<double>(vm_hwm_size);
}
}
#if BUILDFLAG(IS_ANDROID)
void MemoryUsageMonitorPosix::ResetFileDescriptors() {
if (file_descriptors_reset_)
return;
file_descriptors_reset_ = true;
// See https://goo.gl/KjWnZP For details about why we read these files from
// sandboxed renderer. Keep these files open when detection is enabled.
if (!statm_fd_.is_valid())
statm_fd_.reset(open("/proc/self/statm", O_RDONLY));
if (!status_fd_.is_valid())
status_fd_.reset(open("/proc/self/status", O_RDONLY));
}
#endif
void MemoryUsageMonitorPosix::SetProcFiles(base::File statm_file,
base::File status_file) {
DCHECK(statm_file.IsValid());
DCHECK(status_file.IsValid());
DCHECK_EQ(-1, statm_fd_.get());
DCHECK_EQ(-1, status_fd_.get());
statm_fd_.reset(statm_file.TakePlatformFile());
status_fd_.reset(status_file.TakePlatformFile());
}
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// static
void MemoryUsageMonitorPosix::Bind(
mojo::PendingReceiver<mojom::blink::MemoryUsageMonitorLinux> receiver) {
// This should be called only once per process on RenderProcessWillLaunch.
DCHECK(!GetMemoryUsageMonitor().receiver_.is_bound());
GetMemoryUsageMonitor().receiver_.Bind(std::move(receiver));
}
#endif
} // namespace blink
|