1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0+ 3# 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. 7# 8# Usage: jitter.sh me jittering-path duration [ sleepmax [ spinmax ] ] 9# 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. 15# 16# Copyright (C) IBM Corporation, 2016 17# 18# Authors: Paul E. McKenney <paulmck@linux.ibm.com> 19 20me=$(($1 * 1000)) 21jittering=$2 22duration=$3 23sleepmax=${4-1000000} 24spinmax=${5-1000} 25 26n=1 27 28starttime=`gawk 'BEGIN { print systime(); }' < /dev/null` 29 30nohotplugcpus= 31for i in /sys/devices/system/cpu/cpu[0-9]* 32do 33 if test -f $i/online 34 then 35 : 36 else 37 curcpu=`echo $i | sed -e 's/^[^0-9]*//'` 38 nohotplugcpus="$nohotplugcpus $curcpu" 39 fi 40done 41 42while : 43do 44 # Check for done. 45 t=`gawk -v s=$starttime 'BEGIN { print systime() - s; }' < /dev/null` 46 if test "$t" -gt "$duration" 47 then 48 exit 0; 49 fi 50 51 # Check for stop request. 52 if ! test -f "$jittering" 53 then 54 exit 1; 55 fi 56 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]*//'` 60 then 61 : 62 else 63 cpus= 64 fi 65 # Do not leave out non-hot-pluggable CPUs 66 cpus="$cpus $nohotplugcpus" 67 68 cpumask=`awk -v cpus="$cpus" -v me=$me -v n=$n 'BEGIN { 69 srand(n + me + systime()); 70 ncpus = split(cpus, ca); 71 curcpu = ca[int(rand() * ncpus + 1)]; 72 z = ""; 73 for (i = 1; 4 * i <= curcpu; i++) 74 z = z "0"; 75 print "0x" 2 ^ (curcpu % 4) z; 76 }' < /dev/null` 77 n=$(($n+1)) 78 if ! taskset -p $cpumask $$ > /dev/null 2>&1 79 then 80 echo taskset failure: '"taskset -p ' $cpumask $$ '"' 81 exit 1 82 fi 83 84 # Sleep a random duration 85 sleeptime=`awk -v me=$me -v n=$n -v sleepmax=$sleepmax 'BEGIN { 86 srand(n + me + systime()); 87 printf("%06d", int(rand() * sleepmax)); 88 }' < /dev/null` 89 n=$(($n+1)) 90 sleep .$sleeptime 91 92 # Spin a random duration 93 limit=`awk -v me=$me -v n=$n -v spinmax=$spinmax 'BEGIN { 94 srand(n + me + systime()); 95 printf("%06d", int(rand() * spinmax)); 96 }' < /dev/null` 97 n=$(($n+1)) 98 for i in {1..$limit} 99 do 100 echo > /dev/null 101 done 102done 103 104exit 1 105