treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / rcutorture / bin / jitter.sh
blob30cb5b27d32e56cb629b36a911d8bda029db2def
1 #!/bin/bash
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
6 # test.
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>
19 me=$(($1 * 1000))
20 duration=$2
21 sleepmax=${3-1000000}
22 spinmax=${4-1000}
24 n=1
26 starttime=`gawk 'BEGIN { print systime(); }' < /dev/null`
28 nohotplugcpus=
29 for i in /sys/devices/system/cpu/cpu[0-9]*
31 if test -f $i/online
32 then
34 else
35 curcpu=`echo $i | sed -e 's/^[^0-9]*//'`
36 nohotplugcpus="$nohotplugcpus $curcpu"
38 done
40 while :
42 # Check for done.
43 t=`gawk -v s=$starttime 'BEGIN { print systime() - s; }' < /dev/null`
44 if test "$t" -gt "$duration"
45 then
46 exit 0;
49 # Set affinity to randomly selected online CPU
50 if cpus=`grep 1 /sys/devices/system/cpu/*/online 2>&1 |
51 sed -e 's,/[^/]*$,,' -e 's/^[^0-9]*//'`
52 then
54 else
55 cpus=
57 # Do not leave out non-hot-pluggable CPUs
58 cpus="$cpus $nohotplugcpus"
60 cpumask=`awk -v cpus="$cpus" -v me=$me -v n=$n 'BEGIN {
61 srand(n + me + systime());
62 ncpus = split(cpus, ca);
63 curcpu = ca[int(rand() * ncpus + 1)];
64 mask = lshift(1, curcpu);
65 if (mask + 0 <= 0)
66 mask = 1;
67 printf("%#x\n", mask);
68 }' < /dev/null`
69 n=$(($n+1))
70 if ! taskset -p $cpumask $$ > /dev/null 2>&1
71 then
72 echo taskset failure: '"taskset -p ' $cpumask $$ '"'
73 exit 1
76 # Sleep a random duration
77 sleeptime=`awk -v me=$me -v n=$n -v sleepmax=$sleepmax 'BEGIN {
78 srand(n + me + systime());
79 printf("%06d", int(rand() * sleepmax));
80 }' < /dev/null`
81 n=$(($n+1))
82 sleep .$sleeptime
84 # Spin a random duration
85 limit=`awk -v me=$me -v n=$n -v spinmax=$spinmax 'BEGIN {
86 srand(n + me + systime());
87 printf("%06d", int(rand() * spinmax));
88 }' < /dev/null`
89 n=$(($n+1))
90 for i in {1..$limit}
92 echo > /dev/null
93 done
94 done
96 exit 1