-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpu.gov.sh
executable file
·258 lines (238 loc) · 8.42 KB
/
cpu.gov.sh
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
valid_governors=(
powersave
ondemand
performance
conservative
userspace)
initd_file="cpu_gov" #use name without sh for global use without sh out of /usr/bin
first_cmd_arg="$1"
second_cmd_arg="$2"
# testing if governor tests works and change of startup script
function governor_changer {
sudo echo $1 | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > /dev/null 2>&1
RESULT=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
if [ "$RESULT" == "$1" ]; then
local_fs='$local_fs'
network='$network'
named='$named'
time='$time'
syslog='$syslog'
echo -e "#!/bin/sh\n### BEGIN INIT INFO\n# Provides: "$initd_file"\n# Required-Start: $local_fs $network $named $time $syslog\n# Required-Stop: $local_fs $network $named $time $syslog\n# Default-Start: 2 3 4 5\n# Default-Stop: 0 1 6\n# Description: Script to automatically set cpu governors and ondemand threshold to desired value at boot\n### END INIT INFO\n\nsudo echo $1 | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" | sudo tee /etc/init.d/cpu_gov > /dev/null 2>&1
echo "Success: Set governor to $RESULT"
else
echo "Failed. You probably don't have sudo rights"
fi
}
function governor_cmd {
if [[ "$first_cmd_arg" == "--governor" ]] || [[ "$first_cmd_arg" == "-g" ]]; then
INPUT_GOVERNOR="$second_cmd_arg"
for i in "${valid_governors[@]}"
do
if [ "$i" == "$INPUT_GOVERNOR" ]; then
governor_changer "$INPUT_GOVERNOR"
if [ "$INPUT_GOVERNOR" == "ondemand" ] || [ "$INPUT_GOVERNOR" == "conservative" ]; then
echo 'Your custom threshold does not stay conserved when changing it. use gui or "-t" to change it to your desired value'
fi
exit
fi
done
echo "$second_cmd_arg is not a valid governor"
exit
fi
}
function gui {
OPTION=$(whiptail --title "CPU.gov" --menu "Choose standard cpu governor" 25 92 16 \
"powersave" "locks the CPU frequency at the lowest frequency set by the user"\
"ondemand" "gets clocked to max at about 90% load"\
"performance" "locks the CPU at maximum frequency"\
"conservative" "larger, more persistent load take place before CPU clockspeed will rise"\
"userspace" "allows any program executed by the user to set the CPU's operating frequency" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
governor_changer "$OPTION"
if [ "$OPTION" == "ondemand" ] || [ "$OPTION" == "conservative" ]; then
if (whiptail --title "Governor Threshold" --yes-button "Change Threshold" --no-button "No don't change" --yesno "Would you like to change to threshold at which CPU clocks at max speed? (0-100)" 10 60) then
if [[ "$OPTION" == "conservative" ]];then
threshold_input=$(whiptail --title "UP-Threshold Input" --inputbox "What is your desired threshold at which CPU clocks at max speed? (0-100)" 10 60 80 3>&1 1>&2 2>&3)
else
threshold_input=$(whiptail --title "UP-Threshold Input" --inputbox "What is your desired threshold at which CPU clocks at max speed? (0-100)" 10 60 95 3>&1 1>&2 2>&3)
fi
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "Your desired threshold is:" $threshold_input
if [[ $threshold_input -le 100 ]] && [[ $threshold_input -gt 0 ]]; then
change_up_threshold "$threshold_input"
else
echo "Not a valid threshold. Did not change"
fi
else
echo "Ok did not change threshold."
fi
exit
else
echo "Ok did not change threshold"
exit
fi
fi
exit
else
echo "You chose Cancel therefore failed"
fi
}
function change_up_threshold {
if [[ $1 -le 100 ]] && [[ $1 -ge 0 ]]; then
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
if [[ "$governor" == "ondemand" ]] || [[ "$governor" == "conservative" ]]; then
echo $1 | sudo tee /sys/devices/system/cpu/cpufreq/"$governor"/up_threshold > /dev/null 2>&1
echo "echo $1 | sudo tee /sys/devices/system/cpu/cpufreq/$governor/up_threshold" | sudo tee --append /etc/init.d/cpu_gov > /dev/null 2>&1
else
echo "Failed to change threshold. Not the correct governor."
fi
fi
}
function threshold_input_handler {
if [ "$first_cmd_arg" == "-t" ] || [ "$first_cmd_arg" == "--threshold" ]; then
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
if [[ "$governor" != "ondemand" ]] && [[ "$governor" != "conservative" ]]; then
PS3='Which governor that supports thresholds should be activated: '
options=("ondemand" "conservative" "Cancel")
select opt in "${options[@]}"
do
case $opt in
"ondemand")
governor_changer "ondemand"
break
;;
"conservative")
governor_changer "conservative"
break
;;
"Cancel")
exit
;;
*) echo invalid option;;
esac
done
fi
if [[ $second_cmd_arg -le 100 ]] && [[ $second_cmd_arg -ge 0 ]]; then
change_up_threshold "$second_cmd_arg"
else
echo "Not a valid threshold"
fi
echo "done changed governor to ondemand and threshold to $second_cmd_arg"
exit
fi
}
function show_state {
if [ "$first_cmd_arg" == "-s" ] || [ "$first_cmd_arg" == "--show" ]; then
RESULT=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
echo "Current Governor: $RESULT"
CURRENT_FREQ=$(sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq)
CURRENT_FREQ=$(expr "$CURRENT_FREQ" / 1000)
echo "Current CPU freq: $CURRENT_FREQ mhz"
if [[ "$RESULT" == "ondemand" ]] || [[ "$RESULT" == "conservative" ]];then
RESULT_THRESHOLD=$(cat /sys/devices/system/cpu/cpufreq/$RESULT/up_threshold)
echo "$RESULT threshold: $RESULT_THRESHOLD %"
fi
exit
fi
}
function dialog_handler {
if [ "$first_cmd_arg" == "-d" ] || [ "$first_cmd_arg" == "--dialog" ]; then
PS3='Which governor should be activated: '
options=("powersave" "ondemand" "performance" "userspace" "conservative" "Cancel")
select opt in "${options[@]}"
do
case $opt in
"powersave")
governor_changer "powersave"
break
;;
"ondemand")
governor_changer "ondemand"
read -p "Change CPU boost threshold (empty leaves default): " threshold
threshold=${threshold:-95}
if [[ $threshold != 95 ]]; then
change_up_threshold $threshold
fi
break
;;
"performance")
governor_changer "performance"
break
;;
"userspace")
governor_changer "userspace"
break
;;
"conservative")
governor_changer "conservative"
read -p "Change CPU boost threshold (empty leaves default): " threshold
threshold=${threshold:-80}
if [[ $threshold != 80 ]]; then
change_up_threshold $threshold
fi
break
;;
"Cancel")
exit
;;
*) echo invalid option;;
esac
done
exit
fi
}
function uninstall_handler {
if [ "$first_cmd_arg" == "-u" ] || [ "$first_cmd_arg" == "--uninstall" ]; then
read -p "Are you sure you want to uninstall cpu.gov? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "Cancelled, did not uninstall"
exit
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
sudo update-rc.d cpu_gov remove
sudo rm /usr/local/bin/cpu.gov
sudo rm /etc/init.d/cpu_gov
if [[ $(sudo cat /etc/init.d/cpu_gov 3>&1 1>&2 2>&3) == *"No such file or directory"* ]] && [[ $(sudo cat /usr/local/bin/cpu.gov 3>&1 1>&2 2>&3) == *"No such file or directory"* ]]; then
echo "CPU.GOV is now uninstalled"
exit
fi
echo "Uninstall was not succesfull"
exit
fi
}
function help_display {
echo "Usage:"
echo ""
echo "start with no arguments -> starts with gui to choose governor"
echo "-d --dialog use pseudo gui dialogs"
echo "-h --help display this help information"
echo "-s --show display active governor and threshold of ondemand if active"
echo "-g --governor give a valid cpu governor as argument two"
echo "-t --threshold changes governor to ondemand and give a valid ondemand threshold as argument two"
echo "-u --uninstall removes cpu.gov from system (all changes back to kernel standard after reboot)"
}
function check_invalid_args_and_help {
if [ "$first_cmd_arg" == "-h" ] || [ "$first_cmd_arg" == "--help" ]; then
help_display
exit
fi
if [[ -z "${first_cmd_arg// }" ]] && [[ -z "${second_cmd_arg// }" ]]; then
echo "No valid command line argument given. starting gui"
else
echo "Invalid arguments given. Please use this:"
help_display
exit
fi
}
show_state
governor_cmd
threshold_input_handler
dialog_handler
uninstall_handler
check_invalid_args_and_help
gui