cpuset_funcs.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/sh
  2. # usage: . cpuset_funcs.sh
  3. # functions for cpuset test
  4. ################################################################################
  5. ## ##
  6. ## Copyright (c) 2009 FUJITSU LIMITED ##
  7. ## ##
  8. ## This program is free software; you can redistribute it and#or modify ##
  9. ## it under the terms of the GNU General Public License as published by ##
  10. ## the Free Software Foundation; either version 2 of the License, or ##
  11. ## (at your option) any later version. ##
  12. ## ##
  13. ## This program is distributed in the hope that it will be useful, but ##
  14. ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
  15. ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##
  16. ## for more details. ##
  17. ## ##
  18. ## You should have received a copy of the GNU General Public License ##
  19. ## along with this program; if not, write to the Free Software ##
  20. ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ##
  21. ## ##
  22. ## Author: Miao Xie <miaox@cn.fujitsu.com> ##
  23. ## ##
  24. ################################################################################
  25. . test.sh
  26. NR_CPUS=`tst_ncpus`
  27. if [ -f "/sys/devices/system/node/has_high_memory" ]; then
  28. N_NODES="`cat /sys/devices/system/node/has_high_memory`"
  29. else
  30. N_NODES="`cat /sys/devices/system/node/has_normal_memory`"
  31. fi
  32. mem_string="$N_NODES"
  33. N_NODES=${N_NODES#*-*}
  34. N_NODES=$(($N_NODES + 1))
  35. CPUSET="/dev/cpuset"
  36. CPUSET_TMP="/tmp/cpuset_tmp"
  37. CLONE_CHILDREN="/dev/cpuset/cgroup.clone_children"
  38. CHILDREN_VALUE="0"
  39. HOTPLUG_CPU="1"
  40. cpuset_log()
  41. {
  42. tst_resm TINFO "$*"
  43. }
  44. # cpuset_log_error <error_file>
  45. cpuset_log_error()
  46. {
  47. local error_message=
  48. while read error_message
  49. do
  50. cpuset_log "$error_message"
  51. done < "$1"
  52. }
  53. version_check()
  54. {
  55. if tst_kvcmp -lt "2.6.28"; then
  56. tst_brkm TCONF "kernel is below 2.6.28"
  57. fi
  58. }
  59. ncpus_check()
  60. {
  61. if [ $NR_CPUS -lt $1 ]; then
  62. tst_brkm TCONF "The total of CPUs is less than $1"
  63. fi
  64. }
  65. nnodes_check()
  66. {
  67. if [ $N_NODES -lt $1 ]; then
  68. tst_brkm TCONF "The total of nodes is less than $1"
  69. fi
  70. }
  71. user_check()
  72. {
  73. if [ $(id -u) != 0 ]; then
  74. tst_brkm TCONF "Test must be run as root"
  75. fi
  76. }
  77. cpuset_check()
  78. {
  79. if [ -f /proc/cgroups ]; then
  80. CPUSET_CONTROLLER=`grep -w cpuset /proc/cgroups | cut -f1`
  81. CPUSET_CONTROLLER_VALUE=`grep -w cpuset /proc/cgroups | cut -f4`
  82. if [ "$CPUSET_CONTROLLER" = "cpuset" ] && [ "$CPUSET_CONTROLLER_VALUE" = "1" ]
  83. then
  84. return 0
  85. fi
  86. fi
  87. tst_brkm TCONF "Cpuset is not supported"
  88. }
  89. # optional parameters (pass both or none of them):
  90. # $1 - required number of cpus (default 2)
  91. # $2 - required number of memory nodes (default 2)
  92. check()
  93. {
  94. user_check
  95. cpuset_check
  96. version_check
  97. ncpus_check ${1:-2}
  98. nnodes_check ${2:-2}
  99. }
  100. # Create /dev/cpuset & mount the cgroup file system with cpuset
  101. # clean any group created eralier (if any)
  102. setup()
  103. {
  104. if [ -e "$CPUSET" ]
  105. then
  106. tst_resm TWARN "$CPUSET already exist.. overwriting"
  107. cleanup || tst_brkm TFAIL "Can't cleanup... Exiting"
  108. fi
  109. mkdir -p "$CPUSET_TMP"
  110. mkdir "$CPUSET"
  111. mount -t cgroup -o cpuset cpuset "$CPUSET" 2> /dev/null
  112. if [ $? -ne 0 ]; then
  113. cleanup
  114. tst_brkm TFAIL "Could not mount cgroup filesystem with"\
  115. " cpuset on $CPUSET..Exiting test"
  116. fi
  117. CHILDREN_VALUE="`cat $CLONE_CHILDREN`"
  118. }
  119. # Write the cleanup function
  120. cleanup()
  121. {
  122. grep "$CPUSET" /proc/mounts >/dev/null 2>&1 || {
  123. rm -rf "$CPUSET" >/dev/null 2>&1
  124. return 0
  125. }
  126. echo $CHILDREN_VALUE > $CLONE_CHILDREN
  127. find "$CPUSET" -type d | sort | sed -n '2,$p' | tac | while read subdir
  128. do
  129. while read pid
  130. do
  131. /bin/kill -9 $pid > /dev/null 2>&1
  132. if [ $? -ne 0 ]; then
  133. tst_brkm TFAIL "Couldn't kill task - "\
  134. "$pid in the cpuset"
  135. fi
  136. done < "$subdir/tasks"
  137. rmdir "$subdir"
  138. if [ $? -ne 0 ]; then
  139. tst_brkm TFAIL "Couldn't remove subdir - "
  140. "$subdir in the cpuset"
  141. fi
  142. done
  143. umount "$CPUSET"
  144. if [ $? -ne 0 ]; then
  145. tst_brkm TFAIL "Couldn't umount cgroup filesystem with"\
  146. " cpuset on $CPUSET..Exiting test"
  147. fi
  148. rmdir "$CPUSET" > /dev/null 2>&1
  149. rm -rf "$CPUSET_TMP" > /dev/null 2>&1
  150. }
  151. # set the cpuset's parameter
  152. # cpuset_set <cpusetpath> <cpus> <mems> <load_balance>
  153. cpuset_set()
  154. {
  155. local path="$1"
  156. mkdir -p "$path"
  157. if [ $? -ne 0 ]; then
  158. return 1
  159. fi
  160. local cpus="$2"
  161. local mems="$3"
  162. local load_balance="$4"
  163. if [ "$path" != "$CPUSET" ]; then
  164. if [ "$cpus" != "-" ]; then
  165. /bin/echo $cpus > $path/cpuset.cpus
  166. if [ $? -ne 0 ]; then
  167. return 1
  168. fi
  169. fi
  170. /bin/echo $mems > $path/cpuset.mems
  171. if [ $? -ne 0 ]; then
  172. return 1
  173. fi
  174. fi
  175. /bin/echo $load_balance > $path/cpuset.sched_load_balance
  176. if [ $? -ne 0 ]; then
  177. return 1
  178. fi
  179. }
  180. # cpu_hotplug cpu_id offline/online
  181. cpu_hotplug()
  182. {
  183. if [ "$2" = "online" ]; then
  184. /bin/echo 1 > "/sys/devices/system/cpu/cpu$1/online"
  185. if [ $? -ne 0 ]; then
  186. return 1
  187. fi
  188. elif [ "$2" = "offline" ]; then
  189. /bin/echo 0 > "/sys/devices/system/cpu/cpu$1/online"
  190. if [ $? -ne 0 ]; then
  191. return 1
  192. fi
  193. fi
  194. }
  195. # setup_test_environment <online | offline>
  196. # online - online a CPU in testing, so we must offline a CPU first
  197. # offline - offline a CPU in testing, we needn't do anything
  198. setup_test_environment()
  199. {
  200. if [ "$1" = "online" ]; then
  201. cpu_hotplug $HOTPLUG_CPU offline
  202. if [ $? -ne 0 ]; then
  203. return 1
  204. fi
  205. fi
  206. }
  207. cpu_hotplug_cleanup()
  208. {
  209. local cpus_array="$(seq -s' ' 1 $((NR_CPUS-1)))"
  210. local cpuid=
  211. for cpuid in $cpus_array
  212. do
  213. local file="/sys/devices/system/cpu/cpu$cpuid/online"
  214. local offline="$(cat $file)"
  215. if [ $offline -eq 0 ]; then
  216. cpu_hotplug $cpuid "online"
  217. fi
  218. done
  219. }