Skip to content

Commit

Permalink
Cherry pick PR #4155: Fallback to 100 ticks per second for CPU usage … (
Browse files Browse the repository at this point in the history
#4442)

Fix CPU usage measurement when getting the clock ticks per second
reading failed (e.g. Android).

Refer to the original PR: #4155

b/341774149

Co-authored-by: cobalt-github-releaser-bot <[email protected]>
Co-authored-by: aee <[email protected]>
  • Loading branch information
3 people authored Nov 19, 2024
1 parent d5047e3 commit 2ee62f0
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions cobalt/base/process/process_metrics_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ double CalculateCPUUsageSeconds(const std::string& utime_string,

// static
int ProcessMetricsHelper::GetClockTicksPerS() {
return clock_ticks_per_s.load();
int result = clock_ticks_per_s.load();
return result > 0 ? result : 100;
}

// static
Expand Down Expand Up @@ -104,15 +105,12 @@ void ProcessMetricsHelper::PopulateClockTicksPerS() {

// 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);
return GetCPUUsage(FilePath("/proc/self"), GetClockTicksPerS());
}

// static
Value ProcessMetricsHelper::GetCumulativeCPUUsagePerThread() {
int ticks_per_s = clock_ticks_per_s.load();
if (ticks_per_s == 0) return Value();
int ticks_per_s = GetClockTicksPerS();
ListValue cpu_per_thread;
FileEnumerator file_enum(FilePath("/proc/self/task"), /*recursive=*/false,
FileEnumerator::DIRECTORIES);
Expand Down

0 comments on commit 2ee62f0

Please sign in to comment.