From 929f8cf8216bca0ef86e67b25c390afd52d7bd17 Mon Sep 17 00:00:00 2001 From: cobalt-github-releaser-bot <95661244+cobalt-github-releaser-bot@users.noreply.github.com> Date: Tue, 1 Oct 2024 13:17:47 -0700 Subject: [PATCH] Cherry pick PR #4155: Fallback to 100 ticks per second for CPU usage calculation. (#4180) Refer to the original PR: https://github.com/youtube/cobalt/pull/4155 b/341774149 Co-authored-by: aee --- cobalt/base/process/process_metrics_helper.cc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cobalt/base/process/process_metrics_helper.cc b/cobalt/base/process/process_metrics_helper.cc index 9300ab810362..df20c890cf5c 100644 --- a/cobalt/base/process/process_metrics_helper.cc +++ b/cobalt/base/process/process_metrics_helper.cc @@ -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 @@ -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);