memcg_control_test.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/sh
  2. ################################################################################
  3. ## ##
  4. ## Copyright (c) 2010 Mohamed Naufal Basheer ##
  5. ## ##
  6. ## This program is free software; you can redistribute it and/or modify ##
  7. ## it under the terms of the GNU General Public License as published by ##
  8. ## the Free Software Foundation; either version 2 of the License, or ##
  9. ## (at your option) any later version. ##
  10. ## ##
  11. ## This program is distributed in the hope that it will be useful, ##
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See ##
  14. ## the GNU General Public License for more details. ##
  15. ## ##
  16. ## You should have received a copy of the GNU General Public License ##
  17. ## along with this program; if not, write to the Free Software ##
  18. ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ##
  19. ## ##
  20. ## ##
  21. ## File: memcg_control_test.sh ##
  22. ## ##
  23. ## Purpose: Implement various memory controller tests ##
  24. ## ##
  25. ## Author: Mohamed Naufal Basheer <naufal11@gmail.com> ##
  26. ## ##
  27. ################################################################################
  28. if [ "x$(grep -w memory /proc/cgroups | cut -f4)" != "x1" ]; then
  29. echo "WARNING:"
  30. echo "Either kernel does not support memory resource controller or feature not enabled"
  31. echo "Skipping all memcg_control testcases...."
  32. exit 0
  33. fi
  34. export TCID="memcg_control"
  35. export TST_TOTAL=1
  36. export TST_COUNT=0
  37. export TMP=${TMP:-/tmp}
  38. cd $TMP
  39. PAGE_SIZE=$(tst_getconf PAGESIZE)
  40. TOT_MEM_LIMIT=$PAGE_SIZE
  41. ACTIVE_MEM_LIMIT=$PAGE_SIZE
  42. PROC_MEM=$((PAGE_SIZE * 2))
  43. TST_PATH=$PWD
  44. STATUS_PIPE="$TMP/status_pipe"
  45. PASS=0
  46. FAIL=1
  47. # Check if the test process is killed on crossing boundary
  48. test_proc_kill()
  49. {
  50. cd $TMP
  51. mem_process -m $PROC_MEM &
  52. cd $OLDPWD
  53. sleep 1
  54. echo $! > tasks
  55. #Instruct the test process to start acquiring memory
  56. echo m > $STATUS_PIPE
  57. sleep 5
  58. #Check if killed
  59. ps -p $! > /dev/null 2> /dev/null
  60. if [ $? -eq 0 ]; then
  61. echo m > $STATUS_PIPE
  62. echo x > $STATUS_PIPE
  63. else
  64. : $((KILLED_CNT += 1))
  65. fi
  66. }
  67. # Validate the memory usage limit imposed by the hierarchically topmost group
  68. testcase_1()
  69. {
  70. TST_COUNT=1
  71. tst_resm TINFO "Test #1: Checking if the memory usage limit imposed by the topmost group is enforced"
  72. echo "$ACTIVE_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.limit_in_bytes
  73. echo "$TOT_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.memsw.limit_in_bytes
  74. mkdir sub
  75. (cd sub
  76. KILLED_CNT=0
  77. test_proc_kill
  78. if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then
  79. result $FAIL "Test #1: failed"
  80. else
  81. result $PASS "Test #1: passed"
  82. fi)
  83. rmdir sub
  84. }
  85. # Record the test results
  86. #
  87. # $1: Result of the test case, $PASS or $FAIL
  88. # $2: Output information
  89. result()
  90. {
  91. RES=$1
  92. INFO=$2
  93. if [ $RES -eq $PASS ]; then
  94. tst_resm TPASS "$INFO"
  95. else
  96. : $((FAILED_CNT += 1))
  97. tst_resm TFAIL "$INFO"
  98. fi
  99. }
  100. cleanup()
  101. {
  102. if [ -e $TST_PATH/mnt ]; then
  103. umount $TST_PATH/mnt 2> /dev/null
  104. rm -rf $TST_PATH/mnt
  105. fi
  106. }
  107. do_mount()
  108. {
  109. cleanup
  110. mkdir $TST_PATH/mnt
  111. mount -t cgroup -o memory cgroup $TST_PATH/mnt 2> /dev/null
  112. if [ $? -ne 0 ]; then
  113. tst_brkm TBROK NULL "Mounting cgroup to temp dir failed"
  114. rmdir $TST_PATH/mnt
  115. exit 1
  116. fi
  117. }
  118. do_mount
  119. echo 1 > mnt/memory.use_hierarchy 2> /dev/null
  120. FAILED_CNT=0
  121. TST_NUM=1
  122. while [ $TST_NUM -le $TST_TOTAL ]; do
  123. mkdir $TST_PATH/mnt/$TST_NUM
  124. (cd $TST_PATH/mnt/$TST_NUM && testcase_$TST_NUM)
  125. rmdir $TST_PATH/mnt/$TST_NUM
  126. : $((TST_NUM += 1))
  127. done
  128. echo 0 > mnt/memory.use_hierarchy 2> /dev/null
  129. cleanup
  130. if [ "$FAILED_CNT" -ne 0 ]; then
  131. tst_resm TFAIL "memcg_control: failed"
  132. exit 1
  133. else
  134. tst_resm TPASS "memcg_control: passed"
  135. exit 0
  136. fi