Skip to content

Commit

Permalink
Reformatted codebase (#325)
Browse files Browse the repository at this point in the history
All python scripts are now reformatted with 'black' using line length
of 100.

Co-authored-by: Akos Varady <akos.varady@ericsson.com>
  • Loading branch information
varaki and Akos Varady committed Dec 22, 2021
1 parent 42491b8 commit 3121e2f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 106 deletions.
86 changes: 33 additions & 53 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from shutil import which
from subprocess import getoutput, call, run, check_output, DEVNULL

sys.path.append('../')
sys.path.append("../")
from auto_cpufreq.power_helper import *

warnings.filterwarnings("ignore")
Expand Down Expand Up @@ -65,8 +65,9 @@ def file_stats():
auto_cpufreq_stats_file = open(auto_cpufreq_stats_path, "w")
sys.stdout = auto_cpufreq_stats_file

def get_config(config_file=''):
if not hasattr(get_config, 'dict'):

def get_config(config_file=""):
if not hasattr(get_config, "dict"):
get_config.dict = dict()

config = configparser.ConfigParser()
Expand All @@ -77,6 +78,7 @@ def get_config(config_file=''):

return get_config.dict


# get distro name
try:
dist_name = distro.id()
Expand All @@ -85,8 +87,12 @@ def get_config(config_file=''):
print("Warning: Cannot get distro name")
if os.path.exists("/etc/pop-os/os-release"):
print("Pop!_OS detected")
print("Pop!_OS uses a symbolic link for the os-release file, this causes issues and can be fixed by converting to a hard link")
print("Attempting to change symlink to hard link for /etc/os-release -> /etc/pop-os/os-release")
print(
"Pop!_OS uses a symbolic link for the os-release file, this causes issues and can be fixed by converting to a hard link"
)
print(
"Attempting to change symlink to hard link for /etc/os-release -> /etc/pop-os/os-release"
)

yN = input("Continue? [y/N] ")
if yN.lower() == "y":
Expand Down Expand Up @@ -238,6 +244,7 @@ def charging():
# we cannot determine discharging state, assume we are on powercable
return True


def get_avail_gov():
f = Path("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")
return f.read_text().strip().split(" ")
Expand Down Expand Up @@ -278,13 +285,9 @@ def cpufreqctl():
else:
# deploy cpufreqctl.auto-cpufreq script
if os.path.isfile("/usr/bin/cpufreqctl"):
shutil.copy(
SCRIPTS_DIR / "cpufreqctl.sh", "/usr/bin/cpufreqctl.auto-cpufreq"
)
shutil.copy(SCRIPTS_DIR / "cpufreqctl.sh", "/usr/bin/cpufreqctl.auto-cpufreq")
else:
shutil.copy(
SCRIPTS_DIR / "cpufreqctl.sh", "/usr/bin/cpufreqctl.auto-cpufreq"
)
shutil.copy(SCRIPTS_DIR / "cpufreqctl.sh", "/usr/bin/cpufreqctl.auto-cpufreq")


def cpufreqctl_restore():
Expand Down Expand Up @@ -312,17 +315,9 @@ def daemon_not_found():


def deploy_complete_msg():
print(
"\n"
+ "-" * 17
+ " auto-cpufreq daemon installed and running "
+ "-" * 17
+ "\n"
)
print("\n" + "-" * 17 + " auto-cpufreq daemon installed and running " + "-" * 17 + "\n")
print("To view live stats, run:\nauto-cpufreq --stats")
print(
"\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove"
)
print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove")
footer()


Expand Down Expand Up @@ -351,9 +346,7 @@ def deploy_daemon():
auto_cpufreq_stats_path.touch(exist_ok=True)

print("\n* Deploy auto-cpufreq install script")
shutil.copy(
SCRIPTS_DIR / "auto-cpufreq-install.sh", "/usr/bin/auto-cpufreq-install"
)
shutil.copy(SCRIPTS_DIR / "auto-cpufreq-install.sh", "/usr/bin/auto-cpufreq-install")

print("\n* Deploy auto-cpufreq remove script")
shutil.copy(SCRIPTS_DIR / "auto-cpufreq-remove.sh", "/usr/bin/auto-cpufreq-remove")
Expand All @@ -367,6 +360,7 @@ def deploy_daemon():

call("/usr/bin/auto-cpufreq-install", shell=True)


# remove auto-cpufreq daemon
def remove():

Expand Down Expand Up @@ -400,19 +394,13 @@ def remove():
# restore original cpufrectl script
cpufreqctl_restore()


def gov_check():
for gov in get_avail_gov():
if gov not in ALL_GOVERNORS:
print(
"\n"
+ "-" * 18
+ " Checking for necessary scaling governors "
+ "-" * 19
+ "\n"
)
sys.exit(
"ERROR:\n\nCouldn't find any of the necessary scaling governors.\n"
)
print("\n" + "-" * 18 + " Checking for necessary scaling governors " + "-" * 19 + "\n")
sys.exit("ERROR:\n\nCouldn't find any of the necessary scaling governors.\n")


# root check func
def root_check():
Expand Down Expand Up @@ -474,11 +462,8 @@ def set_powersave():
print(f'Setting to use: "{gov}" governor')
run(f"cpufreqctl.auto-cpufreq --governor --set={gov}", shell=True)
if (
Path(
"/sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference"
).exists()
and Path("/sys/devices/system/cpu/intel_pstate/hwp_dynamic_boost").exists()
is False
Path("/sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference").exists()
and Path("/sys/devices/system/cpu/intel_pstate/hwp_dynamic_boost").exists() is False
):
run("cpufreqctl.auto-cpufreq --epp --set=balance_power", shell=True)
print('Setting to use: "balance_power" EPP')
Expand Down Expand Up @@ -686,11 +671,8 @@ def set_performance():
shell=True,
)
if (
Path(
"/sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference"
).exists()
and Path("/sys/devices/system/cpu/intel_pstate/hwp_dynamic_boost").exists()
is False
Path("/sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference").exists()
and Path("/sys/devices/system/cpu/intel_pstate/hwp_dynamic_boost").exists() is False
):
run("cpufreqctl.auto-cpufreq --epp --set=balance_performance", shell=True)
print('Setting to use: "balance_performance" EPP')
Expand Down Expand Up @@ -949,10 +931,10 @@ def distro_info():
with open("/var/lib/snapd/hostfs/etc/os-release", "r") as searchfile:
for line in searchfile:
if line.startswith("NAME="):
dist = line[5:line.find("$")].strip('"')
dist = line[5 : line.find("$")].strip('"')
continue
elif line.startswith("VERSION="):
version = line[8:line.find("$")].strip('"')
version = line[8 : line.find("$")].strip('"')
continue
except PermissionError as e:
print(repr(e))
Expand Down Expand Up @@ -1002,9 +984,7 @@ def sysinfo():
print(f"CPU min frequency: {min_freq:.0f} MHz\n")

# get coreid's and frequencies of online cpus by parsing /proc/cpuinfo
coreid_info = getoutput("egrep 'processor|cpu MHz|core id' /proc/cpuinfo").split(
"\n"
)
coreid_info = getoutput("egrep 'processor|cpu MHz|core id' /proc/cpuinfo").split("\n")
cpu_core = dict()
freq_per_cpu = []
for i in range(0, len(coreid_info), 3):
Expand Down Expand Up @@ -1043,9 +1023,7 @@ def sysinfo():
pass

print("Core\tUsage\tTemperature\tFrequency")
for (cpu, usage, freq, temp) in zip(
cpu_core, usage_per_cpu, freq_per_cpu, temp_per_cpu
):
for (cpu, usage, freq, temp) in zip(cpu_core, usage_per_cpu, freq_per_cpu, temp_per_cpu):
print(f"CPU{cpu}:\t{usage:>5.1f}% {temp:>3.0f} °C {freq:>5.0f} MHz")

if offline_cpus:
Expand Down Expand Up @@ -1087,13 +1065,15 @@ def is_running(program, argument):
if argument in p.cmdline():
return True


def daemon_running_msg():
print("\n" + "-" * 24 + " auto-cpufreq running " + "-" * 30 + "\n")
print(
'ERROR: auto-cpufreq is running in daemon mode.\n\nMake sure to stop the deamon before running with --live or --monitor mode'
"ERROR: auto-cpufreq is running in daemon mode.\n\nMake sure to stop the deamon before running with --live or --monitor mode"
)
footer()


# check if auto-cpufreq --daemon is running
def running_daemon():
if is_running("auto-cpufreq", "--daemon"):
Expand Down
Loading

0 comments on commit 3121e2f

Please sign in to comment.