Skip to content

Commit 1453eab

Browse files
Jie Jiangchromeos-ci-prod
authored andcommitted
Revert "Clean up UsePowerMonitorWithThreadController"
This reverts commit f66896faa0eae6b4b3fa65eb4fe70bc8a7b76e47. Reason for revert: b/374173161. This breaks the suspend behavior on CrOS. Original change's description: > Clean up UsePowerMonitorWithThreadController > > The feature has been enabled by default for a long time, removing it > should be a no-op. > > Bug: 356234726 > Change-Id: I85739a627364102ec97b00c99fc46616e97825e1 > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5832196 > Reviewed-by: Maks Orlovich <[email protected]> > Reviewed-by: Francois Pierre Doray <[email protected]> > Commit-Queue: Anthony Vallée-Dubois <[email protected]> > Cr-Commit-Position: refs/heads/main@{#1359479} Bug: 356234726, 374173161 Change-Id: Icd591c4e6dd4881ce93e48254b28a4cd6bc869f1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5960956 Commit-Queue: Jie Jiang <[email protected]> Reviewed-by: Maks Orlovich <[email protected]> Reviewed-by: Francois Pierre Doray <[email protected]> Cr-Commit-Position: refs/heads/main@{#1373928} CrOS-Libchrome-Original-Commit: 353ef9d1248cbd62fdd7280d89750ac7d65f68bc
1 parent 162d0ec commit 1453eab

8 files changed

+54
-25
lines changed

base/features.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#endif
2626

2727
#if BUILDFLAG(IS_WIN)
28+
#include "base/task/sequence_manager/thread_controller_power_monitor.h"
2829
#include "base/threading/platform_thread_win.h"
2930
#endif
3031

@@ -147,6 +148,8 @@ void Init(EmitThreadControllerProfilerMetadata
147148
#endif
148149

149150
#if BUILDFLAG(IS_WIN)
151+
sequence_manager::internal::ThreadControllerPowerMonitor::
152+
InitializeFeatures();
150153
InitializePlatformThreadFeatures();
151154
#endif
152155
}

base/task/sequence_manager/thread_controller_power_monitor.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ namespace base {
1212
namespace sequence_manager {
1313
namespace internal {
1414

15+
namespace {
16+
17+
// Activate the power management events that affect task scheduling.
18+
BASE_FEATURE(kUsePowerMonitorWithThreadController,
19+
"UsePowerMonitorWithThreadController",
20+
FEATURE_ENABLED_BY_DEFAULT);
21+
22+
// TODO(crbug.com/40127966): Remove this when the experiment becomes the
23+
// default.
24+
bool g_use_thread_controller_power_monitor_ = false;
25+
26+
} // namespace
27+
1528
ThreadControllerPowerMonitor::ThreadControllerPowerMonitor() = default;
1629

1730
ThreadControllerPowerMonitor::~ThreadControllerPowerMonitor() {
@@ -35,7 +48,27 @@ bool ThreadControllerPowerMonitor::IsProcessInPowerSuspendState() {
3548
return is_power_suspended_;
3649
}
3750

51+
// static
52+
void ThreadControllerPowerMonitor::InitializeFeatures() {
53+
DCHECK(!g_use_thread_controller_power_monitor_);
54+
g_use_thread_controller_power_monitor_ =
55+
FeatureList::IsEnabled(kUsePowerMonitorWithThreadController);
56+
}
57+
58+
// static
59+
void ThreadControllerPowerMonitor::OverrideUsePowerMonitorForTesting(
60+
bool use_power_monitor) {
61+
g_use_thread_controller_power_monitor_ = use_power_monitor;
62+
}
63+
64+
// static
65+
void ThreadControllerPowerMonitor::ResetForTesting() {
66+
g_use_thread_controller_power_monitor_ = false;
67+
}
68+
3869
void ThreadControllerPowerMonitor::OnSuspend() {
70+
if (!g_use_thread_controller_power_monitor_)
71+
return;
3972
DCHECK(!is_power_suspended_);
4073

4174
TRACE_EVENT_BEGIN("base", "ThreadController::Suspended",
@@ -45,6 +78,9 @@ void ThreadControllerPowerMonitor::OnSuspend() {
4578
}
4679

4780
void ThreadControllerPowerMonitor::OnResume() {
81+
if (!g_use_thread_controller_power_monitor_)
82+
return;
83+
4884
// It is possible a suspend was already happening before the observer was
4985
// added to the power monitor. Ignoring the resume notification in that case.
5086
if (is_power_suspended_) {

base/task/sequence_manager/thread_controller_power_monitor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class BASE_EXPORT ThreadControllerPowerMonitor : public PowerSuspendObserver {
3131
// notifications.
3232
bool IsProcessInPowerSuspendState();
3333

34+
// Initializes features for this class. See `base::features::Init()`.
35+
static void InitializeFeatures();
36+
37+
static void OverrideUsePowerMonitorForTesting(bool use_power_monitor);
38+
static void ResetForTesting();
39+
3440
// base::PowerSuspendObserver:
3541
void OnSuspend() override;
3642
void OnResume() override;

base/task/sequence_manager/thread_controller_power_monitor_unittest.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ class ThreadControllerPowerMonitorTest : public testing::Test {
2121
void SetUp() override {
2222
thread_controller_power_monitor_ =
2323
std::make_unique<ThreadControllerPowerMonitor>();
24+
internal::ThreadControllerPowerMonitor::OverrideUsePowerMonitorForTesting(
25+
true);
2426
}
2527

2628
void TearDown() override {
2729
thread_controller_power_monitor_.reset();
30+
internal::ThreadControllerPowerMonitor::ResetForTesting();
2831
}
2932

3033
protected:

base/task/sequence_manager/thread_controller_with_message_pump_impl_unittest.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ class ThreadControllerWithMessagePumpTestBase : public testing::Test {
234234
thread_controller_.SetSequencedTaskSource(&task_source_);
235235
}
236236

237+
void SetUp() override {
238+
ThreadControllerPowerMonitor::OverrideUsePowerMonitorForTesting(true);
239+
}
240+
241+
void TearDown() override { ThreadControllerPowerMonitor::ResetForTesting(); }
242+
237243
TimeTicks FromNow(TimeDelta delta) { return clock_.NowTicks() + delta; }
238244

239245
protected:

base/test/power_monitor_test.cc

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,6 @@ ScopedPowerMonitorTestSource::ScopedPowerMonitorTestSource() {
108108
}
109109

110110
ScopedPowerMonitorTestSource::~ScopedPowerMonitorTestSource() {
111-
if (is_suspended_) {
112-
// Generate a resume here because there are global, leaky disk cache
113-
// threads that lives throughout a long portion of some test targets.
114-
// This thread is created the first time the disk cache is created, which
115-
// instantiates a disk cache backend, which in turn creates a CacheThread as
116-
// a global LazyInstance<>::Lazy. This thread gets its own
117-
// ThreadControllerPowerMonitor, so if this test case doesn't simulate a
118-
// resume, the next test will hit a DCHECK when it tries to generate its
119-
// first suspend event. The "correct" solution to this problem would be to
120-
// refactor the offending disk caches to support passing a thread through to
121-
// the disk cache backend in tests, and avoid having a global/leaky instance
122-
// around but this is a significant undertaking.
123-
GenerateResumeEvent();
124-
}
125111
base::PowerMonitor::GetInstance()->ShutdownForTesting();
126112
}
127113

@@ -136,13 +122,11 @@ ScopedPowerMonitorTestSource::GetBatteryPowerStatus() const {
136122
}
137123

138124
void ScopedPowerMonitorTestSource::Suspend() {
139-
is_suspended_ = true;
140125
power_monitor_test_source_->Suspend();
141126
}
142127

143128
void ScopedPowerMonitorTestSource::Resume() {
144129
power_monitor_test_source_->Resume();
145-
is_suspended_ = false;
146130
}
147131

148132
void ScopedPowerMonitorTestSource::SetBatteryPowerStatus(
@@ -151,13 +135,11 @@ void ScopedPowerMonitorTestSource::SetBatteryPowerStatus(
151135
}
152136

153137
void ScopedPowerMonitorTestSource::GenerateSuspendEvent() {
154-
is_suspended_ = true;
155138
power_monitor_test_source_->GenerateSuspendEvent();
156139
}
157140

158141
void ScopedPowerMonitorTestSource::GenerateResumeEvent() {
159142
power_monitor_test_source_->GenerateResumeEvent();
160-
is_suspended_ = false;
161143
}
162144

163145
void ScopedPowerMonitorTestSource::GeneratePowerStateEvent(

base/test/power_monitor_test.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class ScopedPowerMonitorTestSource {
5858
// Owned by PowerMonitor.
5959
raw_ptr<PowerMonitorTestSource, DanglingUntriaged>
6060
power_monitor_test_source_ = nullptr;
61-
bool is_suspended_ = false;
6261
};
6362

6463
class PowerMonitorTestObserver : public PowerSuspendObserver,

base/timer/wall_clock_timer_unittest.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,6 @@ TEST_F(WallClockTimerTest, NonStopTickClockWithLongPause) {
249249
task_environment_.FastForwardBy(past_time);
250250
fake_power_monitor_source_.Resume();
251251

252-
// The WallClockTimer restarts its task with a shorter timeout when the system
253-
// resumes, so it needs to be taken out of the queue and executed. In this
254-
// case, the time is already elapsed, so fast forwarding by 0 to pump the task
255-
// without advancing time.
256-
task_environment_.FastForwardBy(base::TimeDelta());
257-
258252
::testing::Mock::VerifyAndClearExpectations(&callback);
259253
EXPECT_FALSE(wall_clock_timer.IsRunning());
260254
}

0 commit comments

Comments
 (0)