提交 2580f7e4 编写于 作者: C Cleber Rosa

CPU Utils: use boolean for enable/disable idle states

Instead of using integers or even strings, let's use a more Pythonic
interface to disable/enable idle states.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 7a57b6e9
...@@ -220,12 +220,39 @@ def get_cpuidle_state(): ...@@ -220,12 +220,39 @@ def get_cpuidle_state():
return cpu_idlestate return cpu_idlestate
def set_cpuidle_state(state_number="all", disable=1, setstate=None): def _bool_to_binary(value):
'''
Turns a boolean value (True or False) into data suitable for writing to
/proc/* and /sys/* files.
'''
if value is True:
return b'1'
if value is False:
return b'0'
raise TypeError('Value is not a boolean: %s', value)
def _legacy_disable(value):
'''
Support for the original acceptable disable parameter values
TODO: this should be removed in the near future
Reference: https://trello.com/c/aJzNUeA5/
'''
if value is 0:
return b'0'
if value is 1:
return b'1'
return _bool_to_binary(value)
def set_cpuidle_state(state_number="all", disable=True, setstate=None):
""" """
Set/Reset cpu idle states for all cpus Set/Reset cpu idle states for all cpus
:param state_number: cpuidle state number, default: `all` all states :param state_number: cpuidle state number, default: `all` all states
:param disable: whether to disable/enable given cpu idle state, default: 1 :param disable: whether to disable/enable given cpu idle state,
default is to disable (True)
:param setstate: cpuidle state value, output of `get_cpuidle_state()` :param setstate: cpuidle state value, output of `get_cpuidle_state()`
""" """
cpus_list = cpu_online_list() cpus_list = cpu_online_list()
...@@ -238,8 +265,9 @@ def set_cpuidle_state(state_number="all", disable=1, setstate=None): ...@@ -238,8 +265,9 @@ def set_cpuidle_state(state_number="all", disable=1, setstate=None):
for cpu in cpus_list: for cpu in cpus_list:
for state_no in states: for state_no in states:
state_file = "/sys/devices/system/cpu/cpu%s/cpuidle/state%s/disable" % (cpu, state_no) state_file = "/sys/devices/system/cpu/cpu%s/cpuidle/state%s/disable" % (cpu, state_no)
disable = _legacy_disable(disable)
try: try:
open(state_file, "wb").write(bytes(disable)) open(state_file, "wb").write(disable)
except IOError as err: except IOError as err:
logging.warning("Failed to set idle state on cpu %s " logging.warning("Failed to set idle state on cpu %s "
"for state %s:\n%s", cpu, state_no, err) "for state %s:\n%s", cpu, state_no, err)
...@@ -247,8 +275,9 @@ def set_cpuidle_state(state_number="all", disable=1, setstate=None): ...@@ -247,8 +275,9 @@ def set_cpuidle_state(state_number="all", disable=1, setstate=None):
for cpu, stateval in setstate.items(): for cpu, stateval in setstate.items():
for state_no, value in stateval.items(): for state_no, value in stateval.items():
state_file = "/sys/devices/system/cpu/cpu%s/cpuidle/state%s/disable" % (cpu, state_no) state_file = "/sys/devices/system/cpu/cpu%s/cpuidle/state%s/disable" % (cpu, state_no)
disable = _legacy_disable(value)
try: try:
open(state_file, "wb").write(bytes(value)) open(state_file, "wb").write(disable)
except IOError as err: except IOError as err:
logging.warning("Failed to set idle state on cpu %s " logging.warning("Failed to set idle state on cpu %s "
"for state %s:\n%s", cpu, state_no, err) "for state %s:\n%s", cpu, state_no, err)
...@@ -549,7 +549,7 @@ uarch : sifive,rocket0 ...@@ -549,7 +549,7 @@ uarch : sifive,rocket0
with mock.patch('glob.glob', return_value=['/sys/devices/system/cpu/cpu0/cpuidle/state1']): with mock.patch('glob.glob', return_value=['/sys/devices/system/cpu/cpu0/cpuidle/state1']):
with mock.patch('avocado.utils.cpu.open', return_value=output): with mock.patch('avocado.utils.cpu.open', return_value=output):
cpu.set_cpuidle_state() cpu.set_cpuidle_state()
self.assertEqual(output.getvalue(), "1") self.assertEqual(output.getvalue(), b'1')
@unittest.skipUnless(recent_mock(), @unittest.skipUnless(recent_mock(),
"mock library version cannot (easily) patch open()") "mock library version cannot (easily) patch open()")
...@@ -558,8 +558,8 @@ uarch : sifive,rocket0 ...@@ -558,8 +558,8 @@ uarch : sifive,rocket0
with mock.patch('avocado.utils.cpu.cpu_online_list', return_value=[0]): with mock.patch('avocado.utils.cpu.cpu_online_list', return_value=[0]):
with mock.patch('glob.glob', return_value=['/sys/devices/system/cpu/cpu0/cpuidle/state2']): with mock.patch('glob.glob', return_value=['/sys/devices/system/cpu/cpu0/cpuidle/state2']):
with mock.patch('avocado.utils.cpu.open', return_value=output): with mock.patch('avocado.utils.cpu.open', return_value=output):
cpu.set_cpuidle_state(disable='0', state_number='2') cpu.set_cpuidle_state(disable=False, state_number='2')
self.assertEqual(output.getvalue(), "0") self.assertEqual(output.getvalue(), b'0')
@unittest.skipUnless(recent_mock(), @unittest.skipUnless(recent_mock(),
"mock library version cannot (easily) patch open()") "mock library version cannot (easily) patch open()")
...@@ -569,7 +569,7 @@ uarch : sifive,rocket0 ...@@ -569,7 +569,7 @@ uarch : sifive,rocket0
with mock.patch('glob.glob', return_value=['/sys/devices/system/cpu/cpu0/cpuidle/state1']): with mock.patch('glob.glob', return_value=['/sys/devices/system/cpu/cpu0/cpuidle/state1']):
with mock.patch('avocado.utils.cpu.open', return_value=output): with mock.patch('avocado.utils.cpu.open', return_value=output):
cpu.set_cpuidle_state(setstate={0: {0: 1}, 2: {0: 0}}) cpu.set_cpuidle_state(setstate={0: {0: 1}, 2: {0: 0}})
self.assertEqual(output.getvalue(), "10") self.assertEqual(output.getvalue(), b'10')
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册