cpufreq.sh 4.9 KB

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