set-gpu-freq 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. # defines
  3. FREQ=0 #percent
  4. SERVICES=(perfd thermal-engine thermald mpdecision)
  5. DIR="/sys/class/kgsl/kgsl-3d0"
  6. ###################### SETUP ######################
  7. # helper functions
  8. fileexists() {
  9. [ `adb shell "[ -f $1 ] && echo found"` ]
  10. }
  11. getprop() {
  12. if fileexists $1; then
  13. echo `adb shell cat $1 | tr -d '\r'`
  14. else
  15. echo "FILE $1 NOT FOUND"
  16. fi
  17. }
  18. setprop() {
  19. if fileexists $1; then
  20. adb shell "echo -n $2 > $1"
  21. else
  22. echo "FILE $1 NOT FOUND"
  23. fi
  24. }
  25. # use passed in percent frequency
  26. if [[ $# -eq 1 ]]; then
  27. FREQ=$1
  28. fi
  29. # setup
  30. if [[ "`adb shell id | tr -d '\r' | awk -F'[()]' '{print $2}'`" != "root" ]]; then
  31. adb root
  32. fi
  33. adb wait-for-device
  34. # device name
  35. echo Device: `adb shell getprop ro.product.model`
  36. # get available gpu frequencies in sorted order
  37. if fileexists $DIR/devfreq/available_frequencies; then
  38. frequencies=(`getprop $DIR/devfreq/available_frequencies`)
  39. elif fileexists $DIR/gpu_available_frequencies; then
  40. frequencies=(`getprop $DIR/gpu_available_frequencies`)
  41. elif fileexists /d/clock/gbus/possible_rates; then
  42. frequencies=(`getprop /d/clock/gbus/possible_rates`)
  43. else
  44. echo "Unable to find available GPU frequencies"
  45. echo
  46. exit
  47. fi
  48. frequencies=( `printf "%s\n" "${frequencies[@]}" | sort -n` )
  49. if [ ${frequencies[0]} == "(kHz)" ]; then
  50. frequencies=(`printf "%s000\n" "${frequencies[@]:1}"`)
  51. fi
  52. minFreq=${frequencies[0]}
  53. maxFreq=${frequencies[-1]}
  54. echo Frequencies: ${frequencies[@]}
  55. # find closest frequency
  56. targetFreq=$(( FREQ * ( maxFreq - minFreq ) / 100 + minFreq ))
  57. freq=`printf "%d\n" "${frequencies[@]}" | awk -v closest=${frequencies[0]} -v target=$targetFreq 'BEGIN{diff=$0-target;if(diff<0)diff=-diff;lowest=diff};{diff=$0-target;if(diff<0)diff=-diff;if(diff<lowest){lowest=diff;closest=$0}};END{print closest}'`
  58. echo Selected: $freq
  59. echo
  60. # freeze system
  61. for service in ${SERVICES[@]}; do
  62. adb shell stop $service
  63. done
  64. # set GPU properties if possible
  65. setprop $DIR/bus_split 0
  66. setprop $DIR/devfreq/governor performance
  67. setprop $DIR/force_bus_on 1
  68. setprop $DIR/force_rail_on 1
  69. setprop $DIR/force_clk_on 1
  70. setprop $DIR/idle_timer 1000000
  71. setprop $DIR/max_gpuclk $freq
  72. setprop $DIR/gpuclk $freq
  73. setprop /d/clock/override.gbus/state 1
  74. setprop /d/clock/override.gbus/rate $freq
  75. # final message
  76. echo "GPU Frequency set to $freq"