Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 1 | # Copyright 2014-2015 ARM Limited |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | # pylint: disable=attribute-defined-outside-init |
| 16 | from devlib.module import Module |
| 17 | from devlib.utils.misc import memoized |
| 18 | from devlib.utils.types import integer, boolean |
| 19 | |
| 20 | |
| 21 | class CpuidleState(object): |
| 22 | |
| 23 | @property |
| 24 | def usage(self): |
| 25 | return integer(self.get('usage')) |
| 26 | |
| 27 | @property |
| 28 | def time(self): |
| 29 | return integer(self.get('time')) |
| 30 | |
| 31 | @property |
| 32 | def is_enabled(self): |
| 33 | return not boolean(self.get('disable')) |
| 34 | |
| 35 | @property |
| 36 | def ordinal(self): |
| 37 | i = len(self.id) |
| 38 | while self.id[i - 1].isdigit(): |
| 39 | i -= 1 |
| 40 | if not i: |
| 41 | raise ValueError('invalid idle state name: "{}"'.format(self.id)) |
| 42 | return int(self.id[i:]) |
| 43 | |
| 44 | def __init__(self, target, index, path): |
| 45 | self.target = target |
| 46 | self.index = index |
| 47 | self.path = path |
| 48 | self.id = self.target.path.basename(self.path) |
| 49 | self.cpu = self.target.path.basename(self.target.path.dirname(path)) |
Brendan Jackman | d3a02d9 | 2016-10-12 18:24:15 +0100 | [diff] [blame] | 50 | |
| 51 | @property |
| 52 | @memoized |
| 53 | def desc(self): |
| 54 | return self.get('desc') |
| 55 | |
| 56 | @property |
| 57 | @memoized |
| 58 | def name(self): |
| 59 | return self.get('name') |
| 60 | |
| 61 | @property |
| 62 | @memoized |
| 63 | def latency(self): |
Brendan Jackman | c9f7e0e | 2016-12-06 11:49:46 +0000 | [diff] [blame] | 64 | """Exit latency in uS""" |
Brendan Jackman | d3a02d9 | 2016-10-12 18:24:15 +0100 | [diff] [blame] | 65 | return self.get('latency') |
| 66 | |
| 67 | @property |
| 68 | @memoized |
| 69 | def power(self): |
Brendan Jackman | c9f7e0e | 2016-12-06 11:49:46 +0000 | [diff] [blame] | 70 | """Power usage in mW |
| 71 | |
| 72 | ..note:: |
| 73 | |
| 74 | This value is not always populated by the kernel and may be garbage. |
| 75 | """ |
Brendan Jackman | d3a02d9 | 2016-10-12 18:24:15 +0100 | [diff] [blame] | 76 | return self.get('power') |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 77 | |
Brendan Jackman | 7cc8675 | 2016-12-06 11:46:16 +0000 | [diff] [blame] | 78 | @property |
| 79 | @memoized |
| 80 | def target_residency(self): |
Brendan Jackman | c9f7e0e | 2016-12-06 11:49:46 +0000 | [diff] [blame] | 81 | """Target residency in uS |
| 82 | |
| 83 | This is the amount of time in the state required to 'break even' on |
| 84 | power - the system should avoid entering the state for less time than |
| 85 | this. |
| 86 | """ |
Brendan Jackman | 7cc8675 | 2016-12-06 11:46:16 +0000 | [diff] [blame] | 87 | return self.get('residency') |
| 88 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 89 | def enable(self): |
| 90 | self.set('disable', 0) |
| 91 | |
| 92 | def disable(self): |
| 93 | self.set('disable', 1) |
| 94 | |
| 95 | def get(self, prop): |
| 96 | property_path = self.target.path.join(self.path, prop) |
| 97 | return self.target.read_value(property_path) |
| 98 | |
| 99 | def set(self, prop, value): |
| 100 | property_path = self.target.path.join(self.path, prop) |
| 101 | self.target.write_value(property_path, value) |
| 102 | |
| 103 | def __eq__(self, other): |
| 104 | if isinstance(other, CpuidleState): |
| 105 | return (self.name == other.name) and (self.desc == other.desc) |
| 106 | elif isinstance(other, basestring): |
| 107 | return (self.name == other) or (self.desc == other) |
| 108 | else: |
| 109 | return False |
| 110 | |
| 111 | def __ne__(self, other): |
| 112 | return not self.__eq__(other) |
| 113 | |
| 114 | def __str__(self): |
| 115 | return 'CpuidleState({}, {})'.format(self.name, self.desc) |
| 116 | |
| 117 | __repr__ = __str__ |
| 118 | |
| 119 | |
| 120 | class Cpuidle(Module): |
| 121 | |
| 122 | name = 'cpuidle' |
| 123 | root_path = '/sys/devices/system/cpu/cpuidle' |
| 124 | |
| 125 | @staticmethod |
| 126 | def probe(target): |
| 127 | return target.file_exists(Cpuidle.root_path) |
| 128 | |
| 129 | def get_driver(self): |
| 130 | return self.target.read_value(self.target.path.join(self.root_path, 'current_driver')) |
| 131 | |
| 132 | def get_governor(self): |
| 133 | return self.target.read_value(self.target.path.join(self.root_path, 'current_governor_ro')) |
| 134 | |
| 135 | @memoized |
| 136 | def get_states(self, cpu=0): |
| 137 | if isinstance(cpu, int): |
| 138 | cpu = 'cpu{}'.format(cpu) |
| 139 | states_dir = self.target.path.join(self.target.path.dirname(self.root_path), cpu, 'cpuidle') |
| 140 | idle_states = [] |
| 141 | for state in self.target.list_directory(states_dir): |
| 142 | if state.startswith('state'): |
| 143 | index = int(state[5:]) |
| 144 | idle_states.append(CpuidleState(self.target, index, self.target.path.join(states_dir, state))) |
| 145 | return idle_states |
| 146 | |
| 147 | def get_state(self, state, cpu=0): |
| 148 | if isinstance(state, int): |
| 149 | try: |
Michele Di Giorgio | f9cb932 | 2016-08-01 10:41:36 +0100 | [diff] [blame] | 150 | return self.get_states(cpu)[state] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 151 | except IndexError: |
| 152 | raise ValueError('Cpuidle state {} does not exist'.format(state)) |
| 153 | else: # assume string-like |
| 154 | for s in self.get_states(cpu): |
| 155 | if state in [s.id, s.name, s.desc]: |
| 156 | return s |
| 157 | raise ValueError('Cpuidle state {} does not exist'.format(state)) |
| 158 | |
| 159 | def enable(self, state, cpu=0): |
| 160 | self.get_state(state, cpu).enable() |
| 161 | |
| 162 | def disable(self, state, cpu=0): |
| 163 | self.get_state(state, cpu).disable() |
| 164 | |
| 165 | def enable_all(self, cpu=0): |
| 166 | for state in self.get_states(cpu): |
| 167 | state.enable() |
| 168 | |
| 169 | def disable_all(self, cpu=0): |
| 170 | for state in self.get_states(cpu): |
| 171 | state.disable() |
| 172 | |
Brendan Jackman | 6cdae6b | 2016-11-01 18:21:49 +0000 | [diff] [blame] | 173 | def perturb_cpus(self): |
| 174 | """ |
| 175 | Momentarily wake each CPU. Ensures cpu_idle events in trace file. |
| 176 | """ |
| 177 | output = self.target._execute_util('cpuidle_wake_all_cpus') |
| 178 | print(output) |