#!/bin/sh
# Hotplug test

usage() {
	echo "Usage: $0 [# cpus]"
	echo "   If number of cpus is not given, defaults to 2"
	exit
}

# Default to 2 CPUs
NR_CPUS=${1:-2}

[ $NR_CPUS -lt 2 ] && usage 1>&2

MAXCPU=$((NR_CPUS-1))
MAX=`cat /sys/devices/system/cpu/kernel_max`

[ $MAXCPU -gt $MAX ] && echo "Too many CPUs" 1>&2 && usage 1>&2

cpu_path() {
	echo /sys/devices/system/cpu/cpu$1
}

checkpoint_test() {
	if [ $(($1 % 50)) -eq 0 ]; then
		echo "**** Finished test $1 ****"
	fi
}

echo '****'
echo "Testing $NR_CPUS CPUs"
echo '****'

TEST=0
while :
do
	N=$((RANDOM % MAXCPU + 1))
	ON=`cat $(cpu_path $N)/online`
	echo $((1-ON)) > $(cpu_path $N)/online
	TEST=$((TEST+1))
	checkpoint_test $TEST
done
