[arch] fix -> not restoring the CPUs to previous scaling governors after the package is built when optimizations are on

This commit is contained in:
Vinicius Moreira
2021-04-04 10:27:35 -03:00
parent 6685e0b6c3
commit e67e5bc0a9
4 changed files with 65 additions and 27 deletions

View File

@@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixes ### Fixes
- Arch - Arch
- not skipping dependency checking when the user opts to proceed with a transaction that would break other packages - not skipping dependency checking when the user opts to proceed with a transaction that would break other packages
- AUR: not restoring the CPUs to previous scaling governors after the package is built when optimizations are on
## [0.9.15] 2021-03-03 ## [0.9.15] 2021-03-03
### Improvements ### Improvements

View File

@@ -218,7 +218,7 @@ suggestions:
c) `ccache` will be added to `BUILDENV` if it is installed on the system and already not defined c) `ccache` will be added to `BUILDENV` if it is installed on the system and already not defined
d) set the device processors to performance mode d) set the device CPUs to performance scaling governor
Obs: For more information about them, have a look at [Makepkg](https://wiki.archlinux.org/index.php/Makepkg) Obs: For more information about them, have a look at [Makepkg](https://wiki.archlinux.org/index.php/Makepkg)

View File

@@ -1944,13 +1944,12 @@ class ArchManager(SoftwareManager):
# building main package # building main package
context.watcher.change_substatus(self.i18n['arch.building.package'].format(bold(context.name))) context.watcher.change_substatus(self.i18n['arch.building.package'].format(bold(context.name)))
optimize = bool(context.config['optimize']) and cpu_manager.supports_performance_mode() and not cpu_manager.all_in_performance() optimize = bool(context.config['optimize']) and cpu_manager.supports_performance_mode()
cpus_changed, cpu_prev_governors = False, None
cpu_optimized = False
if optimize: if optimize:
self.logger.info("Setting cpus to performance mode") cpus_changed, cpu_prev_governors = cpu_manager.set_all_cpus_to('performance', context.root_password, self.logger)
cpu_manager.set_mode('performance', context.root_password)
cpu_optimized = True
try: try:
pkgbuilt, output = makepkg.make(pkgdir=context.project_dir, pkgbuilt, output = makepkg.make(pkgdir=context.project_dir,
@@ -1958,9 +1957,9 @@ class ArchManager(SoftwareManager):
handler=context.handler, handler=context.handler,
custom_pkgbuild=context.custom_pkgbuild_path) custom_pkgbuild=context.custom_pkgbuild_path)
finally: finally:
if cpu_optimized: if cpus_changed and cpu_prev_governors:
self.logger.info("Setting cpus to powersave mode") self.logger.info("Restoring CPU governors")
cpu_manager.set_mode('powersave', context.root_password) cpu_manager.set_cpus(cpu_prev_governors, context.root_password, {'performance'}, self.logger)
self._update_progress(context, 65) self._update_progress(context, 65)

View File

@@ -1,6 +1,8 @@
import multiprocessing import multiprocessing
import os import os
import traceback import traceback
from logging import Logger
from typing import Optional, Set, Tuple, Dict
from bauh.commons.system import new_root_subprocess from bauh.commons.system import new_root_subprocess
@@ -9,30 +11,67 @@ def supports_performance_mode():
return os.path.exists('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor') return os.path.exists('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor')
def all_in_performance() -> bool: def current_governors() -> Dict[str, Set[int]]:
for i in range(multiprocessing.cpu_count()): governors = {}
with open('/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor'.format(i)) as f: for cpu in range(multiprocessing.cpu_count()):
if f.read().strip() != 'performance': with open('/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor'.format(cpu)) as f:
return False gov = f.read().strip()
cpus = governors.get(gov, set())
cpus.add(cpu)
governors[gov] = cpus
return False return governors
def set_mode(mode: str, root_password: str): def set_governor(governor: str, root_password: str, cpu_idxs: Optional[Set[int]] = None):
new_gov_file = '/tmp/bauh_scaling_governor' new_gov_file = '/tmp/bauh_scaling_governor'
with open(new_gov_file, 'w+') as f: with open(new_gov_file, 'w+') as f:
f.write(mode) f.write(governor)
for i in range(multiprocessing.cpu_count()): for idx in (cpu_idxs if cpu_idxs else range(multiprocessing.cpu_count())):
_change_governor(idx, new_gov_file, root_password)
if os.path.exists(new_gov_file):
try: try:
gov_file = '/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor'.format(i) os.remove(new_gov_file)
replace = new_root_subprocess(['cp', new_gov_file, gov_file], root_password=root_password)
replace.wait()
except: except:
traceback.print_exc() traceback.print_exc()
if os.path.exists(new_gov_file):
try: def _change_governor(cpu_idx: int, new_gov_file_path: str, root_password: str):
os.remove(new_gov_file) try:
except: gov_file = '/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor'.format(cpu_idx)
traceback.print_exc() replace = new_root_subprocess(['cp', new_gov_file_path, gov_file], root_password=root_password)
replace.wait()
except:
traceback.print_exc()
def set_all_cpus_to(governor: str, root_password: str, logger: Optional[Logger] = None) -> Tuple[bool, Optional[Dict[str, Set[int]]]]:
"""
"""
cpus_changed, cpu_governors = False, current_governors()
if cpu_governors:
not_in_performance = set()
for gov, cpus in cpu_governors.items():
if gov != governor:
not_in_performance.update(cpus)
if not_in_performance:
if logger:
logger.info("Changing CPUs {} governors to '{}'".format(not_in_performance, governor))
set_governor(governor, root_password, not_in_performance)
cpus_changed = True
return cpus_changed, cpu_governors
def set_cpus(governors: Dict[str, Set[int]], root_password: str, ignore_governors: Optional[Set[str]] = None, logger: Optional[Logger] = None):
for gov, cpus in governors.items():
if not ignore_governors or gov not in ignore_governors:
if logger:
logger.info("Changing CPUs {} governors to '{}'".format(cpus, gov))
set_governor(gov, root_password, cpus)