summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjeremy <jeremy@chromium.org>2014-11-17 04:09:33 -0800
committerCommit bot <commit-bot@chromium.org>2014-11-17 12:09:58 +0000
commit76a267615a7bc5fcb76f3a6c425c57eb99b66745 (patch)
treef76ccdec51c4839c298908d499dc2b641a94e472
parentc39ccf5724057a6671fef3c0dc3c1933c1668ba8 (diff)
downloadchromium_src-76a267615a7bc5fcb76f3a6c425c57eb99b66745.zip
chromium_src-76a267615a7bc5fcb76f3a6c425c57eb99b66745.tar.gz
chromium_src-76a267615a7bc5fcb76f3a6c425c57eb99b66745.tar.bz2
Report UMA stats on battery discharge rate when Chrome is running.
Stats reported: * Battery depletion percent over 5, 15 and 30 minute period. * Battery discharge rate if battery was discharged for more than 30 minutes. Reports are only generated after 30 minutes of uptime so as not to be affected by increased power draw at system startup. In-progress stat collection is cancelled on sleep or when all renderers are closed. BUG=418407 Review URL: https://codereview.chromium.org/560553005 Cr-Commit-Position: refs/heads/master@{#304412}
-rw-r--r--chrome/browser/chrome_browser_main.cc6
-rw-r--r--content/browser/power_usage_monitor_impl.cc276
-rw-r--r--content/browser/power_usage_monitor_impl.h133
-rw-r--r--content/browser/power_usage_monitor_impl_unittest.cc167
-rw-r--r--content/content_browser.gypi2
-rw-r--r--content/content_tests.gypi7
-rw-r--r--content/public/browser/power_usage_monitor.h17
-rw-r--r--tools/metrics/histograms/histograms.xml60
8 files changed, 667 insertions, 1 deletions
diff --git a/chrome/browser/chrome_browser_main.cc b/chrome/browser/chrome_browser_main.cc
index 3f65f35..61f32ff 100644
--- a/chrome/browser/chrome_browser_main.cc
+++ b/chrome/browser/chrome_browser_main.cc
@@ -119,6 +119,7 @@
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
+#include "content/public/browser/power_usage_monitor.h"
#include "content/public/browser/site_instance.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
@@ -1594,6 +1595,11 @@ int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() {
}
browser_creator_.reset();
+#if !defined(OS_LINUX) || defined(OS_CHROMEOS) // http://crbug.com/426393
+ if (g_browser_process->metrics_service()->reporting_active())
+ content::StartPowerUsageMonitor();
+#endif
+
process_power_collector_.reset(new ProcessPowerCollector);
process_power_collector_->Initialize();
#endif // !defined(OS_ANDROID)
diff --git a/content/browser/power_usage_monitor_impl.cc b/content/browser/power_usage_monitor_impl.cc
new file mode 100644
index 0000000..fb294f3
--- /dev/null
+++ b/content/browser/power_usage_monitor_impl.cc
@@ -0,0 +1,276 @@
+// 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.
+
+#include "content/browser/power_usage_monitor_impl.h"
+
+#include "base/bind.h"
+#include "base/lazy_instance.h"
+#include "base/logging.h"
+#include "base/macros.h"
+#include "base/metrics/histogram.h"
+#include "base/strings/stringprintf.h"
+#include "base/sys_info.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/notification_service.h"
+#include "content/public/browser/notification_source.h"
+#include "content/public/browser/notification_types.h"
+#include "content/public/browser/power_usage_monitor.h"
+#include "content/public/browser/render_process_host.h"
+
+namespace content {
+
+namespace {
+
+// Wait this long after power on before enabling power usage monitoring.
+const int kMinUptimeMinutes = 30;
+
+// Minimum discharge time after which we collect the discharge rate.
+const int kMinDischargeMinutes = 30;
+
+class PowerUsageMonitorSystemInterface
+ : public PowerUsageMonitor::SystemInterface {
+ public:
+ explicit PowerUsageMonitorSystemInterface(PowerUsageMonitor* owner)
+ : power_usage_monitor_(owner),
+ weak_ptr_factory_(this) {}
+ ~PowerUsageMonitorSystemInterface() override {}
+
+ void ScheduleHistogramReport(base::TimeDelta delay) override {
+ BrowserThread::PostDelayedTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(
+ &PowerUsageMonitorSystemInterface::ReportBatteryLevelHistogram,
+ weak_ptr_factory_.GetWeakPtr(),
+ Now(),
+ delay),
+ delay);
+ }
+
+ void CancelPendingHistogramReports() override {
+ weak_ptr_factory_.InvalidateWeakPtrs();
+ }
+
+ void RecordDischargePercentPerHour(int percent_per_hour) override {
+ UMA_HISTOGRAM_PERCENTAGE("Power.BatteryDischargePercentPerHour",
+ percent_per_hour);
+ }
+
+ base::Time Now() override { return base::Time::Now(); }
+
+ protected:
+ void ReportBatteryLevelHistogram(base::Time start_time,
+ base::TimeDelta discharge_time) {
+ // It's conceivable that the code to cancel pending histogram reports on
+ // system suspend, will only get called after the system has woken up.
+ // To mitigage this, check whether more time has passed than expected and
+ // abort histogram recording in this case.
+
+ // Delayed tasks are subject to timer coalescing and can fire anywhere from
+ // delay -> delay * 1.5) . In most cases, the OS should fire the task
+ // at the next wakeup and not as late as it can.
+ // A threshold of 2 minutes is used, since that should be large enough to
+ // take the slop factor due to coalescing into account.
+ base::TimeDelta threshold = discharge_time +
+ base::TimeDelta::FromMinutes(2);
+ if ((Now() - start_time) > threshold) {
+ return;
+ }
+
+ const std::string histogram_name = base::StringPrintf(
+ "Power.BatteryDischarge_%d", discharge_time.InMinutes());
+ base::HistogramBase* histogram =
+ base::Histogram::FactoryGet(histogram_name,
+ 1,
+ 100,
+ 101,
+ base::Histogram::kUmaTargetedHistogramFlag);
+ double discharge_amount = power_usage_monitor_->discharge_amount();
+ histogram->Add(discharge_amount * 100);
+ }
+
+ private:
+ PowerUsageMonitor* power_usage_monitor_; // Not owned.
+
+ // Used to cancel in progress delayed tasks.
+ base::WeakPtrFactory<PowerUsageMonitorSystemInterface> weak_ptr_factory_;
+};
+
+} // namespace
+
+void StartPowerUsageMonitor() {
+ static base::LazyInstance<PowerUsageMonitor>::Leaky monitor =
+ LAZY_INSTANCE_INITIALIZER;
+ monitor.Get().Start();
+}
+
+PowerUsageMonitor::PowerUsageMonitor()
+ : callback_(base::Bind(&PowerUsageMonitor::OnBatteryStatusUpdate,
+ base::Unretained(this))),
+ system_interface_(new PowerUsageMonitorSystemInterface(this)),
+ started_(false),
+ tracking_discharge_(false),
+ on_battery_power_(false),
+ initial_battery_level_(0),
+ current_battery_level_(0) {
+}
+
+PowerUsageMonitor::~PowerUsageMonitor() {
+ if (started_)
+ base::PowerMonitor::Get()->RemoveObserver(this);
+}
+
+void PowerUsageMonitor::Start() {
+ // Power monitoring may be delayed based on uptime, but renderer process
+ // lifetime tracking needs to start immediately so processes created before
+ // then are accounted for.
+ registrar_.Add(this,
+ NOTIFICATION_RENDERER_PROCESS_CREATED,
+ NotificationService::AllBrowserContextsAndSources());
+ registrar_.Add(this,
+ NOTIFICATION_RENDERER_PROCESS_CLOSED,
+ NotificationService::AllBrowserContextsAndSources());
+ subscription_ =
+ device::BatteryStatusService::GetInstance()->AddCallback(callback_);
+
+ // Delay initialization until the system has been up for a while.
+ // This is to mitigate the effect of increased power draw during system start.
+ base::TimeDelta uptime =
+ base::TimeDelta::FromMilliseconds(base::SysInfo::Uptime());
+ base::TimeDelta min_uptime = base::TimeDelta::FromMinutes(kMinUptimeMinutes);
+ if (uptime < min_uptime) {
+ base::TimeDelta delay = min_uptime - uptime;
+ BrowserThread::PostDelayedTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&PowerUsageMonitor::StartInternal, base::Unretained(this)),
+ delay);
+ } else {
+ StartInternal();
+ }
+}
+
+void PowerUsageMonitor::StartInternal() {
+ DCHECK(!started_);
+ started_ = true;
+
+ // PowerMonitor is used to get suspend/resume notifications.
+ base::PowerMonitor::Get()->AddObserver(this);
+}
+
+void PowerUsageMonitor::DischargeStarted(double battery_level) {
+ on_battery_power_ = true;
+
+ // If all browser windows are closed, don't report power metrics since
+ // Chrome's power draw is likely not significant.
+ if (live_renderer_ids_.empty())
+ return;
+
+ // Cancel any in-progress ReportBatteryLevelHistogram() calls.
+ system_interface_->CancelPendingHistogramReports();
+
+ tracking_discharge_ = true;
+ start_discharge_time_ = system_interface_->Now();
+
+ initial_battery_level_ = battery_level;
+ current_battery_level_ = battery_level;
+
+ const int kBatteryReportingIntervalMinutes[] = {5, 15, 30};
+ for (auto reporting_interval : kBatteryReportingIntervalMinutes) {
+ base::TimeDelta delay = base::TimeDelta::FromMinutes(reporting_interval);
+ system_interface_->ScheduleHistogramReport(delay);
+ }
+}
+
+void PowerUsageMonitor::WallPowerConnected(double battery_level) {
+ on_battery_power_ = false;
+
+ if (tracking_discharge_) {
+ DCHECK(!start_discharge_time_.is_null());
+ base::TimeDelta discharge_time =
+ system_interface_->Now() - start_discharge_time_;
+
+ if (discharge_time.InMinutes() > kMinDischargeMinutes) {
+ // Record the rate at which the battery discharged over the entire period
+ // the system was on battery power.
+ double discharge_hours = discharge_time.InSecondsF() / 3600.0;
+ int percent_per_hour =
+ floor(((discharge_amount() / discharge_hours) * 100.0) + 0.5);
+ system_interface_->RecordDischargePercentPerHour(percent_per_hour);
+ }
+ }
+
+ // Cancel any in-progress ReportBatteryLevelHistogram() calls.
+ system_interface_->CancelPendingHistogramReports();
+
+ initial_battery_level_ = 0;
+ current_battery_level_ = 0;
+ start_discharge_time_ = base::Time();
+ tracking_discharge_ = false;
+}
+
+void PowerUsageMonitor::OnBatteryStatusUpdate(
+ const device::BatteryStatus& status) {
+ bool now_on_battery_power = (status.charging == 0);
+ bool was_on_battery_power = on_battery_power_;
+ double battery_level = status.level;
+
+ if (now_on_battery_power == was_on_battery_power) {
+ if (now_on_battery_power)
+ current_battery_level_ = battery_level;
+ return;
+ } else if (now_on_battery_power) { // Wall power disconnected.
+ DischargeStarted(battery_level);
+ } else { // Wall power connected.
+ WallPowerConnected(battery_level);
+ }
+}
+
+void PowerUsageMonitor::OnRenderProcessNotification(int type, int rph_id) {
+ size_t previous_num_live_renderers = live_renderer_ids_.size();
+
+ if (type == NOTIFICATION_RENDERER_PROCESS_CREATED) {
+ live_renderer_ids_.insert(rph_id);
+ } else if (type == NOTIFICATION_RENDERER_PROCESS_CLOSED) {
+ live_renderer_ids_.erase(rph_id);
+ } else {
+ NOTREACHED() << "Unexpected notification type: " << type;
+ }
+
+ if (live_renderer_ids_.empty() && previous_num_live_renderers != 0) {
+ // All render processes have died.
+ CancelPendingHistogramReporting();
+ tracking_discharge_ = false;
+ }
+
+}
+
+void PowerUsageMonitor::SetSystemInterfaceForTest(
+ scoped_ptr<SystemInterface> interface) {
+ system_interface_ = interface.Pass();
+}
+
+void PowerUsageMonitor::OnPowerStateChange(bool on_battery_power) {
+}
+
+void PowerUsageMonitor::OnResume() {
+}
+
+void PowerUsageMonitor::OnSuspend() {
+ CancelPendingHistogramReporting();
+}
+
+void PowerUsageMonitor::Observe(int type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr();
+ OnRenderProcessNotification(type, rph->GetID());
+}
+
+void PowerUsageMonitor::CancelPendingHistogramReporting() {
+ // Cancel any in-progress histogram reports and reporting of discharge UMA.
+ system_interface_->CancelPendingHistogramReports();
+}
+
+} // namespace content
diff --git a/content/browser/power_usage_monitor_impl.h b/content/browser/power_usage_monitor_impl.h
new file mode 100644
index 0000000..06216bb
--- /dev/null
+++ b/content/browser/power_usage_monitor_impl.h
@@ -0,0 +1,133 @@
+// 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/common/content_export.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
+// 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 CONTENT_EXPORT 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 percent per hour over the time the system
+ // is on battery power, legal values [0,100].
+ virtual void RecordDischargePercentPerHour(int percent_per_hour) = 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_
diff --git a/content/browser/power_usage_monitor_impl_unittest.cc b/content/browser/power_usage_monitor_impl_unittest.cc
new file mode 100644
index 0000000..985b132
--- /dev/null
+++ b/content/browser/power_usage_monitor_impl_unittest.cc
@@ -0,0 +1,167 @@
+// 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.
+
+#include "content/browser/power_usage_monitor_impl.h"
+
+#include "content/public/browser/notification_types.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "device/battery/battery_monitor.mojom.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+// Dummy ID to identify a phantom RenderProcessHost in tests.
+const int kDummyRenderProcessHostID = 1;
+
+class SystemInterfaceForTest : public PowerUsageMonitor::SystemInterface {
+ public:
+ SystemInterfaceForTest()
+ : num_pending_histogram_reports_(0),
+ discharge_percent_per_hour_(0),
+ now_(base::Time::FromInternalValue(1000)) {}
+ ~SystemInterfaceForTest() override {}
+
+ int num_pending_histogram_reports() const {
+ return num_pending_histogram_reports_;
+ }
+
+ int discharge_percent_per_hour() const {
+ return discharge_percent_per_hour_;
+ }
+
+ void AdvanceClockSeconds(int seconds) {
+ now_ += base::TimeDelta::FromSeconds(seconds);
+ }
+
+ void AdvanceClockMinutes(int minutes) {
+ now_ += base::TimeDelta::FromMinutes(minutes);
+ }
+
+ void ScheduleHistogramReport(base::TimeDelta delay) override {
+ num_pending_histogram_reports_++;
+ }
+
+ void CancelPendingHistogramReports() override {
+ num_pending_histogram_reports_ = 0;
+ }
+
+ void RecordDischargePercentPerHour(int percent_per_hour) override {
+ discharge_percent_per_hour_ = percent_per_hour;
+ }
+
+ base::Time Now() override { return now_; }
+
+ private:
+ int num_pending_histogram_reports_;
+ int discharge_percent_per_hour_;
+ base::Time now_;
+};
+
+class PowerUsageMonitorTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ monitor_.reset(new PowerUsageMonitor);
+ // PowerUsageMonitor assumes ownership.
+ scoped_ptr<SystemInterfaceForTest> test_interface(
+ new SystemInterfaceForTest());
+ system_interface_ = test_interface.get();
+ monitor_->SetSystemInterfaceForTest(test_interface.Pass());
+
+ // Without live renderers, the monitor won't do anything.
+ monitor_->OnRenderProcessNotification(NOTIFICATION_RENDERER_PROCESS_CREATED,
+ kDummyRenderProcessHostID);
+ }
+
+ void UpdateBatteryStatus(bool charging, double battery_level) {
+ device::BatteryStatus battery_status;
+ battery_status.charging = charging;
+ battery_status.level = battery_level;
+ monitor_->OnBatteryStatusUpdate(battery_status);
+ }
+
+ void KillTestRenderer() {
+ monitor_->OnRenderProcessNotification(
+ NOTIFICATION_RENDERER_PROCESS_CLOSED, kDummyRenderProcessHostID);
+ }
+
+ scoped_ptr<PowerUsageMonitor> monitor_;
+ SystemInterfaceForTest* system_interface_;
+ TestBrowserThreadBundle thread_bundle_;
+};
+
+TEST_F(PowerUsageMonitorTest, StartStopQuickly) {
+ // Going on battery power.
+ UpdateBatteryStatus(false, 1.0);
+ int initial_num_histogram_reports =
+ system_interface_->num_pending_histogram_reports();
+ ASSERT_GT(initial_num_histogram_reports, 0);
+
+ // Battery level goes down a bit.
+ system_interface_->AdvanceClockSeconds(1);
+ UpdateBatteryStatus(false, 0.9);
+ ASSERT_EQ(initial_num_histogram_reports,
+ system_interface_->num_pending_histogram_reports());
+ ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
+
+ // Wall power connected.
+ system_interface_->AdvanceClockSeconds(30);
+ UpdateBatteryStatus(true, 0);
+ ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
+ ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
+}
+
+TEST_F(PowerUsageMonitorTest, DischargePercentReported) {
+ // Going on battery power.
+ UpdateBatteryStatus(false, 1.0);
+ int initial_num_histogram_reports =
+ system_interface_->num_pending_histogram_reports();
+ ASSERT_GT(initial_num_histogram_reports, 0);
+
+ // Battery level goes down a bit.
+ system_interface_->AdvanceClockSeconds(30);
+ UpdateBatteryStatus(false, 0.9);
+ ASSERT_EQ(initial_num_histogram_reports,
+ system_interface_->num_pending_histogram_reports());
+ ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
+
+ // Wall power connected.
+ system_interface_->AdvanceClockMinutes(31);
+ UpdateBatteryStatus(true, 0);
+ ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
+ ASSERT_GT(system_interface_->discharge_percent_per_hour(), 0);
+}
+
+TEST_F(PowerUsageMonitorTest, NoRenderersDisablesMonitoring) {
+ KillTestRenderer();
+
+ // Going on battery power.
+ UpdateBatteryStatus(false, 1.0);
+ ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
+ ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
+
+ // Wall power connected.
+ system_interface_->AdvanceClockSeconds(30);
+ UpdateBatteryStatus(true, 0.5);
+ ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
+ ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
+}
+
+TEST_F(PowerUsageMonitorTest, NoRenderersCancelsInProgressMonitoring) {
+ // Going on battery power.
+ UpdateBatteryStatus(false, 1.0);
+ ASSERT_GT(system_interface_->num_pending_histogram_reports(), 0);
+ ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
+
+ // All renderers killed.
+ KillTestRenderer();
+ ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
+
+ // Wall power connected.
+ system_interface_->AdvanceClockMinutes(31);
+ UpdateBatteryStatus(true, 0);
+ ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
+ ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
+}
+
+} // namespace content
diff --git a/content/content_browser.gypi b/content/content_browser.gypi
index 8bf540c..6c65fef 100644
--- a/content/content_browser.gypi
+++ b/content/content_browser.gypi
@@ -957,6 +957,8 @@
'browser/power_save_blocker_ozone.cc',
'browser/power_save_blocker_win.cc',
'browser/power_save_blocker_x11.cc',
+ 'browser/power_usage_monitor_impl.cc',
+ 'browser/power_usage_monitor_impl.h',
'browser/profiler_controller_impl.cc',
'browser/profiler_controller_impl.h',
'browser/profiler_message_filter.cc',
diff --git a/content/content_tests.gypi b/content/content_tests.gypi
index 79e7532..9ba42e1 100644
--- a/content/content_tests.gypi
+++ b/content/content_tests.gypi
@@ -357,7 +357,11 @@
'test_support_content',
'../base/base.gyp:test_support_base',
'../crypto/crypto.gyp:crypto',
+ '../device/battery/battery.gyp:device_battery',
+ '../device/battery/battery.gyp:device_battery_mojo_bindings',
'../mojo/edk/mojo_edk.gyp:mojo_common_test_support',
+ '../mojo/mojo_base.gyp:mojo_environment_chromium',
+ '../mojo/public/mojo_public.gyp:mojo_cpp_bindings',
'../net/net.gyp:net_test_support',
'../skia/skia.gyp:skia',
'../sql/sql.gyp:sql',
@@ -537,6 +541,7 @@
'browser/notification_service_impl_unittest.cc',
'browser/plugin_loader_posix_unittest.cc',
'browser/power_monitor_message_broadcaster_unittest.cc',
+ 'browser/power_usage_monitor_impl_unittest.cc',
'browser/power_profiler/power_profiler_service_unittest.cc',
'browser/quota/mock_quota_manager.cc',
'browser/quota/mock_quota_manager.h',
@@ -1258,7 +1263,7 @@
'renderer/pepper/pepper_file_chooser_host_unittest.cc',
'renderer/pepper/pepper_graphics_2d_host_unittest.cc',
'renderer/pepper/pepper_plugin_instance_throttler_unittest.cc',
- 'renderer/pepper/pepper_url_request_unittest.cc',
+ 'renderer/pepper/pepper_url_request_unittest.cc',
'renderer/pepper/plugin_power_saver_helper_browsertest.cc',
'renderer/render_thread_impl_browsertest.cc',
'renderer/render_view_browsertest.cc',
diff --git a/content/public/browser/power_usage_monitor.h b/content/public/browser/power_usage_monitor.h
new file mode 100644
index 0000000..f28b98c
--- /dev/null
+++ b/content/public/browser/power_usage_monitor.h
@@ -0,0 +1,17 @@
+// 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 CONTENT_PUBLIC_BROWSER_POWER_USAGE_MONITOR_H_
+#define CONTENT_PUBLIC_BROWSER_POWER_USAGE_MONITOR_H_
+
+#include "content/common/content_export.h"
+
+namespace content {
+
+// Start the PowerUsageMonitor.
+CONTENT_EXPORT void StartPowerUsageMonitor();
+
+} // namespace content
+
+#endif // CONTENT_PUBLIC_BROWSER_POWER_USAGE_MONITOR_H_
diff --git a/tools/metrics/histograms/histograms.xml b/tools/metrics/histograms/histograms.xml
index 044e9a0..c3b487f 100644
--- a/tools/metrics/histograms/histograms.xml
+++ b/tools/metrics/histograms/histograms.xml
@@ -25370,6 +25370,24 @@ Therefore, the affected-histogram name has to have at least one dot in it.
</summary>
</histogram>
+<histogram name="Power.BatteryDischargePercentPerHour" units="%">
+ <owner>jeremy@chromium.org</owner>
+ <summary>
+ The percentage of battery capacity used per hour relative to a full
+ battery. Reported once when the power adaptor is plugged back in after the
+ system is on battery power for more than 30 minutes. If at any point the
+ system is suspended or all Chrome renderers are closed the measurement is
+ not recorded. Anytime the user unplugs the power adaptor, a new measurement
+ will begin being recorded. Collection of this histogram only starts after 30
+ minutes of uptime at which point the clock starts (assuming the user is on
+ battery power at that point). The system will need to remain unplugged for
+ at least another 30 minutes in order for any measurement to be recorded.
+ Values are normalized to a percent per hour scale. This measurement is tied
+ tightly to hardware model/OS and is not comparable across different hardware
+ configurations.
+ </summary>
+</histogram>
+
<histogram name="Power.BatteryDischargeRate" units="mW">
<owner>derat@chromium.org</owner>
<summary>
@@ -25378,6 +25396,48 @@ Therefore, the affected-histogram name has to have at least one dot in it.
</summary>
</histogram>
+<histogram name="Power.BatteryDischargeRate_15" units="%">
+ <owner>jeremy@chromium.org</owner>
+ <summary>
+ The percent of depleted battery capacity relative to a full battery over
+ the first 15 minutes after battery power collection information starts.
+ Collection of this histogram only starts after 30 minutes of uptime at which
+ point the clock starts (assuming the user is on battery power at that
+ point). The system will need to remain unplugged for at least another 15
+ minutes in order for any measurement to be recorded. Values are normalized
+ to a percent per hour scale. This measurement is tied tightly to hardware
+ model/OS and is not comparable across different hardware configurations.
+ </summary>
+</histogram>
+
+<histogram name="Power.BatteryDischargeRate_30" units="%">
+ <owner>jeremy@chromium.org</owner>
+ <summary>
+ The percent of depleted battery capacity relative to a full battery over
+ the first 30 minutes after battery power collection information starts.
+ Collection of this histogram only starts after 30 minutes of uptime at which
+ point the clock starts (assuming the user is on battery power at that
+ point). The system will need to remain unplugged for at least another 30
+ minutes in order for any measurement to be recorded. Values are normalized
+ to a percent per hour scale. This measurement is tied tightly to hardware
+ model/OS and is not comparable across different hardware configurations.
+ </summary>
+</histogram>
+
+<histogram name="Power.BatteryDischargeRate_5" units="%">
+ <owner>jeremy@chromium.org</owner>
+ <summary>
+ The percent of depleted battery capacity relative to a full battery over
+ the first 5 minutes after battery power collection information starts.
+ Collection of this histogram only starts after 30 minutes of uptime at which
+ point the clock starts (assuming the user is on battery power at that
+ point). The system will need to remain unplugged for at least another 5
+ minutes in order for any measurement to be recorded. Values are normalized
+ to a percent per hour scale. This measurement is tied tightly to hardware
+ model/OS and is not comparable across different hardware configurations.
+ </summary>
+</histogram>
+
<histogram name="Power.BatteryDischargeRateWhileSuspended" units="mW">
<owner>derat@chromium.org</owner>
<summary>