with_stress.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Kselftest framework requirement - SKIP code is 4.
  4. ksft_skip=4
  5. stress_fork()
  6. {
  7. while true ; do
  8. /usr/bin/true
  9. sleep 0.01
  10. done
  11. }
  12. stress_subsys()
  13. {
  14. local verb=+
  15. while true ; do
  16. echo $verb$subsys_ctrl >$sysfs/cgroup.subtree_control
  17. [ $verb = "+" ] && verb=- || verb=+
  18. # incommensurable period with other stresses
  19. sleep 0.011
  20. done
  21. }
  22. init_and_check()
  23. {
  24. sysfs=`mount -t cgroup2 | head -1 | awk '{ print $3 }'`
  25. if [ ! -d "$sysfs" ]; then
  26. echo "Skipping: cgroup2 is not mounted" >&2
  27. exit $ksft_skip
  28. fi
  29. if ! echo +$subsys_ctrl >$sysfs/cgroup.subtree_control ; then
  30. echo "Skipping: cannot enable $subsys_ctrl in $sysfs" >&2
  31. exit $ksft_skip
  32. fi
  33. if ! echo -$subsys_ctrl >$sysfs/cgroup.subtree_control ; then
  34. echo "Skipping: cannot disable $subsys_ctrl in $sysfs" >&2
  35. exit $ksft_skip
  36. fi
  37. }
  38. declare -a stresses
  39. declare -a stress_pids
  40. duration=5
  41. rc=0
  42. subsys_ctrl=cpuset
  43. sysfs=
  44. while getopts c:d:hs: opt; do
  45. case $opt in
  46. c)
  47. subsys_ctrl=$OPTARG
  48. ;;
  49. d)
  50. duration=$OPTARG
  51. ;;
  52. h)
  53. echo "Usage $0 [ -s stress ] ... [ -d duration ] [-c controller] cmd args .."
  54. echo -e "\t default duration $duration seconds"
  55. echo -e "\t default controller $subsys_ctrl"
  56. exit
  57. ;;
  58. s)
  59. func=stress_$OPTARG
  60. if [ "x$(type -t $func)" != "xfunction" ] ; then
  61. echo "Unknown stress $OPTARG"
  62. exit 1
  63. fi
  64. stresses+=($func)
  65. ;;
  66. esac
  67. done
  68. shift $((OPTIND - 1))
  69. init_and_check
  70. for s in ${stresses[*]} ; do
  71. $s &
  72. stress_pids+=($!)
  73. done
  74. time=0
  75. start=$(date +%s)
  76. while [ $time -lt $duration ] ; do
  77. $*
  78. rc=$?
  79. [ $rc -eq 0 ] || break
  80. time=$(($(date +%s) - $start))
  81. done
  82. for pid in ${stress_pids[*]} ; do
  83. kill -SIGTERM $pid
  84. wait $pid
  85. done
  86. exit $rc