1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

module/cpuidle: Simplify Cpuidle.__init__

Replace stateful loop with a nested comprehension that makes obvious:
    * that self._states is a dict(cpu, [CpuidleState])
    * the sysfs folder being used and the constraint applied to make use
      of each level (i.e. which subfolder is used)
    * that the states are sorted by index

As a side effect:
    * Gracefully handle non-contiguous idle state names like "state0,
      state2" without a state1 (not sure if that can happen)
    * Remove some antipatterns while iterating over a dict and counting
      iterations.
This commit is contained in:
douglas-raillard-arm 2020-06-05 16:17:32 +01:00 committed by Marc Bonnici
parent ccde9de257
commit b717deb8e4

View File

@ -15,6 +15,9 @@
# pylint: disable=attribute-defined-outside-init # pylint: disable=attribute-defined-outside-init
from past.builtins import basestring from past.builtins import basestring
from operator import attrgetter
from pprint import pformat
from devlib.module import Module from devlib.module import Module
from devlib.utils.types import integer, boolean from devlib.utils.types import integer, boolean
@ -96,40 +99,35 @@ class Cpuidle(Module):
def __init__(self, target): def __init__(self, target):
super(Cpuidle, self).__init__(target) super(Cpuidle, self).__init__(target)
self._states = {}
basepath = '/sys/devices/system/cpu/' basepath = '/sys/devices/system/cpu/'
values_tree = self.target.read_tree_values(basepath, depth=4, check_exit_code=False) values_tree = self.target.read_tree_values(basepath, depth=4, check_exit_code=False)
i = 0
cpu_id = 'cpu{}'.format(i)
while cpu_id in values_tree:
cpu_node = values_tree[cpu_id]
if 'cpuidle' in cpu_node: self._states = {
idle_node = cpu_node['cpuidle'] cpu_name: sorted(
self._states[cpu_id] = [] (
j = 0 CpuidleState(
state_id = 'state{}'.format(j)
while state_id in idle_node:
state_node = idle_node[state_id]
state = CpuidleState(
self.target, self.target,
index=j, # state_name is formatted as "state42"
path=self.target.path.join(basepath, cpu_id, 'cpuidle', state_id), index=int(state_name[len('state'):]),
path=self.target.path.join(basepath, cpu_name, 'cpuidle', state_name),
name=state_node['name'], name=state_node['name'],
desc=state_node['desc'], desc=state_node['desc'],
power=int(state_node['power']), power=int(state_node['power']),
latency=int(state_node['latency']), latency=int(state_node['latency']),
residency=int(state_node['residency']) if 'residency' in state_node else None, residency=int(state_node['residency']) if 'residency' in state_node else None,
) )
msg = 'Adding {} state {}: {} {}' for state_name, state_node in cpu_node['cpuidle'].items()
self.logger.debug(msg.format(cpu_id, j, state.name, state.desc)) if state_name.startswith('state')
self._states[cpu_id].append(state) ),
j += 1 key=attrgetter('index'),
state_id = 'state{}'.format(j) )
i += 1 for cpu_name, cpu_node in values_tree.items()
cpu_id = 'cpu{}'.format(i) if cpu_name.startswith('cpu') and 'cpuidle' in cpu_node
}
self.logger.debug('Adding cpuidle states:\n{}'.format(pformat(self._states)))
def get_states(self, cpu=0): def get_states(self, cpu=0):
if isinstance(cpu, int): if isinstance(cpu, int):