cpuspeed.init 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/bin/sh
  2. # Startup script for cpuspeed
  3. #
  4. # chkconfig: 12345 06 99
  5. # description: Run dynamic CPU speed daemon and/or load appropriate
  6. # cpu frequency scaling kernel modules and/or governors
  7. # Source function library.
  8. . /etc/rc.d/init.d/functions
  9. [ -r /proc/cmdline ] && cmdline=$(cat /proc/cmdline)
  10. if (strstr "$cmdline" nocpufreq) ; then exit 0 ; fi
  11. prog="cpuspeed"
  12. [ -f /usr/sbin/$prog ] || exit 5
  13. # Get config.
  14. if [ -f /etc/$prog.conf ]; then
  15. . /etc/$prog.conf
  16. fi
  17. cpufreqd=/sys/devices/system/cpu/cpufreq
  18. cpu0freqd=/sys/devices/system/cpu/cpu0/cpufreq
  19. cpus='/sys/devices/system/cpu/cpu[0-9]*'
  20. testpat="${cpus}/cpufreq/scaling_driver"
  21. lockfile="/var/lock/subsys/$prog"
  22. xendir="/proc/xen"
  23. logger="/usr/bin/logger -p info -t $prog"
  24. IGNORE_NICE=${IGNORE_NICE:-0}
  25. module_loaded=false
  26. some_file_exist() {
  27. while [ "$1" ] ; do
  28. [ -f "$1" ] && return 0
  29. shift
  30. done
  31. return 1
  32. }
  33. governor_is_module() {
  34. # Check to see if the requested cpufreq governor
  35. # is provided as a kernel module or not
  36. module_info=`/sbin/modinfo cpufreq-${governor}`
  37. return $?
  38. }
  39. is_p4_clockmod() {
  40. if [ `/sbin/lsmod | grep -c -w "p4.clockmod"` -ge 1 -a -d "/sys/devices/system/cpu/cpu0/thermal_throttle" ]; then
  41. return 0
  42. fi
  43. return 1
  44. }
  45. governor_module_loaded() {
  46. # Check to see if we have a module loaded for
  47. # the current cpufreq governor
  48. if [ -e ${cpu0freqd}/scaling_governor ]; then
  49. governor=`cat ${cpu0freqd}/scaling_governor`
  50. else
  51. governor="none"
  52. fi
  53. if [ "${governor}" != "none" -a `/sbin/lsmod | grep -c -w "cpufreq.${governor}"` -ge 1 ]; then
  54. return 0
  55. fi
  56. return 1
  57. }
  58. adjust_cpufreq() {
  59. # First arg is a param under $cpufreqd
  60. # Second arg is the value you want to set that param to
  61. echo $2 > $cpufreqd/$1
  62. }
  63. adjust_governor() {
  64. # First arg is a param under $cpu/cpufreq/
  65. # Second arg is the value you want to set that param to
  66. for cpu in ${cpus}; do
  67. echo $2 > $cpu/cpufreq/$1
  68. done
  69. }
  70. start_cpuspeed() {
  71. echo -n $"Starting $prog: "
  72. # cpuspeed daemon thresholds are specified as idle percentages,
  73. # cpufreq modules as busy percentages, so we need to do some
  74. # math here for use of unified config...
  75. # DOWN_THRESHOLD doesn't mean exactly the same thing for
  76. # cpuspeed as it does for the cpufreq governors, but close
  77. # enough, and if not specified, we use same defaults as governors.
  78. if [ -n "$UP_THRESHOLD" ]; then
  79. let UP_THRESHOLD=100-$UP_THRESHOLD
  80. else
  81. UP_THRESHOLD=20
  82. fi
  83. if [ -n "$DOWN_THRESHOLD" ]; then
  84. let DOWN_THRESHOLD=100-$DOWN_THRESHOLD
  85. else
  86. DOWN_THRESHOLD=80
  87. fi
  88. OPTS="$OPTS -p $UP_THRESHOLD $DOWN_THRESHOLD"
  89. if [ -n "$MIN_SPEED" ]; then
  90. OPTS="$OPTS -m $MIN_SPEED"
  91. fi
  92. if [ -n "$MAX_SPEED" ]; then
  93. OPTS="$OPTS -M $MAX_SPEED"
  94. fi
  95. if [ "$IGNORE_NICE" -eq 1 ]; then
  96. OPTS="$OPTS -n"
  97. fi
  98. daemon $prog -d $OPTS
  99. RETVAL=$?
  100. return $RETVAL
  101. }
  102. stop_cpuspeed() {
  103. if [ -n "`pidof $prog`" ]; then
  104. echo -n $"Stopping $prog: "
  105. killproc $prog -USR1
  106. killproc $prog -INT
  107. fi
  108. if [ -n "`pidof $prog`" ]; then
  109. killproc $prog
  110. fi
  111. RETVAL=$?
  112. return $RETVAL
  113. }
  114. start() {
  115. if [ ! -f $lockfile ] && [ ! -d "$xendir" ]; then
  116. cpu_vendor=$(awk '/vendor_id/{print $3}' /proc/cpuinfo | tail -n 1)
  117. cpu_family=$(awk '/cpu family/{print $4}' /proc/cpuinfo | tail -n 1)
  118. if ! some_file_exist $testpat ; then
  119. # Attempt to load scaling_driver if not loaded
  120. # but it is configured
  121. if [ -n "$DRIVER" ]; then
  122. /sbin/modprobe "$DRIVER"
  123. elif [ "$cpu_vendor" == AuthenticAMD ] && [ "$cpu_family" -ge 7 ]; then
  124. # Try loading powernow-k8 if this is an AMD processor,
  125. # family 7 or greater (Athlon XP/MP was family 6)
  126. pk8m=$(/sbin/modinfo powernow-k8 > /dev/null 2>&1)
  127. if [ "$?" -eq 0 ]; then
  128. /sbin/modprobe powernow-k8 2> /dev/null
  129. [ -d ${cpu0freqd} ] || /sbin/modprobe -r powernow-k8 2> /dev/null
  130. fi
  131. elif [ -d /proc/acpi ]; then
  132. # use ACPI as a fallback
  133. /sbin/modprobe acpi-cpufreq 2> /dev/null
  134. # if even ACPI didn't work, remove it
  135. # and then next test will bail out.
  136. [ -d ${cpu0freqd} ] || /sbin/modprobe -r acpi-cpufreq 2> /dev/null
  137. fi
  138. if [ ! -d ${cpu0freqd} -a "$cpu_vendor" == GenuineIntel ]; then
  139. # last-ditch effort for Intel proc boxes, try our neutered p4-clockmod
  140. # to get at least passive cooling support (no clock changes)
  141. /sbin/modprobe p4-clockmod 2> /dev/null
  142. if [ -d ${cpu0freqd} ]; then
  143. echo -n "Enabling p4-clockmod driver (passive cooling only): "
  144. success; echo
  145. return 0
  146. else
  147. /sbin/modprobe -r p4-clockmod 2> /dev/null
  148. fi
  149. fi
  150. fi
  151. # If we get this far with no driver, we must have no scaling.
  152. # We're doomed.
  153. [ ! -f ${cpu0freqd}/scaling_driver ] && return 6
  154. # Okay, we have a driver, carry on...
  155. drv=`cat ${cpu0freqd}/scaling_driver`
  156. # Figure out default governor to use
  157. case "$drv" in
  158. centrino|powernow-k8|acpi-cpufreq|e_powersaver)
  159. default_governor=ondemand
  160. ;;
  161. *)
  162. default_governor=userspace
  163. ;;
  164. esac
  165. governor=${GOVERNOR:-${default_governor}}
  166. # Load governor module, if need be, and validate
  167. governor_is_module && /sbin/modprobe cpufreq-${governor}
  168. if [ `grep -c -w ${governor} ${cpu0freqd}/scaling_available_governors` -ge 1 ]; then
  169. $logger "Enabling ${governor} cpu frequency scaling governor"
  170. else
  171. $logger "Invalid governor \"${governor}\" specified, falling back to ${default_governor}"
  172. governor_is_module && /sbin/modprobe -r cpufreq-${governor}
  173. governor=${default_governor}
  174. governor_is_module && /sbin/modprobe cpufreq-${governor}
  175. fi
  176. # Set governor
  177. adjust_governor scaling_governor ${governor}
  178. # Run cpuspeed daemon for userspace gov, kernel ones otherwise
  179. if [ "${governor}" == "userspace" ]; then
  180. start_cpuspeed
  181. RETVAL=$?
  182. else
  183. if [ -n "$MIN_SPEED" ]; then
  184. adjust_cpufreq scaling_min_freq $MIN_SPEED
  185. fi
  186. if [ -n "$MAX_SPEED" ]; then
  187. adjust_cpufreq scaling_max_freq $MAX_SPEED
  188. fi
  189. if [ -n "$UP_THRESHOLD" -a ${governor} == "ondemand" ]; then
  190. adjust_cpufreq ondemand/up_threshold $UP_THRESHOLD
  191. fi
  192. if [ -n "$DOWN_THRESHOLD" -a ${governor} == "conservative" ]; then
  193. adjust_cpufreq conservative/down_threshold $DOWN_THRESHOLD
  194. fi
  195. if [ "$IGNORE_NICE" -eq 1 -a ${governor} == "ondemand" -o ${governor} == "conservative" ]; then
  196. adjust_cpufreq ${governor}/ignore_nice_load $IGNORE_NICE
  197. fi
  198. echo -n "Enabling ${governor} cpu frequency scaling: "
  199. success
  200. RETVAL=0
  201. fi
  202. echo
  203. # Technically, not quite right in non-cpuspeed daemon
  204. # cases, but close enough to indicate that we're
  205. # doing some sort of cpu frequency scaling.
  206. [ $RETVAL = 0 ] && touch $lockfile
  207. else
  208. if [ -d "$xendir" ]; then
  209. $logger "CPU Frequency scaling is currently not supported on xen kernels"
  210. fi
  211. return 0
  212. fi
  213. return $RETVAL
  214. }
  215. stop() {
  216. is_p4_clockmod && p4status="true"
  217. if [ "$p4status" == "true" ]; then
  218. echo "p4-clockmod passive cooling support cannot be stopped"
  219. return 0
  220. fi
  221. [ ! -f ${cpu0freqd}/scaling_driver ] && return 0
  222. drv=`cat ${cpu0freqd}/scaling_driver`
  223. governor_module_loaded && module_loaded=true
  224. if [ "${governor}" != "userspace" ]; then
  225. echo -n "Disabling ${governor} cpu frequency scaling: "
  226. $logger "Disabling ${governor} cpu frequency scaling governor"
  227. for cpu in ${cpus}; do
  228. echo userspace > $cpu/cpufreq/scaling_governor
  229. cat $cpu/cpufreq/cpuinfo_max_freq > $cpu/cpufreq/scaling_setspeed
  230. done
  231. if [ $module_loaded == true ]; then
  232. /sbin/modprobe -r cpufreq-${governor}
  233. fi
  234. success
  235. RETVAL=0
  236. else
  237. stop_cpuspeed
  238. RETVAL=$?
  239. fi
  240. echo
  241. [ -n "$DRIVER" ] && /sbin/modprobe -r $DRIVER
  242. [ $RETVAL = 0 ] && RETVAL=$?
  243. [ $RETVAL = 0 ] && rm -f $lockfile
  244. return $RETVAL
  245. }
  246. case "$1" in
  247. start)
  248. start
  249. ;;
  250. stop)
  251. stop
  252. ;;
  253. status)
  254. is_p4_clockmod && p4status="true"
  255. if [ "$p4status" == "true" ]; then
  256. echo "p4-clockmod passive cooling is enabled"
  257. exit 0
  258. fi
  259. governor_module_loaded && module_loaded=true
  260. if [ -d "$xendir" ]; then
  261. echo "Frequency scaling not supported under xen kernels"
  262. RETVAL=0
  263. elif [ $module_loaded == true -o ${governor} == "performance" ]; then
  264. echo "Frequency scaling enabled using ${governor} governor"
  265. RETVAL=0
  266. else
  267. status $prog
  268. RETVAL="$?"
  269. fi
  270. ;;
  271. restart)
  272. stop
  273. start
  274. ;;
  275. condrestart)
  276. governor_module_loaded && module_loaded=true
  277. if [ $module_loaded == true -o -n "`pidof $prog`" -o ${governor} == "performance" ]; then
  278. stop
  279. start
  280. fi
  281. ;;
  282. *)
  283. echo $"Usage: $0 {start|stop|restart|condrestart|status}"
  284. exit 3
  285. ;;
  286. esac
  287. exit $RETVAL