#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/process/process_metrics.h"
#include <dirent.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <optional>
#include <string_view>
#include <utility>
#include "base/containers/span.h"
#include "base/cpu.h"
#include "base/files/dir_reader_posix.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/numerics/clamped_math.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/internal_linux.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/system/sys_info.h"
#include "base/threading/thread_restrictions.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
namespace base {
class ScopedAllowBlockingForProcessMetrics : public ScopedAllowBlocking { … };
namespace {
#if BUILDFLAG(IS_CHROMEOS)
uint64_t ReadFileToUint64(const FilePath& file) {
std::string file_contents;
if (!ReadFileToString(file, &file_contents))
return 0;
TrimWhitespaceASCII(file_contents, TRIM_ALL, &file_contents);
uint64_t file_contents_uint64 = 0;
if (!StringToUint64(file_contents, &file_contents_uint64))
return 0;
return file_contents_uint64;
}
#endif
base::expected<TimeDelta, ProcessCPUUsageError> ParseTotalCPUTimeFromStats(
base::span<const std::string> proc_stats) { … }
}
std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
ProcessHandle process) { … }
size_t ProcessMetrics::GetResidentSetSize() const { … }
base::expected<TimeDelta, ProcessCPUUsageError>
ProcessMetrics::GetCumulativeCPUUsage() { … }
bool ProcessMetrics::GetCumulativeCPUUsagePerThread(
CPUUsagePerThread& cpu_per_thread) { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
uint64_t ProcessMetrics::GetVmSwapBytes() const { … }
bool ProcessMetrics::GetPageFaultCounts(PageFaultCounts* counts) const { … }
#endif
int ProcessMetrics::GetOpenFdCount() const { … }
int ProcessMetrics::GetOpenFdSoftLimit() const { … }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX)
ProcessMetrics::ProcessMetrics(ProcessHandle process)
: … { … }
#else
ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process) {}
#endif
size_t GetSystemCommitCharge() { … }
size_t GetSystemCommitChargeFromMeminfo(const SystemMemoryInfoKB& meminfo) { … }
int ParseProcStatCPU(std::string_view input) { … }
int64_t GetNumberOfThreads(ProcessHandle process) { … }
const char kProcSelfExe[] = …;
namespace {
const size_t kDiskDriveName = …;
const size_t kDiskReads = …;
const size_t kDiskReadsMerged = …;
const size_t kDiskSectorsRead = …;
const size_t kDiskReadTime = …;
const size_t kDiskWrites = …;
const size_t kDiskWritesMerged = …;
const size_t kDiskSectorsWritten = …;
const size_t kDiskWriteTime = …;
const size_t kDiskIO = …;
const size_t kDiskIOTime = …;
const size_t kDiskWeightedIOTime = …;
}
Value::Dict SystemMemoryInfoKB::ToDict() const { … }
bool ParseProcMeminfo(std::string_view meminfo_data,
SystemMemoryInfoKB* meminfo) { … }
bool ParseProcVmstat(std::string_view vmstat_data, VmStatInfo* vmstat) { … }
bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { … }
Value::Dict VmStatInfo::ToDict() const { … }
bool GetVmStatInfo(VmStatInfo* vmstat) { … }
SystemDiskInfo::SystemDiskInfo() { … }
SystemDiskInfo::SystemDiskInfo(const SystemDiskInfo&) = default;
SystemDiskInfo& SystemDiskInfo::operator=(const SystemDiskInfo&) = default;
Value::Dict SystemDiskInfo::ToDict() const { … }
bool IsValidDiskName(std::string_view candidate) { … }
bool GetSystemDiskInfo(SystemDiskInfo* diskinfo) { … }
TimeDelta GetUserCpuTimeSinceBoot() { … }
#if BUILDFLAG(IS_CHROMEOS)
Value::Dict SwapInfo::ToDict() const {
Value::Dict res;
res.Set("num_reads", static_cast<double>(num_reads));
res.Set("num_writes", static_cast<double>(num_writes));
res.Set("orig_data_size", static_cast<double>(orig_data_size));
res.Set("compr_data_size", static_cast<double>(compr_data_size));
res.Set("mem_used_total", static_cast<double>(mem_used_total));
double ratio = compr_data_size ? static_cast<double>(orig_data_size) /
static_cast<double>(compr_data_size)
: 0;
res.Set("compression_ratio", ratio);
return res;
}
Value::Dict GraphicsMemoryInfoKB::ToDict() const {
Value::Dict res;
res.Set("gpu_objects", gpu_objects);
res.Set("gpu_memory_size", static_cast<double>(gpu_memory_size));
return res;
}
bool ParseZramMmStat(std::string_view mm_stat_data, SwapInfo* swap_info) {
std::vector<std::string_view> tokens = SplitStringPiece(
mm_stat_data, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
if (tokens.size() < 7) {
DLOG(WARNING) << "zram mm_stat: tokens: " << tokens.size()
<< " malformed line: " << mm_stat_data;
return false;
}
if (!StringToUint64(tokens[0], &swap_info->orig_data_size))
return false;
if (!StringToUint64(tokens[1], &swap_info->compr_data_size))
return false;
if (!StringToUint64(tokens[2], &swap_info->mem_used_total))
return false;
return true;
}
bool ParseZramStat(std::string_view stat_data, SwapInfo* swap_info) {
std::vector<std::string_view> tokens = SplitStringPiece(
stat_data, kWhitespaceASCII, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
if (tokens.size() < 11) {
DLOG(WARNING) << "zram stat: tokens: " << tokens.size()
<< " malformed line: " << stat_data;
return false;
}
if (!StringToUint64(tokens[0], &swap_info->num_reads))
return false;
if (!StringToUint64(tokens[4], &swap_info->num_writes))
return false;
return true;
}
namespace {
bool IgnoreZramFirstPage(uint64_t orig_data_size, SwapInfo* swap_info) {
if (orig_data_size <= 4096) {
swap_info->orig_data_size = 0;
swap_info->num_reads = 0;
swap_info->num_writes = 0;
swap_info->compr_data_size = 0;
swap_info->mem_used_total = 0;
return true;
}
return false;
}
void ParseZramPath(SwapInfo* swap_info) {
FilePath zram_path("/sys/block/zram0");
uint64_t orig_data_size =
ReadFileToUint64(zram_path.Append("orig_data_size"));
if (IgnoreZramFirstPage(orig_data_size, swap_info))
return;
swap_info->orig_data_size = orig_data_size;
swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads"));
swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes"));
swap_info->compr_data_size =
ReadFileToUint64(zram_path.Append("compr_data_size"));
swap_info->mem_used_total =
ReadFileToUint64(zram_path.Append("mem_used_total"));
}
bool GetSwapInfoImpl(SwapInfo* swap_info) {
ScopedAllowBlockingForProcessMetrics allow_blocking;
static std::optional<bool> use_new_zram_interface;
FilePath zram_mm_stat_file("/sys/block/zram0/mm_stat");
if (!use_new_zram_interface.has_value()) {
use_new_zram_interface = PathExists(zram_mm_stat_file);
}
if (!use_new_zram_interface.value()) {
ParseZramPath(swap_info);
return true;
}
std::string mm_stat_data;
if (!ReadFileToStringNonBlocking(zram_mm_stat_file, &mm_stat_data)) {
DLOG(WARNING) << "Failed to open " << zram_mm_stat_file.value();
return false;
}
if (!ParseZramMmStat(mm_stat_data, swap_info)) {
DLOG(WARNING) << "Failed to parse " << zram_mm_stat_file.value();
return false;
}
if (IgnoreZramFirstPage(swap_info->orig_data_size, swap_info))
return true;
FilePath zram_stat_file("/sys/block/zram0/stat");
std::string stat_data;
if (!ReadFileToStringNonBlocking(zram_stat_file, &stat_data)) {
DLOG(WARNING) << "Failed to open " << zram_stat_file.value();
return false;
}
if (!ParseZramStat(stat_data, swap_info)) {
DLOG(WARNING) << "Failed to parse " << zram_stat_file.value();
return false;
}
return true;
}
}
bool GetSwapInfo(SwapInfo* swap_info) {
if (!GetSwapInfoImpl(swap_info)) {
*swap_info = SwapInfo();
return false;
}
return true;
}
namespace {
size_t ParseSize(const std::string& value) {
size_t pos = value.find(' ');
std::string base = value.substr(0, pos);
std::string units = value.substr(pos + 1);
size_t ret = 0;
base::StringToSizeT(base, &ret);
if (units == "KiB") {
ret *= 1024;
} else if (units == "MiB") {
ret *= 1024 * 1024;
}
return ret;
}
struct DrmFdInfo {
size_t memory_total;
size_t memory_shared;
};
void GetFdInfoFromPid(pid_t pid,
std::map<unsigned int, struct DrmFdInfo>& fdinfo_table) {
const FilePath pid_path =
FilePath("/proc").AppendASCII(base::NumberToString(pid));
const FilePath fd_path = pid_path.AppendASCII("fd");
DirReaderPosix dir_reader(fd_path.value().c_str());
if (!dir_reader.IsValid()) {
return;
}
for (; dir_reader.Next();) {
const char* name = dir_reader.name();
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
}
struct stat stat;
int err = fstatat(dir_reader.fd(), name, &stat, 0);
if (err) {
continue;
}
if (!S_ISCHR(stat.st_mode) || major(stat.st_rdev) != 226) {
continue;
}
const FilePath fdinfo_path =
pid_path.AppendASCII("fdinfo").AppendASCII(name);
std::string fdinfo_data;
if (!ReadFileToStringNonBlocking(fdinfo_path, &fdinfo_data)) {
continue;
}
std::stringstream ss(fdinfo_data);
std::string line;
struct DrmFdInfo fdinfo = {};
unsigned int client_id = 0;
while (std::getline(ss, line, '\n')) {
size_t pos = line.find(':');
if (pos == std::string::npos) {
continue;
}
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
value = value.substr(value.find_first_not_of(" \t"));
if (key == "drm-client-id") {
base::StringToUint(value, &client_id);
} else if (key == "drm-total-memory") {
fdinfo.memory_total = ParseSize(value);
} else if (key == "drm-shared-memory") {
fdinfo.memory_shared = ParseSize(value);
}
}
if (client_id && fdinfo.memory_shared != fdinfo.memory_total) {
fdinfo_table[client_id] = fdinfo;
}
}
}
bool GetGraphicsMemoryInfoFdInfo(GraphicsMemoryInfoKB* gpu_meminfo) {
const FilePath clients_path("/run/debugfs_gpu/clients");
std::string clients_data;
std::map<unsigned int, struct DrmFdInfo> fdinfo_table;
if (!ReadFileToStringNonBlocking(clients_path, &clients_data)) {
return false;
}
std::istringstream clients_stream(clients_data);
std::string line;
while (std::getline(clients_stream, line)) {
pid_t pid;
int num_res = sscanf(&line.c_str()[21], "%5d", &pid);
if (num_res == 1) {
GetFdInfoFromPid(pid, fdinfo_table);
}
}
if (fdinfo_table.size() == 0) {
return false;
}
gpu_meminfo->gpu_memory_size = 0;
for (auto const& p : fdinfo_table) {
gpu_meminfo->gpu_memory_size += p.second.memory_total;
}
return true;
}
}
bool GetGraphicsMemoryInfo(GraphicsMemoryInfoKB* gpu_meminfo) {
if (GetGraphicsMemoryInfoFdInfo(gpu_meminfo)) {
return true;
}
#if defined(ARCH_CPU_X86_FAMILY)
static bool is_newer_kernel =
base::StartsWith(base::SysInfo::KernelVersion(), "5.");
static bool is_intel_cpu = base::CPU().vendor_name() == "GenuineIntel";
if (is_newer_kernel && is_intel_cpu)
return false;
#endif
#if defined(ARCH_CPU_ARM_FAMILY)
const FilePath geminfo_path("/run/debugfs_gpu/exynos_gem_objects");
#else
const FilePath geminfo_path("/run/debugfs_gpu/i915_gem_objects");
#endif
std::string geminfo_data;
gpu_meminfo->gpu_objects = -1;
gpu_meminfo->gpu_memory_size = -1;
if (ReadFileToStringNonBlocking(geminfo_path, &geminfo_data)) {
int gpu_objects = -1;
int64_t gpu_memory_size = -1;
int num_res = sscanf(geminfo_data.c_str(), "%d objects, %" SCNd64 " bytes",
&gpu_objects, &gpu_memory_size);
if (num_res == 2) {
gpu_meminfo->gpu_objects = gpu_objects;
gpu_meminfo->gpu_memory_size = gpu_memory_size;
}
}
#if defined(ARCH_CPU_ARM_FAMILY)
FilePath mali_memory_file("/sys/class/misc/mali0/device/memory");
std::string mali_memory_data;
if (ReadFileToStringNonBlocking(mali_memory_file, &mali_memory_data)) {
int64_t mali_size = -1;
int num_res =
sscanf(mali_memory_data.c_str(), "%" SCNd64 " bytes", &mali_size);
if (num_res == 1)
gpu_meminfo->gpu_memory_size += mali_size;
}
#endif
return gpu_meminfo->gpu_memory_size != -1;
}
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX)
int ProcessMetrics::GetIdleWakeupsPerSecond() { … }
#endif
}