-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpufreq
executable file
·229 lines (183 loc) · 5.17 KB
/
cpufreq
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
#!/usr/bin/env bash
# ┏━╸┏━┓╻ ╻┏━╸┏━┓┏━╸┏━┓
# ┃ ┣━┛┃ ┃┣╸ ┣┳┛┣╸ ┃┓┃
# ┗━╸╹ ┗━┛╹ ╹┗╸┗━╸┗┻┛
#
# manage CPU frequency scaling
# * displays available scaling modes
# * displays current scaling governor
#
# deps: <notify-send>
# deps optional: <yad>
set -o errexit
set -o pipefail
PROG=$(basename "$0")
AVAILABLE="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
CURRENT=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
GOVERNOR=(/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
# notification
DISPLAY=${DISPLAY:-:0}
# always ask password
sudo --askpass --reset-timestamp
# colors
RED=$(tput setaf 1)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
BOLD=$(tput bold)
WHITE=$(tput setaf 7)
RESET="$(tput sgr0)"
function help_modes {
local scaling_available
IFS=" " read -r -a scaling_available <<<"$(cat "$AVAILABLE")"
for available in "${scaling_available[@]}"; do
printf " - %s\n" "$available"
done
}
function usage {
cat <<-_EOF
usage: $PROG [OPTIONS]
Sets the cpu scaling mode
available:
$(help_modes)
options:
-s, --set Set mode <MODE>
-c, --current Display current governor
-m, --menu Display governors to chose
-d, --dialog Display a dialog to chose from
-h, --help Display this help and exit
_EOF
}
function log_err {
local msg="$1"
printf "%s: %s\n" "$PROG" "$msg" >&2
exit 1
}
function logme {
local msg="$1"
local color="${2:-$BLUE}"
printf "%s: %b%s%b\n" "$PROG" "$color" "$msg" "$RESET"
}
function send_notification {
if [[ -z "$DISPLAY" ]]; then
return
fi
if ! command -v notify-send >/dev/null; then
logme "'notify-send' not found"
return
fi
local prog
local mesg="<b>$1</b>"
local icon="${2:-dialog-warning}"
local hints="string:x-canonical-private-synchronous:cpufreq"
prog=$(echo "$PROG" | tr '[:lower:]' '[:upper:]')
notify-send -r 666 -a "$PROG" -i "$icon" "$prog" "$mesg" -h "$hints"
}
function get_notification_icon {
local mode="$1"
if [[ "$mode" = "performance" ]]; then
echo "power-profile-performance-symbolic"
return
fi
echo "power-profile-power-saver-symbolic"
return
}
function mode_available {
local mode="$1"
local scaling_available available
IFS=" " read -r -a scaling_available <<<"$(cat "$AVAILABLE")"
for available in "${scaling_available[@]}"; do
if [[ "$mode" == "$available" ]]; then
logme "mode '${mode}' is available" "$MAGENTA"
return 0
fi
done
logme "mode '$mode' not available" "$RED"
mode_list
return 1
}
function mode_list {
local scaling_available
IFS=" " read -r -a scaling_available <<<"$(cat "$AVAILABLE")"
logme "available modes"
for available in "${scaling_available[@]}"; do
printf " - %b%s%b\n" "$MAGENTA" "$available" "$RESET"
done
}
function mode_check {
local mode="$1"
[[ -z "$mode" ]] && logme "err mode not found" "$RED" && exit 130
if ! mode_available "$mode"; then
return 1
fi
return 0
}
function mode_set {
local mode="$1"
if ! mode_check "$mode"; then
return 1
fi
if [[ "$mode" = "$CURRENT" ]]; then
logme "already in profile: $mode"
send_notification "current: <i>$mode</i>" "$(get_notification_icon "$mode")"
return
fi
logme "setting to '$mode' mode" "$BLUE"
echo "$mode" | sudo -p "[sudo] setting to '$mode' : " tee "${GOVERNOR[@]}" >/dev/null
send_notification "setting profile: <i><b>$mode</b></i>" "$(get_notification_icon "$mode")"
}
function show_current {
logme "$CURRENT"
}
function prompt {
local color="$1"
local mesg="$2"
echo -ne "${color}${BOLD}${mesg}${RESET}"
}
function show_menu {
local governors
IFS=" " read -r -a governors <<<"$(cat "$AVAILABLE")"
while true; do
count=1
prompt "${WHITE}" "select mode:\n"
echo
# show options
for gov in "${governors[@]}"; do
printf " %s. %s\n" "$count" "$gov"
count=$((count + 1))
done
prompt "${BLUE}" "\nenter choice: "
read -r CHOSEN
# set mode
case "$CHOSEN" in
[1-9] | [1-9][0-9] | [1-9][0-9][0-9])
mode_set "${governors[$((CHOSEN - 1))]}"
break
;;
*) ;;
esac
clear
done
}
function show_dialog {
local scaling_available selection
declare -a yad_args=(--title="Select Mode" --list --column="Mode")
IFS=" " read -r -a scaling_available <<<"$(cat "$AVAILABLE")"
if ! command -v yad >/dev/null; then
log_err "'yad' not found"
fi
selection=$(yad "${yad_args[@]}" "${scaling_available[@]}" 2>/dev/null)
[[ -z "$selection" ]] && log_err "action aborted"
selection=${selection%?}
mode_set "$selection"
}
function main {
case "${1-}" in
-s | *set) shift && mode_set "$@" ;;
-c | *current) show_current ;;
-m | *menu) show_menu ;;
-d | *dialog) show_dialog ;;
*) usage ;;
esac
return 0
}
main "$@"