#! /bin/bash

WORKDIR=`pwd`
CGDIR=/cgroup/cpu
TASKSLIST="16 32 48"
TIMETORUN=60

if [ $1 > 0 ]
then
    TIMETORUN=$1;
fi

cd $CGDIR
for i in `echo $TASKSLIST`
do
    echo -ne "Starting task group fair$i..."
    mkdir -p fair$i
    
    for j in `seq 1 $i`
    do
	$WORKDIR/dowhile &
	echo $! > fair$i/tasks
    done
    echo "done"
done

cd $WORKDIR
echo "Waiting for the task to run for $TIMETORUN secs"

sleep $TIMETORUN

echo "Interpreting the results. Please wait...."
cat /proc/sched_debug | grep "dowhile" > fairtest.log
for i in `echo $TASKSLIST`
do
echo -ne  "Time consumed by fair$i cgroup:  "
FAIRRESULT=`sed -ne "/fair$i\$/p" fairtest.log | gawk '$1 == "dowhile" { sum += $7 } $1 =="R" { sum += $8} END { printf "%20d Tasks: %d\n", sum,NR }'`
echo $FAIRRESULT
done

killall dowhile
