cpu-on-off-test.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. SYSFS=
  4. # Kselftest framework requirement - SKIP code is 4.
  5. ksft_skip=4
  6. retval=0
  7. prerequisite()
  8. {
  9. msg="skip all tests:"
  10. if [ $UID != 0 ]; then
  11. echo $msg must be run as root >&2
  12. exit $ksft_skip
  13. fi
  14. taskset -p 01 $$
  15. SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
  16. if [ ! -d "$SYSFS" ]; then
  17. echo $msg sysfs is not mounted >&2
  18. exit $ksft_skip
  19. fi
  20. if ! ls $SYSFS/devices/system/cpu/cpu* > /dev/null 2>&1; then
  21. echo $msg cpu hotplug is not supported >&2
  22. exit $ksft_skip
  23. fi
  24. echo "CPU online/offline summary:"
  25. online_cpus=`cat $SYSFS/devices/system/cpu/online`
  26. online_max=${online_cpus##*-}
  27. if [[ "$online_cpus" = "$online_max" ]]; then
  28. echo "$msg: since there is only one cpu: $online_cpus"
  29. exit $ksft_skip
  30. fi
  31. present_cpus=`cat $SYSFS/devices/system/cpu/present`
  32. present_max=${present_cpus##*-}
  33. echo "present_cpus = $present_cpus present_max = $present_max"
  34. echo -e "\t Cpus in online state: $online_cpus"
  35. offline_cpus=`cat $SYSFS/devices/system/cpu/offline`
  36. if [[ "a$offline_cpus" = "a" ]]; then
  37. offline_cpus=0
  38. else
  39. offline_max=${offline_cpus##*-}
  40. fi
  41. echo -e "\t Cpus in offline state: $offline_cpus"
  42. }
  43. #
  44. # list all hot-pluggable CPUs
  45. #
  46. hotpluggable_cpus()
  47. {
  48. local state=${1:-.\*}
  49. for cpu in $SYSFS/devices/system/cpu/cpu*; do
  50. if [ -f $cpu/online ] && grep -q $state $cpu/online; then
  51. echo ${cpu##/*/cpu}
  52. fi
  53. done
  54. }
  55. hotplaggable_offline_cpus()
  56. {
  57. hotpluggable_cpus 0
  58. }
  59. hotpluggable_online_cpus()
  60. {
  61. hotpluggable_cpus 1
  62. }
  63. cpu_is_online()
  64. {
  65. grep -q 1 $SYSFS/devices/system/cpu/cpu$1/online
  66. }
  67. cpu_is_offline()
  68. {
  69. grep -q 0 $SYSFS/devices/system/cpu/cpu$1/online
  70. }
  71. online_cpu()
  72. {
  73. echo 1 > $SYSFS/devices/system/cpu/cpu$1/online
  74. }
  75. offline_cpu()
  76. {
  77. echo 0 > $SYSFS/devices/system/cpu/cpu$1/online
  78. }
  79. online_cpu_expect_success()
  80. {
  81. local cpu=$1
  82. if ! online_cpu $cpu; then
  83. echo $FUNCNAME $cpu: unexpected fail >&2
  84. retval=1
  85. elif ! cpu_is_online $cpu; then
  86. echo $FUNCNAME $cpu: unexpected offline >&2
  87. retval=1
  88. fi
  89. }
  90. online_cpu_expect_fail()
  91. {
  92. local cpu=$1
  93. if online_cpu $cpu 2> /dev/null; then
  94. echo $FUNCNAME $cpu: unexpected success >&2
  95. retval=1
  96. elif ! cpu_is_offline $cpu; then
  97. echo $FUNCNAME $cpu: unexpected online >&2
  98. retval=1
  99. fi
  100. }
  101. offline_cpu_expect_success()
  102. {
  103. local cpu=$1
  104. if ! offline_cpu $cpu; then
  105. echo $FUNCNAME $cpu: unexpected fail >&2
  106. retval=1
  107. elif ! cpu_is_offline $cpu; then
  108. echo $FUNCNAME $cpu: unexpected offline >&2
  109. retval=1
  110. fi
  111. }
  112. offline_cpu_expect_fail()
  113. {
  114. local cpu=$1
  115. if offline_cpu $cpu 2> /dev/null; then
  116. echo $FUNCNAME $cpu: unexpected success >&2
  117. retval=1
  118. elif ! cpu_is_online $cpu; then
  119. echo $FUNCNAME $cpu: unexpected offline >&2
  120. retval=1
  121. fi
  122. }
  123. online_all_hot_pluggable_cpus()
  124. {
  125. for cpu in `hotplaggable_offline_cpus`; do
  126. online_cpu_expect_success $cpu
  127. done
  128. }
  129. offline_all_hot_pluggable_cpus()
  130. {
  131. local reserve_cpu=$online_max
  132. for cpu in `hotpluggable_online_cpus`; do
  133. # Reserve one cpu oneline at least.
  134. if [ $cpu -eq $reserve_cpu ];then
  135. continue
  136. fi
  137. offline_cpu_expect_success $cpu
  138. done
  139. }
  140. allcpus=0
  141. online_cpus=0
  142. online_max=0
  143. offline_cpus=0
  144. offline_max=0
  145. present_cpus=0
  146. present_max=0
  147. while getopts ah opt; do
  148. case $opt in
  149. a)
  150. allcpus=1
  151. ;;
  152. h)
  153. echo "Usage $0 [ -a ]"
  154. echo -e "\t default offline one cpu"
  155. echo -e "\t run with -a option to offline all cpus"
  156. exit
  157. ;;
  158. esac
  159. done
  160. prerequisite
  161. #
  162. # Safe test (default) - offline and online one cpu
  163. #
  164. if [ $allcpus -eq 0 ]; then
  165. echo "Limited scope test: one hotplug cpu"
  166. echo -e "\t (leaves cpu in the original state):"
  167. echo -e "\t online to offline to online: cpu $online_max"
  168. offline_cpu_expect_success $online_max
  169. online_cpu_expect_success $online_max
  170. if [[ $offline_cpus -gt 0 ]]; then
  171. echo -e "\t online to offline to online: cpu $present_max"
  172. online_cpu_expect_success $present_max
  173. offline_cpu_expect_success $present_max
  174. online_cpu $present_max
  175. fi
  176. exit $retval
  177. else
  178. echo "Full scope test: all hotplug cpus"
  179. echo -e "\t online all offline cpus"
  180. echo -e "\t offline all online cpus"
  181. echo -e "\t online all offline cpus"
  182. fi
  183. online_all_hot_pluggable_cpus
  184. offline_all_hot_pluggable_cpus
  185. online_all_hot_pluggable_cpus
  186. exit $retval