Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/power_usage_monitor_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
|
Daniel Erat
2014/11/04 17:04:02
don't think you're using this now
jeremy
2014/11/05 11:40:19
Done.
| |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "base/sys_info.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/notification_service.h" | |
| 17 #include "content/public/browser/notification_source.h" | |
| 18 #include "content/public/browser/notification_types.h" | |
| 19 #include "content/public/browser/power_usage_monitor.h" | |
| 20 #include "content/public/browser/render_process_host.h" | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class PowerUsageMonitorSystemInterface | |
| 27 : public PowerUsageMonitor::SystemInterface { | |
| 28 public: | |
| 29 explicit PowerUsageMonitorSystemInterface(PowerUsageMonitor* owner) | |
| 30 : power_usage_monitor_(owner), | |
| 31 weak_ptr_factory_(this) {} | |
| 32 virtual ~PowerUsageMonitorSystemInterface() {} | |
|
Daniel Erat
2014/11/04 17:04:03
override instead of virtual
jeremy
2014/11/05 11:40:19
Done.
| |
| 33 | |
| 34 void ScheduleHistogramReport(base::TimeDelta delay) override { | |
| 35 BrowserThread::PostDelayedTask( | |
| 36 BrowserThread::UI, | |
| 37 FROM_HERE, | |
| 38 base::Bind( | |
| 39 &PowerUsageMonitorSystemInterface::ReportBatteryLevelHistogram, | |
| 40 weak_ptr_factory_.GetWeakPtr(), | |
| 41 Now(), | |
| 42 delay), | |
| 43 delay); | |
| 44 } | |
| 45 | |
| 46 void CancelPendingHistogramReports() override { | |
| 47 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 48 } | |
| 49 | |
| 50 base::Time Now() override { return base::Time::Now(); } | |
| 51 | |
| 52 protected: | |
| 53 void ReportBatteryLevelHistogram(base::Time start_time, | |
| 54 base::TimeDelta discharge_time) { | |
| 55 // It's conceivable that the code to cancel pending histogram reports on | |
| 56 // system suspend, will only get called after the system has woken up. | |
| 57 // To mitigage this, check whether more time has passed than expected and | |
| 58 // abort histogram recording in this case. | |
| 59 | |
| 60 // Delayed tasks are subject to timer coalescing and can fire anywhere from | |
| 61 // delay -> delay * 1.5) . In most cases, the OS should fire the task | |
| 62 // at the next wakeup and not as late as it can. | |
| 63 // A threshold of 2 minutes is used, since that should be large enough to | |
| 64 // take the slop factor due to coalescing into account. | |
| 65 base::TimeDelta threshold = discharge_time + | |
| 66 base::TimeDelta::FromMinutes(2); | |
| 67 if ((Now() - start_time) > threshold) { | |
| 68 UMA_HISTOGRAM_BOOLEAN("Power.BatteryDischargeReportsCancelled", true); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 const std::string histogram_name = base::StringPrintf( | |
| 73 "Power.BatteryDischarge_%d", discharge_time.InMinutes()); | |
| 74 base::HistogramBase* histogram = | |
| 75 base::Histogram::FactoryGet(histogram_name, | |
| 76 1, | |
| 77 100, | |
| 78 101, | |
| 79 base::Histogram::kUmaTargetedHistogramFlag); | |
| 80 double discharge_amount = power_usage_monitor_->discharge_amount(); | |
| 81 histogram->Add(discharge_amount * 100); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 PowerUsageMonitor* power_usage_monitor_; // Not owned. | |
| 86 | |
| 87 // Used to cancel in progress delayed tasks. | |
| 88 base::WeakPtrFactory<PowerUsageMonitorSystemInterface> weak_ptr_factory_; | |
| 89 }; | |
| 90 | |
| 91 } // namespace | |
| 92 | |
| 93 void StartPowerUsageMonitor() { | |
| 94 static base::LazyInstance<PowerUsageMonitor>::Leaky monitor = | |
| 95 LAZY_INSTANCE_INITIALIZER; | |
| 96 monitor.Get().Start(); | |
| 97 } | |
| 98 | |
| 99 PowerUsageMonitor::PowerUsageMonitor() | |
| 100 : callback_(base::Bind(&PowerUsageMonitor::OnBatteryStatusUpdate, | |
| 101 base::Unretained(this))), | |
| 102 system_interface_(new PowerUsageMonitorSystemInterface(this)), | |
| 103 started_(false), | |
| 104 on_battery_power_(false), | |
| 105 initial_battery_level_(0), | |
| 106 current_battery_level_(0) { | |
| 107 } | |
| 108 | |
| 109 PowerUsageMonitor::~PowerUsageMonitor() { | |
| 110 if (started_) | |
| 111 base::PowerMonitor::Get()->RemoveObserver(this); | |
| 112 } | |
| 113 | |
| 114 void PowerUsageMonitor::Start() { | |
| 115 // Delay initialization until the system has been up for at least 30 minutes. | |
| 116 // This is to mitigate the effect of increased power draw during system start. | |
| 117 int64 uptime_in_minutes = base::SysInfo::Uptime() / (60 * 1000); | |
|
Daniel Erat
2014/11/04 17:04:03
cleaner to just convert this to a TimeDelta immedi
jeremy
2014/11/05 11:40:19
Done.
| |
| 118 if (uptime_in_minutes < 30) { | |
| 119 base::TimeDelta delay = | |
| 120 base::TimeDelta::FromMinutes(30 - uptime_in_minutes); | |
| 121 BrowserThread::PostDelayedTask( | |
| 122 BrowserThread::UI, | |
| 123 FROM_HERE, | |
| 124 base::Bind(&PowerUsageMonitor::StartInternal, base::Unretained(this)), | |
| 125 delay); | |
| 126 } else { | |
| 127 StartInternal(); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 void PowerUsageMonitor::StartInternal() { | |
|
Daniel Erat
2014/11/04 17:04:03
nit: CHECK(!started_);
jeremy
2014/11/05 11:40:19
Done.
| |
| 132 started_ = true; | |
| 133 | |
| 134 // PowerMonitor is used to get suspend/resume notifications. | |
| 135 base::PowerMonitor::Get()->AddObserver(this); | |
| 136 | |
| 137 registrar_.Add(this, | |
| 138 NOTIFICATION_RENDERER_PROCESS_CREATED, | |
| 139 NotificationService::AllBrowserContextsAndSources()); | |
| 140 registrar_.Add(this, | |
| 141 NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 142 NotificationService::AllBrowserContextsAndSources()); | |
| 143 registrar_.Add(this, | |
| 144 NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 145 NotificationService::AllBrowserContextsAndSources()); | |
| 146 subscription_ = | |
| 147 device::BatteryStatusService::GetInstance()->AddCallback(callback_); | |
| 148 } | |
| 149 | |
| 150 void PowerUsageMonitor::DischargeStarted(double battery_level) { | |
| 151 // If all browser windows are closed, don't report power metrics since | |
| 152 // Chrome's power draw is likely not significant. | |
| 153 if (live_renderer_ids_.empty()) | |
| 154 return; | |
| 155 | |
| 156 // Cancel any in-progress ReportBatteryLevelHistogram() calls. | |
| 157 system_interface_->CancelPendingHistogramReports(); | |
| 158 | |
| 159 on_battery_power_ = true; | |
| 160 initial_battery_level_ = battery_level; | |
| 161 current_battery_level_ = battery_level; | |
| 162 start_discharge_time_ = system_interface_->Now(); | |
| 163 | |
| 164 const int kBatteryReportingIntervalMinutes[] = {5, 15, 30}; | |
| 165 for (auto reporting_interval : kBatteryReportingIntervalMinutes) { | |
| 166 base::TimeDelta delay = base::TimeDelta::FromMinutes(reporting_interval); | |
| 167 system_interface_->ScheduleHistogramReport(delay); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void PowerUsageMonitor::WallPowerConnected(double battery_level) { | |
| 172 // Cancel any in-progress ReportBatteryLevelHistogram() calls. | |
| 173 system_interface_->CancelPendingHistogramReports(); | |
| 174 | |
| 175 if (!start_discharge_time_.is_null()) { | |
| 176 base::TimeDelta discharge_time = | |
| 177 system_interface_->Now() - start_discharge_time_; | |
| 178 | |
| 179 if (discharge_time.InMinutes() > 30) { | |
|
Daniel Erat
2014/11/04 17:04:02
move the "30" to a constant at the top of the file
jeremy
2014/11/05 11:40:19
Done.
| |
| 180 double discharge_percent = initial_battery_level_ - battery_level; | |
| 181 double discharge_rate = discharge_percent / discharge_time.InMinutes(); | |
| 182 | |
| 183 UMA_HISTOGRAM_PERCENTAGE("Power.BatteryDischargeRateWhenUnplugged", | |
| 184 discharge_rate * 100.0); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 on_battery_power_ = false; | |
| 189 initial_battery_level_ = 0; | |
| 190 current_battery_level_ = 0; | |
| 191 start_discharge_time_ = base::Time(); | |
| 192 } | |
| 193 | |
| 194 void PowerUsageMonitor::OnBatteryStatusUpdate( | |
| 195 const device::BatteryStatus& status) { | |
| 196 bool now_on_battery_power = (status.charging == 0); | |
| 197 bool was_on_battery_power = on_battery_power_; | |
|
Daniel Erat
2014/11/04 17:04:02
i've mentioned it before, but i feel pretty strong
jeremy
2014/11/05 11:40:19
Done.
| |
| 198 double battery_level = status.level; | |
| 199 | |
| 200 if (now_on_battery_power == was_on_battery_power) { | |
| 201 if (now_on_battery_power) | |
| 202 current_battery_level_ = battery_level; | |
| 203 return; | |
| 204 } else if (now_on_battery_power) { // Wall power disconnected. | |
| 205 DischargeStarted(battery_level); | |
| 206 } else { // Wall power connected. | |
| 207 WallPowerConnected(battery_level); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 void PowerUsageMonitor::SetSystemInterfaceForTest( | |
| 212 scoped_ptr<SystemInterface> interface) { | |
| 213 system_interface_ = interface.Pass(); | |
| 214 } | |
| 215 | |
| 216 void PowerUsageMonitor::SetRenderersForTest(bool has_live_renderers) { | |
| 217 live_renderer_ids_.clear(); | |
| 218 if (has_live_renderers) | |
| 219 live_renderer_ids_.insert(1); | |
| 220 } | |
| 221 | |
| 222 void PowerUsageMonitor::OnSuspend() { | |
| 223 CancelPendingHistogramReporting(); | |
| 224 } | |
| 225 | |
| 226 void PowerUsageMonitor::Observe(int type, | |
| 227 const NotificationSource& source, | |
| 228 const NotificationDetails& details) { | |
| 229 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | |
| 230 | |
| 231 size_t previous_num_live_renderers = live_renderer_ids_.size(); | |
| 232 | |
| 233 if (type == NOTIFICATION_RENDERER_PROCESS_CREATED) { | |
| 234 live_renderer_ids_.insert(rph->GetID()); | |
| 235 } else if (type == NOTIFICATION_RENDERER_PROCESS_TERMINATED || | |
| 236 type == NOTIFICATION_RENDERER_PROCESS_CLOSED) { | |
| 237 live_renderer_ids_.erase(rph->GetID()); | |
| 238 } else { | |
| 239 NOTREACHED() << "Unexpected notification type: " << type; | |
| 240 } | |
| 241 | |
| 242 if (live_renderer_ids_.empty() && previous_num_live_renderers != 0) { | |
| 243 // All render processes have died. | |
| 244 CancelPendingHistogramReporting(); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 void PowerUsageMonitor::CancelPendingHistogramReporting() { | |
| 249 // Cancel any in-progress histogram reports and reporting of discharge UMA. | |
| 250 system_interface_->CancelPendingHistogramReports(); | |
| 251 start_discharge_time_ = base::Time(); | |
| 252 } | |
| 253 | |
| 254 } // namespace content | |
| OLD | NEW |