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
base / power_monitor / power_monitor.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/power_monitor/power_monitor.h"
#include <utility>
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/power_monitor/power_monitor_source.h"
#include "base/trace_event/base_tracing.h"
#include "build/build_config.h"
#include "power_observer.h"
namespace base {
void PowerMonitor::Initialize(std::unique_ptr<PowerMonitorSource> source) {
DCHECK(!IsInitialized());
source_ = std::move(source);
// When a power source is associated with the power monitor, ensure the
// initial state is propagated to observers, if needed.
NotifyPowerStateChange(Source()->GetBatteryPowerStatus());
NotifyThermalStateChange(Source()->GetCurrentThermalState());
NotifySpeedLimitChange(Source()->GetInitialSpeedLimit());
}
bool PowerMonitor::IsInitialized() const {
return source_ != nullptr;
}
void PowerMonitor::AddPowerSuspendObserver(PowerSuspendObserver* obs) {
power_suspend_observers_->AddObserver(obs);
}
void PowerMonitor::RemovePowerSuspendObserver(PowerSuspendObserver* obs) {
power_suspend_observers_->RemoveObserver(obs);
}
void PowerMonitor::AddPowerStateObserver(PowerStateObserver* obs) {
power_state_observers_->AddObserver(obs);
}
void PowerMonitor::RemovePowerStateObserver(PowerStateObserver* obs) {
power_state_observers_->RemoveObserver(obs);
}
void PowerMonitor::AddPowerThermalObserver(PowerThermalObserver* obs) {
thermal_state_observers_->AddObserver(obs);
}
void PowerMonitor::RemovePowerThermalObserver(PowerThermalObserver* obs) {
thermal_state_observers_->RemoveObserver(obs);
}
bool PowerMonitor::AddPowerSuspendObserverAndReturnSuspendedState(
PowerSuspendObserver* obs) {
AutoLock auto_lock(is_system_suspended_lock_);
power_suspend_observers_->AddObserver(obs);
return is_system_suspended_;
}
// static
bool PowerMonitor::AddPowerStateObserverAndReturnOnBatteryState(
PowerStateObserver* obs) {
return AddPowerStateObserverAndReturnBatteryPowerStatus(obs) ==
PowerStateObserver::BatteryPowerStatus::kBatteryPower;
}
PowerStateObserver::BatteryPowerStatus
PowerMonitor::AddPowerStateObserverAndReturnBatteryPowerStatus(
PowerStateObserver* obs) {
AutoLock auto_lock(battery_power_status_lock_);
power_state_observers_->AddObserver(obs);
return battery_power_status_;
}
// static
PowerThermalObserver::DeviceThermalState
PowerMonitor::AddPowerStateObserverAndReturnPowerThermalState(
PowerThermalObserver* obs) {
AutoLock auto_lock(power_thermal_state_lock_);
thermal_state_observers_->AddObserver(obs);
return power_thermal_state_;
}
const PowerMonitorSource* PowerMonitor::Source() const {
return source_.get();
}
bool PowerMonitor::IsOnBatteryPower() const {
DCHECK(IsInitialized());
return GetBatteryPowerStatus() ==
PowerStateObserver::BatteryPowerStatus::kBatteryPower;
}
PowerStateObserver::BatteryPowerStatus PowerMonitor::GetBatteryPowerStatus()
const {
DCHECK(IsInitialized());
AutoLock auto_lock(battery_power_status_lock_);
return battery_power_status_;
}
TimeTicks PowerMonitor::GetLastSystemResumeTime() const {
AutoLock auto_lock(is_system_suspended_lock_);
return last_system_resume_time_;
}
void PowerMonitor::ShutdownForTesting() {
source_ = nullptr;
{
AutoLock auto_lock(is_system_suspended_lock_);
is_system_suspended_ = false;
last_system_resume_time_ = TimeTicks();
}
{
AutoLock auto_lock(battery_power_status_lock_);
battery_power_status_ = PowerStateObserver::BatteryPowerStatus::kUnknown;
}
{
AutoLock auto_lock(power_thermal_state_lock_);
power_thermal_state_ = PowerThermalObserver::DeviceThermalState::kUnknown;
}
}
// static
PowerThermalObserver::DeviceThermalState PowerMonitor::GetCurrentThermalState()
const {
DCHECK(IsInitialized());
return source_->GetCurrentThermalState();
}
// static
void PowerMonitor::SetCurrentThermalState(
PowerThermalObserver::DeviceThermalState state) {
DCHECK(IsInitialized());
source_->SetCurrentThermalState(state);
}
#if BUILDFLAG(IS_ANDROID)
int PowerMonitor::GetRemainingBatteryCapacity() const {
DCHECK(IsInitialized());
return Source()->GetRemainingBatteryCapacity();
}
#endif // BUILDFLAG(IS_ANDROID)
void PowerMonitor::NotifyPowerStateChange(bool on_battery_power) {
DCHECK(IsInitialized());
NotifyPowerStateChange(
on_battery_power
? PowerStateObserver::BatteryPowerStatus::kBatteryPower
: PowerStateObserver::BatteryPowerStatus::kExternalPower);
}
void PowerMonitor::NotifyPowerStateChange(
PowerStateObserver::BatteryPowerStatus battery_power_status) {
DCHECK(IsInitialized());
if (battery_power_status ==
PowerStateObserver::BatteryPowerStatus::kUnknown) {
DVLOG(1) << "PowerStateChange: with unknown value";
} else {
DVLOG(1) << "PowerStateChange: "
<< (battery_power_status ==
PowerStateObserver::BatteryPowerStatus::kBatteryPower
? "On"
: "Off")
<< " battery";
TRACE_EVENT_INSTANT(
"power",
battery_power_status ==
PowerStateObserver::BatteryPowerStatus::kBatteryPower
? perfetto::StaticString("PowerMonitor::BatteryPower")
: perfetto::StaticString("PowerMonitor::ExternalPower"),
process_track_);
}
AutoLock auto_lock(battery_power_status_lock_);
if (battery_power_status_ != battery_power_status) {
battery_power_status_ = battery_power_status;
power_state_observers_->Notify(
FROM_HERE, &PowerStateObserver::OnBatteryPowerStatusChange,
battery_power_status);
}
}
void PowerMonitor::NotifySuspend() {
DCHECK(IsInitialized());
TRACE_EVENT_INSTANT("power", "PowerMonitor::NotifySuspend", process_track_);
DVLOG(1) << "Power Suspending";
AutoLock auto_lock(is_system_suspended_lock_);
if (!is_system_suspended_) {
is_system_suspended_ = true;
last_system_resume_time_ = TimeTicks::Max();
power_suspend_observers_->Notify(FROM_HERE,
&PowerSuspendObserver::OnSuspend);
}
}
void PowerMonitor::NotifyResume() {
DCHECK(IsInitialized());
TRACE_EVENT_INSTANT("power", "PowerMonitor::NotifyResume", process_track_);
DVLOG(1) << "Power Resuming";
TimeTicks resume_time = TimeTicks::Now();
AutoLock auto_lock(is_system_suspended_lock_);
if (is_system_suspended_) {
is_system_suspended_ = false;
last_system_resume_time_ = resume_time;
power_suspend_observers_->Notify(FROM_HERE,
&PowerSuspendObserver::OnResume);
}
}
void PowerMonitor::NotifyThermalStateChange(
PowerThermalObserver::DeviceThermalState new_state) {
DCHECK(IsInitialized());
DVLOG(1) << "ThermalStateChange: "
<< PowerMonitorSource::DeviceThermalStateToString(new_state);
AutoLock auto_lock(power_thermal_state_lock_);
if (power_thermal_state_ != new_state) {
power_thermal_state_ = new_state;
thermal_state_observers_->Notify(
FROM_HERE, &PowerThermalObserver::OnThermalStateChange, new_state);
}
}
void PowerMonitor::NotifySpeedLimitChange(int speed_limit) {
DCHECK(IsInitialized());
DVLOG(1) << "SpeedLimitChange: " << speed_limit;
AutoLock auto_lock(power_thermal_state_lock_);
if (speed_limit_ != speed_limit) {
speed_limit_ = speed_limit;
thermal_state_observers_->Notify(
FROM_HERE, &PowerThermalObserver::OnSpeedLimitChange, speed_limit);
}
}
PowerMonitor* PowerMonitor::GetInstance() {
static base::NoDestructor<PowerMonitor> power_monitor;
return power_monitor.get();
}
PowerMonitor::PowerMonitor()
: process_track_("PowerMonitor"),
power_state_observers_(
base::MakeRefCounted<ObserverListThreadSafe<PowerStateObserver>>()),
power_suspend_observers_(
base::MakeRefCounted<ObserverListThreadSafe<PowerSuspendObserver>>()),
thermal_state_observers_(
base::MakeRefCounted<
ObserverListThreadSafe<PowerThermalObserver>>()) {}
PowerMonitor::~PowerMonitor() = default;
} // namespace base