Subversion Repositories configs

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 - 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
    echo $2 > $cpufreq/$1
89
  else
90
    for cpu in ${cpus}; do
91
      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
      *)
203
        default_governor=userspace
204
        ;;
205
    esac
206
    governor=${GOVERNOR:-${default_governor}}
207
 
208
    if [ "${governor}" == "p4passive" ]; then
209
      echo -n "Enabling p4-clockmod driver (passive cooling only): "
210
      success; echo
211
      return 0
212
    fi
213
 
214
    # Load governor module, if need be, and validate
215
    governor_is_module && /sbin/modprobe cpufreq-${governor}
216
    if [ `grep -c -w ${governor} ${cpu0freqd}/scaling_available_governors` -ge 1 ]; then
217
      $logger "Enabling ${governor} cpu frequency scaling governor"
218
    else
219
      $logger "Invalid governor \"${governor}\" specified, falling back to ${default_governor}"
220
      governor_is_module && /sbin/modprobe -r cpufreq-${governor}
221
      governor=${default_governor}
222
      governor_is_module && /sbin/modprobe cpufreq-${governor}
223
    fi
224
 
225
    # Set governor
226
    adjust_cpufreq scaling_governor ${governor}
227
 
228
    # Run cpuspeed daemon for userspace gov, kernel ones otherwise
229
    if [ "${governor}" == "userspace" ]; then
230
      start_cpuspeed
231
      RETVAL=$?
232
    else
233
      if [ -n "$MIN_SPEED" ]; then
234
        adjust_cpufreq scaling_min_freq $MIN_SPEED
235
      else
236
        adjust_cpufreq scaling_min_freq 0
237
      fi
238
      if [ -n "$MAX_SPEED" ]; then
239
        adjust_cpufreq scaling_max_freq $MAX_SPEED
240
      else
241
        adjust_cpufreq scaling_max_freq `cat $cpu0freqd/cpuinfo_max_freq`
242
      fi
243
      if [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; then
244
        adjust_cpufreq ondemand/up_threshold $UP_THRESHOLD
245
      fi
246
      if [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; then
247
        adjust_cpufreq conservative/down_threshold $DOWN_THRESHOLD
248
      fi
249
      if [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; then
250
        adjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICE
251
      fi
252
      echo -n "Enabling ${governor} cpu frequency scaling: "
253
      success
254
      RETVAL=0
255
    fi
256
    echo
257
    # Technically, not quite right in non-cpuspeed daemon
258
    # cases, but close enough to indicate that we're
259
    # doing some sort of cpu frequency scaling.
260
    [ $RETVAL = 0 ] && touch $lockfile
261
  else
262
    if [ -d "$xendir" ]; then
263
      $logger "CPU Frequency scaling is currently not supported on xen kernels"
264
    fi
265
    return 0
266
  fi
267
  return $RETVAL
268
}
269
 
270
stop() {
271
  if [ $(id -u) -ne 0 ]; then
272
    echo -n "Insufficient privileges to stop cpuspeed service: "
273
    failure; echo
274
    return 4
275
  fi
276
  is_p4_clockmod && p4status="true"
277
  if [ "$p4status" == "true" -a "x${GOVERNOR}" == "x" ]; then
278
    echo "p4-clockmod passive cooling support cannot be truly stopped"
279
  fi
280
  [ ! -f ${cpu0freqd}/scaling_driver ] && return 0
281
  drv=`cat ${cpu0freqd}/scaling_driver`
282
  governor_module_loaded && module_loaded=true
283
 
284
  if [ "${governor}" != "userspace" ]; then
285
    echo -n "Disabling ${governor} cpu frequency scaling: "
286
    $logger "Disabling ${governor} cpu frequency scaling governor"
287
    for cpu in ${cpus}; do
288
      echo userspace > $cpu/cpufreq/scaling_governor
289
      cat $cpu/cpufreq/cpuinfo_max_freq > $cpu/cpufreq/scaling_setspeed
290
    done
291
    if [ $module_loaded == true ]; then
292
      /sbin/modprobe -r cpufreq-${governor}
293
    fi
294
    success
295
    RETVAL=0
296
  else
297
    stop_cpuspeed
298
    RETVAL=$?
299
  fi
300
  echo
301
  [ -n "$DRIVER" ] && /sbin/modprobe -r $DRIVER
302
  [ $RETVAL = 0 ] && RETVAL=$?
303
  [ $RETVAL = 0 ] && rm -f $lockfile
304
  return $RETVAL
305
}
306
 
307
reload() {
308
  if [ $(id -u) -ne 0 ]; then
309
    echo -n "Insufficient privileges to stop cpuspeed service: "
310
    failure; echo
311
    return 4
312
  fi
313
  governor_module_loaded && module_loaded=true
314
  if [ "${governor}" == "userspace" ]; then
315
    failure; echo
316
    return 3
317
  else
318
    if [ -n "$MIN_SPEED" ]; then
319
      adjust_cpufreq scaling_min_freq $MIN_SPEED
320
    fi
321
    if [ -n "$MAX_SPEED" ]; then
322
      adjust_cpufreq scaling_max_freq $MAX_SPEED
323
    fi
324
    if [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; then
325
      adjust_cpufreq ondemand/up_threshold $UP_THRESHOLD
326
    fi
327
    if [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; then
328
      adjust_cpufreq conservative/down_threshold $DOWN_THRESHOLD
329
    fi
330
    if [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; then
331
      adjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICE
332
    fi
333
    echo -n "Reloading configuration for ${governor}: "
334
    success; echo
335
    return 0
336
  fi
337
}
338
 
339
case "$1" in
340
  start)
341
    start
342
    ;;
343
 
344
  stop)
345
    stop
346
    ;;
347
 
348
  status)
349
    is_p4_clockmod && p4status="true"
350
    if [ "$p4status" == "true" -a "x${GOVERNOR}" == "x" ]; then
351
      echo "p4-clockmod passive cooling is enabled"
352
      exit 0
353
    fi
354
    governor_module_loaded && module_loaded=true
355
    if [ -d "$xendir" ]; then
356
      echo "Frequency scaling not supported under xen kernels"
357
      RETVAL=0
358
    elif [ $module_loaded == true -o ${governor} == "performance" ]; then
359
      echo "Frequency scaling enabled using ${governor} governor"
360
      RETVAL=0
361
    else
362
      status $prog
363
      RETVAL="$?"
364
    fi
365
    ;;
366
 
367
  restart|force-reload)
368
    stop
369
    start
370
    ;;
371
 
372
  reload)
373
    reload
374
    ;;
375
 
376
  condrestart|try-restart)
377
    governor_module_loaded && module_loaded=true
378
    if [ $module_loaded == true -o -n "`pidof $prog`" -o ${governor} == "performance" ]; then
379
      stop
380
      start
381
    fi
382
    ;;
383
 
384
  *)
385
    echo $"Usage: $0 {start|stop|restart|condrestart|status}"
386
    exit 2
387
    ;;
388
esac
389
 
390
exit $RETVAL