| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/power_usage_monitor_impl.h" | 5 #include "chrome/browser/power_usage_monitor/power_usage_monitor.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/notification_types.h" | 20 #include "content/public/browser/notification_types.h" |
| 21 #include "content/public/browser/power_usage_monitor.h" | |
| 22 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 23 | 22 |
| 24 namespace content { | |
| 25 | |
| 26 namespace { | 23 namespace { |
| 27 | 24 |
| 28 // Wait this long after power on before enabling power usage monitoring. | 25 // Wait this long after power on before enabling power usage monitoring. |
| 29 const int kMinUptimeMinutes = 30; | 26 const int kMinUptimeMinutes = 30; |
| 30 | 27 |
| 31 // Minimum discharge time after which we collect the discharge rate. | 28 // Minimum discharge time after which we collect the discharge rate. |
| 32 const int kMinDischargeMinutes = 30; | 29 const int kMinDischargeMinutes = 30; |
| 33 | 30 |
| 34 class PowerUsageMonitorSystemInterface | 31 class PowerUsageMonitorSystemInterface |
| 35 : public PowerUsageMonitor::SystemInterface { | 32 : public PowerUsageMonitor::SystemInterface { |
| 36 public: | 33 public: |
| 37 explicit PowerUsageMonitorSystemInterface(PowerUsageMonitor* owner) | 34 explicit PowerUsageMonitorSystemInterface(PowerUsageMonitor* owner) |
| 38 : power_usage_monitor_(owner), | 35 : power_usage_monitor_(owner), weak_ptr_factory_(this) {} |
| 39 weak_ptr_factory_(this) {} | |
| 40 ~PowerUsageMonitorSystemInterface() override {} | 36 ~PowerUsageMonitorSystemInterface() override {} |
| 41 | 37 |
| 42 void ScheduleHistogramReport(base::TimeDelta delay) override { | 38 void ScheduleHistogramReport(base::TimeDelta delay) override { |
| 43 BrowserThread::PostDelayedTask( | 39 content::BrowserThread::PostDelayedTask( |
| 44 BrowserThread::UI, | 40 content::BrowserThread::UI, FROM_HERE, |
| 45 FROM_HERE, | |
| 46 base::Bind( | 41 base::Bind( |
| 47 &PowerUsageMonitorSystemInterface::ReportBatteryLevelHistogram, | 42 &PowerUsageMonitorSystemInterface::ReportBatteryLevelHistogram, |
| 48 weak_ptr_factory_.GetWeakPtr(), | 43 weak_ptr_factory_.GetWeakPtr(), Now(), delay), |
| 49 Now(), | |
| 50 delay), | |
| 51 delay); | 44 delay); |
| 52 } | 45 } |
| 53 | 46 |
| 54 void CancelPendingHistogramReports() override { | 47 void CancelPendingHistogramReports() override { |
| 55 weak_ptr_factory_.InvalidateWeakPtrs(); | 48 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 56 } | 49 } |
| 57 | 50 |
| 58 void RecordDischargePercentPerHour(int percent_per_hour) override { | 51 void RecordDischargePercentPerHour(int percent_per_hour) override { |
| 59 UMA_HISTOGRAM_PERCENTAGE("Power.BatteryDischargePercentPerHour", | 52 UMA_HISTOGRAM_PERCENTAGE("Power.BatteryDischargePercentPerHour", |
| 60 percent_per_hour); | 53 percent_per_hour); |
| 61 } | 54 } |
| 62 | 55 |
| 63 base::Time Now() override { return base::Time::Now(); } | 56 base::Time Now() override { return base::Time::Now(); } |
| 64 | 57 |
| 65 protected: | 58 protected: |
| 66 void ReportBatteryLevelHistogram(base::Time start_time, | 59 void ReportBatteryLevelHistogram(base::Time start_time, |
| 67 base::TimeDelta discharge_time) { | 60 base::TimeDelta discharge_time) { |
| 68 // It's conceivable that the code to cancel pending histogram reports on | 61 // It's conceivable that the code to cancel pending histogram reports on |
| 69 // system suspend, will only get called after the system has woken up. | 62 // system suspend, will only get called after the system has woken up. |
| 70 // To mitigage this, check whether more time has passed than expected and | 63 // To mitigage this, check whether more time has passed than expected and |
| 71 // abort histogram recording in this case. | 64 // abort histogram recording in this case. |
| 72 | 65 |
| 73 // Delayed tasks are subject to timer coalescing and can fire anywhere from | 66 // Delayed tasks are subject to timer coalescing and can fire anywhere from |
| 74 // delay -> delay * 1.5) . In most cases, the OS should fire the task | 67 // delay -> delay * 1.5) . In most cases, the OS should fire the task |
| 75 // at the next wakeup and not as late as it can. | 68 // at the next wakeup and not as late as it can. |
| 76 // A threshold of 2 minutes is used, since that should be large enough to | 69 // A threshold of 2 minutes is used, since that should be large enough to |
| 77 // take the slop factor due to coalescing into account. | 70 // take the slop factor due to coalescing into account. |
| 78 base::TimeDelta threshold = discharge_time + | 71 base::TimeDelta threshold = |
| 79 base::TimeDelta::FromMinutes(2); | 72 discharge_time + base::TimeDelta::FromMinutes(2); |
| 80 if ((Now() - start_time) > threshold) { | 73 if ((Now() - start_time) > threshold) { |
| 81 return; | 74 return; |
| 82 } | 75 } |
| 83 | 76 |
| 84 const std::string histogram_name = base::StringPrintf( | 77 const std::string histogram_name = base::StringPrintf( |
| 85 "Power.BatteryDischarge_%d", discharge_time.InMinutes()); | 78 "Power.BatteryDischarge_%d", discharge_time.InMinutes()); |
| 86 base::HistogramBase* histogram = | 79 base::HistogramBase* histogram = |
| 87 base::Histogram::FactoryGet(histogram_name, | 80 base::Histogram::FactoryGet(histogram_name, 1, 100, 101, |
| 88 1, | |
| 89 100, | |
| 90 101, | |
| 91 base::Histogram::kUmaTargetedHistogramFlag); | 81 base::Histogram::kUmaTargetedHistogramFlag); |
| 92 double discharge_amount = power_usage_monitor_->discharge_amount(); | 82 double discharge_amount = power_usage_monitor_->discharge_amount(); |
| 93 histogram->Add(discharge_amount * 100); | 83 histogram->Add(discharge_amount * 100); |
| 94 } | 84 } |
| 95 | 85 |
| 96 private: | 86 private: |
| 97 PowerUsageMonitor* power_usage_monitor_; // Not owned. | 87 PowerUsageMonitor* power_usage_monitor_; // Not owned. |
| 98 | 88 |
| 99 // Used to cancel in progress delayed tasks. | 89 // Used to cancel in progress delayed tasks. |
| 100 base::WeakPtrFactory<PowerUsageMonitorSystemInterface> weak_ptr_factory_; | 90 base::WeakPtrFactory<PowerUsageMonitorSystemInterface> weak_ptr_factory_; |
| 101 }; | 91 }; |
| 102 | 92 |
| 103 } // namespace | 93 } // namespace |
| 104 | 94 |
| 105 void StartPowerUsageMonitor() { | |
| 106 static base::LazyInstance<PowerUsageMonitor>::Leaky monitor = | |
| 107 LAZY_INSTANCE_INITIALIZER; | |
| 108 monitor.Get().Start(); | |
| 109 } | |
| 110 | |
| 111 PowerUsageMonitor::PowerUsageMonitor() | 95 PowerUsageMonitor::PowerUsageMonitor() |
| 112 : callback_(base::Bind(&PowerUsageMonitor::OnBatteryStatusUpdate, | 96 : callback_(base::Bind(&PowerUsageMonitor::OnBatteryStatusUpdate, |
| 113 base::Unretained(this))), | 97 base::Unretained(this))), |
| 114 system_interface_(new PowerUsageMonitorSystemInterface(this)), | 98 system_interface_(new PowerUsageMonitorSystemInterface(this)), |
| 115 started_(false), | 99 started_(false), |
| 116 tracking_discharge_(false), | 100 tracking_discharge_(false), |
| 117 on_battery_power_(false), | 101 on_battery_power_(false), |
| 118 initial_battery_level_(0), | 102 initial_battery_level_(0), |
| 119 current_battery_level_(0) { | 103 current_battery_level_(0) {} |
| 120 } | |
| 121 | 104 |
| 122 PowerUsageMonitor::~PowerUsageMonitor() { | 105 PowerUsageMonitor::~PowerUsageMonitor() { |
| 123 if (started_) | 106 if (started_) |
| 124 base::PowerMonitor::Get()->RemoveObserver(this); | 107 base::PowerMonitor::Get()->RemoveObserver(this); |
| 125 } | 108 } |
| 126 | 109 |
| 127 void PowerUsageMonitor::Start() { | 110 void PowerUsageMonitor::Start() { |
| 128 // Power monitoring may be delayed based on uptime, but renderer process | 111 // Power monitoring may be delayed based on uptime, but renderer process |
| 129 // lifetime tracking needs to start immediately so processes created before | 112 // lifetime tracking needs to start immediately so processes created before |
| 130 // then are accounted for. | 113 // then are accounted for. |
| 131 registrar_.Add(this, | 114 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 132 NOTIFICATION_RENDERER_PROCESS_CREATED, | 115 content::NotificationService::AllBrowserContextsAndSources()); |
| 133 NotificationService::AllBrowserContextsAndSources()); | 116 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 134 registrar_.Add(this, | 117 content::NotificationService::AllBrowserContextsAndSources()); |
| 135 NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 136 NotificationService::AllBrowserContextsAndSources()); | |
| 137 subscription_ = | 118 subscription_ = |
| 138 device::BatteryStatusService::GetInstance()->AddCallback(callback_); | 119 device::BatteryStatusService::GetInstance()->AddCallback(callback_); |
| 139 | 120 |
| 140 // Delay initialization until the system has been up for a while. | 121 // Delay initialization until the system has been up for a while. |
| 141 // This is to mitigate the effect of increased power draw during system start. | 122 // This is to mitigate the effect of increased power draw during system start. |
| 142 base::TimeDelta uptime = base::SysInfo::Uptime(); | 123 base::TimeDelta uptime = base::SysInfo::Uptime(); |
| 143 base::TimeDelta min_uptime = base::TimeDelta::FromMinutes(kMinUptimeMinutes); | 124 base::TimeDelta min_uptime = base::TimeDelta::FromMinutes(kMinUptimeMinutes); |
| 144 if (uptime < min_uptime) { | 125 if (uptime < min_uptime) { |
| 145 base::TimeDelta delay = min_uptime - uptime; | 126 base::TimeDelta delay = min_uptime - uptime; |
| 146 BrowserThread::PostDelayedTask( | 127 content::BrowserThread::PostDelayedTask( |
| 147 BrowserThread::UI, | 128 content::BrowserThread::UI, FROM_HERE, |
| 148 FROM_HERE, | |
| 149 base::Bind(&PowerUsageMonitor::StartInternal, base::Unretained(this)), | 129 base::Bind(&PowerUsageMonitor::StartInternal, base::Unretained(this)), |
| 150 delay); | 130 delay); |
| 151 } else { | 131 } else { |
| 152 StartInternal(); | 132 StartInternal(); |
| 153 } | 133 } |
| 154 } | 134 } |
| 155 | 135 |
| 156 void PowerUsageMonitor::StartInternal() { | 136 void PowerUsageMonitor::StartInternal() { |
| 157 DCHECK(!started_); | 137 DCHECK(!started_); |
| 158 started_ = true; | 138 started_ = true; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } else if (now_on_battery_power) { // Wall power disconnected. | 205 } else if (now_on_battery_power) { // Wall power disconnected. |
| 226 DischargeStarted(battery_level); | 206 DischargeStarted(battery_level); |
| 227 } else { // Wall power connected. | 207 } else { // Wall power connected. |
| 228 WallPowerConnected(battery_level); | 208 WallPowerConnected(battery_level); |
| 229 } | 209 } |
| 230 } | 210 } |
| 231 | 211 |
| 232 void PowerUsageMonitor::OnRenderProcessNotification(int type, int rph_id) { | 212 void PowerUsageMonitor::OnRenderProcessNotification(int type, int rph_id) { |
| 233 size_t previous_num_live_renderers = live_renderer_ids_.size(); | 213 size_t previous_num_live_renderers = live_renderer_ids_.size(); |
| 234 | 214 |
| 235 if (type == NOTIFICATION_RENDERER_PROCESS_CREATED) { | 215 if (type == content::NOTIFICATION_RENDERER_PROCESS_CREATED) { |
| 236 live_renderer_ids_.insert(rph_id); | 216 live_renderer_ids_.insert(rph_id); |
| 237 } else if (type == NOTIFICATION_RENDERER_PROCESS_CLOSED) { | 217 } else if (type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { |
| 238 live_renderer_ids_.erase(rph_id); | 218 live_renderer_ids_.erase(rph_id); |
| 239 } else { | 219 } else { |
| 240 NOTREACHED() << "Unexpected notification type: " << type; | 220 NOTREACHED() << "Unexpected notification type: " << type; |
| 241 } | 221 } |
| 242 | 222 |
| 243 if (live_renderer_ids_.empty() && previous_num_live_renderers != 0) { | 223 if (live_renderer_ids_.empty() && previous_num_live_renderers != 0) { |
| 244 // All render processes have died. | 224 // All render processes have died. |
| 245 CancelPendingHistogramReporting(); | 225 CancelPendingHistogramReporting(); |
| 246 tracking_discharge_ = false; | 226 tracking_discharge_ = false; |
| 247 } | 227 } |
| 248 | |
| 249 } | 228 } |
| 250 | 229 |
| 251 void PowerUsageMonitor::SetSystemInterfaceForTest( | 230 void PowerUsageMonitor::SetSystemInterfaceForTest( |
| 252 std::unique_ptr<SystemInterface> interface) { | 231 std::unique_ptr<SystemInterface> interface) { |
| 253 system_interface_ = std::move(interface); | 232 system_interface_ = std::move(interface); |
| 254 } | 233 } |
| 255 | 234 |
| 256 void PowerUsageMonitor::OnPowerStateChange(bool on_battery_power) { | 235 void PowerUsageMonitor::OnPowerStateChange(bool on_battery_power) {} |
| 257 } | |
| 258 | 236 |
| 259 void PowerUsageMonitor::OnResume() { | 237 void PowerUsageMonitor::OnResume() {} |
| 260 } | |
| 261 | 238 |
| 262 void PowerUsageMonitor::OnSuspend() { | 239 void PowerUsageMonitor::OnSuspend() { |
| 263 CancelPendingHistogramReporting(); | 240 CancelPendingHistogramReporting(); |
| 264 } | 241 } |
| 265 | 242 |
| 266 void PowerUsageMonitor::Observe(int type, | 243 void PowerUsageMonitor::Observe(int type, |
| 267 const NotificationSource& source, | 244 const content::NotificationSource& source, |
| 268 const NotificationDetails& details) { | 245 const content::NotificationDetails& details) { |
| 269 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | 246 content::RenderProcessHost* rph = |
| 247 content::Source<content::RenderProcessHost>(source).ptr(); |
| 270 OnRenderProcessNotification(type, rph->GetID()); | 248 OnRenderProcessNotification(type, rph->GetID()); |
| 271 } | 249 } |
| 272 | 250 |
| 273 void PowerUsageMonitor::CancelPendingHistogramReporting() { | 251 void PowerUsageMonitor::CancelPendingHistogramReporting() { |
| 274 // Cancel any in-progress histogram reports and reporting of discharge UMA. | 252 // Cancel any in-progress histogram reports and reporting of discharge UMA. |
| 275 system_interface_->CancelPendingHistogramReports(); | 253 system_interface_->CancelPendingHistogramReports(); |
| 276 } | 254 } |
| 277 | |
| 278 } // namespace content | |
| OLD | NEW |