1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/cpu_reduction_experiment.h"
9 #include "base/check.h"
10 #include "base/dcheck_is_on.h"
11 #include "base/feature_list.h"
12 #include "base/rand_util.h"
13 #include "base/synchronization/lock.h"
14 #include "base/thread_annotations.h"
20 // Whether to enable a series of optimizations that reduce total CPU
22 BASE_FEATURE(kReduceCpuUtilization,
23 "ReduceCpuUtilization2",
24 FEATURE_ENABLED_BY_DEFAULT);
26 class CpuReductionExperimentSubSampler {
28 CpuReductionExperimentSubSampler() = default;
30 bool ShouldLogHistograms() {
32 return sub_sampler_.ShouldSample(0.001);
37 MetricsSubSampler sub_sampler_ GUARDED_BY(lock_);
40 // Singleton instance of CpuReductionExperimentSubSampler. This is only set when
41 // the ReduceCpuUtilization experiment is enabled -- as a result, it's ok to
42 // assume that the experiment is disabled when this is not set.
43 CpuReductionExperimentSubSampler* g_subsampler = nullptr;
46 // Atomic to support concurrent writes from IsRunningCpuReductionExperiment().
47 std::atomic_bool g_accessed_subsampler = false;
52 bool IsRunningCpuReductionExperiment() {
54 g_accessed_subsampler.store(true, std::memory_order_seq_cst);
56 return !!g_subsampler;
59 void InitializeCpuReductionExperiment() {
61 // TSAN should generate an error if InitializeCpuReductionExperiment() races
62 // with IsRunningCpuReductionExperiment().
63 DCHECK(!g_accessed_subsampler.load(std::memory_order_seq_cst));
65 if (FeatureList::IsEnabled(kReduceCpuUtilization)) {
66 g_subsampler = new CpuReductionExperimentSubSampler();
70 bool ShouldLogHistogramForCpuReductionExperiment() {
71 if (!IsRunningCpuReductionExperiment())
73 return g_subsampler->ShouldLogHistograms();