forked from jothi-prasath/auto-epp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-epp
121 lines (111 loc) · 4.18 KB
/
auto-epp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/python3
import os
import time
import configparser
CONFIG_FILE = "/etc/auto-epp.conf"
DEFAULT_CONFIG = """# see available epp state by running: cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_available_preferences
[Settings]
epp_state_for_AC=balance_performance
epp_state_for_BAT=power
"""
def check_root():
if os.geteuid() == 0:
return
else:
print("auto-epp must be run with root privileges.")
exit(1)
def check_driver():
scaling_driver_path = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_driver"
try:
with open(scaling_driver_path) as f:
scaling_driver = f.read()[:-1]
except:
scaling_driver = None
if scaling_driver == "amd-pstate-epp":
return
else:
print("The system not runing amd-pstate-epp")
exit(1)
def read_config():
if not os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'w') as file:
file.write(DEFAULT_CONFIG)
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
epp_state_for_AC = config.get("Settings", "epp_state_for_AC")
epp_state_for_BAT = config.get("Settings", "epp_state_for_BAT")
return epp_state_for_AC, epp_state_for_BAT
def charging():
power_supply_path = "/sys/class/power_supply/"
power_supplies = os.listdir(power_supply_path)
# sort it so AC is 'always' first
power_supplies = sorted(power_supplies)
if len(power_supplies) == 0:
return True
else:
for supply in power_supplies:
try:
with open(power_supply_path + supply + "/type") as f:
supply_type = f.read()[:-1]
if supply_type == "Mains":
# we found an AC
try:
with open(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(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
def set_governor():
cpu_count = os.cpu_count()
for cpu in range(cpu_count):
governor_file_path = f'/sys/devices/system/cpu/cpu{cpu}/cpufreq/scaling_governor'
try:
with open(governor_file_path, 'w') as file:
file.write("powersave")
except:
exit(1)
def set_epp(epp_value):
cpu_count = os.cpu_count()
for cpu in range(cpu_count):
epp_file_path = f'/sys/devices/system/cpu/cpu{cpu}/cpufreq/energy_performance_preference'
try:
with open(epp_file_path, 'w') as file:
file.write(epp_value)
except:
exit(1)
def main():
check_root()
check_driver()
epp_state_for_AC, epp_state_for_BAT = read_config()
while True:
set_governor()
if charging():
set_epp(epp_state_for_AC)
else:
set_epp(epp_state_for_BAT)
time.sleep(2)
if __name__ == "__main__":
main()