#!/bin/bash

FULLCPUMASK=$(taskset -p 1 | cut -d " " -f 6-6)

if [ $(cat /proc/loadavg | cut -c 1-1) != 0 ] ; then
    echo "FAILED: Machine must be idle (ie. load ave < 1) to run this test"
    uptime
    exit 1
fi

# make this task stay out of the way
taskset -cp 0-3 $$ > /dev/null

LOOPS=30
FAILS=0

checkfail(){
    # if more than 1 in 15 are failures, it's bad.
    if [ $FAILS -gt $(($LOOPS/15)) ] ; then
	echo "Test Failed"
	exit 1
    fi
}

echo Testing takes upto $(($LOOPS*2 + 10)) secs

for i in {1..30} ; do
    while :; do :; done &

    PID=$!

    taskset -cp 5 $PID > /dev/null
    sleep 1 
    taskset -p $FULLCPUMASK $PID > /dev/null
    sleep 1

    CPU=$(cat /proc/$PID/stat |cut -d' ' -f 39-39)

    kill $PID; wait $PID 2>/dev/null # kill silently 

    echo "Final cpu = $CPU"

    if [ $(($CPU % 4)) != "0" ] ; then
	FAILS=$(($FAILS + 1))
	checkfail
    fi
done

echo "Test Passed"
exit 0


