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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
// Copyright 2023 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/performance_manager/metrics/metrics_provider_desktop.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/power_monitor/cpu_frequency_utils.h"
#include "base/process/process_metrics.h"
#include "base/system/sys_info.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/timer/timer.h"
#include "base/trace_event/base_tracing.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/performance_manager/public/user_tuning/user_performance_tuning_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/performance_manager/public/user_tuning/prefs.h"
#include "components/prefs/pref_service.h"
#include "ui/accessibility/ax_mode.h"
#include "ui/accessibility/platform/ax_platform_node.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/registry.h"
#endif
using performance_manager::user_tuning::prefs::kMemorySaverModeState;
using performance_manager::user_tuning::prefs::MemorySaverModeState;
namespace performance_manager {
namespace {
MetricsProviderDesktop* g_metrics_provider = nullptr;
uint64_t kBytesPerMb = 1024 * 1024;
#if SHOULD_COLLECT_CPU_FREQUENCY_METRICS()
enum class CpuThroughputEstimatedStatus {
kNormal,
kUnknown,
kThrottled,
kDescheduled,
kMigrated,
};
CpuThroughputEstimatedStatus EstimateCpuThroughputStatus(
bool migrated,
std::optional<double> cpu_frequency_percent,
std::optional<double> thread_time_percent,
base::TimeDelta queued_time) {
if (migrated) {
// If the task migrated from one CPU to the other, report the status as
// such. It's not relevant to check the thread time % in this instance,
// since a migrated task has by definition been descheduled. Likewise,
// checking the estimate vs nominal frequencies is also irrelevant, since
// the frequency of the original and migrated cores might be different.
return CpuThroughputEstimatedStatus::kMigrated;
}
if (!thread_time_percent) {
return CpuThroughputEstimatedStatus::kUnknown;
} else if (*thread_time_percent < 75.0) {
// If the task is actually running for only 75% of its wall time or less, we
// report it as having been descheduled
return CpuThroughputEstimatedStatus::kDescheduled;
}
if (!cpu_frequency_percent) {
return CpuThroughputEstimatedStatus::kUnknown;
} else if (*cpu_frequency_percent < 75.0) {
// If the task had a thread time close to its wall time, but we estimate its
// CPU frequency as 75% of nominal or less, we report the CPU as being
// throttled.
return CpuThroughputEstimatedStatus::kThrottled;
}
return CpuThroughputEstimatedStatus::kNormal;
}
constexpr char kCpuEstimationEventCategory[] = "power";
constexpr char kCpuEstimationEvent[] = "CpuStatusSampling";
constexpr char kCpuEstimationStatusNormalEvent[] =
"CpuStatusSampling.Status.Normal";
constexpr char kCpuEstimationStatusUnknownEvent[] =
"CpuStatusSampling.Status.Unknown";
constexpr char kCpuEstimationStatusThrottledEvent[] =
"CpuStatusSampling.Status.Throttled";
constexpr char kCpuEstimationStatusDescheduledEvent[] =
"CpuStatusSampling.Status.Descheduled";
constexpr char kCpuEstimationStatusMigratedEvent[] =
"CpuStatusSampling.Status.Migrated";
constexpr char kCpuEstimationQueuedEvent[] = "CpuStatusSampling.Queued";
constexpr char kCpuEstimationRunningEvent[] = "CpuStatusSampling.Running";
constexpr char kCpuEstimationThreadTimeEvent[] = "CpuStatusSampling.ThreadTime";
constexpr char kCpuEstimationDescheduledEvent[] =
"CpuStatusSampling.Descheduled";
// This function emits trace events related to the status of the CPU throughput
// estimation task. In a trace, these events might look like this (where CSS is
// CpuStatusSampling):
//
// +-----------------------------------------------+
// | CpuStatusSampling |
// +-----------------------------------------------+
// | CSS.Status.{estimated_status} |
// +------------+----------------------------------+
// | CSS.Queued | CSS.Running |
// +------------+----------------+-----------------+
// | CSS.ThreadTime | CSS.Descheduled |
// +----------------+-----------------+
//
// CpuStatusSampling.Status.{estimated_status} will reflect what the estimation
// code thinks the status of the CPU is, between Normal, Unknown, Throttled,
// Descheduled, Migrated.
//
// CpuStatusSampling.Queued is the time between when the estimation task was
// posted and when it started running.
//
// CpuStatusSampling.Running is the time between when the task started running
// and when it finished
//
// CpuStatusSampling.ThreadTime is the CPU time of the running task
//
// CpuStatusSampling.Descheduled is the wall time the task spent off-cpu
void EmitCpuStatusSamplingTraceEvents(base::TimeTicks posted_at_time,
base::TimeTicks started_running_time,
base::TimeDelta thread_time,
base::TimeDelta wall_time,
CpuThroughputEstimatedStatus status) {
void* id = g_metrics_provider;
base::TimeTicks end_time = started_running_time + wall_time;
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationEvent, TRACE_ID_LOCAL(id),
posted_at_time);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(kCpuEstimationEventCategory,
kCpuEstimationEvent,
TRACE_ID_LOCAL(id), end_time);
const char* selected;
switch (status) {
case CpuThroughputEstimatedStatus::kNormal:
selected = kCpuEstimationStatusNormalEvent;
break;
case CpuThroughputEstimatedStatus::kUnknown:
selected = kCpuEstimationStatusUnknownEvent;
break;
case CpuThroughputEstimatedStatus::kThrottled:
selected = kCpuEstimationStatusThrottledEvent;
break;
case CpuThroughputEstimatedStatus::kDescheduled:
selected = kCpuEstimationStatusDescheduledEvent;
break;
case CpuThroughputEstimatedStatus::kMigrated:
selected = kCpuEstimationStatusMigratedEvent;
break;
}
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(kCpuEstimationEventCategory,
selected, TRACE_ID_LOCAL(id),
posted_at_time);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, selected, TRACE_ID_LOCAL(id), end_time);
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationQueuedEvent,
TRACE_ID_LOCAL(id), posted_at_time);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationQueuedEvent,
TRACE_ID_LOCAL(id), started_running_time);
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationRunningEvent,
TRACE_ID_LOCAL(id), started_running_time);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(kCpuEstimationEventCategory,
kCpuEstimationRunningEvent,
TRACE_ID_LOCAL(id), end_time);
// Emit a block for the running thread time
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationThreadTimeEvent,
TRACE_ID_LOCAL(id), started_running_time);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationThreadTimeEvent,
TRACE_ID_LOCAL(id), started_running_time + thread_time);
// And then one of the wall time spent descheduled
TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationDescheduledEvent,
TRACE_ID_LOCAL(id), started_running_time + thread_time);
TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
kCpuEstimationEventCategory, kCpuEstimationDescheduledEvent,
TRACE_ID_LOCAL(id), started_running_time + wall_time);
}
#endif // SHOULD_COLLECT_CPU_FREQUENCY_METRICS()
#if BUILDFLAG(IS_WIN)
// Reports histograms describing the value of the HKEY_LOCAL_MACHINE ->
// Software\Microsoft\Windows NT\CurrentVersion\Image File ->
// FrontEndHeapDebugOptions registry key. We observed locally that the 0x10 bit
// activates stack collection on heap allocation, which results in unacceptable
// performance. We want to be sure that this isn't used widely in the field.
void RecordFrontEndHeapDebugOptionsHistogram() {
// Outcome of reading the registry key. These values are persisted to logs.
// Entries should not be renumbered and numeric values should never be reused.
// LINT.IfChange(FrontEndHeapDebugOptionsOutcome)
enum class FrontEndHeapDebugOptionsOutcome {
kCannotOpenKey = 0,
kCannotReadValue = 1,
kSuccess = 2,
kMaxValue = kSuccess,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/performance_manager/enums.xml:FrontEndHeapDebugOptionsOutcome)
std::optional<FrontEndHeapDebugOptionsOutcome> outcome;
base::win::RegKey key;
if (key.Open(HKEY_LOCAL_MACHINE,
L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Image File "
L"Execution Options\\chrome.exe",
KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
DWORD value = 0;
if (key.ReadValueDW(L"FrontEndHeapDebugOptions", &value) == ERROR_SUCCESS) {
base::UmaHistogramSparse(
"PerformanceManager.RegistryStats.FrontEndHeapDebugOptionsValue",
// Limit the number of distinct values recorded to this histogram, as
// recommended by `base::UmaHistogramSparse()` documentation. The
// highest bit observed being set in practice is 0x10 (for stack
// collection on heap allocation). We set the maximum a little bit
// above that, to be aware if higher bits are used in the field.
std::clamp(base::saturated_cast<int>(value), 0, 0xff));
outcome = FrontEndHeapDebugOptionsOutcome::kSuccess;
} else {
outcome = FrontEndHeapDebugOptionsOutcome::kCannotReadValue;
}
} else {
outcome = FrontEndHeapDebugOptionsOutcome::kCannotOpenKey;
}
CHECK(outcome.has_value());
base::UmaHistogramEnumeration(
"PerformanceManager.RegistryStats.FrontEndHeapDebugOptionsOutcome",
outcome.value());
}
#endif // BUILDFLAG(IS_WIN)
} // namespace
// Tracks the proportion of time a specific mode was enabled during this
// object's entire lifetime, and records it to a specified histogram on
// destruction.
class ScopedTimeInModeTracker {
public:
ScopedTimeInModeTracker(bool enabled, const std::string& histogram_name)
: currently_enabled_(enabled),
current_interval_start_(base::LiveTicks::Now()),
start_(current_interval_start_),
histogram_name_(histogram_name) {}
~ScopedTimeInModeTracker() {
// Ensure `time_spent_enabled_` is updated if the mode was currently
// enabled. This doesn't call `ModeChanged` directly to ensure the value of
// `now` used for the total time computation is the same as was used to
// close the interval.
base::LiveTicks now = base::LiveTicks::Now();
CHECK(current_interval_start_ <= now);
CHECK(start_ <= now);
if (currently_enabled_) {
time_spent_enabled_ += now - current_interval_start_;
}
base::TimeDelta total_time = now - start_;
// Time spent enabled should be lower or equal to the total time this was
// active.
CHECK_LE(time_spent_enabled_, total_time);
// Check that the `time_spent_enabled_ * 100` operation can't overflow.
CHECK_LE(time_spent_enabled_.InMicroseconds(),
std::numeric_limits<int64_t>::max() / 100);
// `total_time` being 0 would mean the object was constructed and destructed
// without the clock advancing a single microsecond. This shouldn't happen
// in production but can happen in tests that use mock time. Treat this as
// an interval that has only been in the current state.
unsigned int percent_enabled = currently_enabled_ ? 100 : 0;
if (total_time.is_positive()) {
// Do the computation in microseconds to avoid prior truncation since it's
// `TimeDelta`'s internal representation.
int64_t checked_percent =
(base::CheckMul(time_spent_enabled_.InMicroseconds(), 100) /
total_time.InMicroseconds())
.ValueOrDie();
CHECK(base::IsValueInRangeForNumericType<unsigned int>(checked_percent));
percent_enabled = checked_percent;
}
CHECK_LE(percent_enabled, 100U);
base::UmaHistogramPercentage(histogram_name_, percent_enabled);
}
void ModeChanged(bool enabled) {
if (currently_enabled_ == enabled) {
// It's possible for the pref to be notified as "changed" even if it's
// "changing" to the same state it's already in when going to/from
// "enabled with heuristic mode" to/from "enabled on timer mode".
return;
}
base::LiveTicks now = base::LiveTicks::Now();
CHECK(current_interval_start_ <= now);
if (currently_enabled_) {
time_spent_enabled_ += now - current_interval_start_;
}
currently_enabled_ = enabled;
current_interval_start_ = now;
}
private:
bool currently_enabled_;
base::TimeDelta time_spent_enabled_;
base::LiveTicks current_interval_start_;
base::LiveTicks start_;
std::string histogram_name_;
};
// static
MetricsProviderDesktop* MetricsProviderDesktop::GetInstance() {
DCHECK(g_metrics_provider);
return g_metrics_provider;
}
MetricsProviderDesktop::~MetricsProviderDesktop() {
DCHECK_EQ(this, g_metrics_provider);
g_metrics_provider = nullptr;
}
void MetricsProviderDesktop::Initialize() {
DCHECK(!initialized_);
pref_change_registrar_.Init(local_state_);
pref_change_registrar_.Add(
kMemorySaverModeState,
base::BindRepeating(&MetricsProviderDesktop::OnMemorySaverPrefChanged,
base::Unretained(this)));
performance_manager::user_tuning::BatterySaverModeManager::GetInstance()
->AddObserver(this);
battery_saver_enabled_ =
performance_manager::user_tuning::BatterySaverModeManager::GetInstance()
->IsBatterySaverActive();
initialized_ = true;
current_mode_ = ComputeCurrentMode();
ResetTrackers();
PostDiskMetricsTask();
}
void MetricsProviderDesktop::ProvideCurrentSessionData(
metrics::ChromeUserMetricsExtension* uma_proto) {
// It's valid for this to be called when `initialized_` is false if the finch
// features controlling battery saver and memory saver are disabled.
// TODO(crbug.com/40233418): CHECK(initialized_) when the features are enabled
// and removed.
base::UmaHistogramEnumeration("PerformanceManager.UserTuning.EfficiencyMode",
current_mode_);
// Resetting the trackers will cause the existing ones to record their
// histogram.
ResetTrackers();
// Set `current_mode_` to represent the state of the modes as they are now, so
// that this mode is what is adequately reported at the next report, unless it
// changes in the meantime.
current_mode_ = ComputeCurrentMode();
RecordDiskMetrics();
#if BUILDFLAG(IS_WIN)
RecordFrontEndHeapDebugOptionsHistogram();
#endif // BUILDFLAG(IS_WIN)
// Request a disk measurement so it's ready for the next interval
PostDiskMetricsTask();
}
MetricsProviderDesktop::MetricsProviderDesktop(PrefService* local_state)
: local_state_(local_state),
disk_metrics_getter_(
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})) {
DCHECK(!g_metrics_provider);
g_metrics_provider = this;
#if SHOULD_COLLECT_CPU_FREQUENCY_METRICS()
ScheduleCpuFrequencyTask();
#endif // SHOULD_COLLECT_CPU_FREQUENCY_METRICS()
}
void MetricsProviderDesktop::OnBatterySaverActiveChanged(bool is_active) {
battery_saver_enabled_ = is_active;
battery_saver_mode_tracker_->ModeChanged(battery_saver_enabled_);
OnTuningModesChanged();
}
void MetricsProviderDesktop::OnMemorySaverPrefChanged() {
memory_saver_mode_tracker_->ModeChanged(IsMemorySaverEnabled());
OnTuningModesChanged();
}
void MetricsProviderDesktop::OnTuningModesChanged() {
EfficiencyMode new_mode = ComputeCurrentMode();
// If the mode changes between UMA reports, mark it as Mixed for this
// interval.
if (current_mode_ != new_mode) {
current_mode_ = EfficiencyMode::kMixed;
}
}
MetricsProviderDesktop::EfficiencyMode
MetricsProviderDesktop::ComputeCurrentMode() const {
// It's valid for this to be uninitialized if the battery saver/high
// efficiency modes are unavailable. In that case, the browser is running in
// normal mode, so return kNormal.
// TODO(crbug.com/40233418): Change this to a DCHECK when the features are
// enabled and removed.
if (!initialized_) {
return EfficiencyMode::kNormal;
}
// It's possible for this function to be called during shutdown, after
// BatterySaverModeManager is destroyed. Do not access UPTM directly from
// here.
bool high_efficiency_enabled = IsMemorySaverEnabled();
if (high_efficiency_enabled && battery_saver_enabled_) {
return EfficiencyMode::kBoth;
}
if (high_efficiency_enabled) {
return EfficiencyMode::kMemorySaver;
}
if (battery_saver_enabled_) {
return EfficiencyMode::kBatterySaver;
}
return EfficiencyMode::kNormal;
}
bool MetricsProviderDesktop::IsMemorySaverEnabled() const {
return local_state_->GetInteger(kMemorySaverModeState) !=
static_cast<int>(MemorySaverModeState::kDisabled);
}
void MetricsProviderDesktop::ResetTrackers() {
battery_saver_mode_tracker_ = std::make_unique<ScopedTimeInModeTracker>(
battery_saver_enabled_,
"PerformanceManager.UserTuning.BatterySaverModeEnabledPercent");
memory_saver_mode_tracker_ = std::make_unique<ScopedTimeInModeTracker>(
IsMemorySaverEnabled(),
"PerformanceManager.UserTuning.MemorySaverModeEnabledPercent");
}
#if SHOULD_COLLECT_CPU_FREQUENCY_METRICS()
// static
void MetricsProviderDesktop::RecordCpuFrequencyMetrics(
base::TimeTicks posted_at_time) {
auto started_running_time = base::TimeTicks::Now();
auto queued_time = started_running_time - posted_at_time;
static const double kHzInMhz = 1000 * 1000;
std::optional<base::CpuThroughputEstimationResult> cpu_throughput =
base::EstimateCpuThroughput();
base::CpuFrequencyInfo cpu_frequency_info = base::GetCpuFrequencyInfo();
if (!cpu_throughput) {
return;
}
std::string_view core_type_suffix = "Performance";
if (cpu_frequency_info.type == base::CpuFrequencyInfo::CoreType::kBalanced) {
core_type_suffix = "Balanced";
} else if (cpu_frequency_info.type ==
base::CpuFrequencyInfo::CoreType::kEfficiency) {
core_type_suffix = "Efficiency";
}
base::UmaHistogramCustomMicrosecondsTimes(
base::StrCat(
{"CPU.Experimental.CpuEstimationTaskQueuedTime.", core_type_suffix}),
queued_time, base::Microseconds(1), base::Seconds(1), 50);
base::UmaHistogramCustomMicrosecondsTimes(
base::StrCat(
{"CPU.Experimental.CpuEstimationTaskTotalTime.", core_type_suffix}),
queued_time + cpu_throughput->wall_time, base::Microseconds(1),
base::Seconds(1), 50);
base::UmaHistogramCustomMicrosecondsTimes(
base::StrCat(
{"CPU.Experimental.CpuEstimationTaskThreadTime.", core_type_suffix}),
cpu_throughput->thread_time, base::Microseconds(1), base::Seconds(1), 50);
base::UmaHistogramCustomMicrosecondsTimes(
base::StrCat(
{"CPU.Experimental.CpuEstimationTaskWallTime.", core_type_suffix}),
cpu_throughput->wall_time, base::Microseconds(1), base::Seconds(1), 50);
base::UmaHistogramBoolean("CPU.Experimental.CpuEstimationTaskMigrated",
cpu_throughput->migrated);
std::optional<double> cpu_frequency_percent = std::nullopt;
if (!cpu_throughput->migrated) {
// Don't record frequency metrics if the code migrated from one CPU to
// another in the middle of the estimation loop. This is because the nominal
// frequency of the start and end cores might be different.
double estimated_mhz = cpu_throughput->estimated_frequency / kHzInMhz;
// Max/Limit can (rarely) be 0 in the field, perhaps in virtualized or
// sandboxed environments.
if (cpu_frequency_info.max_mhz > 0UL) {
cpu_frequency_percent = estimated_mhz * 100.0 /
static_cast<double>(cpu_frequency_info.max_mhz);
base::UmaHistogramPercentage(
base::StrCat({"CPU.Experimental.EstimatedFrequencyAsPercentOfMax.",
core_type_suffix}),
static_cast<int>(*cpu_frequency_percent));
}
if (cpu_frequency_info.mhz_limit > 0UL) {
base::UmaHistogramPercentage(
base::StrCat({"CPU.Experimental.EstimatedFrequencyAsPercentOfLimit.",
core_type_suffix}),
static_cast<int>(estimated_mhz * 100.0 /
static_cast<double>(cpu_frequency_info.mhz_limit)));
}
}
// These can be 0 in tests
if (!cpu_throughput->thread_time.is_zero() &&
!cpu_throughput->wall_time.is_zero()) {
std::optional<double> thread_time_percent =
cpu_throughput->thread_time / cpu_throughput->wall_time * 100.0;
base::UmaHistogramPercentage(
base::StrCat({"CPU.Experimental.CpuEstimationThreadTimePercent.",
core_type_suffix}),
static_cast<int>(*thread_time_percent));
CpuThroughputEstimatedStatus status = EstimateCpuThroughputStatus(
cpu_throughput->migrated, cpu_frequency_percent, thread_time_percent,
queued_time);
EmitCpuStatusSamplingTraceEvents(posted_at_time, started_running_time,
cpu_throughput->thread_time,
cpu_throughput->wall_time, status);
}
ScheduleCpuFrequencyTask();
}
// static
void MetricsProviderDesktop::ScheduleCpuFrequencyTask() {
static constexpr base::TimeDelta kCpuThroughputSamplingInterval =
base::Minutes(5);
base::ThreadPool::PostDelayedTask(
FROM_HERE,
{base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&MetricsProviderDesktop::PostCpuFrequencyEstimation),
kCpuThroughputSamplingInterval);
}
// static
void MetricsProviderDesktop::PostCpuFrequencyEstimation() {
base::ThreadPool::PostTask(
FROM_HERE,
{base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&MetricsProviderDesktop::RecordCpuFrequencyMetrics,
base::TimeTicks::Now()));
}
#endif // SHOULD_COLLECT_CPU_FREQUENCY_METRICS()
void MetricsProviderDesktop::RecordDiskMetrics() {
if (!pending_disk_metrics_) {
// The measurements aren't ready yet, don't report anything.
return;
}
if (pending_disk_metrics_->free_bytes == -1 ||
pending_disk_metrics_->total_bytes == -1) {
return;
}
base::UmaHistogramCustomCounts(
"PerformanceManager.DiskStats.UserDataDirFreeSpaceMb",
pending_disk_metrics_->free_bytes /
kBytesPerMb, // space_info is bytes, convert to Mb
0, 10240, // It's fine to bucket everything >10Gb as "large enough"
100);
// Also report as a percentage of capacity
base::UmaHistogramPercentage(
"PerformanceManager.DiskStats.UserDataDirFreeSpacePercent",
pending_disk_metrics_->free_bytes * 100 /
pending_disk_metrics_->total_bytes);
pending_disk_metrics_ = std::nullopt;
}
void MetricsProviderDesktop::PostDiskMetricsTask() {
if (!g_browser_process || !g_browser_process->profile_manager()) {
// It's possible to have a null browser process or a null profile manager in
// unit tests.
return;
}
// Records the free/available space on the disk that hosts the user data dir.
ProfileManager* profile_manager = g_browser_process->profile_manager();
const base::FilePath& user_data_dir = profile_manager->user_data_dir();
disk_metrics_getter_
.AsyncCall(&MetricsProviderDesktop::DiskMetricsThreadPoolGetter::
ComputeDiskMetrics)
.WithArgs(user_data_dir)
.Then(base::BindOnce(&MetricsProviderDesktop::SavePendingDiskMetrics,
base::Unretained(this)));
}
MetricsProviderDesktop::DiskMetrics
MetricsProviderDesktop::DiskMetricsThreadPoolGetter::ComputeDiskMetrics(
const base::FilePath& user_data_dir) {
return {
.free_bytes = base::SysInfo::AmountOfFreeDiskSpace(user_data_dir),
.total_bytes = base::SysInfo::AmountOfTotalDiskSpace(user_data_dir),
};
}
void MetricsProviderDesktop::SavePendingDiskMetrics(DiskMetrics metrics) {
pending_disk_metrics_ = metrics;
}
} // namespace performance_manager
|