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 5, 2024
1 parent 678cd4f commit d27d65d
Show file tree
Hide file tree
Showing 5 changed files with 472 additions and 0 deletions.
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
203 changes: 203 additions & 0 deletions cobalt/base/process/process_metrics_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// 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/strings/string_number_conversions.h"
#include "base/strings/string_split.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;
}
}

// Get starttime.
auto fields = GetProcStatFields(std::move(stat_callback), {21});
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
PlatformThreadId ProcessMetricsHelper::GetPid() {
PlatformThreadId pid;
auto fields = GetProcStatFields(FilePath("/proc/self"), {0});
if (fields.size() != 1 || !StringToInt(fields[0], &pid)) {
return -1;
}
return pid;
}

// 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
base::Value ProcessMetricsHelper::GetCumulativeCPUUsagePerThread() {
int ticks_per_s = clock_ticks_per_s.load();
if (ticks_per_s == 0) return base::Value();
base::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()) {
// tid, thread name, utime, and stime.
Fields fields = GetProcStatFields(path, {0, 1, 13, 14});
if (fields.size() != 4) continue;
int id;
if (!StringToInt(fields[0], &id)) continue;
base::Value::Dict entry =
base::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 base::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();

// Between the first '(' and last ')' is the second field called comm. It
// contains the process name and can include spaces.
int comm_start = contents->find('(') + 1;
int comm_end = contents->rfind(')');
// End before " (".
std::string pid = contents->substr(0, comm_start - 2);
std::string comm = contents->substr(comm_start, comm_end - comm_start);
// Field after comm is state. Start after ") ".
int state_start = comm_end + 2;
// Split the string starting with the state field.
auto parts = SplitString(contents->substr(state_start), " ", TRIM_WHITESPACE,
SPLIT_WANT_NONEMPTY);
Fields fields;
for (int index : indices) {
if (index < 0 || index >= parts.size() + 2) {
return Fields();
}
if (index == 0) {
fields.push_back(pid);
} else if (index == 1) {
fields.push_back(comm);
} else {
// Shift index to account for pid and comm.
int offset_index = index - 2;
fields.push_back(parts[offset_index]);
}
}
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) {
// Get utime and stime.
auto fields = ProcessMetricsHelper::GetProcStatFields(
std::move(read_callback), {13, 14});
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
55 changes: 55 additions & 0 deletions cobalt/base/process/process_metrics_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.

#ifndef COBALT_BASE_PROCESS_PROCESS_METRICS_HELPER_H_
#define COBALT_BASE_PROCESS_PROCESS_METRICS_HELPER_H_

#include <string>
#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "base/values.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace base {

class ProcessMetricsHelper {
public:
using CPUUsagePerThread = std::vector<std::pair<std::string, TimeDelta>>;
using ReadCallback = OnceCallback<absl::optional<std::string>()>;
using Fields = std::vector<std::string>;

static int GetClockTicksPerS();
static void PopulateClockTicksPerS();
static PlatformThreadId GetPid();
static TimeDelta GetCumulativeCPUUsage();
static base::Value GetCumulativeCPUUsagePerThread();

private:
friend class ProcessMetricsHelperTest;

static int GetClockTicksPerS(ReadCallback, ReadCallback);
static Fields GetProcStatFields(ReadCallback, std::initializer_list<int>);
static Fields GetProcStatFields(const FilePath&, std::initializer_list<int>);
static TimeDelta GetCPUUsage(ReadCallback, int);
static TimeDelta GetCPUUsage(const FilePath&, int);
};

} // namespace base

#endif // COBALT_BASE_PROCESS_PROCESS_METRICS_HELPER_H_
Loading

0 comments on commit d27d65d

Please sign in to comment.