Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformatted codebase #325

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -234,6 +240,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 @@ -274,13 +281,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 @@ -308,17 +311,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 @@ -347,9 +342,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 @@ -363,6 +356,7 @@ def deploy_daemon():

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


# remove auto-cpufreq daemon
def remove():

Expand Down Expand Up @@ -396,19 +390,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 @@ -470,11 +458,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 @@ -682,11 +667,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 @@ -945,10 +927,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 @@ -998,9 +980,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 @@ -1039,9 +1019,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 @@ -1093,13 +1071,15 @@ def is_running(program, argument):
print(repr(e))
continue


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