test_vmalloc.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright (C) 2018 Uladzislau Rezki (Sony) <urezki@gmail.com>
  5. #
  6. # This is a test script for the kernel test driver to analyse vmalloc
  7. # allocator. Therefore it is just a kernel module loader. You can specify
  8. # and pass different parameters in order to:
  9. # a) analyse performance of vmalloc allocations;
  10. # b) stressing and stability check of vmalloc subsystem.
  11. TEST_NAME="vmalloc"
  12. DRIVER="test_${TEST_NAME}"
  13. NUM_CPUS=`grep -c ^processor /proc/cpuinfo`
  14. # Default number of times we allocate percpu objects:
  15. NR_PCPU_OBJECTS=35000
  16. # 1 if fails
  17. exitcode=1
  18. # Kselftest framework requirement - SKIP code is 4.
  19. ksft_skip=4
  20. #
  21. # Static templates for performance, stressing and smoke tests.
  22. # Also it is possible to pass any supported parameters manualy.
  23. #
  24. PERF_PARAM="sequential_test_order=1 test_repeat_count=3"
  25. SMOKE_PARAM="test_loop_count=10000 test_repeat_count=10"
  26. STRESS_PARAM="nr_threads=$NUM_CPUS test_repeat_count=20"
  27. PCPU_OBJ_PARAM="nr_pcpu_objects=$NR_PCPU_OBJECTS"
  28. check_test_requirements()
  29. {
  30. uid=$(id -u)
  31. if [ $uid -ne 0 ]; then
  32. echo "$0: Must be run as root"
  33. exit $ksft_skip
  34. fi
  35. if ! which modprobe > /dev/null 2>&1; then
  36. echo "$0: You need modprobe installed"
  37. exit $ksft_skip
  38. fi
  39. if ! modinfo $DRIVER > /dev/null 2>&1; then
  40. echo "$0: You must have the following enabled in your kernel:"
  41. echo "CONFIG_TEST_VMALLOC=m"
  42. exit $ksft_skip
  43. fi
  44. }
  45. check_memory_requirement()
  46. {
  47. # The pcpu_alloc_test allocates nr_pcpu_objects per cpu. If the
  48. # PAGE_SIZE is on the larger side it is easier to set a value
  49. # that can cause oom events during testing. Since we are
  50. # testing the functionality of vmalloc and not the oom-killer,
  51. # calculate what is 90% of available memory and divide it by
  52. # the number of online CPUs.
  53. pages=$(($(getconf _AVPHYS_PAGES) * 90 / 100 / $NUM_CPUS))
  54. if (($pages < $NR_PCPU_OBJECTS)); then
  55. echo "Updated nr_pcpu_objects to 90% of available memory."
  56. echo "nr_pcpu_objects is now set to: $pages."
  57. PCPU_OBJ_PARAM="nr_pcpu_objects=$pages"
  58. fi
  59. }
  60. run_performance_check()
  61. {
  62. echo "Run performance tests to evaluate how fast vmalloc allocation is."
  63. echo "It runs all test cases on one single CPU with sequential order."
  64. check_memory_requirement
  65. modprobe $DRIVER $PERF_PARAM $PCPU_OBJ_PARAM > /dev/null 2>&1
  66. echo "Done."
  67. echo "Check the kernel message buffer to see the summary."
  68. }
  69. run_stability_check()
  70. {
  71. echo "Run stability tests. In order to stress vmalloc subsystem all"
  72. echo "available test cases are run by NUM_CPUS workers simultaneously."
  73. echo "It will take time, so be patient."
  74. check_memory_requirement
  75. modprobe $DRIVER $STRESS_PARAM $PCPU_OBJ_PARAM > /dev/null 2>&1
  76. echo "Done."
  77. echo "Check the kernel ring buffer to see the summary."
  78. }
  79. run_smoke_check()
  80. {
  81. echo "Run smoke test. Note, this test provides basic coverage."
  82. echo "Please check $0 output how it can be used"
  83. echo "for deep performance analysis as well as stress testing."
  84. check_memory_requirement
  85. modprobe $DRIVER $SMOKE_PARAM $PCPU_OBJ_PARAM > /dev/null 2>&1
  86. echo "Done."
  87. echo "Check the kernel ring buffer to see the summary."
  88. }
  89. usage()
  90. {
  91. echo -n "Usage: $0 [ performance ] | [ stress ] | | [ smoke ] | "
  92. echo "manual parameters"
  93. echo
  94. echo "Valid tests and parameters:"
  95. echo
  96. modinfo $DRIVER
  97. echo
  98. echo "Example usage:"
  99. echo
  100. echo "# Shows help message"
  101. echo "./${DRIVER}.sh"
  102. echo
  103. echo "# Runs 1 test(id_1), repeats it 5 times by NUM_CPUS workers"
  104. echo "./${DRIVER}.sh nr_threads=$NUM_CPUS run_test_mask=1 test_repeat_count=5"
  105. echo
  106. echo -n "# Runs 4 tests(id_1|id_2|id_4|id_16) on one CPU with "
  107. echo "sequential order"
  108. echo -n "./${DRIVER}.sh sequential_test_order=1 "
  109. echo "run_test_mask=23"
  110. echo
  111. echo -n "# Runs all tests by NUM_CPUS workers, shuffled order, repeats "
  112. echo "20 times"
  113. echo "./${DRIVER}.sh nr_threads=$NUM_CPUS test_repeat_count=20"
  114. echo
  115. echo "# Performance analysis"
  116. echo "./${DRIVER}.sh performance"
  117. echo
  118. echo "# Stress testing"
  119. echo "./${DRIVER}.sh stress"
  120. echo
  121. exit 0
  122. }
  123. function validate_passed_args()
  124. {
  125. VALID_ARGS=`modinfo $DRIVER | awk '/parm:/ {print $2}' | sed 's/:.*//'`
  126. #
  127. # Something has been passed, check it.
  128. #
  129. for passed_arg in $@; do
  130. key=${passed_arg//=*/}
  131. val="${passed_arg:$((${#key}+1))}"
  132. valid=0
  133. for valid_arg in $VALID_ARGS; do
  134. if [[ $key = $valid_arg ]] && [[ $val -gt 0 ]]; then
  135. valid=1
  136. break
  137. fi
  138. done
  139. if [[ $valid -ne 1 ]]; then
  140. echo "Error: key or value is not correct: ${key} $val"
  141. exit $exitcode
  142. fi
  143. done
  144. }
  145. function run_manual_check()
  146. {
  147. #
  148. # Validate passed parameters. If there is wrong one,
  149. # the script exists and does not execute further.
  150. #
  151. validate_passed_args $@
  152. echo "Run the test with following parameters: $@"
  153. modprobe $DRIVER $@ > /dev/null 2>&1
  154. echo "Done."
  155. echo "Check the kernel ring buffer to see the summary."
  156. }
  157. function run_test()
  158. {
  159. if [ $# -eq 0 ]; then
  160. usage
  161. else
  162. if [[ "$1" = "performance" ]]; then
  163. run_performance_check
  164. elif [[ "$1" = "stress" ]]; then
  165. run_stability_check
  166. elif [[ "$1" = "smoke" ]]; then
  167. run_smoke_check
  168. else
  169. run_manual_check $@
  170. fi
  171. fi
  172. }
  173. check_test_requirements
  174. run_test $@
  175. exit 0