WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / rcutorture / bin / jitter.sh
blob188b864bc4bf61fa5a6e74eb5641098c044ddd5f
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 # Check for stop request.
50 if test -f "$TORTURE_STOPFILE"
51 then
52 exit 1;
55 # Set affinity to randomly selected online CPU
56 if cpus=`grep 1 /sys/devices/system/cpu/*/online 2>&1 |
57 sed -e 's,/[^/]*$,,' -e 's/^[^0-9]*//'`
58 then
60 else
61 cpus=
63 # Do not leave out non-hot-pluggable CPUs
64 cpus="$cpus $nohotplugcpus"
66 cpumask=`awk -v cpus="$cpus" -v me=$me -v n=$n 'BEGIN {
67 srand(n + me + systime());
68 ncpus = split(cpus, ca);
69 curcpu = ca[int(rand() * ncpus + 1)];
70 mask = lshift(1, curcpu);
71 if (mask + 0 <= 0)
72 mask = 1;
73 printf("%#x\n", mask);
74 }' < /dev/null`
75 n=$(($n+1))
76 if ! taskset -p $cpumask $$ > /dev/null 2>&1
77 then
78 echo taskset failure: '"taskset -p ' $cpumask $$ '"'
79 exit 1
82 # Sleep a random duration
83 sleeptime=`awk -v me=$me -v n=$n -v sleepmax=$sleepmax 'BEGIN {
84 srand(n + me + systime());
85 printf("%06d", int(rand() * sleepmax));
86 }' < /dev/null`
87 n=$(($n+1))
88 sleep .$sleeptime
90 # Spin a random duration
91 limit=`awk -v me=$me -v n=$n -v spinmax=$spinmax 'BEGIN {
92 srand(n + me + systime());
93 printf("%06d", int(rand() * spinmax));
94 }' < /dev/null`
95 n=$(($n+1))
96 for i in {1..$limit}
98 echo > /dev/null
99 done
100 done
102 exit 1