cgroup_fj_common.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh
  2. ################################################################################
  3. ## ##
  4. ## Copyright (c) 2009 FUJITSU LIMITED ##
  5. ## Author: Shi Weihua <shiwh@cn.fujitsu.com> ##
  6. ## Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com> ##
  7. ## Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz> ##
  8. ## ##
  9. ## This program is free software; you can redistribute it and#or modify ##
  10. ## it under the terms of the GNU General Public License as published by ##
  11. ## the Free Software Foundation; either version 2 of the License, or ##
  12. ## (at your option) any later version. ##
  13. ## ##
  14. ## This program is distributed in the hope that it will be useful, but ##
  15. ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
  16. ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##
  17. ## for more details. ##
  18. ## ##
  19. ## You should have received a copy of the GNU General Public License ##
  20. ## along with this program; if not, write to the Free Software Foundation, ##
  21. ## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ##
  22. ## ##
  23. ################################################################################
  24. for arg; do
  25. TCID="${TCID}_$arg"
  26. done
  27. . test.sh
  28. exist_subsystem()
  29. {
  30. local subsystem="$1"
  31. local exist=`grep -w $subsystem /proc/cgroups | cut -f1`
  32. if [ -z "$exist" ]; then
  33. tst_brkm TCONF "Subsystem $subsystem not supported"
  34. fi
  35. }
  36. attach_and_check()
  37. {
  38. local pid="$1"
  39. local path="$2"
  40. local task
  41. shift
  42. tst_resm TINFO "Attaching task $pid to $path"
  43. ROD echo "$pid" \> "$path/tasks"
  44. for task in $(cat "$path/tasks"); do
  45. if [ "$task" -ne "$pid" ]; then
  46. tst_resm TINFO "Unexpected pid $task in $path/tasks, expected $pid"
  47. return 1
  48. fi
  49. done
  50. return 0
  51. }
  52. create_subgroup()
  53. {
  54. path="$1"
  55. ROD mkdir "$path"
  56. # cpuset.cpus and cpuset.mems must be initialized with suitable value
  57. # before any pids are attached
  58. if [ "$subsystem" = "cpuset" ]; then
  59. if [ -e "$mount_point/cpus" ]; then
  60. ROD cat "$mount_point/cpus" \> "$path/cpus"
  61. ROD cat "$mount_point/mems" \> "$path/mems"
  62. else
  63. ROD cat "$mount_point/cpuset.cpus" \> "$path/cpuset.cpus"
  64. ROD cat "$mount_point/cpuset.mems" \> "$path/cpuset.mems"
  65. fi
  66. fi
  67. }
  68. setup()
  69. {
  70. tst_require_root
  71. tst_require_cmds killall
  72. if [ ! -f /proc/cgroups ]; then
  73. tst_brkm TCONF "Kernel does not support for control groups"
  74. fi
  75. exist_subsystem "$subsystem"
  76. tst_tmpdir
  77. TST_CLEANUP=cleanup
  78. mount_point=`grep -w $subsystem /proc/mounts | grep -w "cgroup" | \
  79. cut -f 2 | cut -d " " -f2`
  80. if [ -z "$mount_point" ]; then
  81. try_umount=1
  82. mount_point="/dev/cgroup"
  83. tst_resm TINFO "Subsystem $subsystem is not mounted, mounting it at $mount_point"
  84. ROD mkdir $mount_point
  85. ROD mount -t cgroup -o "$subsystem" "ltp_cgroup" "$mount_point"
  86. else
  87. tst_resm TINFO "Subsystem $subsystem is mounted at $mount_point"
  88. fi
  89. }
  90. cleanup()
  91. {
  92. tst_rmdir
  93. killall -9 cgroup_fj_proc >/dev/null 2>&1
  94. tst_resm TINFO "Removing all ltp subgroups..."
  95. find "$mount_point/ltp/" -depth -type d -exec rmdir '{}' \;
  96. if [ -z "$try_umount" ]; then
  97. return
  98. fi
  99. if grep -q "$mount_point" /proc/mounts; then
  100. umount "$mount_point"
  101. fi
  102. if [ -e "$mount_point" ]; then
  103. rmdir "$mount_point"
  104. fi
  105. }