Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Unified Diff: content/browser/power_usage_monitor_impl.h

Issue 560553005: Battery impact UMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove debug code Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/chrome_browser_main.cc ('k') | content/browser/power_usage_monitor_impl.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/power_usage_monitor_impl.h
diff --git a/content/browser/power_usage_monitor_impl.h b/content/browser/power_usage_monitor_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..f6b31600e0a56c9fad560ed788bae6aac903f85f
--- /dev/null
+++ b/content/browser/power_usage_monitor_impl.h
@@ -0,0 +1,139 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_
+#define CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_
+
+#include "base/basictypes.h"
+#include "base/containers/hash_tables.h"
+#include "base/gtest_prod_util.h"
+#include "base/memory/singleton.h"
+#include "base/power_monitor/power_monitor.h"
+#include "base/time/time.h"
+#include "content/public/browser/browser_message_filter.h"
+#include "content/public/browser/notification_observer.h"
+#include "content/public/browser/notification_registrar.h"
+#include "device/battery/battery_status_service.h"
+
+namespace content {
+
+// Record statistics on power usage.
+//
+// Two main statics are recorded by this class:
+// * Power.BatteryDischarge_{5,15,30} - delta between battery level when
Daniel Erat 2014/11/06 18:56:19 (and then Power.BatteryDischargePercent_... for th
+// unplugged from wallpower, over the specified period - in minutes.
+// * Power.BatteryDischargeRateWhenUnplugged - the rate of battery discharge
+// from the device being unplugged until it's plugged back in, if said period
+// was longer than 30 minutes.
+//
+// Heuristics:
+// * Data collection starts after system uptime exceeds 30 minutes.
+// * If the machine goes to sleep or all renderers are closed then the current
+// measurement is cancelled.
+class PowerUsageMonitor : public base::PowerObserver,
+ public NotificationObserver {
+ public:
+ class SystemInterface {
+ public:
+ virtual ~SystemInterface() {}
+
+ virtual void ScheduleHistogramReport(base::TimeDelta delay) = 0;
+ virtual void CancelPendingHistogramReports() = 0;
+
+ // Record the battery discharge rate over a period when the system is
+ // on battery power. Rate refers to the amount the battery charge has
+ // decreased over a unit of time.
+ // Possible values [0,100].
+ // The number is scaled so that 0 signifies very slow discharge, arbitrarily
+ // defined as total battery discharge in 8 hours or more. 100 indicates
+ // a very quick discharge.
+ // This scale is arbitrary and only meant to provide sufficient precision to
+ // 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
+ virtual void RecordDischargeRate(int discharge_rate) = 0;
+
+ // Allow tests to override clock.
+ virtual base::Time Now() = 0;
+ };
+
+ public:
+ PowerUsageMonitor();
+ ~PowerUsageMonitor() override;
+
+ double discharge_amount() const {
+ return initial_battery_level_ - current_battery_level_;
+ }
+
+ // Start monitoring power usage.
+ // Note that the actual monitoring will be delayed until 30 minutes after
+ // system boot.
+ void Start();
+
+ void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface);
+
+ // Overridden from base::PowerObserver:
+ void OnPowerStateChange(bool on_battery_power) override;
+ void OnResume() override;
+ void OnSuspend() override;
+
+ // Overridden from NotificationObserver:
+ void Observe(int type,
+ const NotificationSource& source,
+ const NotificationDetails& details) override;
+
+ private:
+ friend class PowerUsageMonitorTest;
+ FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate);
+ FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification);
+
+ // Start monitoring system power usage.
+ // This function may be called after a delay, see Start() for details.
+ void StartInternal();
+
+ void OnBatteryStatusUpdate(const device::BatteryStatus& status);
+ void OnRenderProcessNotification(int type, int rph_id);
+
+ void DischargeStarted(double battery_level);
+ void WallPowerConnected(double battery_level);
+
+ void CancelPendingHistogramReporting();
+
+ device::BatteryStatusService::BatteryUpdateCallback callback_;
+ scoped_ptr<device::BatteryStatusService::BatteryUpdateSubscription>
+ subscription_;
+
+ NotificationRegistrar registrar_;
+
+ scoped_ptr<SystemInterface> system_interface_;
+
+ // True if monitoring was started (Start() called).
+ bool started_;
+
+ // True if collecting metrics for the current discharge cycle e.g. if no
+ // renderers are open we don't keep track of discharge.
+ bool tracking_discharge_;
+
+ // True if the system is running on battery power, false if on wall power.
+ bool on_battery_power_;
+
+ // Battery level when wall power disconnected. [0.0, 1.0] - 0 if on wall
+ // power, 1 means fully charged.
+ double initial_battery_level_;
+
+ // Current battery level. [0.0, 1.0] - 0 if on wall power, 1 means fully
+ // charged.
+ double current_battery_level_;
+
+ // Timestamp when wall power was disconnected, null Time object otherwise.
+ base::Time start_discharge_time_;
+
+ // IDs of live renderer processes.
+ base::hash_set<int> live_renderer_ids_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor);
+};
+
+} // namespace content
+
+#endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_
« no previous file with comments | « chrome/browser/chrome_browser_main.cc ('k') | content/browser/power_usage_monitor_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698