Commit 52d869a7 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron
Browse files

[SCRIPTS] Fix set_cpu_affinity CPUFreqRawFileSetter

parent ed4ae5fe
......@@ -131,11 +131,11 @@ class CPUFreqRawFileSetter:
@classmethod
def getAvailableGovernorsFile(klass, cpu):
return os.path.join(klass.getBaseDir(cpu), 'scaling_available_governor')
return os.path.join(klass.getBaseDir(cpu), 'scaling_available_governors')
@classmethod
def getAvailableGovernors(klass, cpu):
fname = klass.getAvailableGovernorsFile()
fname = klass.getAvailableGovernorsFile(cpu)
cmd = SystemCmd(['cat', fname], try_sudo=False)
cat = cmd.doPopen(stdout=PIPE, text=True)
return cat.stdout.readline().strip().split()
......@@ -144,7 +144,7 @@ class CPUFreqRawFileSetter:
def getCurrGovernor(klass, cpu_list):
governor_list = []
for cpu in cpu_list:
fname = klass.getScalingGovernorFile()
fname = klass.getScalingGovernorFile(cpu)
cmd = SystemCmd(['cat', fname], try_sudo=False)
cat = cmd.doPopen(stdout=PIPE, text=True)
governor_list.append(cat.stdout.readline().strip())
......@@ -154,7 +154,7 @@ class CPUFreqRawFileSetter:
def setCurrGovernor(klass, cpu_list, governor):
cpu_cmds = []
for cpu in cpu_list:
fname = klass.getScalingGovernorFile()
fname = klass.getScalingGovernorFile(cpu)
cpu_cmds.append(f'echo {governor} > {fname}')
bash_cmd = ' && '.join(cpu_cmds)
cmd = SystemCmd(['bash', '-c', bash_cmd])
......@@ -206,30 +206,28 @@ class CPUFreqMgr:
PerformanceGovernors = ['performance']
PowerSaveGovernors = ['ondemand', 'powersave']
def __init__(self, target_governors=None,
def __init__(self, target_governors=[None],
default_governors=PowerSaveGovernors, setter=None):
self.m_setter = setter or self.getSetter()
if target_governors:
if not self.m_setter:
raise RuntimeError('No available CPUFreq setter: '
'install cpufreq-utils or provide '
'sudo privileges')
available_targets = self.getAvailableGovernorsFrom(target_governors)
if not available_targets:
raise ValueError(f'None of the target governors '
f'{target_governors} is available')
self.m_target_governor = available_targets[0]
else:
if not target_governors or not target_governors[0]:
self.m_target_governor = None
return
elif not self.m_setter:
raise RuntimeError('No available CPUFreq setter: install '
'cpufreq-utils or provide sudo privileges')
available_targets = self.getAvailableGovernorsFrom(target_governors)
if not available_targets:
raise ValueError(f'None of the target governors '
f'{target_governors} is available')
self.m_target_governor = available_targets[0]
available_defaults = self.getAvailableGovernorsFrom(default_governors)
if not available_defaults:
raise ValueError(f'None of the default governors '
f'{default_governors} is available')
self.m_default_governor = available_defaults[0]
print(f'default_governor={self.m_default_governor}')
def getSetter(self):
for klass in [CPUFreqUtilsSetter, CPUFreqRawFileSetter]:
......@@ -244,14 +242,13 @@ class CPUFreqMgr:
return None
def getAvailableGovernorsFrom(self, governors):
available_governors = None
for cpu in range(SMPSystem.getNbCPUs()):
available_governors = self.m_setter.getAvailableGovernors(0)
for cpu in range(1, SMPSystem.getNbCPUs()):
available_for_this = self.m_setter.getAvailableGovernors(cpu)
if available_governors is None:
available_governors = available_for_this
else:
available_governors = [g for g in available_for_this
if g in available_governors]
available_governors = [g for g in available_for_this
if g in available_governors]
if not available_governors:
break
return [g for g in governors if g in available_governors]
def setAllGovernors(self, governor):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment