| 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_impl.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() { | 95 void PowerUsageMonitor::StartPowerUsageMonitor() { |
| 106 static base::LazyInstance<PowerUsageMonitor>::Leaky monitor = | 96 static base::LazyInstance<PowerUsageMonitor>::Leaky monitor = |
| 107 LAZY_INSTANCE_INITIALIZER; | 97 LAZY_INSTANCE_INITIALIZER; |
| 108 monitor.Get().Start(); | 98 monitor.Get().Start(); |
| 109 } | 99 } |
| 110 | 100 |
| 111 PowerUsageMonitor::PowerUsageMonitor() | 101 PowerUsageMonitor::PowerUsageMonitor() |
| 112 : callback_(base::Bind(&PowerUsageMonitor::OnBatteryStatusUpdate, | 102 : callback_(base::Bind(&PowerUsageMonitor::OnBatteryStatusUpdate, |
| 113 base::Unretained(this))), | 103 base::Unretained(this))), |
| 114 system_interface_(new PowerUsageMonitorSystemInterface(this)), | 104 system_interface_(new PowerUsageMonitorSystemInterface(this)), |
| 115 started_(false), | 105 started_(false), |
| 116 tracking_discharge_(false), | 106 tracking_discharge_(false), |
| 117 on_battery_power_(false), | 107 on_battery_power_(false), |
| 118 initial_battery_level_(0), | 108 initial_battery_level_(0), |
| 119 current_battery_level_(0) { | 109 current_battery_level_(0) {} |
| 120 } | |
| 121 | 110 |
| 122 PowerUsageMonitor::~PowerUsageMonitor() { | 111 PowerUsageMonitor::~PowerUsageMonitor() { |
| 123 if (started_) | 112 if (started_) |
| 124 base::PowerMonitor::Get()->RemoveObserver(this); | 113 base::PowerMonitor::Get()->RemoveObserver(this); |
| 125 } | 114 } |
| 126 | 115 |
| 127 void PowerUsageMonitor::Start() { | 116 void PowerUsageMonitor::Start() { |
| 128 // Power monitoring may be delayed based on uptime, but renderer process | 117 // Power monitoring may be delayed based on uptime, but renderer process |
| 129 // lifetime tracking needs to start immediately so processes created before | 118 // lifetime tracking needs to start immediately so processes created before |
| 130 // then are accounted for. | 119 // then are accounted for. |
| 131 registrar_.Add(this, | 120 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 132 NOTIFICATION_RENDERER_PROCESS_CREATED, | 121 content::NotificationService::AllBrowserContextsAndSources()); |
| 133 NotificationService::AllBrowserContextsAndSources()); | 122 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 134 registrar_.Add(this, | 123 content::NotificationService::AllBrowserContextsAndSources()); |
| 135 NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 136 NotificationService::AllBrowserContextsAndSources()); | |
| 137 subscription_ = | 124 subscription_ = |
| 138 device::BatteryStatusService::GetInstance()->AddCallback(callback_); | 125 device::BatteryStatusService::GetInstance()->AddCallback(callback_); |
| 139 | 126 |
| 140 // Delay initialization until the system has been up for a while. | 127 // 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. | 128 // This is to mitigate the effect of increased power draw during system start. |
| 142 base::TimeDelta uptime = base::SysInfo::Uptime(); | 129 base::TimeDelta uptime = base::SysInfo::Uptime(); |
| 143 base::TimeDelta min_uptime = base::TimeDelta::FromMinutes(kMinUptimeMinutes); | 130 base::TimeDelta min_uptime = base::TimeDelta::FromMinutes(kMinUptimeMinutes); |
| 144 if (uptime < min_uptime) { | 131 if (uptime < min_uptime) { |
| 145 base::TimeDelta delay = min_uptime - uptime; | 132 base::TimeDelta delay = min_uptime - uptime; |
| 146 BrowserThread::PostDelayedTask( | 133 content::BrowserThread::PostDelayedTask( |
| 147 BrowserThread::UI, | 134 content::BrowserThread::UI, FROM_HERE, |
| 148 FROM_HERE, | |
| 149 base::Bind(&PowerUsageMonitor::StartInternal, base::Unretained(this)), | 135 base::Bind(&PowerUsageMonitor::StartInternal, base::Unretained(this)), |
| 150 delay); | 136 delay); |
| 151 } else { | 137 } else { |
| 152 StartInternal(); | 138 StartInternal(); |
| 153 } | 139 } |
| 154 } | 140 } |
| 155 | 141 |
| 156 void PowerUsageMonitor::StartInternal() { | 142 void PowerUsageMonitor::StartInternal() { |
| 157 DCHECK(!started_); | 143 DCHECK(!started_); |
| 158 started_ = true; | 144 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. | 211 } else if (now_on_battery_power) { // Wall power disconnected. |
| 226 DischargeStarted(battery_level); | 212 DischargeStarted(battery_level); |
| 227 } else { // Wall power connected. | 213 } else { // Wall power connected. |
| 228 WallPowerConnected(battery_level); | 214 WallPowerConnected(battery_level); |
| 229 } | 215 } |
| 230 } | 216 } |
| 231 | 217 |
| 232 void PowerUsageMonitor::OnRenderProcessNotification(int type, int rph_id) { | 218 void PowerUsageMonitor::OnRenderProcessNotification(int type, int rph_id) { |
| 233 size_t previous_num_live_renderers = live_renderer_ids_.size(); | 219 size_t previous_num_live_renderers = live_renderer_ids_.size(); |
| 234 | 220 |
| 235 if (type == NOTIFICATION_RENDERER_PROCESS_CREATED) { | 221 if (type == content::NOTIFICATION_RENDERER_PROCESS_CREATED) { |
| 236 live_renderer_ids_.insert(rph_id); | 222 live_renderer_ids_.insert(rph_id); |
| 237 } else if (type == NOTIFICATION_RENDERER_PROCESS_CLOSED) { | 223 } else if (type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { |
| 238 live_renderer_ids_.erase(rph_id); | 224 live_renderer_ids_.erase(rph_id); |
| 239 } else { | 225 } else { |
| 240 NOTREACHED() << "Unexpected notification type: " << type; | 226 NOTREACHED() << "Unexpected notification type: " << type; |
| 241 } | 227 } |
| 242 | 228 |
| 243 if (live_renderer_ids_.empty() && previous_num_live_renderers != 0) { | 229 if (live_renderer_ids_.empty() && previous_num_live_renderers != 0) { |
| 244 // All render processes have died. | 230 // All render processes have died. |
| 245 CancelPendingHistogramReporting(); | 231 CancelPendingHistogramReporting(); |
| 246 tracking_discharge_ = false; | 232 tracking_discharge_ = false; |
| 247 } | 233 } |
| 248 | |
| 249 } | 234 } |
| 250 | 235 |
| 251 void PowerUsageMonitor::SetSystemInterfaceForTest( | 236 void PowerUsageMonitor::SetSystemInterfaceForTest( |
| 252 std::unique_ptr<SystemInterface> interface) { | 237 std::unique_ptr<SystemInterface> interface) { |
| 253 system_interface_ = std::move(interface); | 238 system_interface_ = std::move(interface); |
| 254 } | 239 } |
| 255 | 240 |
| 256 void PowerUsageMonitor::OnPowerStateChange(bool on_battery_power) { | 241 void PowerUsageMonitor::OnPowerStateChange(bool on_battery_power) {} |
| 257 } | |
| 258 | 242 |
| 259 void PowerUsageMonitor::OnResume() { | 243 void PowerUsageMonitor::OnResume() {} |
| 260 } | |
| 261 | 244 |
| 262 void PowerUsageMonitor::OnSuspend() { | 245 void PowerUsageMonitor::OnSuspend() { |
| 263 CancelPendingHistogramReporting(); | 246 CancelPendingHistogramReporting(); |
| 264 } | 247 } |
| 265 | 248 |
| 266 void PowerUsageMonitor::Observe(int type, | 249 void PowerUsageMonitor::Observe(int type, |
| 267 const NotificationSource& source, | 250 const content::NotificationSource& source, |
| 268 const NotificationDetails& details) { | 251 const content::NotificationDetails& details) { |
| 269 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | 252 content::RenderProcessHost* rph = |
| 253 content::Source<content::RenderProcessHost>(source).ptr(); |
| 270 OnRenderProcessNotification(type, rph->GetID()); | 254 OnRenderProcessNotification(type, rph->GetID()); |
| 271 } | 255 } |
| 272 | 256 |
| 273 void PowerUsageMonitor::CancelPendingHistogramReporting() { | 257 void PowerUsageMonitor::CancelPendingHistogramReporting() { |
| 274 // Cancel any in-progress histogram reports and reporting of discharge UMA. | 258 // Cancel any in-progress histogram reports and reporting of discharge UMA. |
| 275 system_interface_->CancelPendingHistogramReports(); | 259 system_interface_->CancelPendingHistogramReports(); |
| 276 } | 260 } |
| 277 | |
| 278 } // namespace content | |
| OLD | NEW |