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 duration [ sleepmax [ spinmax ] ]
10 # me: Random-number-generator seed salt.
11 # duration: Time to run in seconds.
12 # sleepmax: Maximum microseconds to sleep, defaults to one second.
13 # spinmax: Maximum microseconds to spin, defaults to one millisecond.
15 # Copyright (C) IBM Corporation, 2016
17 # Authors: Paul E. McKenney <paulmck@linux.ibm.com>
26 starttime=`awk 'BEGIN { print systime(); }' < /dev/null`
31 t=`awk -v s=$starttime 'BEGIN { print systime() - s; }' < /dev/null`
32 if test "$t" -gt "$duration"
37 # Set affinity to randomly selected CPU
38 cpus=`ls /sys/devices/system/cpu/*/online |
39 sed -e 's,/[^/]*$,,' -e 's/^[^0-9]*//' |
41 cpumask=`awk -v cpus="$cpus" -v me=$me -v n=$n 'BEGIN {
42 srand(n + me + systime());
43 ncpus = split(cpus, ca);
44 curcpu = ca[int(rand() * ncpus + 1)];
45 mask = lshift(1, curcpu);
48 printf("%#x\n", mask);
51 if ! taskset -p $cpumask $$ > /dev/null 2>&1
53 echo taskset failure: '"taskset -p ' $cpumask $$ '"'
57 # Sleep a random duration
58 sleeptime=`awk -v me=$me -v n=$n -v sleepmax=$sleepmax 'BEGIN {
59 srand(n + me + systime());
60 printf("%06d", int(rand() * sleepmax));
65 # Spin a random duration
66 limit=`awk -v me=$me -v n=$n -v spinmax=$spinmax 'BEGIN {
67 srand(n + me + systime());
68 printf("%06d", int(rand() * spinmax));