summaryrefslogtreecommitdiffstats
path: root/content/browser/power_usage_monitor_impl.cc
blob: 65d4d9dd7a5f56bbe75f2546719c57faf3af5198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// 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 <stddef.h>
#include <utility>

#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::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_ = std::move(interface);
}

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