cpufreq.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # protect against multiple inclusion
  4. if [ $FILE_CPUFREQ ]; then
  5. return 0
  6. else
  7. FILE_CPUFREQ=DONE
  8. fi
  9. source cpu.sh
  10. # $1: cpu
  11. cpu_should_have_cpufreq_directory()
  12. {
  13. if [ ! -d $CPUROOT/$1/cpufreq ]; then
  14. printf "Warning: No cpufreq directory present for $1\n"
  15. fi
  16. }
  17. cpu_should_not_have_cpufreq_directory()
  18. {
  19. if [ -d $CPUROOT/$1/cpufreq ]; then
  20. printf "Warning: cpufreq directory present for $1\n"
  21. fi
  22. }
  23. for_each_policy()
  24. {
  25. policies=$(ls $CPUFREQROOT| grep "policy[0-9].*")
  26. for policy in $policies; do
  27. $@ $policy
  28. done
  29. }
  30. for_each_policy_concurrent()
  31. {
  32. policies=$(ls $CPUFREQROOT| grep "policy[0-9].*")
  33. for policy in $policies; do
  34. $@ $policy &
  35. done
  36. }
  37. # $1: Path
  38. read_cpufreq_files_in_dir()
  39. {
  40. local files=`ls $1`
  41. printf "Printing directory: $1\n\n"
  42. for file in $files; do
  43. if [ -f $1/$file ]; then
  44. printf "$file:"
  45. #file is readable ?
  46. local rfile=$(ls -l $1/$file | awk '$1 ~ /^.*r.*/ { print $NF; }')
  47. if [ ! -z $rfile ]; then
  48. cat $1/$file
  49. else
  50. printf "$file is not readable\n"
  51. fi
  52. else
  53. printf "\n"
  54. read_cpufreq_files_in_dir "$1/$file"
  55. fi
  56. done
  57. printf "\n"
  58. }
  59. read_all_cpufreq_files()
  60. {
  61. printf "** Test: Running ${FUNCNAME[0]} **\n\n"
  62. read_cpufreq_files_in_dir $CPUFREQROOT
  63. printf "%s\n\n" "------------------------------------------------"
  64. }
  65. # UPDATE CPUFREQ FILES
  66. # $1: directory path
  67. update_cpufreq_files_in_dir()
  68. {
  69. local files=`ls $1`
  70. printf "Updating directory: $1\n\n"
  71. for file in $files; do
  72. if [ -f $1/$file ]; then
  73. # file is readable and writable ?
  74. local rwfile=$(ls -l $1/$file | awk '$1 ~ /^.*rw.*/ { print $NF; }')
  75. if [ ! -z $rwfile ]; then
  76. # scaling_setspeed is a special file and we
  77. # should skip updating it
  78. if [ $file != "scaling_setspeed" ]; then
  79. local val=$(cat $1/$file)
  80. printf "Writing $val to: $file\n"
  81. echo $val > $1/$file
  82. fi
  83. fi
  84. else
  85. printf "\n"
  86. update_cpufreq_files_in_dir "$1/$file"
  87. fi
  88. done
  89. printf "\n"
  90. }
  91. # Update all writable files with their existing values
  92. update_all_cpufreq_files()
  93. {
  94. printf "** Test: Running ${FUNCNAME[0]} **\n\n"
  95. update_cpufreq_files_in_dir $CPUFREQROOT
  96. printf "%s\n\n" "------------------------------------------------"
  97. }
  98. # CHANGE CPU FREQUENCIES
  99. # $1: policy
  100. find_current_freq()
  101. {
  102. cat $CPUFREQROOT/$1/scaling_cur_freq
  103. }
  104. # $1: policy
  105. # $2: frequency
  106. set_cpu_frequency()
  107. {
  108. printf "Change frequency for $1 to $2\n"
  109. echo $2 > $CPUFREQROOT/$1/scaling_setspeed
  110. }
  111. # $1: policy
  112. test_all_frequencies()
  113. {
  114. local filepath="$CPUFREQROOT/$1"
  115. backup_governor $1
  116. local found=$(switch_governor $1 "userspace")
  117. if [ $found = 1 ]; then
  118. printf "${FUNCNAME[0]}: userspace governor not available for: $1\n"
  119. return;
  120. fi
  121. printf "Switched governor for $1 to userspace\n\n"
  122. local freqs=$(cat $filepath/scaling_available_frequencies)
  123. printf "Available frequencies for $1: $freqs\n\n"
  124. # Set all frequencies one-by-one
  125. for freq in $freqs; do
  126. set_cpu_frequency $1 $freq
  127. done
  128. printf "\n"
  129. restore_governor $1
  130. }
  131. # $1: loop count
  132. shuffle_frequency_for_all_cpus()
  133. {
  134. printf "** Test: Running ${FUNCNAME[0]} for $1 loops **\n\n"
  135. for i in `seq 1 $1`; do
  136. for_each_policy test_all_frequencies
  137. done
  138. printf "\n%s\n\n" "------------------------------------------------"
  139. }
  140. # Basic cpufreq tests
  141. cpufreq_basic_tests()
  142. {
  143. printf "*** RUNNING CPUFREQ SANITY TESTS ***\n"
  144. printf "====================================\n\n"
  145. count=$(count_cpufreq_managed_cpus)
  146. if [ $count = 0 ]; then
  147. ktap_exit_fail_msg "No cpu is managed by cpufreq core, exiting\n"
  148. else
  149. printf "CPUFreq manages: $count CPUs\n\n"
  150. fi
  151. # Detect & print which CPUs are not managed by cpufreq
  152. print_unmanaged_cpus
  153. # read/update all cpufreq files
  154. read_all_cpufreq_files
  155. update_all_cpufreq_files
  156. # hotplug cpus
  157. reboot_cpus 5
  158. # Test all frequencies
  159. shuffle_frequency_for_all_cpus 2
  160. # Test all governors
  161. shuffle_governors_for_all_cpus 1
  162. }
  163. # Suspend/resume
  164. # $1: "suspend" or "hibernate", $2: loop count
  165. do_suspend()
  166. {
  167. printf "** Test: Running ${FUNCNAME[0]}: Trying $1 for $2 loops **\n\n"
  168. # Is the directory available
  169. if [ ! -d $SYSFS/power/ -o ! -f $SYSFS/power/state ]; then
  170. printf "$SYSFS/power/state not available\n"
  171. return 1
  172. fi
  173. if [ $1 = "suspend" ]; then
  174. filename="mem"
  175. elif [ $1 = "hibernate" ]; then
  176. filename="disk"
  177. else
  178. printf "$1 is not a valid option\n"
  179. return 1
  180. fi
  181. if [ -n $filename ]; then
  182. present=$(cat $SYSFS/power/state | grep $filename)
  183. if [ -z "$present" ]; then
  184. printf "Tried to $1 but $filename isn't present in $SYSFS/power/state\n"
  185. return 1;
  186. fi
  187. for i in `seq 1 $2`; do
  188. printf "Starting $1\n"
  189. if [ "$3" = "rtc" ]; then
  190. if ! command -v rtcwake &> /dev/null; then
  191. printf "rtcwake could not be found, please install it.\n"
  192. return 1
  193. fi
  194. rtcwake -m $filename -s 15
  195. if [ $? -ne 0 ]; then
  196. printf "Failed to suspend using RTC wake alarm\n"
  197. return 1
  198. fi
  199. else
  200. echo $filename > $SYSFS/power/state
  201. fi
  202. printf "Came out of $1\n"
  203. printf "Do basic tests after finishing $1 to verify cpufreq state\n\n"
  204. cpufreq_basic_tests
  205. done
  206. fi
  207. }