-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpumode
298 lines (262 loc) · 8.13 KB
/
cpumode
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#! /bin/bash
#----------------------------------------------------------------------
# Description: Put CPU to and backto full powersave mode
# Author: Artem S. Tashkinov
# Created at: Sun Jun 19 00:14:40 2005
# System: Linux 2.6.12-k8 on i686
#
# Thu Jul 1 19:18:10 2010
# 1.0 Added SMP support
#
# Sun Dec 25 16:50:36 2011
# 1.1 Completely reworked modules loading mechanism; userspace governor
#
# Sun Jul 22 05:30:16 2012
# 1.2 Set/Reset; Improved output
#
# Mon Aug 6 00:46:25 2012
# 1.2.1 unified userspace and maxfreq; unified everything; more error checkings
#
# Mon Feb 11 06:19:04 2013
# 1.2.2 --now shows frequency in MHz
#
# Fri May 3 00:41:13 2013
# 1.2.3 I've discovered that after suspend/resume max CPU cores 2, 3 and 4
# frequences have been reset. This script "thought" those numbers are always
# the same, now we tread carefully and check if they are indeed different
# Also fix userspace set frequency - maxfreq also has to be adjusted
#
# Sun Aug 6 03:01:58 2017
# 1.2.4 add an option to show available frequencies
#
# Mon 14 Oct 2019 04:21:23 PM
# 1.2.5 add boost support
# truncate the resulting frequencies
#
# Wed Mar 23 09:35:13 PM 2022
# 1.2.6 add schedutil governor support
# show the CPU frequency driver
#
# Tue Apr 5 06:18:12 AM 2022
# 1.2.7 fix setting [max] CPU frequency with amd-pstate
#
# Fri Jun 17 01:04:33 PM 2022
# 1.2.8 multiple fixes for intel-pstate
#
# Thu Jun 23 07:38:05 AM 2022
# 1.2.9 multiple optimizations and fixes for intel-pstate boost
#
# Copyright (c) 2005-2022 Artem S. Tashkinov. All rights reserved.
#----------------------------------------------------------------------
#set -x
#PS4='+ $(date "+%s.%N ($LINENO) ")'
version=1.2.9
DRIVER=acpi_cpufreq
lead=/sys/devices/system/cpu
cpu0=$lead/cpu0/cpufreq
min0=`cat $cpu0/cpuinfo_min_freq 2>/dev/null`
max0=`cat $cpu0/cpuinfo_max_freq 2>/dev/null`
lim0=`cat $cpu0/scaling_max_freq 2>/dev/null`
favailfreq=$cpu0/scaling_available_frequencies
SG=$lead/cpu*/cpufreq/scaling_governor
SD=`cat $cpu0/scaling_driver 2>/dev/null`
hz2mhz()
{
echo "$@" | awk 'END{ORS=" "; for (i=1;i<=NF;i++) print int($i/1000)"MHz"}' | sed 's/.$//'
}
setgov()
{
echo "$1" | tee $SG &> /dev/null
test -n "`cat $SG | grep -v "$1"`" && echo "Failed swithing to the '$1' governor! Might not be supported or compiled in." && exit 100
echo "$1 mode/governor has been activated"
}
getgov()
{
echo "Governor: `cat $SG | sort | uniq`"
}
boost()
{
on=1
off=0
fboost=/sys/devices/system/cpu/cpufreq/boost
test "$SD" = "intel_pstate" && fboost=/sys/devices/system/cpu/intel_pstate/no_turbo && on=0 && off=1
echo -n "Boost: "
test ! -f "$fboost" && echo "is not supported" && return
if [ -z "$1" ]; then
test "`cat $fboost`" == "$on" && echo "enabled" || echo "disabled"
else
arg=`echo "$1" | tr A-Z a-z`
if [ "$arg" == "1" -o "$arg" == "on" -o "$arg" == "yes" ]; then
echo "$on" > $fboost
test "`cat $fboost`" == "$on" && echo "enabled" || echo "FAILED to enable"
else
echo "$off" > $fboost
test "`cat $fboost`" == "$off" && echo "disabled" || echo "FAILED to disable"
fi
fi
}
availfreq()
{
echo -n "The available frequencies are: "
if [ -f "$favailfreq" ]; then
cat "$favailfreq"
else
echo "anything from $(hz2mhz $min0) ($min0 KHz) to $(hz2mhz $max0)($max0 KHz)"
fi
}
setspeed()
{
allowed=`cat $favailfreq 2>/dev/null`
if [ -z "$2" ]; then
echo -n "Need an argument to continue. "
availfreq
return 1
fi
if [ -z "`echo "$allowed" | grep -w "$2"`" -a -f $favailfreq ]; then
echo "'$2' is not amongst the allowed frequencies: "
echo $allowed
return 2
fi
if [ "$1" == "userspace" ]; then
setgov userspace
echo -n "Setting user defined frequency of `hz2mhz $2` ... "
echo "$2" | tee $lead/cpu*/cpufreq/scaling_max_freq > /dev/null
echo "$2" | tee $lead/cpu*/cpufreq/scaling_setspeed > /dev/null
[ "`cat $cpu0/scaling_setspeed 2>/dev/null`" == "$2" ] && echo OK || echo Failed
elif [ "$1" == "maxfreq" ]; then
getgov
echo "Setting maximum frequency of `hz2mhz $2`"
echo "$2" | tee $lead/cpu*/cpufreq/scaling_max_freq > /dev/null
echo "Result: $(cat $lead/cpu*/cpufreq/scaling_max_freq | awk '{print $1/1000"MHz"}' | uniq)"
else
echo "Don't know what to do"
fi
}
if [ ! -n "$SD" -a "$1" != "-l" -a "$1" != "--load" ]; then
echo "Error: cpufrequency subsystem is not running!"
exit 1
fi
governors=`cat $cpu0/scaling_available_governors 2>&1`
for i in performance conservative userspace powersave ondemand; do
cgovern=`echo $governors | grep $i`
if [ -z "$cgovern" ]; then
#echo "CPU governor '$i' is not present" # intel_pstate doesn't work with conservative userspace ondemand - no idea why - let's silence everything
module="cpufreq_$i"
#echo "Installing $module ... " # likewise
modprobe $module >/dev/null 2>&1
if [ "$?" != "0" ]; then
echo "Error: can't load $module module. Do you have it installed?"
exit 1
else
#echo "OK"
:
fi
fi
done
test "$1" = "-l" -o "$1" = "--load" || current=`cat $SG`
case $1 in
--version|-v) #print the version number
echo "`basename $0` version "$version
exit 0;;
--help|-h) #Display the help screen
echo "Usage: `basename $0` [OPTION]"
echo " -M --maximum - performance mode (minimum powersavings)"
echo " -m --mimimum - powersave mode (minimum performance)"
echo " -a --auto - ondemand mode (rapid auto performance)"
echo " -b --boost - enable/disable boost"
echo " -c --conservative - conservative mode (gradual auto performance)"
echo " -s --schedutil - schedutil governor"
echo " -u --userspace FREQ - user defined CPU frequency"
echo " -l --load - load CPU powersave driver"
echo " -n --now - show current mode"
echo " -S --set FREQ - set maximum CPU frequency"
echo " -r --reset - reset maximum CPU frequency"
echo " -f --frequencies - show available frequencies"
echo " -t --time - percentage of time spent in states"
echo " -v --version - show application version and exit"
echo " -h --help - show this help and exit"
exit 0;;
--load|-l)
KDRIVER=`echo $DRIVER | sed 's/-/_/g'`
if [ -z "`lsmod | grep $KDRIVER`" ]; then
modprobe $DRIVER && echo "CPU powersave driver has been loaded successfully"
else
echo "CPU powersave driver is already loaded"
fi
exit;;
--userspace|-u)
setspeed userspace "$2"
exit;;
--conservative|-c)
setgov conservative
exit;;
--schedutil|-s)
setgov schedutil
exit;;
--minimum|-m)
setgov powersave
exit;;
--maximum|-M)
setgov performance
exit;;
--set|-S)
setspeed maxfreq "$2"
exit;;
--reset|-r)
getgov
echo "Resetting maximum frequency ... "
echo $max0 | tee $lead/cpu*/cpufreq/scaling_max_freq > /dev/null
cat $lead/cpu*/cpufreq/scaling_max_freq
exit;;
--auto|-a)
setgov ondemand
exit;;
--boost|-b)
boost "$2"
exit;;
--frequencies|-f)
availfreq
exit;;
--time|-t)
if [ -f /sys/devices/system/cpu/cpufreq/policy0/stats/time_in_state ]; then
awk '{time[$1]+=$2;sum+=$2}END{for (var in time) print var,":",time[var]/sum*100,"%"}' /sys/devices/system/cpu/cpufreq/policy*/stats/time_in_state
else
echo "time_in_state is not supported"
fi
exit;;
--now|-n|*)
echo "Driver: $SD"
boost
getgov
cores=`cat $lead/cpu*/cpufreq/scaling_cur_freq 2>/dev/null`
if [ -n "$cores" ]; then
echo -n "Average and maximum frequencies now: "
hz2mhz `echo $cores | awk 'BEGIN{max=0;avr=0} {for (i=1;i<=NF;i++) {if ($i>max) max=$i; avr+=$i}} END{print avr/NF, max}'`
echo
#echo -n "Cores are at: " # no one cares considering there are PCs with 32 virtual cores
#hz2mhz $cores
#echo
else
echo "This should have never happened!"
fi
# Check if maximum CPU frequences are mixed or maxed out
for i in `cat $lead/cpu*/cpufreq/scaling_max_freq`; do
this=$i
test "$max0" != "$i" && notmaxed=1
test "$next" != "$this" -a -n "$vin" && nonuni=1 # vin - do not run on a first loop invocation
next=$this
vin=1
done
if [ -z "$notmaxed" ]; then
echo "No frequency limit is enforced."
else
if [ -n "$nonuni" ]; then
echo "Maximum CPU frequency per CPU is :"
hz2mhz `cat $lead/cpu*/cpufreq/scaling_max_freq`
echo
else
echo "Maximum allowed frequency is `hz2mhz $lim0` (out of `hz2mhz $max0`)"
fi
fi
exit;;
esac