-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpufreqctl
executable file
·114 lines (85 loc) · 2.4 KB
/
cpufreqctl
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
#!/usr/bin/env bash
CPU_LAST_FILE=/tmp/noahhuppert-alligator-cpufreq-last
CPUS_DIR=/sys/devices/system/cpu
CPU_FREQ_FILE=cpufreq/scaling_governor
check() {
if [[ "$?" != "0" ]]; then
die "$@"
fi
}
bold() {
echo "$(tput bold)$@$(tput sgr0)"
}
die() {
echo "Error: $@" >&2
exit 1
}
# Sets all CPU frequencies
set_cpu_freq() { # ( freq_value )
freq_value="$1"
now_freq=$(cat "${CPUS_DIR}/cpu0/${CPU_FREQ_FILE}")
check "Failed to get CPU0's frequency to save as previous frequency"
prev_freq=$(get_last_cpu_freq)
check "Failed to get previous frequency"
if [[ "$now_freq" != "$prev_freq" ]]; then
echo "$now_freq" > "$CPU_LAST_FILE"
check "Failed to save previous CPU frequency"
fi
echo "$freq_value" | tee "${CPUS_DIR}/"cpu*/"${CPU_FREQ_FILE}"
check "Failed to set CPUs to \"$freq_value\" mode"
bold "Set CPU's frequency scaling to \"$freq_value\" mode"
}
# Gets last CPU frequency
get_last_cpu_freq() {
cat "$CPU_LAST_FILE"
}
while getopts "hpsgu" opt; do
case "$opt" in
h)
cat <<EOF
cpufreqctl - Adjusts the linux Kernel's CPU frequency scaling mechanism
USAGE
cpufreqctl [-h] -p|-r|-s
OPTIONS
-h Show help text
-p Set CPU frequency scale to performance mode
-s Set CPU frequency scale to powersave mode
-g Get CPU frequency scale values
-u Undo to last CPU frequency mode
BEHAVIOR
Options -p, -s, -g, and -u cannot be provided at the
same time.
Stores the state of the CPU frequency before making
any change in
$CPU_LAST_FILE
Uses the file
${CPUS_DIR}/cpu*/${CPU_FREQ_FILE}
to control the Kernel CPU frequency scaling.
EOF
exit 0
;;
p) opt_perf=true ;;
s) opt_save=true ;;
g) opt_get=true ;;
u) opt_undo=true ;;
'?') die "Unknown option" ;;
esac
done
if [ -n "$opt_perf" ] && [ -n "$opt_save" ] && ["$opt_undo" ]; then
die "Options -p, -s, and -u cannot both be provided"
fi
if [ -n "$opt_perf" ]; then
set_cpu_freq performance
elif [ -n "$opt_save" ]; then
set_cpu_freq powersave
elif [ -n "$opt_undo" ]; then
last=$(get_last_cpu_freq)
check "Failed to get last CPU frequency"
set_cpu_freq "$last"
bold "Reverted CPU's frequency"
elif [ -n "$opt_get" ]; then
cat "${CPUS_DIR}/"cpu*/"${CPU_FREQ_FILE}"
check "Failed to get CPU frequencies"
else
die "Must specify options -p, -s, and -u"
fi