summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/cgroup/with_stress.sh
blob: e28c35008f5b7a0f66654b10d8fccf4678120f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

# Kselftest framework requirement - SKIP code is 4.
ksft_skip=4

stress_fork()
{
	while true ; do
		/usr/bin/true
		sleep 0.01
	done
}

stress_subsys()
{
	local verb=+
	while true ; do
		echo $verb$subsys_ctrl >$sysfs/cgroup.subtree_control
		[ $verb = "+" ] && verb=- || verb=+
		# incommensurable period with other stresses
		sleep 0.011
	done
}

init_and_check()
{
	sysfs=`mount -t cgroup2 | head -1 | awk '{ print $3 }'`
	if [ ! -d "$sysfs" ]; then
		echo "Skipping: cgroup2 is not mounted" >&2
		exit $ksft_skip
	fi

	if ! echo +$subsys_ctrl >$sysfs/cgroup.subtree_control ; then
		echo "Skipping: cannot enable $subsys_ctrl in $sysfs" >&2
		exit $ksft_skip
	fi

	if ! echo -$subsys_ctrl >$sysfs/cgroup.subtree_control ; then
		echo "Skipping: cannot disable $subsys_ctrl in $sysfs" >&2
		exit $ksft_skip
	fi
}

declare -a stresses
declare -a stress_pids
duration=5
rc=0
subsys_ctrl=cpuset
sysfs=

while getopts c:d:hs: opt; do
	case $opt in
	c)
		subsys_ctrl=$OPTARG
		;;
	d)
		duration=$OPTARG
		;;
	h)
		echo "Usage $0 [ -s stress ] ... [ -d duration ] [-c controller] cmd args .."
		echo -e "\t default duration $duration seconds"
		echo -e "\t default controller $subsys_ctrl"
		exit
		;;
	s)
		func=stress_$OPTARG
		if [ "x$(type -t $func)" != "xfunction" ] ; then
			echo "Unknown stress $OPTARG"
			exit 1
		fi
		stresses+=($func)
		;;
	esac
done
shift $((OPTIND - 1))

init_and_check

for s in ${stresses[*]} ; do
	$s &
	stress_pids+=($!)
done


time=0
start=$(date +%s)

while [ $time -lt $duration ] ; do
	$*
	rc=$?
	[ $rc -eq 0 ] || break
	time=$(($(date +%s) - $start))
done

for pid in ${stress_pids[*]} ; do
	kill -SIGTERM $pid
	wait $pid
done

exit $rc