-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimistically read from /proc for process stats.
b/341774149
- Loading branch information
1 parent
27addc6
commit 9f95721
Showing
7 changed files
with
517 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
// 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_starboard.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<double> 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); | ||
} | ||
|
||
} // 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.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() { | ||
return GetCPUUsage(FilePath("/proc/self")); | ||
} | ||
|
||
// static | ||
bool ProcessMetricsHelper::GetCumulativeCPUUsagePerThread( | ||
CPUUsagePerThread& cpu_per_thread) { | ||
cpu_per_thread.clear(); | ||
FileEnumerator file_enum(FilePath("/proc/self/task"), /*recursive=*/false, | ||
FileEnumerator::DIRECTORIES); | ||
for (FilePath path = file_enum.Next(); !path.empty(); | ||
path = file_enum.Next()) { | ||
PlatformThreadId tid; | ||
if (!StringToInt(path.GetComponents().back(), &tid)) { | ||
return false; | ||
} | ||
cpu_per_thread.emplace_back(tid, GetCPUUsage(path)); | ||
} | ||
return !cpu_per_thread.empty(); | ||
} | ||
|
||
// 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) { | ||
double ticks_per_s = clock_ticks_per_s.load(); | ||
if (ticks_per_s == 0) return TimeDelta(); | ||
// Get utime and stime. | ||
auto fields = ProcessMetricsHelper::GetProcStatFields( | ||
std::move(read_callback), {13, 14}); | ||
if (fields.size() != 2) return TimeDelta(); | ||
double utime; | ||
if (!StringToDouble(fields[0], &utime)) return TimeDelta(); | ||
double stime; | ||
if (!StringToDouble(fields[1], &stime)) return TimeDelta(); | ||
return TimeDelta::FromSecondsD((utime + stime) / ticks_per_s); | ||
} | ||
|
||
// static | ||
TimeDelta ProcessMetricsHelper::GetCPUUsage(const FilePath& path) { | ||
return ProcessMetricsHelper::GetCPUUsage( | ||
GetReadCallback(path.Append("stat"))); | ||
} | ||
|
||
// static | ||
std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( | ||
ProcessHandle process) { | ||
return WrapUnique(new ProcessMetrics(process)); | ||
} | ||
|
||
ProcessMetrics::ProcessMetrics(ProcessHandle process) {} | ||
|
||
ProcessMetrics::~ProcessMetrics() {} | ||
|
||
TimeDelta ProcessMetrics::GetCumulativeCPUUsage() { | ||
return ProcessMetricsHelper::GetCumulativeCPUUsage(); | ||
} | ||
|
||
bool ProcessMetrics::GetCumulativeCPUUsagePerThread( | ||
CPUUsagePerThread& cpu_per_thread) { | ||
return ProcessMetricsHelper::GetCumulativeCPUUsagePerThread(cpu_per_thread); | ||
} | ||
|
||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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_STARBOARD_H_ | ||
#define COBALT_BASE_PROCESS_PROCESS_METRICS_STARBOARD_H_ | ||
|
||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/bind.h" | ||
#include "base/process/process_metrics.h" | ||
#include "base/threading/platform_thread.h" | ||
#include "base/time/time.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
namespace base { | ||
|
||
class ProcessMetricsHelper { | ||
public: | ||
using CPUUsagePerThread = std::vector<std::pair<PlatformThreadId, 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 bool GetCumulativeCPUUsagePerThread(CPUUsagePerThread&); | ||
|
||
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); | ||
static TimeDelta GetCPUUsage(const FilePath&); | ||
}; | ||
|
||
} // namespace base | ||
|
||
#endif // COBALT_BASE_PROCESS_PROCESS_METRICS_STARBOARD_H_ |
Oops, something went wrong.