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 42# Uses global variables startsecs, startns, endsecs, endns, and limit. 43# Exit code is success for time not yet elapsed and failure otherwise. 44function timecheck { 45 local done=`awk -v limit=$limit \ 46 -v startsecs=$startsecs \ 47 -v startns=$startns \ 48 -v endsecs=$endsecs \ 49 -v endns=$endns < /dev/null ' 50 BEGIN { 51 delta = (endsecs - startsecs) * 1000 * 1000; 52 delta += int((endns - startns) / 1000); 53 print delta >= limit; 54 }'` 55 return $done 56} 57 58while : 59do 60 # Check for done. 61 t=`gawk -v s=$starttime 'BEGIN { print systime() - s; }' < /dev/null` 62 if test "$t" -gt "$duration" 63 then 64 exit 0; 65 fi 66 67 # Check for stop request. 68 if ! test -f "$jittering" 69 then 70 exit 1; 71 fi 72 73 # Set affinity to randomly selected online CPU 74 if cpus=`grep 1 /sys/devices/system/cpu/*/online 2>&1 | 75 sed -e 's,/[^/]*$,,' -e 's/^[^0-9]*//'` 76 then 77 : 78 else 79 cpus= 80 fi 81 # Do not leave out non-hot-pluggable CPUs 82 cpus="$cpus $nohotplugcpus" 83 84 cpumask=`awk -v cpus="$cpus" -v me=$me -v n=$n 'BEGIN { 85 srand(n + me + systime()); 86 ncpus = split(cpus, ca); 87 print ca[int(rand() * ncpus + 1)]; 88 }' < /dev/null` 89 n=$(($n+1)) 90 if ! taskset -c -p $cpumask $$ > /dev/null 2>&1 91 then 92 echo taskset failure: '"taskset -c -p ' $cpumask $$ '"' 93 exit 1 94 fi 95 96 # Sleep a random duration 97 sleeptime=`awk -v me=$me -v n=$n -v sleepmax=$sleepmax 'BEGIN { 98 srand(n + me + systime()); 99 printf("%06d", int(rand() * sleepmax)); 100 }' < /dev/null` 101 n=$(($n+1)) 102 sleep .$sleeptime 103 104 # Spin a random duration, but with rather coarse granularity. 105 limit=`awk -v me=$me -v n=$n -v spinmax=$spinmax 'BEGIN { 106 srand(n + me + systime()); 107 printf("%06d", int(rand() * spinmax)); 108 }' < /dev/null` 109 n=$(($n+1)) 110 startsecs=`date +%s` 111 startns=`date +%N` 112 endsecs=$startns 113 endns=$endns 114 while timecheck 115 do 116 endsecs=`date +%s` 117 endns=`date +%N` 118 done 119done 120 121exit 1 122