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 #ifndef CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
| 6 #define CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/containers/hash_tables.h" | |
| 10 #include "base/gtest_prod_util.h" | |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "base/power_monitor/power_monitor.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "content/public/browser/browser_message_filter.h" | |
| 15 #include "content/public/browser/notification_observer.h" | |
| 16 #include "content/public/browser/notification_registrar.h" | |
| 17 #include "device/battery/battery_status_service.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 // Record statistics on power usage. | |
| 22 // | |
| 23 // Two main statics are recorded by this class: | |
| 24 // * Power.BatteryDischarge_{5,15,30} - delta between battery level when | |
|
Daniel Erat
2014/11/06 18:56:19
(and then Power.BatteryDischargePercent_... for th
| |
| 25 // unplugged from wallpower, over the specified period - in minutes. | |
| 26 // * Power.BatteryDischargeRateWhenUnplugged - the rate of battery discharge | |
| 27 // from the device being unplugged until it's plugged back in, if said period | |
| 28 // was longer than 30 minutes. | |
| 29 // | |
| 30 // Heuristics: | |
| 31 // * Data collection starts after system uptime exceeds 30 minutes. | |
| 32 // * If the machine goes to sleep or all renderers are closed then the current | |
| 33 // measurement is cancelled. | |
| 34 class PowerUsageMonitor : public base::PowerObserver, | |
| 35 public NotificationObserver { | |
| 36 public: | |
| 37 class SystemInterface { | |
| 38 public: | |
| 39 virtual ~SystemInterface() {} | |
| 40 | |
| 41 virtual void ScheduleHistogramReport(base::TimeDelta delay) = 0; | |
| 42 virtual void CancelPendingHistogramReports() = 0; | |
| 43 | |
| 44 // Record the battery discharge rate over a period when the system is | |
| 45 // on battery power. Rate refers to the amount the battery charge has | |
| 46 // decreased over a unit of time. | |
| 47 // Possible values [0,100]. | |
| 48 // The number is scaled so that 0 signifies very slow discharge, arbitrarily | |
| 49 // defined as total battery discharge in 8 hours or more. 100 indicates | |
| 50 // a very quick discharge. | |
| 51 // This scale is arbitrary and only meant to provide sufficient precision to | |
| 52 // discern interesting changes in discharge rate. | |
|
Daniel Erat
2014/11/06 17:22:43
mark, any opinions about whether this is a meaning
Mark P
2014/11/06 18:50:03
I like the idea. Here are my concerns:
- When / h
Daniel Erat
2014/11/06 18:54:15
i asked about the device-to-device variance in an
Mark P
2014/11/06 19:33:33
Is hard the slicing by hardware fine-grained enoug
jeremy
2014/11/09 13:49:27
* I changed this to report the percent of battery
| |
| 53 virtual void RecordDischargeRate(int discharge_rate) = 0; | |
| 54 | |
| 55 // Allow tests to override clock. | |
| 56 virtual base::Time Now() = 0; | |
| 57 }; | |
| 58 | |
| 59 public: | |
| 60 PowerUsageMonitor(); | |
| 61 ~PowerUsageMonitor() override; | |
| 62 | |
| 63 double discharge_amount() const { | |
| 64 return initial_battery_level_ - current_battery_level_; | |
| 65 } | |
| 66 | |
| 67 // Start monitoring power usage. | |
| 68 // Note that the actual monitoring will be delayed until 30 minutes after | |
| 69 // system boot. | |
| 70 void Start(); | |
| 71 | |
| 72 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface); | |
| 73 | |
| 74 // Overridden from base::PowerObserver: | |
| 75 void OnPowerStateChange(bool on_battery_power) override; | |
| 76 void OnResume() override; | |
| 77 void OnSuspend() override; | |
| 78 | |
| 79 // Overridden from NotificationObserver: | |
| 80 void Observe(int type, | |
| 81 const NotificationSource& source, | |
| 82 const NotificationDetails& details) override; | |
| 83 | |
| 84 private: | |
| 85 friend class PowerUsageMonitorTest; | |
| 86 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); | |
| 87 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification); | |
| 88 | |
| 89 // Start monitoring system power usage. | |
| 90 // This function may be called after a delay, see Start() for details. | |
| 91 void StartInternal(); | |
| 92 | |
| 93 void OnBatteryStatusUpdate(const device::BatteryStatus& status); | |
| 94 void OnRenderProcessNotification(int type, int rph_id); | |
| 95 | |
| 96 void DischargeStarted(double battery_level); | |
| 97 void WallPowerConnected(double battery_level); | |
| 98 | |
| 99 void CancelPendingHistogramReporting(); | |
| 100 | |
| 101 device::BatteryStatusService::BatteryUpdateCallback callback_; | |
| 102 scoped_ptr<device::BatteryStatusService::BatteryUpdateSubscription> | |
| 103 subscription_; | |
| 104 | |
| 105 NotificationRegistrar registrar_; | |
| 106 | |
| 107 scoped_ptr<SystemInterface> system_interface_; | |
| 108 | |
| 109 // True if monitoring was started (Start() called). | |
| 110 bool started_; | |
| 111 | |
| 112 // True if collecting metrics for the current discharge cycle e.g. if no | |
| 113 // renderers are open we don't keep track of discharge. | |
| 114 bool tracking_discharge_; | |
| 115 | |
| 116 // True if the system is running on battery power, false if on wall power. | |
| 117 bool on_battery_power_; | |
| 118 | |
| 119 // Battery level when wall power disconnected. [0.0, 1.0] - 0 if on wall | |
| 120 // power, 1 means fully charged. | |
| 121 double initial_battery_level_; | |
| 122 | |
| 123 // Current battery level. [0.0, 1.0] - 0 if on wall power, 1 means fully | |
| 124 // charged. | |
| 125 double current_battery_level_; | |
| 126 | |
| 127 // Timestamp when wall power was disconnected, null Time object otherwise. | |
| 128 base::Time start_discharge_time_; | |
| 129 | |
| 130 // IDs of live renderer processes. | |
| 131 base::hash_set<int> live_renderer_ids_; | |
| 132 | |
| 133 private: | |
| 134 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
| 135 }; | |
| 136 | |
| 137 } // namespace content | |
| 138 | |
| 139 #endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
| OLD | NEW |