2016-03-30 05:22:26 +08:00
|
|
|
#!/bin/bash
|
2019-02-11 23:14:09 +08:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2016-03-30 05:22:26 +08:00
|
|
|
#
|
|
|
|
# Alternate sleeping and spinning on randomly selected CPUs. The purpose
|
|
|
|
# of this script is to inflict random OS jitter on a concurrently running
|
|
|
|
# test.
|
|
|
|
#
|
2021-02-12 03:54:43 +08:00
|
|
|
# Usage: jitter.sh me jittering-path duration [ sleepmax [ spinmax ] ]
|
2016-03-30 05:22:26 +08:00
|
|
|
#
|
|
|
|
# me: Random-number-generator seed salt.
|
|
|
|
# duration: Time to run in seconds.
|
2021-02-12 02:39:28 +08:00
|
|
|
# jittering-path: Path to file whose removal will stop this script.
|
2016-03-30 05:22:26 +08:00
|
|
|
# sleepmax: Maximum microseconds to sleep, defaults to one second.
|
|
|
|
# spinmax: Maximum microseconds to spin, defaults to one millisecond.
|
|
|
|
#
|
|
|
|
# Copyright (C) IBM Corporation, 2016
|
|
|
|
#
|
2019-02-11 23:14:09 +08:00
|
|
|
# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
|
2016-03-30 05:22:26 +08:00
|
|
|
|
|
|
|
me=$(($1 * 1000))
|
2021-02-12 03:54:43 +08:00
|
|
|
jittering=$2
|
|
|
|
duration=$3
|
2021-02-12 02:39:28 +08:00
|
|
|
sleepmax=${4-1000000}
|
|
|
|
spinmax=${5-1000}
|
2016-03-30 05:22:26 +08:00
|
|
|
|
|
|
|
n=1
|
|
|
|
|
2019-10-07 05:33:22 +08:00
|
|
|
starttime=`gawk 'BEGIN { print systime(); }' < /dev/null`
|
2016-03-30 05:22:26 +08:00
|
|
|
|
2019-10-14 22:05:38 +08:00
|
|
|
nohotplugcpus=
|
|
|
|
for i in /sys/devices/system/cpu/cpu[0-9]*
|
|
|
|
do
|
|
|
|
if test -f $i/online
|
|
|
|
then
|
|
|
|
:
|
|
|
|
else
|
|
|
|
curcpu=`echo $i | sed -e 's/^[^0-9]*//'`
|
|
|
|
nohotplugcpus="$nohotplugcpus $curcpu"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2016-03-30 05:22:26 +08:00
|
|
|
while :
|
|
|
|
do
|
|
|
|
# Check for done.
|
2019-10-07 05:33:22 +08:00
|
|
|
t=`gawk -v s=$starttime 'BEGIN { print systime() - s; }' < /dev/null`
|
2016-03-30 05:22:26 +08:00
|
|
|
if test "$t" -gt "$duration"
|
|
|
|
then
|
|
|
|
exit 0;
|
|
|
|
fi
|
|
|
|
|
2020-06-10 08:58:30 +08:00
|
|
|
# Check for stop request.
|
2021-02-12 02:39:28 +08:00
|
|
|
if ! test -f "$jittering"
|
2020-06-10 08:58:30 +08:00
|
|
|
then
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
2019-03-27 03:24:10 +08:00
|
|
|
# Set affinity to randomly selected online CPU
|
2019-10-14 22:05:38 +08:00
|
|
|
if cpus=`grep 1 /sys/devices/system/cpu/*/online 2>&1 |
|
|
|
|
sed -e 's,/[^/]*$,,' -e 's/^[^0-9]*//'`
|
|
|
|
then
|
|
|
|
:
|
|
|
|
else
|
|
|
|
cpus=
|
2019-03-27 03:24:11 +08:00
|
|
|
fi
|
2019-10-14 22:05:38 +08:00
|
|
|
# Do not leave out non-hot-pluggable CPUs
|
|
|
|
cpus="$cpus $nohotplugcpus"
|
2019-03-27 03:24:10 +08:00
|
|
|
|
2016-03-30 05:22:26 +08:00
|
|
|
cpumask=`awk -v cpus="$cpus" -v me=$me -v n=$n 'BEGIN {
|
|
|
|
srand(n + me + systime());
|
|
|
|
ncpus = split(cpus, ca);
|
2021-07-09 02:02:14 +08:00
|
|
|
print ca[int(rand() * ncpus + 1)];
|
2016-03-30 05:22:26 +08:00
|
|
|
}' < /dev/null`
|
|
|
|
n=$(($n+1))
|
2021-07-09 02:02:14 +08:00
|
|
|
if ! taskset -c -p $cpumask $$ > /dev/null 2>&1
|
2016-03-30 05:22:26 +08:00
|
|
|
then
|
2021-07-09 02:02:14 +08:00
|
|
|
echo taskset failure: '"taskset -c -p ' $cpumask $$ '"'
|
2016-03-30 05:22:26 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Sleep a random duration
|
|
|
|
sleeptime=`awk -v me=$me -v n=$n -v sleepmax=$sleepmax 'BEGIN {
|
|
|
|
srand(n + me + systime());
|
|
|
|
printf("%06d", int(rand() * sleepmax));
|
|
|
|
}' < /dev/null`
|
|
|
|
n=$(($n+1))
|
|
|
|
sleep .$sleeptime
|
|
|
|
|
|
|
|
# Spin a random duration
|
|
|
|
limit=`awk -v me=$me -v n=$n -v spinmax=$spinmax 'BEGIN {
|
|
|
|
srand(n + me + systime());
|
|
|
|
printf("%06d", int(rand() * spinmax));
|
|
|
|
}' < /dev/null`
|
|
|
|
n=$(($n+1))
|
|
|
|
for i in {1..$limit}
|
|
|
|
do
|
|
|
|
echo > /dev/null
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
exit 1
|