Chromium Code Reviews| Index: tools/telemetry/telemetry/core/platform/power_monitor/android_dumpsys_power_monitor.py |
| diff --git a/tools/telemetry/telemetry/core/platform/power_monitor/android_dumpsys_power_monitor.py b/tools/telemetry/telemetry/core/platform/power_monitor/android_dumpsys_power_monitor.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ed2894867d3ba36a87964a95001922f25a366778 |
| --- /dev/null |
| +++ b/tools/telemetry/telemetry/core/platform/power_monitor/android_dumpsys_power_monitor.py |
| @@ -0,0 +1,78 @@ |
| +# 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. |
| + |
| +import telemetry.core.platform.power_monitor as power_monitor |
| + |
| +import csv |
| + |
| +from collections import defaultdict |
| + |
| + |
| +class DumpsysPowerMonitor(power_monitor.PowerMonitor): |
|
jeremy
2014/04/03 11:16:30
Can you add a header comment, especially interesti
qsr
2014/04/03 12:02:43
Done.
|
| + def __init__(self, adb): |
|
jeremy
2014/04/03 11:16:30
Comment input parameters
qsr
2014/04/03 12:02:43
Done.
|
| + super(DumpsysPowerMonitor, self).__init__() |
| + self._adb = adb |
| + self._browser = None |
| + |
| + def CanMonitorPower(self): |
| + return self._adb.CanControlUsbCharging() |
| + |
| + def StartMonitoringPower(self, browser): |
| + assert not self._browser, ( |
| + 'Must call StopMonitoringPower().') |
| + self._browser = browser |
| + self._adb.DisableUsbCharging() |
|
jeremy
2014/04/03 11:16:30
What does this line do exactly? Can you add a comm
qsr
2014/04/03 12:02:43
Done.
|
| + |
| + def StopMonitoringPower(self): |
| + assert self._browser, ( |
| + 'StartMonitoringPower() not called.') |
| + try: |
| + self._adb.EnableUsbCharging() |
| + package = self._browser._browser_backend.package |
| + result = self._adb.RunShellCommand('dumpsys batterystats -c %s' % package) |
|
jeremy
2014/04/03 11:16:30
I take it that dumpsys measures from disable->enab
qsr
2014/04/03 12:02:43
Done.
|
| + assert result, 'DumpSys produced no output' |
|
jeremy
2014/04/03 11:16:30
nit: Dumpsys per capitalization in http://source.a
qsr
2014/04/03 12:02:43
Done.
|
| + return DumpsysPowerMonitor.ParseSamplingOutput(package, result) |
| + finally: |
| + self._browser = None |
| + |
| + @staticmethod |
| + def ParseSamplingOutput(package, dumpsys_output): |
|
jeremy
2014/04/03 11:16:30
Do you also get any kind of stat which could give
qsr
2014/04/03 12:02:43
Not at this moment. When/If android starts collect
|
| + """Parse output of 'dumpsys batterystats -c' |
| + |
| + Returns: |
| + Dictionary in the format returned by StopMonitoringPower(). |
| + """ |
| + # cvs columns |
|
jeremy
2014/04/03 11:16:30
nit: s/cvs/csv/
qsr
2014/04/03 12:02:43
Done.
|
| + DUMP_VERSION_INDEX = 0 |
| + COLUMN_TYPE_INDEX = 3 |
| + PACKAGE_UID_INDEX = 4 |
| + PWI_POWER_COMSUMPTION_INDEX = 5 |
| + PWI_UID_INDEX = 1 |
| + PWI_AGGREGATION_INDEX = 2 |
| + PWI_SUBTYPE_INDEX = 4 |
| + csvreader = csv.reader(dumpsys_output) |
| + entries_by_type = defaultdict(list) |
| + for entry in csvreader: |
| + if len(entry) < 4 or entry[DUMP_VERSION_INDEX] != '7': |
| + continue |
| + entries_by_type[entry[COLUMN_TYPE_INDEX]].append(entry) |
| + # Find the uid of for the given package |
|
jeremy
2014/04/03 11:16:30
nit: .
qsr
2014/04/03 12:02:43
Done.
|
| + assert package in entries_by_type, 'Expected package not found' |
| + assert len(entries_by_type[package]) == 1, 'Multiple entries for package.' |
| + uid = entries_by_type[package][0][PACKAGE_UID_INDEX] |
| + consumptions_mha = [float(entry[PWI_POWER_COMSUMPTION_INDEX]) |
|
jeremy
2014/04/03 11:16:30
_mah ?
qsr
2014/04/03 12:02:43
Done.
|
| + for entry in entries_by_type['pwi'] |
| + if entry[PWI_UID_INDEX] == uid and |
| + entry[PWI_AGGREGATION_INDEX] == 't' and |
| + entry[PWI_SUBTYPE_INDEX] == 'uid'] |
| + consumption_mah = sum(consumptions_mha) |
| + # Converting at a nominal voltage of 4.0V, as those values are obtained by a |
| + # heuristic, and 4.0V is the voltage we set when using a monsoon device. |
| + consumption_mwh = consumption_mah * 4.0 |
| + # Raw power usage samples. |
| + out_dict = {} |
| + out_dict['identifier'] = 'dumpsys' |
| + out_dict['power_samples_mw'] = [] |
| + out_dict['energy_consumption_mwh'] = consumption_mwh |
| + return out_dict |