cpu.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. # Copyright 2014 the V8 project authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. CPUPATH=/sys/devices/system/cpu
  6. MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}')
  7. set_governor() {
  8. echo "Setting CPU frequency governor to \"$1\""
  9. for (( i=0; i<=$MAXID; i++ )); do
  10. echo "$1" > $CPUPATH/cpu$i/cpufreq/scaling_governor
  11. done
  12. }
  13. enable_cores() {
  14. # $1: How many cores to enable.
  15. for (( i=1; i<=$MAXID; i++ )); do
  16. if [ "$i" -lt "$1" ]; then
  17. echo 1 > $CPUPATH/cpu$i/online
  18. else
  19. echo 0 > $CPUPATH/cpu$i/online
  20. fi
  21. done
  22. }
  23. dual_core() {
  24. echo "Switching to dual-core mode"
  25. enable_cores 2
  26. }
  27. single_core() {
  28. echo "Switching to single-core mode"
  29. enable_cores 1
  30. }
  31. all_cores() {
  32. echo "Reactivating all CPU cores"
  33. enable_cores $((MAXID+1))
  34. }
  35. limit_cores() {
  36. # $1: How many cores to enable.
  37. echo "Limiting to $1 cores"
  38. enable_cores $1
  39. }
  40. case "$1" in
  41. fast | performance)
  42. set_governor "performance"
  43. ;;
  44. slow | powersave)
  45. set_governor "powersave"
  46. ;;
  47. default | ondemand)
  48. set_governor "ondemand"
  49. ;;
  50. dualcore | dual)
  51. dual_core
  52. ;;
  53. singlecore | single)
  54. single_core
  55. ;;
  56. allcores | all)
  57. all_cores
  58. ;;
  59. limit_cores)
  60. if [ $# -ne 2 ]; then
  61. echo "Usage $0 limit_cores <num>"
  62. exit 1
  63. fi
  64. limit_cores $2
  65. ;;
  66. *)
  67. echo "Usage: $0 fast|slow|default|singlecore|dualcore|all|limit_cores"
  68. exit 1
  69. ;;
  70. esac