Skip to content

Commit

Permalink
Optimistically read from /proc for process stats.
Browse files Browse the repository at this point in the history
b/341774149
  • Loading branch information
aee-google committed Jun 7, 2024
1 parent 678cd4f commit f738548
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,13 @@ component("base") {
]
}

if (use_cobalt_customizations) {
sources += [
"process/internal_linux.cc",
"process/internal_linux.h",
]
}

if (!use_cobalt_customizations && (is_linux || is_chromeos || is_android)) {
sources += [
"files/file_path_watcher_inotify.cc",
Expand Down
13 changes: 13 additions & 0 deletions base/process/internal_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

#include "base/process/internal_linux.h"

#if !defined(STARBOARD)
#include <limits.h>
#include <unistd.h>
#endif // !defined(STARBOARD)

#include <map>
#include <string>
Expand All @@ -29,10 +31,12 @@
namespace base {
namespace internal {


const char kProcDir[] = "/proc";

const char kStatFile[] = "stat";

#if !defined(STARBOARD)
FilePath GetProcPidDir(pid_t pid) {
return FilePath(kProcDir).Append(NumberToString(pid));
}
Expand All @@ -56,6 +60,7 @@ pid_t ProcDirSlotToPid(const char* d_name) {
}
return pid;
}
#endif // !defined(STARBOARD)

bool ReadProcFile(const FilePath& file, std::string* buffer) {
DCHECK(FilePath(kProcDir).IsParent(file));
Expand All @@ -70,10 +75,12 @@ bool ReadProcFile(const FilePath& file, std::string* buffer) {
return !buffer->empty();
}

#if !defined(STARBOARD)
bool ReadProcStats(pid_t pid, std::string* buffer) {
FilePath stat_file = internal::GetProcPidDir(pid).Append(kStatFile);
return ReadProcFile(stat_file, buffer);
}
#endif // !defined(STARBOARD)

bool ParseProcStats(const std::string& stats_data,
std::vector<std::string>* proc_stats) {
Expand Down Expand Up @@ -152,16 +159,19 @@ int64_t ReadStatFileAndGetFieldAsInt64(const FilePath& stat_file,
return GetProcStatsFieldAsInt64(proc_stats, field_num);
}

#if !defined(STARBOARD)
int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num) {
FilePath stat_file = internal::GetProcPidDir(pid).Append(kStatFile);
return ReadStatFileAndGetFieldAsInt64(stat_file, field_num);
}
#endif // !defined(STARBOARD)

int64_t ReadProcSelfStatsAndGetFieldAsInt64(ProcStatsFields field_num) {
FilePath stat_file = FilePath(kProcDir).Append("self").Append(kStatFile);
return ReadStatFileAndGetFieldAsInt64(stat_file, field_num);
}

#if !defined(STARBOARD)
size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid,
ProcStatsFields field_num) {
std::string stats_data;
Expand All @@ -172,6 +182,7 @@ size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid,
return 0;
return GetProcStatsFieldAsSizeT(proc_stats, field_num);
}
#endif // !defined(STARBOARD)

Time GetBootTime() {
FilePath path("/proc/stat");
Expand All @@ -189,6 +200,7 @@ Time GetBootTime() {
return Time::FromTimeT(btime);
}

#if !defined(STARBOARD)
TimeDelta GetUserCpuTimeSinceBoot() {
FilePath path("/proc/stat");
std::string contents;
Expand Down Expand Up @@ -228,6 +240,7 @@ TimeDelta ClockTicksToTimeDelta(int64_t clock_ticks) {

return Microseconds(Time::kMicrosecondsPerSecond * clock_ticks / kHertz);
}
#endif // !defined(STARBOARD)

} // namespace internal
} // namespace base
14 changes: 14 additions & 0 deletions base/process/internal_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
#ifndef BASE_PROCESS_INTERNAL_LINUX_H_
#define BASE_PROCESS_INTERNAL_LINUX_H_

#if !defined(STARBOARD)
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#endif // !defined(STARBOARD)
#include <string>
#include <vector>

#if !defined(STARBOARD)
#include "base/files/dir_reader_posix.h"
#endif // !defined(STARBOARD)
#include "base/files/file_path.h"
#include "base/process/process_handle.h"
#include "base/strings/string_number_conversions.h"
Expand All @@ -33,14 +37,17 @@ extern const char kProcDir[];
// "stat"
extern const char kStatFile[];

#if !defined(STARBOARD)
// Returns a FilePath to "/proc/pid".
base::FilePath GetProcPidDir(pid_t pid);
#endif // !defined(STARBOARD)

// Reads a file from /proc into a string. This is allowed on any thread as
// reading from /proc does not hit the disk. Returns true if the file can be
// read and is non-empty.
bool ReadProcFile(const FilePath& file, std::string* buffer);

#if !defined(STARBOARD)
// Take a /proc directory entry named |d_name|, and if it is the directory for
// a process, convert it to a pid_t.
// Returns 0 on failure.
Expand All @@ -50,6 +57,7 @@ pid_t ProcDirSlotToPid(const char* d_name);
// Reads /proc/<pid>/stat into |buffer|. Returns true if the file can be read
// and is non-empty.
bool ReadProcStats(pid_t pid, std::string* buffer);
#endif // !defined(STARBOARD)

// Takes |stats_data| and populates |proc_stats| with the values split by
// spaces. Taking into account the 2nd field may, in itself, contain spaces.
Expand Down Expand Up @@ -89,16 +97,21 @@ size_t GetProcStatsFieldAsSizeT(const std::vector<std::string>& proc_stats,
// ReadProcStats(). See GetProcStatsFieldAsInt64() for details.
int64_t ReadStatsFilendGetFieldAsInt64(const FilePath& stat_file,
ProcStatsFields field_num);
#if !defined(STARBOARD)
int64_t ReadProcStatsAndGetFieldAsInt64(pid_t pid, ProcStatsFields field_num);
#endif // !defined(STARBOARD)
int64_t ReadProcSelfStatsAndGetFieldAsInt64(ProcStatsFields field_num);

#if !defined(STARBOARD)
// Same as ReadProcStatsAndGetFieldAsInt64() but for size_t values.
size_t ReadProcStatsAndGetFieldAsSizeT(pid_t pid,
ProcStatsFields field_num);
#endif // !defined(STARBOARD)

// Returns the time that the OS started. Clock ticks are relative to this.
Time GetBootTime();

#if !defined(STARBOARD)
// Returns the amount of time spent in user space since boot across all CPUs.
TimeDelta GetUserCpuTimeSinceBoot();

Expand Down Expand Up @@ -130,6 +143,7 @@ void ForEachProcessTask(base::ProcessHandle process, Lambda&& lambda) {
lambda(tid, task_path);
}
}
#endif // !defined(STARBOARD)

} // namespace internal
} // namespace base
Expand Down
3 changes: 3 additions & 0 deletions cobalt/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ static_library("base") {
"path_provider.h",
"polymorphic_downcast.h",
"polymorphic_equatable.h",
"process/process_metrics_helper.cc",
"process/process_metrics_helper.h",
"ref_counted_lock.h",
"source_location.cc",
"source_location.h",
Expand Down Expand Up @@ -117,6 +119,7 @@ target(gtest_target_type, "base_test") {
"c_val_time_interval_timer_stats_test.cc",
"circular_buffer_shell_unittest.cc",
"fixed_size_lru_cache_test.cc",
"process/process_metrics_helper_test.cc",
"statistics_test.cc",
"token_test.cc",
]
Expand Down
183 changes: 183 additions & 0 deletions cobalt/base/process/process_metrics_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/base/process/process_metrics_helper.h"

#include <atomic>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ptr_util.h"
#include "base/process/internal_linux.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"

namespace base {

namespace {

static std::atomic<int> clock_ticks_per_s{0};

ProcessMetricsHelper::ReadCallback GetReadCallback(const FilePath& path) {
return BindOnce(
[](const FilePath& path) -> absl::optional<std::string> {
std::string contents;
if (!ReadFileToString(path, &contents)) return absl::nullopt;
return std::move(contents);
},
path);
}

double CalculateCPUUsageSeconds(const std::string& utime_string,
const std::string& stime_string,
int ticks_per_s) {
DCHECK_NE(ticks_per_s, 0);
double utime;
if (!StringToDouble(utime_string, &utime)) return 0.0;
double stime;
if (!StringToDouble(stime_string, &stime)) return 0.0;
return (utime + stime) / static_cast<double>(ticks_per_s);
}

} // namespace

// static
int ProcessMetricsHelper::GetClockTicksPerS() {
return clock_ticks_per_s.load();
}

// static
int ProcessMetricsHelper::GetClockTicksPerS(ReadCallback uptime_callback,
ReadCallback stat_callback) {
double uptime = 0.0;
{
auto uptime_contents = std::move(uptime_callback).Run();
if (!uptime_contents) return 0;
auto parts = SplitString(*uptime_contents, " ", TRIM_WHITESPACE,
SPLIT_WANT_NONEMPTY);
if (parts.size() == 0 || !StringToDouble(parts[0], &uptime) ||
uptime == 0.0) {
return 0;
}
}

auto fields = GetProcStatFields(std::move(stat_callback),
{internal::ProcStatsFields::VM_STARTTIME});
if (fields.size() != 1) return 0;
double starttime;
if (!StringToDouble(fields[0], &starttime) || starttime == 0.0) return 0;
int ticks_per_s = static_cast<int>(starttime / uptime);
int nearest_tens = 10 * ((ticks_per_s + 5) / 10);
return nearest_tens;
}

// static
void ProcessMetricsHelper::PopulateClockTicksPerS() {
DCHECK_EQ(clock_ticks_per_s.load(), 0);
clock_ticks_per_s.store(
GetClockTicksPerS(GetReadCallback(FilePath("/proc/uptime")),
GetReadCallback(FilePath("/proc/self/stat"))));
}

// static
TimeDelta ProcessMetricsHelper::GetCumulativeCPUUsage() {
int ticks_per_s = clock_ticks_per_s.load();
if (ticks_per_s == 0) return TimeDelta();
return GetCPUUsage(FilePath("/proc/self"), ticks_per_s);
}

// static
Value ProcessMetricsHelper::GetCumulativeCPUUsagePerThread() {
int ticks_per_s = clock_ticks_per_s.load();
if (ticks_per_s == 0) return Value();
Value::List cpu_per_thread;
FileEnumerator file_enum(FilePath("/proc/self/task"), /*recursive=*/false,
FileEnumerator::DIRECTORIES);
for (FilePath path = file_enum.Next(); !path.empty();
path = file_enum.Next()) {
Fields fields =
GetProcStatFields(path, {0, internal::ProcStatsFields::VM_COMM,
internal::ProcStatsFields::VM_UTIME,
internal::ProcStatsFields::VM_STIME});
if (fields.size() != 4) continue;
int id;
if (!StringToInt(fields[0], &id)) continue;
Value::Dict entry =
Value::Dict()
.Set("id", id)
.Set("name", fields[1])
.Set("utime", fields[2])
.Set("stime", fields[3])
.Set("usage_seconds",
CalculateCPUUsageSeconds(fields[2], fields[3], ticks_per_s));
cpu_per_thread.Append(std::move(entry));
}
return Value(std::move(cpu_per_thread));
}

// static
ProcessMetricsHelper::Fields ProcessMetricsHelper::GetProcStatFields(
ReadCallback read_callback, std::initializer_list<int> indices) {
absl::optional<std::string> contents = std::move(read_callback).Run();
if (!contents) return Fields();

std::vector<std::string> proc_stats;
if (!internal::ParseProcStats(*contents, &proc_stats)) return Fields();

Fields fields;
for (int index : indices) {
if (index < 0 || index >= proc_stats.size()) return Fields();

std::string field = proc_stats[index];
if (index == 0 && field.back() == ' ') {
field = std::string(TrimWhitespaceASCII(proc_stats[index], TRIM_ALL));
}
fields.push_back(std::move(field));
}
return std::move(fields);
}

// static
ProcessMetricsHelper::Fields ProcessMetricsHelper::GetProcStatFields(
const FilePath& path, std::initializer_list<int> indices) {
return ProcessMetricsHelper::GetProcStatFields(
GetReadCallback(path.Append("stat")), indices);
}

// static
TimeDelta ProcessMetricsHelper::GetCPUUsage(ReadCallback read_callback,
int ticks_per_s) {
auto fields = ProcessMetricsHelper::GetProcStatFields(
std::move(read_callback), {internal::ProcStatsFields::VM_UTIME,
internal::ProcStatsFields::VM_STIME});
if (fields.size() != 2) return TimeDelta();
return TimeDelta::FromSecondsD(
CalculateCPUUsageSeconds(fields[0], fields[1], ticks_per_s));
}

// static
TimeDelta ProcessMetricsHelper::GetCPUUsage(const FilePath& path,
int ticks_per_s) {
return ProcessMetricsHelper::GetCPUUsage(GetReadCallback(path.Append("stat")),
ticks_per_s);
}

} // namespace base
Loading

0 comments on commit f738548

Please sign in to comment.