2021-01-21 07:55:23 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# auto-cpufreq - core functionality
|
|
|
|
|
|
|
|
import os
|
|
|
|
import platform as pl
|
|
|
|
import shutil
|
|
|
|
import sys
|
2021-02-06 20:08:15 +01:00
|
|
|
import psutil
|
|
|
|
import distro
|
2021-01-21 07:55:23 +01:00
|
|
|
import time
|
2021-02-06 20:08:15 +01:00
|
|
|
import click
|
2021-01-21 07:55:23 +01:00
|
|
|
import warnings
|
2021-10-10 08:11:45 +02:00
|
|
|
import configparser
|
2022-01-06 13:08:51 +01:00
|
|
|
import pkg_resources
|
2021-01-21 07:55:23 +01:00
|
|
|
from math import isclose
|
|
|
|
from pathlib import Path
|
2021-11-04 08:32:50 +01:00
|
|
|
from shutil import which
|
2021-01-24 13:14:11 +01:00
|
|
|
from subprocess import getoutput, call, run, check_output, DEVNULL
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
sys.path.append("../")
|
2021-12-04 15:40:03 +01:00
|
|
|
from auto_cpufreq.power_helper import *
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
warnings.filterwarnings("ignore")
|
|
|
|
|
|
|
|
# ToDo:
|
|
|
|
# - re-enable CPU fan speed display and make more generic and not only for thinkpad
|
|
|
|
# - replace get system/CPU load from: psutil.getloadavg() | available in 5.6.2)
|
|
|
|
|
|
|
|
SCRIPTS_DIR = Path("/usr/local/share/auto-cpufreq/scripts/")
|
|
|
|
|
|
|
|
# from the highest performance to the lowest
|
2021-03-11 18:29:20 +01:00
|
|
|
ALL_GOVERNORS = (
|
|
|
|
"performance",
|
|
|
|
"ondemand",
|
|
|
|
"conservative",
|
|
|
|
"schedutil",
|
|
|
|
"userspace",
|
|
|
|
"powersave",
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
CPUS = os.cpu_count()
|
|
|
|
|
2021-12-24 20:21:40 +01:00
|
|
|
# ignore these devices under /sys/class/power_supply/
|
|
|
|
POWER_SUPPLY_IGNORELIST = ["hidpp_battery"]
|
|
|
|
|
2021-01-24 13:14:11 +01:00
|
|
|
# Note:
|
2021-01-21 07:55:23 +01:00
|
|
|
# "load1m" & "cpuload" can't be global vars and to in order to show correct data must be
|
|
|
|
# decraled where their execution takes place
|
|
|
|
|
|
|
|
# powersave/performance system load thresholds
|
2021-03-11 18:29:20 +01:00
|
|
|
powersave_load_threshold = (75 * CPUS) / 100
|
|
|
|
performance_load_threshold = (50 * CPUS) / 100
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
# auto-cpufreq stats file path
|
|
|
|
auto_cpufreq_stats_path = None
|
|
|
|
auto_cpufreq_stats_file = None
|
2021-01-24 13:14:11 +01:00
|
|
|
|
|
|
|
if os.getenv("PKG_MARKER") == "SNAP":
|
2021-02-02 21:40:55 +01:00
|
|
|
auto_cpufreq_stats_path = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.stats")
|
2021-01-24 13:14:11 +01:00
|
|
|
else:
|
2021-02-02 21:40:55 +01:00
|
|
|
auto_cpufreq_stats_path = Path("/var/run/auto-cpufreq.stats")
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
# daemon check
|
|
|
|
dcheck = getoutput("snapctl get daemon")
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
def file_stats():
|
|
|
|
global auto_cpufreq_stats_file
|
|
|
|
auto_cpufreq_stats_file = open(auto_cpufreq_stats_path, "w")
|
|
|
|
sys.stdout = auto_cpufreq_stats_file
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
|
|
|
def get_config(config_file=""):
|
2021-12-26 11:01:32 +01:00
|
|
|
if not hasattr(get_config, "config"):
|
|
|
|
get_config.config = configparser.ConfigParser()
|
2021-10-10 08:11:45 +02:00
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
if os.path.isfile(config_file):
|
|
|
|
get_config.config.read(config_file)
|
|
|
|
get_config.using_cfg_file = True
|
2021-10-10 08:11:45 +02:00
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
return get_config.config
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-02-04 23:27:19 +01:00
|
|
|
# get distro name
|
2021-12-19 09:20:09 +01:00
|
|
|
try:
|
|
|
|
dist_name = distro.id()
|
|
|
|
except PermissionError:
|
2021-12-23 19:22:46 +01:00
|
|
|
# Current work-around for Pop!_OS where symlink causes permission issues
|
|
|
|
print("[!] Warning: Cannot get distro name")
|
2021-12-19 09:20:09 +01:00
|
|
|
if os.path.exists("/etc/pop-os/os-release"):
|
2021-12-23 19:22:46 +01:00
|
|
|
# Check if using a Snap
|
|
|
|
if os.getenv("PKG_MARKER") == "SNAP":
|
|
|
|
print("[!] Snap install on PopOS detected, you must manually run the following"
|
|
|
|
" commands in another terminal:\n")
|
|
|
|
print("[!] Backup the /etc/os-release file:")
|
|
|
|
print("sudo mv /etc/os-release /etc/os-release-backup\n")
|
|
|
|
print("[!] Create hardlink to /etc/os-release:")
|
|
|
|
print("sudo ln /etc/pop-os/os-release /etc/os-release\n")
|
|
|
|
print("[!] Aborting. Restart auto-cpufreq when you created the hardlink")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
# This should not be the case. But better be sure.
|
|
|
|
print("[!] Check /etc/os-release permissions and make sure it is not a symbolic link")
|
|
|
|
print("[!] Aborting...")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-12-19 09:20:09 +01:00
|
|
|
else:
|
2021-12-23 19:22:46 +01:00
|
|
|
print("[!] Check /etc/os-release permissions and make sure it is not a symbolic link")
|
|
|
|
print("[!] Aborting...")
|
2021-12-19 09:20:09 +01:00
|
|
|
sys.exit(1)
|
2021-02-04 23:27:19 +01:00
|
|
|
|
|
|
|
# display running version of auto-cpufreq
|
2021-01-21 07:55:23 +01:00
|
|
|
def app_version():
|
2021-02-04 23:27:19 +01:00
|
|
|
|
2022-01-06 13:08:51 +01:00
|
|
|
print("auto-cpufreq version: ", end="")
|
2021-02-06 20:41:06 +01:00
|
|
|
|
2021-02-04 23:27:19 +01:00
|
|
|
# snap package
|
2021-03-11 18:29:20 +01:00
|
|
|
if os.getenv("PKG_MARKER") == "SNAP":
|
2022-01-08 15:43:43 +01:00
|
|
|
print(getoutput("echo Snap version: $SNAP_VERSION"))
|
2021-02-04 23:27:19 +01:00
|
|
|
# aur package
|
|
|
|
elif dist_name in ["arch", "manjaro", "garuda"]:
|
2021-02-07 19:26:34 +01:00
|
|
|
aur_pkg_check = call("pacman -Qs auto-cpufreq > /dev/null", shell=True)
|
2021-02-06 20:41:06 +01:00
|
|
|
if aur_pkg_check == 1:
|
2022-01-06 13:08:51 +01:00
|
|
|
print(pkg_resources.require("auto-cpufreq")[0].version)
|
2021-02-06 20:41:06 +01:00
|
|
|
else:
|
2021-02-07 19:25:08 +01:00
|
|
|
print(getoutput("pacman -Qi auto-cpufreq | grep Version"))
|
2021-03-11 18:29:20 +01:00
|
|
|
else:
|
2021-02-04 23:27:19 +01:00
|
|
|
# source code (auto-cpufreq-installer)
|
|
|
|
try:
|
2022-01-06 13:08:51 +01:00
|
|
|
print(pkg_resources.require("auto-cpufreq")[0].version)
|
2021-03-11 18:29:20 +01:00
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|
2021-02-04 23:27:19 +01:00
|
|
|
pass
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def app_res_use():
|
|
|
|
p = psutil.Process()
|
|
|
|
print("auto-cpufreq system resource consumption:")
|
|
|
|
print("cpu usage:", p.cpu_percent(), "%")
|
2021-03-11 18:29:20 +01:00
|
|
|
print("memory use:", round(p.memory_percent(), 2), "%")
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
# set/change state of turbo
|
|
|
|
def turbo(value: bool = None):
|
|
|
|
"""
|
|
|
|
Get and set turbo mode
|
|
|
|
"""
|
|
|
|
p_state = Path("/sys/devices/system/cpu/intel_pstate/no_turbo")
|
|
|
|
cpufreq = Path("/sys/devices/system/cpu/cpufreq/boost")
|
|
|
|
|
|
|
|
if p_state.exists():
|
|
|
|
inverse = True
|
|
|
|
f = p_state
|
|
|
|
elif cpufreq.exists():
|
|
|
|
f = cpufreq
|
|
|
|
inverse = False
|
|
|
|
else:
|
|
|
|
print("Warning: CPU turbo is not available")
|
|
|
|
return False
|
|
|
|
|
|
|
|
if value is not None:
|
|
|
|
if inverse:
|
|
|
|
value = not value
|
|
|
|
|
|
|
|
try:
|
|
|
|
f.write_text(str(int(value)) + "\n")
|
|
|
|
except PermissionError:
|
|
|
|
print("Warning: Changing CPU turbo is not supported. Skipping.")
|
|
|
|
return False
|
|
|
|
|
|
|
|
value = bool(int(f.read_text().strip()))
|
|
|
|
if inverse:
|
|
|
|
value = not value
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
|
|
|
# display current state of turbo
|
2021-01-21 07:55:23 +01:00
|
|
|
def get_turbo():
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
if turbo():
|
|
|
|
print("Currently turbo boost is: on")
|
|
|
|
else:
|
|
|
|
print("Currently turbo boost is: off")
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def charging():
|
|
|
|
"""
|
|
|
|
get charge state: is battery charging or discharging
|
|
|
|
"""
|
2021-12-16 08:04:24 +01:00
|
|
|
power_supply_path = "/sys/class/power_supply/"
|
|
|
|
power_supplies = os.listdir(Path(power_supply_path))
|
2021-12-24 20:21:40 +01:00
|
|
|
# sort it so AC is 'always' first
|
|
|
|
power_supplies = sorted(power_supplies)
|
2021-12-16 08:04:24 +01:00
|
|
|
|
|
|
|
# check if we found power supplies. on a desktop these are not found
|
|
|
|
# and we assume we are on a powercable.
|
|
|
|
if len(power_supplies) == 0:
|
|
|
|
# nothing found found, so nothing to check
|
|
|
|
return True
|
|
|
|
# we found some power supplies, lets check their state
|
2021-02-02 21:21:51 +01:00
|
|
|
else:
|
2021-12-16 08:04:24 +01:00
|
|
|
for supply in power_supplies:
|
2021-12-24 20:21:40 +01:00
|
|
|
# Check if supply is in ignore list
|
|
|
|
ignore_supply = any(item in supply for item in POWER_SUPPLY_IGNORELIST)
|
|
|
|
# If found in ignore list, skip it.
|
|
|
|
if ignore_supply:
|
2021-12-21 18:25:46 +01:00
|
|
|
continue
|
2021-12-24 20:21:40 +01:00
|
|
|
|
2021-12-16 08:04:24 +01:00
|
|
|
try:
|
|
|
|
with open(Path(power_supply_path + supply + "/type")) as f:
|
|
|
|
supply_type = f.read()[:-1]
|
|
|
|
if supply_type == "Mains":
|
|
|
|
# we found an AC
|
|
|
|
try:
|
|
|
|
with open(Path(power_supply_path + supply + "/online")) as f:
|
|
|
|
val = int(f.read()[:-1])
|
|
|
|
if val == 1:
|
|
|
|
# we are definitely charging
|
|
|
|
return True
|
|
|
|
except FileNotFoundError:
|
|
|
|
# we could not find online, check next item
|
|
|
|
continue
|
|
|
|
elif supply_type == "Battery":
|
|
|
|
# we found a battery, check if its being discharged
|
|
|
|
try:
|
|
|
|
with open(Path(power_supply_path + supply + "/status")) as f:
|
|
|
|
val = str(f.read()[:-1])
|
|
|
|
if val == "Discharging":
|
|
|
|
# we found a discharging battery
|
|
|
|
return False
|
|
|
|
except FileNotFoundError:
|
|
|
|
# could not find status, check the next item
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# continue to next item because current is not
|
|
|
|
# "Mains" or "Battery"
|
|
|
|
continue
|
|
|
|
except FileNotFoundError:
|
|
|
|
# could not find type, check the next item
|
|
|
|
continue
|
|
|
|
|
|
|
|
# we cannot determine discharging state, assume we are on powercable
|
|
|
|
return True
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def get_avail_gov():
|
|
|
|
f = Path("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")
|
|
|
|
return f.read_text().strip().split(" ")
|
|
|
|
|
|
|
|
|
|
|
|
def get_avail_powersave():
|
|
|
|
"""
|
|
|
|
Iterate over ALL_GOVERNORS in reverse order: from powersave to performance
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
for g in ALL_GOVERNORS[::-1]:
|
|
|
|
if g in get_avail_gov():
|
|
|
|
return g
|
|
|
|
|
|
|
|
|
|
|
|
def get_avail_performance():
|
|
|
|
for g in ALL_GOVERNORS:
|
|
|
|
if g in get_avail_gov():
|
|
|
|
return g
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_gov():
|
2021-03-11 18:29:20 +01:00
|
|
|
return print(
|
|
|
|
"Currently using:",
|
|
|
|
getoutput("cpufreqctl.auto-cpufreq --governor").strip().split(" ")[0],
|
|
|
|
"governor",
|
|
|
|
)
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
def cpufreqctl():
|
|
|
|
"""
|
|
|
|
deploy cpufreqctl script
|
|
|
|
"""
|
|
|
|
|
|
|
|
# detect if running on a SNAP
|
2021-03-11 18:29:20 +01:00
|
|
|
if os.getenv("PKG_MARKER") == "SNAP":
|
2021-01-21 07:55:23 +01:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# deploy cpufreqctl.auto-cpufreq script
|
|
|
|
if os.path.isfile("/usr/bin/cpufreqctl"):
|
2021-12-22 08:28:08 +01:00
|
|
|
shutil.copy(SCRIPTS_DIR / "cpufreqctl.sh", "/usr/bin/cpufreqctl.auto-cpufreq")
|
2021-01-21 07:55:23 +01:00
|
|
|
else:
|
2021-12-22 08:28:08 +01:00
|
|
|
shutil.copy(SCRIPTS_DIR / "cpufreqctl.sh", "/usr/bin/cpufreqctl.auto-cpufreq")
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
def cpufreqctl_restore():
|
|
|
|
"""
|
|
|
|
remove cpufreqctl.auto-cpufreq script
|
|
|
|
"""
|
|
|
|
# detect if running on a SNAP
|
2021-03-11 18:29:20 +01:00
|
|
|
if os.getenv("PKG_MARKER") == "SNAP":
|
2021-01-21 07:55:23 +01:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if os.path.isfile("/usr/bin/cpufreqctl.auto-cpufreq"):
|
|
|
|
os.remove("/usr/bin/cpufreqctl.auto-cpufreq")
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def footer(l=79):
|
|
|
|
print("\n" + "-" * l + "\n")
|
|
|
|
|
|
|
|
|
|
|
|
def daemon_not_found():
|
|
|
|
print("\n" + "-" * 32 + " Daemon check " + "-" * 33 + "\n")
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
"ERROR:\n\nDaemon not enabled, must run install first, i.e: \nsudo auto-cpufreq --install"
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
footer()
|
|
|
|
|
|
|
|
|
|
|
|
def deploy_complete_msg():
|
2021-12-22 08:28:08 +01:00
|
|
|
print("\n" + "-" * 17 + " auto-cpufreq daemon installed and running " + "-" * 17 + "\n")
|
2021-02-02 21:40:55 +01:00
|
|
|
print("To view live stats, run:\nauto-cpufreq --stats")
|
2021-12-22 08:28:08 +01:00
|
|
|
print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove")
|
2021-01-21 07:55:23 +01:00
|
|
|
footer()
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
def deprecated_log_msg():
|
2021-02-02 22:10:42 +01:00
|
|
|
print("\n" + "-" * 24 + " auto-cpufreq log file renamed " + "-" * 24 + "\n")
|
|
|
|
print("The --log flag has been renamed to --stats\n")
|
2021-02-02 21:40:55 +01:00
|
|
|
print("To view live stats, run:\nauto-cpufreq --stats")
|
|
|
|
footer()
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def remove_complete_msg():
|
|
|
|
print("\n" + "-" * 25 + " auto-cpufreq daemon removed " + "-" * 25 + "\n")
|
|
|
|
print("auto-cpufreq successfully removed.")
|
2021-02-02 20:56:56 +01:00
|
|
|
footer()
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def deploy_daemon():
|
|
|
|
print("\n" + "-" * 21 + " Deploying auto-cpufreq as a daemon " + "-" * 22 + "\n")
|
|
|
|
|
|
|
|
# deploy cpufreqctl script func call
|
|
|
|
cpufreqctl()
|
|
|
|
|
2021-12-04 18:32:50 +01:00
|
|
|
# turn off bluetooth on boot
|
|
|
|
bluetooth_disable()
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
auto_cpufreq_stats_path.touch(exist_ok=True)
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
print("\n* Deploy auto-cpufreq install script")
|
2021-12-22 08:28:08 +01:00
|
|
|
shutil.copy(SCRIPTS_DIR / "auto-cpufreq-install.sh", "/usr/bin/auto-cpufreq-install")
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
print("\n* Deploy auto-cpufreq remove script")
|
|
|
|
shutil.copy(SCRIPTS_DIR / "auto-cpufreq-remove.sh", "/usr/bin/auto-cpufreq-remove")
|
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
# output warning if gnome power profile is running
|
2021-12-04 20:56:50 +01:00
|
|
|
gnome_power_detect_install()
|
|
|
|
gnome_power_svc_disable()
|
2021-12-04 10:02:19 +01:00
|
|
|
|
2021-12-11 11:59:41 +01:00
|
|
|
# output warning if TLP service is detected
|
|
|
|
tlp_service_detect()
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
call("/usr/bin/auto-cpufreq-install", shell=True)
|
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# remove auto-cpufreq daemon
|
|
|
|
def remove():
|
|
|
|
|
|
|
|
# check if auto-cpufreq is installed
|
|
|
|
if not os.path.exists("/usr/bin/auto-cpufreq-remove"):
|
|
|
|
print("\nauto-cpufreq daemon is not installed.\n")
|
|
|
|
sys.exit(1)
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
print("\n" + "-" * 21 + " Removing auto-cpufreq daemon " + "-" * 22 + "\n")
|
|
|
|
|
2021-12-04 18:32:50 +01:00
|
|
|
# turn on bluetooth on boot
|
|
|
|
bluetooth_enable()
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
# output warning if gnome power profile is stopped
|
|
|
|
gnome_power_rm_reminder()
|
2021-12-04 20:56:50 +01:00
|
|
|
gnome_power_svc_enable()
|
2021-11-21 09:22:22 +01:00
|
|
|
|
2021-12-04 10:02:19 +01:00
|
|
|
# run auto-cpufreq daemon remove script
|
2021-01-21 07:55:23 +01:00
|
|
|
call("/usr/bin/auto-cpufreq-remove", shell=True)
|
|
|
|
|
|
|
|
# remove auto-cpufreq-remove
|
|
|
|
os.remove("/usr/bin/auto-cpufreq-remove")
|
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
# delete stats file
|
|
|
|
if auto_cpufreq_stats_path.exists():
|
|
|
|
if auto_cpufreq_stats_file is not None:
|
|
|
|
auto_cpufreq_stats_file.close()
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
auto_cpufreq_stats_path.unlink()
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
# restore original cpufrectl script
|
|
|
|
cpufreqctl_restore()
|
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def gov_check():
|
|
|
|
for gov in get_avail_gov():
|
|
|
|
if gov not in ALL_GOVERNORS:
|
2021-12-22 08:28:08 +01:00
|
|
|
print("\n" + "-" * 18 + " Checking for necessary scaling governors " + "-" * 19 + "\n")
|
|
|
|
sys.exit("ERROR:\n\nCouldn't find any of the necessary scaling governors.\n")
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
# root check func
|
|
|
|
def root_check():
|
|
|
|
if not os.geteuid() == 0:
|
|
|
|
print("\n" + "-" * 33 + " Root check " + "-" * 34 + "\n")
|
2021-11-28 14:06:46 +01:00
|
|
|
print("ERROR:\n\nMust be run root for this functionality to work, i.e: \nsudo " + app_name)
|
2021-01-21 07:55:23 +01:00
|
|
|
footer()
|
|
|
|
exit(1)
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# refresh countdown
|
|
|
|
def countdown(s):
|
2021-02-02 21:40:55 +01:00
|
|
|
# Fix for wrong stats output and "TERM environment variable not set"
|
2021-03-11 18:29:20 +01:00
|
|
|
os.environ["TERM"] = "xterm"
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
for remaining in range(s, 0, -1):
|
|
|
|
sys.stdout.write("\r")
|
2021-03-11 18:29:20 +01:00
|
|
|
sys.stdout.write('\t\t\t"auto-cpufreq" refresh in:{:2d}'.format(remaining))
|
2021-01-21 07:55:23 +01:00
|
|
|
sys.stdout.flush()
|
|
|
|
time.sleep(1)
|
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
if auto_cpufreq_stats_file is not None:
|
|
|
|
auto_cpufreq_stats_file.seek(0)
|
|
|
|
auto_cpufreq_stats_file.truncate(0)
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-02-04 20:25:17 +01:00
|
|
|
# execution timestamp
|
|
|
|
from datetime import datetime
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-02-04 20:25:17 +01:00
|
|
|
now = datetime.now()
|
|
|
|
current_time = now.strftime("%B %d (%A) - %H:%M:%S")
|
|
|
|
print("\n\t\tExecuted on:", current_time)
|
|
|
|
|
2021-01-24 13:14:11 +01:00
|
|
|
else:
|
|
|
|
run("clear")
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# get cpu usage + system load for (last minute)
|
|
|
|
def display_load():
|
|
|
|
|
|
|
|
# get CPU utilization as a percentage
|
|
|
|
cpuload = psutil.cpu_percent(interval=1)
|
|
|
|
|
|
|
|
# get system/CPU load
|
|
|
|
load1m, _, _ = os.getloadavg()
|
|
|
|
|
|
|
|
print("\nTotal CPU usage:", cpuload, "%")
|
|
|
|
print("Total system load:", load1m)
|
|
|
|
print("Average temp. of all cores:", avg_all_core_temp, "°C", "\n")
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
# set minimum and maximum CPU frequencies
|
|
|
|
def set_frequencies():
|
2021-12-30 13:29:14 +01:00
|
|
|
"""
|
|
|
|
Sets frequencies:
|
|
|
|
- if option is used in auto-cpufreq.conf: use configured value
|
|
|
|
- if option is disabled/no conf file used: set default frequencies
|
|
|
|
Frequency setting is performed only once on power supply change
|
|
|
|
"""
|
|
|
|
power_supply = "charger" if charging() else "battery"
|
|
|
|
|
|
|
|
# don't do anything if the power supply hasn't changed
|
|
|
|
if (
|
|
|
|
hasattr(set_frequencies, "prev_power_supply")
|
|
|
|
and power_supply == set_frequencies.prev_power_supply
|
|
|
|
):
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
set_frequencies.prev_power_supply = power_supply
|
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
frequency = {
|
|
|
|
"scaling_max_freq": {
|
|
|
|
"cmdargs": "--frequency-max",
|
|
|
|
"minmax": "maximum",
|
|
|
|
},
|
|
|
|
"scaling_min_freq": {
|
|
|
|
"cmdargs": "--frequency-min",
|
|
|
|
"minmax": "minimum",
|
|
|
|
},
|
|
|
|
}
|
2021-12-30 13:29:14 +01:00
|
|
|
if not hasattr(set_frequencies, "max_limit"):
|
|
|
|
set_frequencies.max_limit = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-max-limit"))
|
|
|
|
if not hasattr(set_frequencies, "min_limit"):
|
|
|
|
set_frequencies.min_limit = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-min-limit"))
|
2021-12-26 11:01:32 +01:00
|
|
|
|
2021-12-30 13:29:14 +01:00
|
|
|
conf = get_config()
|
2021-12-26 11:01:32 +01:00
|
|
|
|
|
|
|
for freq_type in frequency.keys():
|
2021-12-30 13:29:14 +01:00
|
|
|
value = None
|
|
|
|
if not conf.has_option(power_supply, freq_type):
|
|
|
|
# fetch and use default frequencies
|
|
|
|
if freq_type == "scaling_max_freq":
|
|
|
|
curr_freq = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-max"))
|
|
|
|
value = set_frequencies.max_limit
|
|
|
|
else:
|
|
|
|
curr_freq = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-min"))
|
|
|
|
value = set_frequencies.min_limit
|
|
|
|
if curr_freq == value:
|
|
|
|
continue
|
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
try:
|
2021-12-30 13:29:14 +01:00
|
|
|
frequency[freq_type]["value"] = (
|
|
|
|
value if value else int(conf[power_supply][freq_type].strip())
|
|
|
|
)
|
2021-12-26 11:01:32 +01:00
|
|
|
except ValueError:
|
|
|
|
print(f"Invalid value for '{freq_type}': {frequency[freq_type]['value']}")
|
|
|
|
exit(1)
|
|
|
|
|
2021-12-30 13:29:14 +01:00
|
|
|
if not (
|
|
|
|
set_frequencies.min_limit <= frequency[freq_type]["value"] <= set_frequencies.max_limit
|
|
|
|
):
|
2021-12-26 11:01:32 +01:00
|
|
|
print(
|
2021-12-30 13:29:14 +01:00
|
|
|
f"Given value for '{freq_type}' is not within the allowed frequencies {set_frequencies.min_limit}-{set_frequencies.max_limit} kHz"
|
2021-12-26 11:01:32 +01:00
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
args = f"{frequency[freq_type]['cmdargs']} --set={frequency[freq_type]['value']}"
|
|
|
|
message = f'Setting {frequency[freq_type]["minmax"]} CPU frequency to {round(frequency[freq_type]["value"]/1000)} Mhz'
|
|
|
|
|
|
|
|
# set the frequency
|
|
|
|
print(message)
|
|
|
|
run(f"cpufreqctl.auto-cpufreq {args}", shell=True)
|
|
|
|
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# set powersave and enable turbo
|
|
|
|
def set_powersave():
|
2021-12-26 11:01:32 +01:00
|
|
|
conf = get_config()
|
|
|
|
if conf.has_option("battery", "governor"):
|
|
|
|
gov = conf["battery"]["governor"]
|
2021-10-10 08:11:45 +02:00
|
|
|
else:
|
|
|
|
gov = get_avail_powersave()
|
|
|
|
print(f'Setting to use: "{gov}" governor')
|
|
|
|
run(f"cpufreqctl.auto-cpufreq --governor --set={gov}", shell=True)
|
2021-03-11 18:29:20 +01:00
|
|
|
if (
|
2021-12-22 08:28:08 +01:00
|
|
|
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
|
2021-03-11 18:29:20 +01:00
|
|
|
):
|
2021-01-21 07:55:23 +01:00
|
|
|
run("cpufreqctl.auto-cpufreq --epp --set=balance_power", shell=True)
|
2021-03-11 18:29:20 +01:00
|
|
|
print('Setting to use: "balance_power" EPP')
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
# set frequencies
|
|
|
|
set_frequencies()
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# get CPU utilization as a percentage
|
|
|
|
cpuload = psutil.cpu_percent(interval=1)
|
|
|
|
|
|
|
|
# get system/CPU load
|
|
|
|
load1m, _, _ = os.getloadavg()
|
|
|
|
|
|
|
|
print("\nTotal CPU usage:", cpuload, "%")
|
|
|
|
print("Total system load:", load1m)
|
|
|
|
print("Average temp. of all cores:", avg_all_core_temp, "°C")
|
|
|
|
|
|
|
|
# conditions for setting turbo in powersave
|
2021-12-26 11:01:32 +01:00
|
|
|
if conf.has_option("battery", "turbo"):
|
|
|
|
auto = conf["battery"]["turbo"]
|
2021-01-21 07:55:23 +01:00
|
|
|
else:
|
2021-10-10 08:11:45 +02:00
|
|
|
auto = "auto"
|
|
|
|
|
|
|
|
if auto == "always":
|
|
|
|
print("Configuration file enforces turbo boost")
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
elif auto == "never":
|
|
|
|
print("Configuration file disables turbo boost")
|
|
|
|
print("setting turbo boost: off")
|
2021-10-17 16:29:16 +02:00
|
|
|
turbo(False)
|
2021-10-10 08:11:45 +02:00
|
|
|
else:
|
|
|
|
if psutil.cpu_percent(percpu=False, interval=0.01) >= 30.0 or isclose(
|
|
|
|
max(psutil.cpu_percent(percpu=True, interval=0.01)), 100
|
|
|
|
):
|
|
|
|
print("\nHigh CPU load")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 20 and avg_all_core_temp >= 70:
|
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
|
|
|
else:
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
|
|
|
|
|
|
|
elif load1m > powersave_load_threshold:
|
|
|
|
print("\nHigh system load")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 20 and avg_all_core_temp >= 65:
|
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
|
|
|
else:
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
else:
|
2021-10-10 08:11:45 +02:00
|
|
|
print("\nLoad optimal")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 20 and avg_all_core_temp >= 60:
|
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
|
|
|
else:
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
footer()
|
|
|
|
|
|
|
|
|
|
|
|
# make turbo suggestions in powersave
|
|
|
|
def mon_powersave():
|
|
|
|
|
|
|
|
# get CPU utilization as a percentage
|
|
|
|
cpuload = psutil.cpu_percent(interval=1)
|
|
|
|
|
|
|
|
# get system/CPU load
|
|
|
|
load1m, _, _ = os.getloadavg()
|
|
|
|
|
|
|
|
print("\nTotal CPU usage:", cpuload, "%")
|
|
|
|
print("Total system load:", load1m)
|
|
|
|
print("Average temp. of all cores:", avg_all_core_temp, "°C")
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
if psutil.cpu_percent(percpu=False, interval=0.01) >= 30.0 or isclose(
|
|
|
|
max(psutil.cpu_percent(percpu=True, interval=0.01)), 100
|
|
|
|
):
|
2021-01-21 07:55:23 +01:00
|
|
|
print("\nHigh CPU load")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 20 and avg_all_core_temp >= 70:
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
else:
|
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
elif load1m > powersave_load_threshold:
|
|
|
|
print("\nHigh system load")
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 20 and avg_all_core_temp >= 65:
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
else:
|
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
else:
|
|
|
|
print("\nLoad optimal")
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 20 and avg_all_core_temp >= 60:
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
else:
|
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
footer()
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# set performance and enable turbo
|
|
|
|
def set_performance():
|
2021-12-26 11:01:32 +01:00
|
|
|
conf = get_config()
|
|
|
|
if conf.has_option("charger", "governor"):
|
|
|
|
gov = conf["charger"]["governor"]
|
2021-10-10 08:11:45 +02:00
|
|
|
else:
|
|
|
|
gov = get_avail_performance()
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-10-10 08:11:45 +02:00
|
|
|
print(f'Setting to use: "{gov}" governor')
|
2021-03-11 18:29:20 +01:00
|
|
|
run(
|
2021-10-10 08:11:45 +02:00
|
|
|
f"cpufreqctl.auto-cpufreq --governor --set={gov}",
|
2021-03-11 18:29:20 +01:00
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
if (
|
2021-12-22 08:28:08 +01:00
|
|
|
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
|
2021-03-11 18:29:20 +01:00
|
|
|
):
|
2021-01-21 07:55:23 +01:00
|
|
|
run("cpufreqctl.auto-cpufreq --epp --set=balance_performance", shell=True)
|
2021-03-11 18:29:20 +01:00
|
|
|
print('Setting to use: "balance_performance" EPP')
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
# set frequencies
|
|
|
|
set_frequencies()
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# get CPU utilization as a percentage
|
|
|
|
cpuload = psutil.cpu_percent(interval=1)
|
|
|
|
|
|
|
|
# get system/CPU load
|
|
|
|
load1m, _, _ = os.getloadavg()
|
|
|
|
|
|
|
|
print("\nTotal CPU usage:", cpuload, "%")
|
|
|
|
print("Total system load:", load1m)
|
|
|
|
print("Average temp. of all cores:", avg_all_core_temp, "°C")
|
|
|
|
|
2021-12-26 11:01:32 +01:00
|
|
|
if conf.has_option("charger", "turbo"):
|
|
|
|
auto = conf["charger"]["turbo"]
|
2021-01-21 07:55:23 +01:00
|
|
|
else:
|
2021-10-10 08:11:45 +02:00
|
|
|
auto = "auto"
|
|
|
|
|
|
|
|
if auto == "always":
|
|
|
|
print("Configuration file enforces turbo boost")
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
elif auto == "never":
|
|
|
|
print("Configuration file disables turbo boost")
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(True)
|
|
|
|
else:
|
|
|
|
if (
|
|
|
|
psutil.cpu_percent(percpu=False, interval=0.01) >= 20.0
|
|
|
|
or max(psutil.cpu_percent(percpu=True, interval=0.01)) >= 75
|
|
|
|
):
|
|
|
|
print("\nHigh CPU load")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
2021-10-17 16:35:55 +02:00
|
|
|
elif avg_all_core_temp >= 70:
|
2021-10-10 08:11:45 +02:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
|
|
|
else:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
|
|
|
|
elif load1m >= performance_load_threshold:
|
|
|
|
print("\nHigh system load")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
2021-10-17 16:35:55 +02:00
|
|
|
elif avg_all_core_temp >= 65:
|
2021-10-10 08:11:45 +02:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
|
|
|
else:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
else:
|
2021-10-10 08:11:45 +02:00
|
|
|
print("\nLoad optimal")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("setting turbo boost: on")
|
|
|
|
turbo(True)
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
2021-10-17 16:35:55 +02:00
|
|
|
elif avg_all_core_temp >= 60:
|
2021-10-10 08:11:45 +02:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
|
|
|
else:
|
|
|
|
print("setting turbo boost: off")
|
|
|
|
turbo(False)
|
2021-01-24 13:14:11 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
footer()
|
|
|
|
|
|
|
|
|
|
|
|
# make turbo suggestions in performance
|
|
|
|
def mon_performance():
|
|
|
|
|
|
|
|
# get CPU utilization as a percentage
|
|
|
|
cpuload = psutil.cpu_percent(interval=1)
|
|
|
|
|
|
|
|
# get system/CPU load
|
|
|
|
load1m, _, _ = os.getloadavg()
|
|
|
|
|
|
|
|
print("\nTotal CPU usage:", cpuload, "%")
|
|
|
|
print("Total system load:", load1m)
|
|
|
|
print("Average temp. of all cores:", avg_all_core_temp, "°C")
|
|
|
|
|
|
|
|
# get system/CPU load
|
|
|
|
load1m, _, _ = os.getloadavg()
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
if (
|
|
|
|
psutil.cpu_percent(percpu=False, interval=0.01) >= 20.0
|
|
|
|
or max(psutil.cpu_percent(percpu=True, interval=0.01)) >= 75
|
|
|
|
):
|
2021-01-21 07:55:23 +01:00
|
|
|
print("\nHigh CPU load")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 25 and avg_all_core_temp >= 70:
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
else:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
elif load1m > performance_load_threshold:
|
|
|
|
print("\nHigh system load")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 25 and avg_all_core_temp >= 65:
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
else:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
else:
|
|
|
|
print("\nLoad optimal")
|
|
|
|
|
|
|
|
# high cpu usage trigger
|
|
|
|
if cpuload >= 20:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
# set turbo state based on average of all core temperatures
|
|
|
|
elif cpuload <= 25 and avg_all_core_temp >= 60:
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
"Optimal total CPU usage:",
|
|
|
|
cpuload,
|
|
|
|
"%, high average core temp:",
|
|
|
|
avg_all_core_temp,
|
|
|
|
"°C",
|
|
|
|
)
|
2021-01-21 07:55:23 +01:00
|
|
|
print("suggesting to set turbo boost: off")
|
|
|
|
get_turbo()
|
|
|
|
else:
|
|
|
|
print("suggesting to set turbo boost: on")
|
|
|
|
get_turbo()
|
|
|
|
|
|
|
|
footer()
|
|
|
|
|
|
|
|
|
|
|
|
def set_autofreq():
|
|
|
|
"""
|
|
|
|
set cpufreq governor based if device is charging
|
|
|
|
"""
|
|
|
|
print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n")
|
|
|
|
|
|
|
|
# determine which governor should be used
|
|
|
|
if charging():
|
|
|
|
print("Battery is: charging\n")
|
|
|
|
set_performance()
|
|
|
|
else:
|
|
|
|
print("Battery is: discharging\n")
|
|
|
|
set_powersave()
|
|
|
|
|
|
|
|
|
|
|
|
def mon_autofreq():
|
|
|
|
"""
|
|
|
|
make cpufreq suggestions
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n")
|
|
|
|
|
|
|
|
# determine which governor should be used
|
|
|
|
if charging():
|
|
|
|
print("Battery is: charging\n")
|
|
|
|
get_current_gov()
|
2021-03-11 18:29:20 +01:00
|
|
|
print(f'Suggesting use of "{get_avail_performance()}" governor')
|
2021-01-21 07:55:23 +01:00
|
|
|
mon_performance()
|
|
|
|
else:
|
|
|
|
print("Battery is: discharging\n")
|
|
|
|
get_current_gov()
|
2021-03-11 18:29:20 +01:00
|
|
|
print(f'Suggesting use of "{get_avail_powersave()}" governor')
|
2021-01-21 07:55:23 +01:00
|
|
|
mon_powersave()
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def python_info():
|
|
|
|
print("Python:", pl.python_version())
|
|
|
|
print("psutil package:", psutil.__version__)
|
|
|
|
print("platform package:", pl.__version__)
|
|
|
|
print("click package:", click.__version__)
|
|
|
|
# workaround: Module 'distro' has no '__version__' member () (https://github.com/nir0s/distro/issues/265)
|
2021-03-11 18:29:20 +01:00
|
|
|
# print("distro:", distro.__version__)
|
|
|
|
run(
|
|
|
|
"echo \"distro package\" $(pip3 show distro | sed -n -e 's/^.*Version: //p')",
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-02-04 08:06:41 +01:00
|
|
|
def device_info():
|
2021-03-11 18:29:20 +01:00
|
|
|
print("Computer type:", getoutput("dmidecode --string chassis-type"))
|
2021-02-04 08:06:41 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
def distro_info():
|
|
|
|
dist = "UNKNOWN distro"
|
|
|
|
version = "UNKNOWN version"
|
|
|
|
|
|
|
|
# get distro information in snap env.
|
|
|
|
if os.getenv("PKG_MARKER") == "SNAP":
|
|
|
|
try:
|
|
|
|
with open("/var/lib/snapd/hostfs/etc/os-release", "r") as searchfile:
|
|
|
|
for line in searchfile:
|
2021-03-11 18:29:20 +01:00
|
|
|
if line.startswith("NAME="):
|
2021-12-22 08:28:08 +01:00
|
|
|
dist = line[5 : line.find("$")].strip('"')
|
2021-01-21 07:55:23 +01:00
|
|
|
continue
|
2021-03-11 18:29:20 +01:00
|
|
|
elif line.startswith("VERSION="):
|
2021-12-22 08:28:08 +01:00
|
|
|
version = line[8 : line.find("$")].strip('"')
|
2021-01-21 07:55:23 +01:00
|
|
|
continue
|
2021-03-11 18:29:20 +01:00
|
|
|
except PermissionError as e:
|
|
|
|
print(repr(e))
|
2021-01-21 07:55:23 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
dist = f"{dist} {version}"
|
|
|
|
else:
|
|
|
|
# get distro information
|
|
|
|
fdist = distro.linux_distribution()
|
|
|
|
dist = " ".join(x for x in fdist)
|
|
|
|
|
|
|
|
print("Linux distro: " + dist)
|
|
|
|
print("Linux kernel: " + pl.release())
|
|
|
|
|
2021-03-11 18:29:20 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
def sysinfo():
|
|
|
|
"""
|
|
|
|
get system information
|
|
|
|
"""
|
|
|
|
|
|
|
|
# processor_info
|
|
|
|
model_name = getoutput("egrep 'model name' /proc/cpuinfo -m 1").split(":")[-1]
|
|
|
|
print(f"Processor:{model_name}")
|
|
|
|
|
|
|
|
# get core count
|
|
|
|
total_cpu_count = int(getoutput("nproc --all"))
|
|
|
|
print("Cores:", total_cpu_count)
|
|
|
|
|
|
|
|
# get architecture
|
|
|
|
cpu_arch = pl.machine()
|
|
|
|
print("Architecture:", cpu_arch)
|
|
|
|
|
|
|
|
# get driver
|
|
|
|
driver = getoutput("cpufreqctl.auto-cpufreq --driver")
|
|
|
|
print("Driver: " + driver)
|
|
|
|
|
|
|
|
# get usage and freq info of cpus
|
|
|
|
usage_per_cpu = psutil.cpu_percent(interval=1, percpu=True)
|
|
|
|
# psutil current freq not used, gives wrong values with offline cpu's
|
|
|
|
minmax_freq_per_cpu = psutil.cpu_freq(percpu=True)
|
|
|
|
|
|
|
|
# max and min freqs, psutil reports wrong max/min freqs whith offline cores with percpu=False
|
|
|
|
max_freq = max([freq.max for freq in minmax_freq_per_cpu])
|
|
|
|
min_freq = min([freq.min for freq in minmax_freq_per_cpu])
|
2021-02-02 21:40:55 +01:00
|
|
|
print("\n" + "-" * 30 + " Current CPU stats " + "-" * 30 + "\n")
|
2021-01-21 07:55:23 +01:00
|
|
|
print(f"CPU max frequency: {max_freq:.0f} MHz")
|
|
|
|
print(f"CPU min frequency: {min_freq:.0f} MHz\n")
|
|
|
|
|
|
|
|
# get coreid's and frequencies of online cpus by parsing /proc/cpuinfo
|
2021-12-22 08:28:08 +01:00
|
|
|
coreid_info = getoutput("egrep 'processor|cpu MHz|core id' /proc/cpuinfo").split("\n")
|
2021-01-21 07:55:23 +01:00
|
|
|
cpu_core = dict()
|
|
|
|
freq_per_cpu = []
|
|
|
|
for i in range(0, len(coreid_info), 3):
|
2021-03-11 18:29:20 +01:00
|
|
|
freq_per_cpu.append(float(coreid_info[i + 1].split(":")[-1]))
|
|
|
|
cpu = int(coreid_info[i].split(":")[-1])
|
|
|
|
core = int(coreid_info[i + 2].split(":")[-1])
|
2021-01-21 07:55:23 +01:00
|
|
|
cpu_core[cpu] = core
|
|
|
|
|
|
|
|
online_cpu_count = len(cpu_core)
|
|
|
|
offline_cpus = [str(cpu) for cpu in range(total_cpu_count) if cpu not in cpu_core]
|
|
|
|
|
|
|
|
# temperatures
|
|
|
|
core_temp = psutil.sensors_temperatures()
|
|
|
|
temp_per_cpu = [float("nan")] * online_cpu_count
|
|
|
|
try:
|
|
|
|
if "coretemp" in core_temp:
|
|
|
|
# list labels in 'coretemp'
|
|
|
|
core_temp_labels = [temp.label for temp in core_temp["coretemp"]]
|
|
|
|
for i, cpu in enumerate(cpu_core):
|
|
|
|
# get correct index in core_temp
|
|
|
|
core = cpu_core[cpu]
|
|
|
|
cpu_temp_index = core_temp_labels.index(f"Core {core}")
|
|
|
|
temp_per_cpu[i] = core_temp["coretemp"][cpu_temp_index].current
|
|
|
|
elif "k10temp" in core_temp:
|
|
|
|
# https://www.kernel.org/doc/Documentation/hwmon/k10temp
|
|
|
|
temp_per_cpu = [core_temp["k10temp"][0].current] * online_cpu_count
|
|
|
|
elif "zenpower" in core_temp:
|
|
|
|
# https://github.com/AdnanHodzic/auto-cpufreq/issues/145#issuecomment-763294009
|
|
|
|
temp_per_cpu = [core_temp["zenpower"][0].current] * online_cpu_count
|
|
|
|
elif "acpitz" in core_temp:
|
|
|
|
temp_per_cpu = [core_temp["acpitz"][0].current] * online_cpu_count
|
2021-10-10 08:20:33 +02:00
|
|
|
elif "thinkpad" in core_temp:
|
|
|
|
temp_per_cpu = [core_temp["thinkpad"][0].current] * online_cpu_count
|
2021-03-11 18:29:20 +01:00
|
|
|
except Exception as e:
|
|
|
|
print(repr(e))
|
2021-01-21 07:55:23 +01:00
|
|
|
pass
|
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
print("Core\tUsage\tTemperature\tFrequency")
|
2021-12-22 08:28:08 +01:00
|
|
|
for (cpu, usage, freq, temp) in zip(cpu_core, usage_per_cpu, freq_per_cpu, temp_per_cpu):
|
2021-01-21 07:55:23 +01:00
|
|
|
print(f"CPU{cpu}:\t{usage:>5.1f}% {temp:>3.0f} °C {freq:>5.0f} MHz")
|
|
|
|
|
|
|
|
if offline_cpus:
|
|
|
|
print(f"\nDisabled CPUs: {','.join(offline_cpus)}")
|
|
|
|
|
|
|
|
# get average temperature of all cores
|
|
|
|
avg_cores_temp = sum(temp_per_cpu)
|
|
|
|
global avg_all_core_temp
|
2021-03-11 18:29:20 +01:00
|
|
|
avg_all_core_temp = float(avg_cores_temp / online_cpu_count)
|
2021-01-21 07:55:23 +01:00
|
|
|
|
|
|
|
# print current fan speed | temporarily commented
|
|
|
|
# current_fans = psutil.sensors_fans()['thinkpad'][0].current
|
|
|
|
# print("\nCPU fan speed:", current_fans, "RPM")
|
|
|
|
|
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
def no_stats_msg():
|
|
|
|
print("\n" + "-" * 29 + " auto-cpufreq stats " + "-" * 30 + "\n")
|
2021-03-11 18:29:20 +01:00
|
|
|
print(
|
|
|
|
'ERROR: auto-cpufreq stats are missing.\n\nMake sure to run: "auto-cpufreq --install" first'
|
|
|
|
)
|
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-02-02 21:40:55 +01:00
|
|
|
# read stats func
|
|
|
|
def read_stats():
|
|
|
|
# read stats
|
|
|
|
if os.path.isfile(auto_cpufreq_stats_path):
|
|
|
|
call(["tail", "-n 50", "-f", str(auto_cpufreq_stats_path)], stderr=DEVNULL)
|
2021-01-21 07:55:23 +01:00
|
|
|
else:
|
2021-02-02 21:40:55 +01:00
|
|
|
no_stats_msg()
|
2021-01-21 07:55:23 +01:00
|
|
|
footer()
|
|
|
|
|
|
|
|
|
|
|
|
# check if program (argument) is running
|
|
|
|
def is_running(program, argument):
|
2021-12-21 20:14:02 +01:00
|
|
|
# iterate over all processes found by psutil
|
|
|
|
# and find the one with name and args passed to the function
|
|
|
|
for p in psutil.process_iter():
|
|
|
|
for s in filter(lambda x: program in x, p.cmdline()):
|
|
|
|
if argument in p.cmdline():
|
|
|
|
return True
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-10-17 17:52:15 +02:00
|
|
|
def daemon_running_msg():
|
|
|
|
print("\n" + "-" * 24 + " auto-cpufreq running " + "-" * 30 + "\n")
|
|
|
|
print(
|
2021-12-22 08:28:08 +01:00
|
|
|
"ERROR: auto-cpufreq is running in daemon mode.\n\nMake sure to stop the deamon before running with --live or --monitor mode"
|
2021-10-17 17:52:15 +02:00
|
|
|
)
|
2021-12-04 18:32:50 +01:00
|
|
|
footer()
|
2021-01-21 07:55:23 +01:00
|
|
|
|
2021-12-22 08:28:08 +01:00
|
|
|
|
2021-01-21 07:55:23 +01:00
|
|
|
# check if auto-cpufreq --daemon is running
|
|
|
|
def running_daemon():
|
2021-03-11 18:29:20 +01:00
|
|
|
if is_running("auto-cpufreq", "--daemon"):
|
2021-10-17 17:52:15 +02:00
|
|
|
daemon_running_msg()
|
2021-01-21 07:55:23 +01:00
|
|
|
exit(1)
|
|
|
|
elif os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
|
2021-10-17 17:52:15 +02:00
|
|
|
daemon_running_msg()
|
2021-12-19 09:20:09 +01:00
|
|
|
exit(1)
|