1 |
#!/bin/sh |
2 |
|
3 |
# the following is the LSB init header see |
4 |
# http://www.linux-foundation.org/spec//booksets/LSB-Core-generic/LSB-Core-generic.html#INITSCRCOMCONV |
5 |
# |
6 |
### BEGIN INIT INFO |
7 |
# Provides: cpuspeed |
8 |
# Should-Start: |
9 |
# Default-Start: 1 2 3 4 5 |
10 |
# Short-Description: processor frequency scaling support |
11 |
# Description: This script enables/disables processor frequency |
12 |
# scaling support, either using the cpuspeed daemon |
13 |
# or in-kernel frequency scaling support |
14 |
### END INIT INFO |
15 |
|
16 |
# the following is the chkconfig init header |
17 |
# |
18 |
# cpuspeed: processor frequency scaling support |
19 |
# |
20 |
# chkconfig: 12345 13 99 |
21 |
# description: Run dynamic CPU speed daemon and/or load appropriate |
22 |
# cpu frequency scaling kernel modules and/or governors |
23 |
# |
24 |
|
25 |
# Source function library. |
26 |
. /etc/rc.d/init.d/functions |
27 |
|
28 |
prog="cpuspeed" |
29 |
|
30 |
[ -f /usr/sbin/$prog ] || exit 5 |
31 |
|
32 |
# Get config. |
33 |
if [ -f /etc/sysconfig/$prog ]; then |
34 |
. /etc/sysconfig/$prog |
35 |
fi |
36 |
|
37 |
cpu0freqd=/sys/devices/system/cpu/cpu0/cpufreq |
38 |
globfreq='/sys/devices/system/cpu' |
39 |
cpufreq="${globfreq}/cpufreq" |
40 |
cpus="${globfreq}/cpu[0-9]*" |
41 |
testpat="${cpus}/cpufreq/scaling_driver" |
42 |
lockfile="/var/lock/subsys/$prog" |
43 |
xendir="/proc/xen" |
44 |
logger="/usr/bin/logger -p info -t $prog" |
45 |
IGNORE_NICE=${IGNORE_NICE:-0} |
46 |
module_loaded=false |
47 |
|
48 |
some_file_exist() { |
49 |
while [ "$1" ] ; do |
50 |
[ -f "$1" ] && return 0 |
51 |
shift |
52 |
done |
53 |
return 1 |
54 |
} |
55 |
|
56 |
governor_is_module() { |
57 |
# Check to see if the requested cpufreq governor |
58 |
# is provided as a kernel module or not |
59 |
module_info=`/sbin/modinfo cpufreq-${governor} > /dev/null 2>&1` |
60 |
return $? |
61 |
} |
62 |
|
63 |
is_p4_clockmod() { |
64 |
if [ `/sbin/lsmod | grep -c -w "p4.clockmod"` -ge 1 -a -d "/sys/devices/system/cpu/cpu0/thermal_throttle" ]; then |
65 |
return 0 |
66 |
fi |
67 |
return 1 |
68 |
} |
69 |
|
70 |
governor_module_loaded() { |
71 |
# Check to see if we have a module loaded for |
72 |
# the current cpufreq governor |
73 |
if [ -e ${cpu0freqd}/scaling_governor ]; then |
74 |
governor=`cat ${cpu0freqd}/scaling_governor` |
75 |
else |
76 |
governor="none" |
77 |
fi |
78 |
if [ "${governor}" != "none" -a `/sbin/lsmod | grep -c -w "cpufreq.${governor}"` -ge 1 ]; then |
79 |
return 0 |
80 |
fi |
81 |
return 1 |
82 |
} |
83 |
|
84 |
adjust_cpufreq() { |
85 |
# First arg is a param under $cpu/cpufreq/ |
86 |
# Second arg is the value you want to set that param to |
87 |
if $(echo ${1} | grep -qE '(threshold|ignore_nice_load)'); then |
88 |
[ -f $cpufreq/$1 ] && echo $2 > $cpufreq/$1 |
89 |
else |
90 |
for cpu in ${cpus}; do |
91 |
[ -f $cpu/cpufreq/$1 ] && echo $2 > $cpu/cpufreq/$1 |
92 |
done |
93 |
fi |
94 |
} |
95 |
|
96 |
start_cpuspeed() { |
97 |
echo -n $"Starting $prog: " |
98 |
# cpuspeed daemon thresholds are specified as idle percentages, |
99 |
# cpufreq modules as busy percentages, so we need to do some |
100 |
# math here for use of unified config... |
101 |
# DOWN_THRESHOLD doesn't mean exactly the same thing for |
102 |
# cpuspeed as it does for the cpufreq governors, but close |
103 |
# enough, and if not specified, we use same defaults as governors. |
104 |
if [ -n "$UP_THRESHOLD" ]; then |
105 |
let UP_THRESHOLD=100-$UP_THRESHOLD |
106 |
else |
107 |
UP_THRESHOLD=20 |
108 |
fi |
109 |
if [ -n "$DOWN_THRESHOLD" ]; then |
110 |
let DOWN_THRESHOLD=100-$DOWN_THRESHOLD |
111 |
else |
112 |
DOWN_THRESHOLD=80 |
113 |
fi |
114 |
OPTS="$OPTS -r -p $UP_THRESHOLD $DOWN_THRESHOLD" |
115 |
if [ -n "$MIN_SPEED" ]; then |
116 |
OPTS="$OPTS -m $MIN_SPEED" |
117 |
fi |
118 |
if [ -n "$MAX_SPEED" ]; then |
119 |
OPTS="$OPTS -M $MAX_SPEED" |
120 |
fi |
121 |
if [ "$IGNORE_NICE" -eq 0 ]; then |
122 |
OPTS="$OPTS -n" |
123 |
fi |
124 |
daemon $prog -d $OPTS |
125 |
RETVAL=$? |
126 |
return $RETVAL |
127 |
} |
128 |
|
129 |
stop_cpuspeed() { |
130 |
if [ -n "`pidof $prog`" ]; then |
131 |
echo -n $"Stopping $prog: " |
132 |
killproc $prog -USR1 |
133 |
killproc $prog -INT |
134 |
fi |
135 |
if [ -n "`pidof $prog`" ]; then |
136 |
killproc $prog |
137 |
fi |
138 |
RETVAL=$? |
139 |
return $RETVAL |
140 |
} |
141 |
|
142 |
start() { |
143 |
if [ $(id -u) -ne 0 ]; then |
144 |
echo -n "Insufficient privileges to start cpuspeed service: " |
145 |
failure; echo |
146 |
return 4 |
147 |
fi |
148 |
if [ ! -f $lockfile ] && [ ! -d "$xendir" ]; then |
149 |
cpu_vendor=$(awk '/vendor_id/{print $3}' /proc/cpuinfo | tail -n 1) |
150 |
cpu_family=$(awk '/cpu family/{print $4}' /proc/cpuinfo | tail -n 1) |
151 |
if ! some_file_exist $testpat ; then |
152 |
# Attempt to load scaling_driver if not loaded |
153 |
# but it is configured |
154 |
if [ -n "$DRIVER" ]; then |
155 |
/sbin/modprobe "$DRIVER" |
156 |
elif [ -d /proc/acpi ]; then |
157 |
/sbin/modprobe pcc-cpufreq 2> /dev/null |
158 |
if [ ! -d ${cpu0freqd} ]; then |
159 |
/sbin/modprobe -r pcc-cpufreq 2> /dev/null |
160 |
if [ "$cpu_vendor" == AuthenticAMD ] && [ "$cpu_family" -ge 7 ]; then |
161 |
# Try loading powernow-k8 if this is an AMD processor, |
162 |
# family 7 or greater (Athlon XP/MP was family 6) |
163 |
pk8m=$(/sbin/modinfo powernow-k8 > /dev/null 2>&1) |
164 |
if [ "$?" -eq 0 ]; then |
165 |
/sbin/modprobe powernow-k8 2> /dev/null |
166 |
[ -d ${cpu0freqd} ] || /sbin/modprobe -r powernow-k8 2> /dev/null |
167 |
fi |
168 |
else |
169 |
# use ACPI as a fallback |
170 |
/sbin/modprobe acpi-cpufreq 2> /dev/null |
171 |
# if even ACPI didn't work, remove it |
172 |
# and then next test will bail out. |
173 |
[ -d ${cpu0freqd} ] || /sbin/modprobe -r acpi-cpufreq 2> /dev/null |
174 |
fi |
175 |
fi |
176 |
fi |
177 |
if [ ! -d ${cpu0freqd} -a "$cpu_vendor" == GenuineIntel ]; then |
178 |
# last-ditch effort for Intel proc boxes, try our neutered p4-clockmod |
179 |
# to get at least passive cooling support (no clock changes) |
180 |
/sbin/modprobe p4-clockmod 2> /dev/null |
181 |
[ -d ${cpu0freqd} ] || /sbin/modprobe -r p4-clockmod 2> /dev/null |
182 |
fi |
183 |
fi |
184 |
|
185 |
# If we get this far with no driver, we must have no scaling. |
186 |
# We're doomed. |
187 |
[ ! -f ${cpu0freqd}/scaling_driver ] && return 6 |
188 |
|
189 |
# Okay, we have a driver, carry on... |
190 |
drv=`cat ${cpu0freqd}/scaling_driver` |
191 |
|
192 |
# Figure out default governor to use |
193 |
case "$drv" in |
194 |
centrino|powernow-k8|pcc-cpufreq|acpi-cpufreq|e_powersaver) |
195 |
default_governor=ondemand |
196 |
;; |
197 |
p4-clockmod) |
198 |
# not actually a governor, we want to bail without doing either |
199 |
# in-kernel scaling or starting up the cpuspeed daemon in this case |
200 |
default_governor=p4passive |
201 |
;; |
202 |
intel_pstate) |
203 |
default_governor=powersave |
204 |
echo "CPU states managed by intel_pstate; use cpupower for configuration." |
205 |
$logger "CPU states managed by intel_pstate; use cpupower for configuration." |
206 |
;; |
207 |
*) |
208 |
default_governor=userspace |
209 |
;; |
210 |
esac |
211 |
governor=${GOVERNOR:-${default_governor}} |
212 |
|
213 |
if [ "${governor}" == "p4passive" ]; then |
214 |
echo -n "Enabling p4-clockmod driver (passive cooling only): " |
215 |
success; echo |
216 |
return 0 |
217 |
fi |
218 |
|
219 |
# Load governor module, if need be, and validate |
220 |
governor_is_module && /sbin/modprobe cpufreq-${governor} |
221 |
if [ `grep -c -w ${governor} ${cpu0freqd}/scaling_available_governors` -ge 1 ]; then |
222 |
$logger "Enabling ${governor} cpu frequency scaling governor" |
223 |
else |
224 |
$logger "Invalid governor \"${governor}\" specified, falling back to ${default_governor}" |
225 |
governor_is_module && /sbin/modprobe -r cpufreq-${governor} |
226 |
governor=${default_governor} |
227 |
governor_is_module && /sbin/modprobe cpufreq-${governor} |
228 |
fi |
229 |
|
230 |
# Set governor |
231 |
adjust_cpufreq scaling_governor ${governor} |
232 |
|
233 |
# Run cpuspeed daemon for userspace gov, kernel ones otherwise |
234 |
if [ "${governor}" == "userspace" ]; then |
235 |
start_cpuspeed |
236 |
RETVAL=$? |
237 |
else |
238 |
if [ -n "$MIN_SPEED" ]; then |
239 |
adjust_cpufreq scaling_min_freq $MIN_SPEED |
240 |
else |
241 |
adjust_cpufreq scaling_min_freq 0 |
242 |
fi |
243 |
if [ -n "$MAX_SPEED" ]; then |
244 |
adjust_cpufreq scaling_max_freq $MAX_SPEED |
245 |
else |
246 |
adjust_cpufreq scaling_max_freq `cat $cpu0freqd/cpuinfo_max_freq` |
247 |
fi |
248 |
if [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; then |
249 |
adjust_cpufreq ondemand/up_threshold $UP_THRESHOLD |
250 |
fi |
251 |
if [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; then |
252 |
adjust_cpufreq conservative/down_threshold $DOWN_THRESHOLD |
253 |
fi |
254 |
if [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; then |
255 |
adjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICE |
256 |
fi |
257 |
echo -n "Enabling ${governor} cpu frequency scaling: " |
258 |
success |
259 |
RETVAL=0 |
260 |
fi |
261 |
echo |
262 |
# Technically, not quite right in non-cpuspeed daemon |
263 |
# cases, but close enough to indicate that we're |
264 |
# doing some sort of cpu frequency scaling. |
265 |
[ $RETVAL = 0 ] && touch $lockfile |
266 |
else |
267 |
if [ -d "$xendir" ]; then |
268 |
$logger "CPU Frequency scaling is currently not supported on xen kernels" |
269 |
fi |
270 |
return 0 |
271 |
fi |
272 |
return $RETVAL |
273 |
} |
274 |
|
275 |
stop() { |
276 |
if [ $(id -u) -ne 0 ]; then |
277 |
echo -n "Insufficient privileges to stop cpuspeed service: " |
278 |
failure; echo |
279 |
return 4 |
280 |
fi |
281 |
is_p4_clockmod && p4status="true" |
282 |
if [ "$p4status" == "true" -a "x${GOVERNOR}" == "x" ]; then |
283 |
echo "p4-clockmod passive cooling support cannot be truly stopped" |
284 |
fi |
285 |
[ ! -f ${cpu0freqd}/scaling_driver ] && return 0 |
286 |
drv=`cat ${cpu0freqd}/scaling_driver` |
287 |
governor_module_loaded && module_loaded=true |
288 |
if [ "${drv}" != "intel_pstate" -a "${governor}" != "userspace" ] || |
289 |
[ "${drv}" == "intel_pstate" -a "${governor}" != "powersave" ]; then |
290 |
echo -n "Disabling ${governor} cpu frequency scaling: " |
291 |
$logger "Disabling ${governor} cpu frequency scaling governor" |
292 |
for cpu in ${cpus}; do |
293 |
if [ "${drv}" == "intel_pstate" ]; then |
294 |
echo powersave > $cpu/cpufreq/scaling_governor |
295 |
else |
296 |
echo userspace > $cpu/cpufreq/scaling_governor |
297 |
cat $cpu/cpufreq/cpuinfo_max_freq > $cpu/cpufreq/scaling_setspeed |
298 |
fi |
299 |
done |
300 |
if [ $module_loaded == true ]; then |
301 |
/sbin/modprobe -r cpufreq-${governor} |
302 |
fi |
303 |
success |
304 |
RETVAL=0 |
305 |
else |
306 |
stop_cpuspeed |
307 |
RETVAL=$? |
308 |
fi |
309 |
echo |
310 |
[ -n "$DRIVER" ] && /sbin/modprobe -r $DRIVER |
311 |
[ $RETVAL = 0 ] && RETVAL=$? |
312 |
[ $RETVAL = 0 ] && rm -f $lockfile |
313 |
return $RETVAL |
314 |
} |
315 |
|
316 |
reload() { |
317 |
if [ $(id -u) -ne 0 ]; then |
318 |
echo -n "Insufficient privileges to stop cpuspeed service: " |
319 |
failure; echo |
320 |
return 4 |
321 |
fi |
322 |
governor_module_loaded && module_loaded=true |
323 |
if [ "${governor}" == "userspace" ]; then |
324 |
failure; echo |
325 |
return 3 |
326 |
else |
327 |
if [ -n "$MIN_SPEED" ]; then |
328 |
adjust_cpufreq scaling_min_freq $MIN_SPEED |
329 |
fi |
330 |
if [ -n "$MAX_SPEED" ]; then |
331 |
adjust_cpufreq scaling_max_freq $MAX_SPEED |
332 |
fi |
333 |
if [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; then |
334 |
adjust_cpufreq ondemand/up_threshold $UP_THRESHOLD |
335 |
fi |
336 |
if [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; then |
337 |
adjust_cpufreq conservative/down_threshold $DOWN_THRESHOLD |
338 |
fi |
339 |
if [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; then |
340 |
adjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICE |
341 |
fi |
342 |
echo -n "Reloading configuration for ${governor}: " |
343 |
success; echo |
344 |
return 0 |
345 |
fi |
346 |
} |
347 |
|
348 |
case "$1" in |
349 |
start) |
350 |
start |
351 |
;; |
352 |
|
353 |
stop) |
354 |
stop |
355 |
;; |
356 |
|
357 |
status) |
358 |
is_p4_clockmod && p4status="true" |
359 |
if [ "$p4status" == "true" -a "x${GOVERNOR}" == "x" ]; then |
360 |
echo "p4-clockmod passive cooling is enabled" |
361 |
exit 0 |
362 |
fi |
363 |
governor_module_loaded && module_loaded=true |
364 |
if [ -d "$xendir" ]; then |
365 |
echo "Frequency scaling not supported under xen kernels" |
366 |
RETVAL=0 |
367 |
elif [ $module_loaded == true -o ${governor} == "performance" ]; then |
368 |
echo "Frequency scaling enabled using ${governor} governor" |
369 |
RETVAL=0 |
370 |
else |
371 |
status $prog |
372 |
RETVAL="$?" |
373 |
fi |
374 |
;; |
375 |
|
376 |
restart|force-reload) |
377 |
stop |
378 |
start |
379 |
;; |
380 |
|
381 |
reload) |
382 |
reload |
383 |
;; |
384 |
|
385 |
condrestart|try-restart) |
386 |
governor_module_loaded && module_loaded=true |
387 |
if [ $module_loaded == true -o -n "`pidof $prog`" -o ${governor} == "performance" ]; then |
388 |
stop |
389 |
start |
390 |
fi |
391 |
;; |
392 |
|
393 |
*) |
394 |
echo $"Usage: $0 {start|stop|restart|condrestart|status}" |
395 |
exit 2 |
396 |
;; |
397 |
esac |
398 |
|
399 |
exit $RETVAL |