102 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4# Kselftest framework requirement - SKIP code is 4.5ksft_skip=46 7stress_fork()8{9 while true ; do10 /usr/bin/true11 sleep 0.0112 done13}14 15stress_subsys()16{17 local verb=+18 while true ; do19 echo $verb$subsys_ctrl >$sysfs/cgroup.subtree_control20 [ $verb = "+" ] && verb=- || verb=+21 # incommensurable period with other stresses22 sleep 0.01123 done24}25 26init_and_check()27{28 sysfs=`mount -t cgroup2 | head -1 | awk '{ print $3 }'`29 if [ ! -d "$sysfs" ]; then30 echo "Skipping: cgroup2 is not mounted" >&231 exit $ksft_skip32 fi33 34 if ! echo +$subsys_ctrl >$sysfs/cgroup.subtree_control ; then35 echo "Skipping: cannot enable $subsys_ctrl in $sysfs" >&236 exit $ksft_skip37 fi38 39 if ! echo -$subsys_ctrl >$sysfs/cgroup.subtree_control ; then40 echo "Skipping: cannot disable $subsys_ctrl in $sysfs" >&241 exit $ksft_skip42 fi43}44 45declare -a stresses46declare -a stress_pids47duration=548rc=049subsys_ctrl=cpuset50sysfs=51 52while getopts c:d:hs: opt; do53 case $opt in54 c)55 subsys_ctrl=$OPTARG56 ;;57 d)58 duration=$OPTARG59 ;;60 h)61 echo "Usage $0 [ -s stress ] ... [ -d duration ] [-c controller] cmd args .."62 echo -e "\t default duration $duration seconds"63 echo -e "\t default controller $subsys_ctrl"64 exit65 ;;66 s)67 func=stress_$OPTARG68 if [ "x$(type -t $func)" != "xfunction" ] ; then69 echo "Unknown stress $OPTARG"70 exit 171 fi72 stresses+=($func)73 ;;74 esac75done76shift $((OPTIND - 1))77 78init_and_check79 80for s in ${stresses[*]} ; do81 $s &82 stress_pids+=($!)83done84 85 86time=087start=$(date +%s)88 89while [ $time -lt $duration ] ; do90 $*91 rc=$?92 [ $rc -eq 0 ] || break93 time=$(($(date +%s) - $start))94done95 96for pid in ${stress_pids[*]} ; do97 kill -SIGTERM $pid98 wait $pid99done100 101exit $rc102