2 # SPDX-License-Identifier: GPL-2.0+
4 # Alternate sleeping and spinning on randomly selected CPUs. The purpose
5 # of this script is to inflict random OS jitter on a concurrently running
8 # Usage: jitter.sh me jittering-path duration [ sleepmax [ spinmax ] ]
10 # me: Random-number-generator seed salt.
11 # duration: Time to run in seconds.
12 # jittering-path: Path to file whose removal will stop this script.
13 # sleepmax: Maximum microseconds to sleep, defaults to one second.
14 # spinmax: Maximum microseconds to spin, defaults to one millisecond.
16 # Copyright (C) IBM Corporation, 2016
18 # Authors: Paul E. McKenney <paulmck@linux.ibm.com>
28 starttime
=`gawk 'BEGIN { print systime(); }' < /dev/null`
31 for i
in /sys
/devices
/system
/cpu
/cpu
[0-9]*
37 curcpu
=`echo $i | sed -e 's/^[^0-9]*//'`
38 nohotplugcpus
="$nohotplugcpus $curcpu"
45 t
=`gawk -v s=$starttime 'BEGIN { print systime() - s; }' < /dev/null`
46 if test "$t" -gt "$duration"
51 # Check for stop request.
52 if ! test -f "$jittering"
57 # Set affinity to randomly selected online CPU
58 if cpus
=`grep 1 /sys/devices/system/cpu/*/online 2>&1 |
59 sed -e 's,/[^/]*$,,' -e 's/^[^0-9]*//'`
65 # Do not leave out non-hot-pluggable CPUs
66 cpus
="$cpus $nohotplugcpus"
68 cpumask
=`awk -v cpus="$cpus" -v me=$me -v n=$n 'BEGIN {
69 srand(n + me + systime());
70 ncpus = split(cpus, ca);
71 print ca[int(rand() * ncpus + 1)];
74 if ! taskset
-c -p $cpumask $$
> /dev
/null
2>&1
76 echo taskset failure
: '"taskset -c -p ' $cpumask $$
'"'
80 # Sleep a random duration
81 sleeptime
=`awk -v me=$me -v n=$n -v sleepmax=$sleepmax 'BEGIN {
82 srand(n + me + systime());
83 printf("%06d", int(rand() * sleepmax));
88 # Spin a random duration
89 limit
=`awk -v me=$me -v n=$n -v spinmax=$spinmax 'BEGIN {
90 srand(n + me + systime());
91 printf("%06d", int(rand() * spinmax));