Commit 4748f66b authored by tmandel@chromium.org's avatar tmandel@chromium.org

[Telemetry] Chrome OS gathers power and time in one command.

In order to ensure that the time of each sample is as accurate as
possible to report valid power usage, gather time and power as
a single shell command instead of two.

BUG=

Review URL: https://codereview.chromium.org/485003004

Cr-Commit-Position: refs/heads/master@{#291544}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@291544 0039d316-1c4b-4281-b951-d872f2087c98
parent 8143565e
......@@ -22,14 +22,12 @@ class CrosPowerMonitor(sysfs_power_monitor.SysfsPowerMonitor):
Attributes:
_cri: The Chrome interface.
_end_time: The epoch time at which the test finishes executing.
_initial_power: The result of 'power_supply_info' before the test.
_start_time: The epoch time at which the test starts executing.
"""
super(CrosPowerMonitor, self).__init__(
cros_sysfs_platform.CrosSysfsPlatform(cri))
self._cri = cri
self._end_time = None
self._initial_power = None
self._start_time = None
......@@ -40,21 +38,41 @@ class CrosPowerMonitor(sysfs_power_monitor.SysfsPowerMonitor):
def StartMonitoringPower(self, browser):
super(CrosPowerMonitor, self).StartMonitoringPower(browser)
if self._IsOnBatteryPower():
self._initial_power = self._cri.RunCmdOnDevice(['power_supply_info'])[0]
self._start_time = int(self._cri.RunCmdOnDevice(['date', '+%s'])[0])
sample = self._cri.RunCmdOnDevice(
['power_supply_info;', 'date', '+%s'])[0]
self._initial_power, self._start_time = CrosPowerMonitor.SplitSample(
sample)
def StopMonitoringPower(self):
cpu_stats = super(CrosPowerMonitor, self).StopMonitoringPower()
power_stats = {}
if self._IsOnBatteryPower():
final_power = self._cri.RunCmdOnDevice(['power_supply_info'])[0]
self._end_time = int(self._cri.RunCmdOnDevice(['date', '+%s'])[0])
sample = self._cri.RunCmdOnDevice(
['power_supply_info;', 'date', '+%s'])[0]
final_power, end_time = CrosPowerMonitor.SplitSample(sample)
# The length of the test is used to measure energy consumption.
length_h = (self._end_time - self._start_time) / 3600.0
length_h = (end_time - self._start_time) / 3600.0
power_stats = CrosPowerMonitor.ParsePower(self._initial_power,
final_power, length_h)
return CrosPowerMonitor.CombineResults(cpu_stats, power_stats)
@staticmethod
def SplitSample(sample):
"""Splits a power and time sample into the two separate values.
Args:
sample: The result of calling 'power_supply_info; date +%s' on the
device.
Returns:
A tuple of power sample and epoch time of the sample.
"""
sample = sample.strip()
index = sample.rfind('\n')
power = sample[:index]
time = sample[index + 1:]
return power, int(time)
@staticmethod
def IsOnBatteryPower(status, board):
"""Determines if the devices is being charged.
......
......@@ -197,6 +197,12 @@ Device: Battery
self.assertAlmostEqual(results['power_samples_mw'][1],
self.expected_power['power_samples_mw'][1])
def testSplitSample(self):
sample = self.initial_power + '\n1408739546\n'
power, time = cros_power_monitor.CrosPowerMonitor.SplitSample(sample)
self.assertEqual(power, self.initial_power)
self.assertEqual(time, 1408739546)
def testCombineResults(self):
result = cros_power_monitor.CrosPowerMonitor.CombineResults(
self.expected_cpu, self.expected_power)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment