1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/metrics/power/process_metrics_recorder_util.h"
#include <cmath>
#include <optional>
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "build/build_config.h"
namespace {
// CPU usage metrics are provided as a double in the [0.0, number of cores *
// 100.0] range. The CPU usage is usually below 1%, so the histograms are
// reported with a 1/10000 granularity to make analyzing the data easier
// (otherwise almost all samples end up in the same [0, 1[ bucket).
constexpr int kCPUUsageFactor = 100;
// Scale up to the equivalent of 16 CPU cores fully loaded. As of 2025-05-06,
// this was capped at 2 cores but the 99th percentile of the data fell in the
// overflow bucket. To analyze rare but heavy workloads, we'd like the 99.999th
// percentile of samples to fall outside the overflow.
constexpr int kCPUUsageHistogramMin = 1;
constexpr int kCPUUsageHistogramMax = 1600 * kCPUUsageFactor;
constexpr int kCPUUsageHistogramBucketCount = 100;
#if BUILDFLAG(IS_WIN)
bool HasConstantRateTSC() {
#if defined(ARCH_CPU_ARM64)
// Constant rate TSC is never support on Arm CPUs.
return false;
#else
// Probe the CPU to detect if constant-rate TSC is supported.
return base::time_internal::HasConstantRateTSC();
#endif
}
#endif // BUILDFLAG(IS_WIN)
void RecordAverageCPUUsage(const char* histogram_suffix,
const std::optional<double>& cpu_usage) {
#if BUILDFLAG(IS_WIN)
// Skip recording the average CPU usage if the CPU doesn't support constant
// rate TSC, since Windows does not offer a way to get a precise measurement
// without it.
if (!HasConstantRateTSC())
return;
#endif
// The metric definition in
// tools/metrics/histograms/metadata/power/histograms.xml says, "If no process
// of type {ProcessName} existed during the interval, a sample of zero is
// still emitted."
base::UmaHistogramCustomCounts(
base::StrCat({"PerformanceMonitor.AverageCPU9.", histogram_suffix}),
cpu_usage.value_or(0.0) * kCPUUsageFactor, kCPUUsageHistogramMin,
kCPUUsageHistogramMax, kCPUUsageHistogramBucketCount);
}
} // namespace
void RecordProcessHistograms(const char* histogram_suffix,
const ProcessMonitor::Metrics& metrics) {
RecordAverageCPUUsage(histogram_suffix, metrics.cpu_usage);
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_AIX)
base::UmaHistogramCounts10000(
base::StrCat({"PerformanceMonitor.IdleWakeups2.", histogram_suffix}),
metrics.idle_wakeups);
#endif
#if BUILDFLAG(IS_MAC)
base::UmaHistogramCounts1000(
base::StrCat(
{"PerformanceMonitor.PackageExitIdleWakeups2.", histogram_suffix}),
metrics.package_idle_wakeups);
#endif
}
|