-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgovctl_cli.py
More file actions
185 lines (149 loc) · 4.57 KB
/
Copy pathgovctl_cli.py
File metadata and controls
185 lines (149 loc) · 4.57 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
import os
import sys
import json
import argparse
import subprocess
from time import sleep
CONFIG_PATH = "/etc/govctl/config.json"
def load_config() -> None:
with open(CONFIG_PATH, "r") as f:
return json.load(f)
def save_config(config) -> None:
with open(CONFIG_PATH, "w") as f:
json.dump(config, f, indent=4)
def check_root() -> None:
if os.geteuid():
print("Root access required, rerun with sudo.", file=sys.stderr)
sys.exit(1)
def get_cur_gov() -> None:
try:
output = subprocess.check_output(["systemctl", "status", "govctl"], text=True)
last_lines = output.strip().splitlines()[-30:]
last_lines.reverse()
for line in last_lines:
if "Applied governor" in line:
return line[line.find('governor "') + 10 : -1]
except:
pass
def main() -> None:
parser = argparse.ArgumentParser(description="Governor configuration tool")
parser.add_argument(
"-g",
"--set-governor",
choices=["powersave", "conservative", "performance"],
help="Set desired governor",
)
parser.add_argument(
"-G",
"--get-governor",
action="store_true",
help="Get the saved governor for scripting (no newlines/formatting)",
)
parser.add_argument(
"-b",
"--set-battery-governor",
choices=["powersave", "conservative", "performance"],
help="Set desired governor while running on battery power",
)
parser.add_argument(
"-e",
"--enable-battery-detection",
action="store_true",
help="Enable battery state detection",
)
parser.add_argument(
"-d",
"--disable-battery-detection",
action="store_true",
help="Disable battery state detection",
)
parser.add_argument(
"-p",
"--powersave-percent",
type=lambda x: (
int(x)
if 0 <= int(x) <= 80
else (_ for _ in ()).throw(
argparse.ArgumentTypeError(f"{x} not in range 0–80")
)
),
help="Percentage at which powersave triggers",
)
parser.add_argument(
"--set-tdp-boost",
type=int,
help="Set TDP for 'performance' governor (in watts)",
)
parser.add_argument(
"--set-tdp-conservative",
type=int,
help="Set TDP for 'conservative' governor (in watts)",
)
parser.add_argument(
"--set-tdp-powersave",
type=int,
help="Set TDP for 'powersave' governor (in watts)",
)
args = parser.parse_args()
config = None
try:
config = load_config()
except:
print("Failed to load config, resetting..")
config = {
"governor": "performance",
"governor_battery": "conservative",
"detect_battery_state": True,
"powersave_point": 20,
"tdp": {"boost": 50, "conservative": 13, "powersave": 8},
}
if args.get_governor:
gov = get_cur_gov()
print(gov if gov is not None else "Unknown", end="")
sys.exit(0)
modified = False
if args.set_governor:
config["governor"] = args.set_governor
modified = True
if args.set_battery_governor:
config["governor_battery"] = args.set_battery_governor
modified = True
if args.enable_battery_detection:
config["detect_battery_state"] = True
modified = True
if args.disable_battery_detection:
config["detect_battery_state"] = False
modified = True
if args.powersave_percent:
config["powersave_point"] = args.powersave_percent
modified = True
if args.set_tdp_boost is not None:
config["tdp"]["boost"] = args.set_tdp_boost
modified = True
if args.set_tdp_conservative is not None:
config["tdp"]["conservative"] = args.set_tdp_conservative
modified = True
if args.set_tdp_powersave is not None:
config["tdp"]["powersave"] = args.set_tdp_powersave
modified = True
if modified:
check_root()
print("Saving configuration..")
save_config(config)
else:
gov = get_cur_gov()
if gov is not None:
info_str = "Currently applied governor: " + gov
print(
"\n"
+ ("-" * len(info_str))
+ "\n"
+ info_str
+ "\n"
+ ("-" * len(info_str))
+ "\n"
)
parser.print_help()
if __name__ == "__main__":
main()