Skip to content

Commit

Permalink
Add CPU reduction experiment.
Browse files Browse the repository at this point in the history
This CL has two parts:

Part 1: This CL adds an experiment to base called
IsRunningCpuReductionExperiment. When enabled, several different
unrelated features will change their behavior to reduce overall CPU
consumption of chrome. As these features are unrelated, this experiment
must live in base as the only common dependency.

Part 2: This CL modifies 6 functions that emit UMA metrics to emit at
1/1000th of their current rate. Combined, the UMA metric emission from
these 6 functions are responsible for 0.33% of all CPU cycles on
ChromeOS, including time from non-chrome processes. This demonstrates
why the code in part 1 must live in base.

Future CLs will add more CPU-savings to the experiment before it is
enabled.

Bug: 1295441
Change-Id: I8202c5a1482ff66fc90a7bc64bb36b3cdc773130
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3449251
Reviewed-by: Ken Rockot <rockot@google.com>
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Francois Pierre Doray <fdoray@chromium.org>
Reviewed-by: Sadrul Chowdhury <sadrul@chromium.org>
Commit-Queue: Erik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/main@{#969512}
  • Loading branch information
erikchen authored and Chromium LUCI CQ committed Feb 10, 2022
1 parent 612e285 commit 0fcb876
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 25 deletions.
2 changes: 2 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ mixed_component("base") {
"containers/vector_buffer.h",
"cpu.cc",
"cpu.h",
"cpu_reduction_experiment.cc",
"cpu_reduction_experiment.h",
"critical_closure.h",
"cxx17_backports.h",
"cxx20_to_address.h",
Expand Down
40 changes: 40 additions & 0 deletions base/cpu_reduction_experiment.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/cpu_reduction_experiment.h"

#include "base/feature_list.h"

namespace base {

namespace {

// This feature controls whether to enable a series of optimizations that
// reduces total CPU utilization of chrome.
constexpr Feature kReduceCpuUtilization{"ReduceCpuUtilization",
FEATURE_DISABLED_BY_DEFAULT};

// Cache of the state of the ReduceCpuUtilization feature. This avoids the need
// to constantly query its enabled state through FeatureList::IsEnabled().
bool g_is_reduce_cpu_enabled =
kReduceCpuUtilization.default_state == FEATURE_ENABLED_BY_DEFAULT;

} // namespace

bool IsRunningCpuReductionExperiment() {
return g_is_reduce_cpu_enabled;
}

void InitializeCpuReductionExperiment() {
g_is_reduce_cpu_enabled = FeatureList::IsEnabled(kReduceCpuUtilization);
}

bool CpuReductionExperimentFilter::ShouldLogHistograms() {
if (!IsRunningCpuReductionExperiment())
return true;

return (++counter_ % 1000) == 1;
}

} // namespace base
35 changes: 35 additions & 0 deletions base/cpu_reduction_experiment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_CPU_REDUCTION_EXPERIMENT_H_
#define BASE_CPU_REDUCTION_EXPERIMENT_H_

#include "base/base_export.h"

namespace base {

// Returns whether the cpu cycle reduction experiment is running.
// The goal of this experiment is to better understand the relationship between
// total CPU cycles used across the fleet and top-line chrome metrics.
BASE_EXPORT bool IsRunningCpuReductionExperiment();

// Must be called after FeatureList initialization and while chrome is still
// single-threaded.
BASE_EXPORT void InitializeCpuReductionExperiment();

// This is a helper class to reduce common duplicate code. If the CPU reduction
// experiment is running, then ShouldLogHistograms returns true on every 1000th
// call. Otherwise it always returns true.
class BASE_EXPORT CpuReductionExperimentFilter {
public:
// Returns true on the first call, and every 1000th call after that.
bool ShouldLogHistograms();

private:
int counter_ = 0;
};

} // namespace base

#endif // BASE_CPU_REDUCTION_EXPERIMENT_H_
4 changes: 4 additions & 0 deletions cc/metrics/compositor_frame_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <utility>

#include "base/cpu_reduction_experiment.h"
#include "base/cxx17_backports.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/strcat.h"
Expand Down Expand Up @@ -819,6 +820,9 @@ void CompositorFrameReporter::EndCurrentStage(base::TimeTicks end_time) {
}

void CompositorFrameReporter::ReportCompositorLatencyHistograms() const {
static base::CpuReductionExperimentFilter filter;
if (!filter.ShouldLogHistograms())
return;
for (const StageData& stage : stage_history_) {
ReportStageHistogramWithBreakdown(stage);
for (size_t type = 0; type < active_trackers_.size(); ++type) {
Expand Down
2 changes: 2 additions & 0 deletions chrome/app/chrome_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/cpu.h"
#include "base/cpu_reduction_experiment.h"
#include "base/cxx17_backports.h"
#include "base/dcheck_is_on.h"
#include "base/files/file_path.h"
Expand Down Expand Up @@ -732,6 +733,7 @@ void ChromeMainDelegate::PostFieldTrialInitialization() {
base::HangWatcher::InitializeOnMainThread(hang_watcher_process_type);

base::internal::TimerBase::InitializeFeatures();
base::InitializeCpuReductionExperiment();
base::sequence_manager::internal::SequenceManagerImpl::InitializeFeatures();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "components/scheduling_metrics/total_duration_metric_reporter.h"

#include "base/cpu_reduction_experiment.h"

namespace scheduling_metrics {

namespace {
Expand Down Expand Up @@ -34,6 +36,9 @@ TotalDurationMetricReporter::~TotalDurationMetricReporter() = default;

void TotalDurationMetricReporter::RecordAdditionalDuration(
base::TimeDelta duration) {
static base::CpuReductionExperimentFilter filter;
if (!filter.ShouldLogHistograms())
return;
if (reported_value_)
negative_histogram_->Add(reported_value_->InSeconds());
reported_value_ = reported_value_.value_or(base::TimeDelta()) + duration;
Expand Down
39 changes: 24 additions & 15 deletions gpu/command_buffer/service/scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/bind.h"
#include "base/callback.h"
#include "base/cpu_reduction_experiment.h"
#include "base/hash/md5_constexpr.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
Expand Down Expand Up @@ -612,11 +613,15 @@ Scheduler::RebuildSchedulingQueueIfNeeded(
}

void Scheduler::RunNextTask() {
static base::CpuReductionExperimentFilter filter;
bool log_histograms = filter.ShouldLogHistograms();
base::AutoLock auto_lock(lock_);
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.ThreadSuspendedTime",
base::TimeTicks::Now() - run_next_task_scheduled_, base::Microseconds(10),
base::Seconds(30), 100);
if (log_histograms) {
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.ThreadSuspendedTime",
base::TimeTicks::Now() - run_next_task_scheduled_,
base::Microseconds(10), base::Seconds(30), 100);
}
auto* task_runner = base::ThreadTaskRunnerHandle::Get().get();

SchedulingState state;
Expand All @@ -641,15 +646,17 @@ void Scheduler::RunNextTask() {
DCHECK(sequence);
DCHECK_EQ(sequence->task_runner(), task_runner);

UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.TaskDependencyTime",
sequence->FrontTaskWaitingDependencyDelta(), base::Microseconds(10),
base::Seconds(30), 100);
if (log_histograms) {
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.TaskDependencyTime",
sequence->FrontTaskWaitingDependencyDelta(), base::Microseconds(10),
base::Seconds(30), 100);

UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.TaskSchedulingDelayTime",
sequence->FrontTaskSchedulingDelay(), base::Microseconds(10),
base::Seconds(30), 100);
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.TaskSchedulingDelayTime",
sequence->FrontTaskSchedulingDelay(), base::Microseconds(10),
base::Seconds(30), 100);
}

base::OnceClosure closure;
uint32_t order_num = sequence->BeginTask(&closure);
Expand Down Expand Up @@ -703,9 +710,11 @@ void Scheduler::RunNextTask() {
}
}

UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.RunTaskTime", task_timer.Elapsed(), base::Microseconds(10),
base::Seconds(30), 100);
if (log_histograms) {
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"GPU.Scheduler.RunTaskTime", task_timer.Elapsed(),
base::Microseconds(10), base::Seconds(30), 100);
}

// Avoid scheduling another RunNextTask if we're done with all tasks.
auto& scheduling_queue = RebuildSchedulingQueueIfNeeded(task_runner);
Expand Down
16 changes: 12 additions & 4 deletions mojo/core/channel_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "base/bind.h"
#include "base/containers/queue.h"
#include "base/cpu_reduction_experiment.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
Expand Down Expand Up @@ -153,10 +154,17 @@ void ChannelPosix::ShutDownImpl() {
}

void ChannelPosix::Write(MessagePtr message) {
UMA_HISTOGRAM_COUNTS_100000("Mojo.Channel.WriteMessageSize",
message->data_num_bytes());
UMA_HISTOGRAM_COUNTS_100("Mojo.Channel.WriteMessageHandles",
message->NumHandlesForTransit());
bool log_histograms = true;
#if !defined(MOJO_CORE_SHARED_LIBRARY)
static base::CpuReductionExperimentFilter filter;
log_histograms = filter.ShouldLogHistograms();
#endif
if (log_histograms) {
UMA_HISTOGRAM_COUNTS_100000("Mojo.Channel.WriteMessageSize",
message->data_num_bytes());
UMA_HISTOGRAM_COUNTS_100("Mojo.Channel.WriteMessageHandles",
message->NumHandlesForTransit());
}

bool write_error = false;
{
Expand Down
3 changes: 3 additions & 0 deletions third_party/blink/renderer/core/frame/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ specific_include_rules = {
"local_frame\.cc": [
"+ui/gfx/transform.h"
],
"local_frame_ukm_aggregator\.cc": [
"+base/cpu_reduction_experiment.h",
],
"local_frame_ukm_aggregator_test\.cc": [
"+base/metrics/statistics_recorder.h"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>

#include "base/cpu_reduction_experiment.h"
#include "base/format_macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/rand_util.h"
Expand Down Expand Up @@ -253,6 +254,10 @@ void LocalFrameUkmAggregator::RecordTimerSample(size_t metric_index,

void LocalFrameUkmAggregator::RecordCountSample(size_t metric_index,
int64_t count) {
static base::CpuReductionExperimentFilter filter;
if (!filter.ShouldLogHistograms())
return;

// Always use RecordForcedLayoutSample for the kForcedStyleAndLayout
// metric id.
DCHECK_NE(metric_index, static_cast<size_t>(kForcedStyleAndLayout));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'base::ApplyMetadataToPastSamples',
'base::AutoReset',
'base::Contains',
'base::CpuReductionExperimentFilter',
'base::CreateSequencedTaskRunner',
'base::ValuesEquivalent',
'base::Days',
Expand Down
16 changes: 10 additions & 6 deletions url/url_canon_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unordered_set>

#include "base/check.h"
#include "base/cpu_reduction_experiment.h"
#include "base/metrics/histogram_macros.h"
#include "url/url_canon.h"
#include "url/url_canon_internal.h"
Expand Down Expand Up @@ -177,12 +178,15 @@ bool DoSimpleHost(const INCHAR* host,
}
}
if (success) {
bool did_escape = !escaped_chars_to_measure.empty();
UMA_HISTOGRAM_BOOLEAN("URL.Host.DidEscape", did_escape);
if (did_escape) {
for (char c : escaped_chars_to_measure) {
UMA_HISTOGRAM_ENUMERATION("URL.Host.EscapeChar",
EscapedHostCharToEnum(c));
static base::CpuReductionExperimentFilter filter;
if (filter.ShouldLogHistograms()) {
bool did_escape = !escaped_chars_to_measure.empty();
UMA_HISTOGRAM_BOOLEAN("URL.Host.DidEscape", did_escape);
if (did_escape) {
for (char c : escaped_chars_to_measure) {
UMA_HISTOGRAM_ENUMERATION("URL.Host.EscapeChar",
EscapedHostCharToEnum(c));
}
}
}
}
Expand Down

0 comments on commit 0fcb876

Please sign in to comment.