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 16from devlib.module import Module 17from devlib.utils.misc import memoized 18from devlib.utils.types import integer, boolean 19 20 21class 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)) 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): 64 """Exit latency in uS""" 65 return self.get('latency') 66 67 @property 68 @memoized 69 def power(self): 70 """Power usage in mW 71 72 ..note:: 73 74 This value is not always populated by the kernel and may be garbage. 75 """ 76 return self.get('power') 77 78 @property 79 @memoized 80 def target_residency(self): 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 """ 87 return self.get('residency') 88 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 120class 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: 150 return self.get_states(cpu)[state] 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 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) 179