Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / power_monitor / cros_sysfs_platform.py
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 from telemetry.core.platform.power_monitor import sysfs_platform
6
7
8 class CrosSysfsPlatform(sysfs_platform.SysfsPlatform):
9   """A SysfsPlatform implementation to be used for ChromeOS devices."""
10   def __init__(self, cri):
11     """Constructor.
12
13     Args:
14         cri: Chrome interface.
15     """
16     super(CrosSysfsPlatform, self).__init__()
17     self._cri = cri
18
19   def RunShellCommand(self, command):
20     return self._cri.RunCmdOnDevice(command.split())[0]
21
22   @staticmethod
23   def ParseStateSample(sample):
24     sample_stats = {}
25     for cpu in sample:
26       values = sample[cpu].splitlines()
27       # There are three values per state after excluding the single time value.
28       num_states = (len(values) - 1) / 3
29       names = values[:num_states]
30       times = values[num_states:2 * num_states]
31       latencies = values[2 * num_states:]
32       # The last line in the sample contains the time.
33       cstates = {'C0': int(values[-1]) * 10 ** 6}
34       for i, state in enumerate(names):
35         if names[i] == 'POLL' and not int(latencies[i]):
36           # C0 state. Kernel stats aren't right, so calculate by
37           # subtracting all other states from total time (using epoch
38           # timer since we calculate differences in the end anyway).
39           # NOTE: Only x86 lists C0 under cpuidle, ARM does not.
40           continue
41         cstates['C0'] -= int(times[i])
42         if names[i] == '<null>':
43           # Kernel race condition that can happen while a new C-state gets
44           # added (e.g. AC->battery). Don't know the 'name' of the state
45           # yet, but its 'time' would be 0 anyway.
46           continue
47         cstates[state] = int(times[i])
48       sample_stats[cpu] = cstates
49     return sample_stats