#!/bin/bash
# Run adrestia benchmark

P=adrestia-bench
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--min-arrival-time)
		ADRESTIA_MIN_ATIME=$2
		shift 2
		;;
	--max-arrival-time)
		ADRESTIA_MAX_ATIME=$2
		shift 2
		;;
	--threads)
		ADRESTIA_THREADS=$2
		shift 2
		;;
	--service-time)
		ADRESTIA_STIME=$2
		shift 2
		;;
	--loops)
		ADRESTIA_LOOPS=$2
		shift 2
		;;
	--test)
		ADRESTIA_TEST=$2
		shift 2
		;;
	--iterations)
		ADRESTIA_ITERATIONS=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/adrestia-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/adrestia-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-adrestia -v ${VERSION}  || die adrestia install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/adrestia-${VERSION}-installed || die Failed to cd to adrestia install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo adrestia installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

# The single test doesn't use arrival time or multiple threads, so
# avoid needless iterations.
if [ "$ADRESTIA_TEST" = "wakeup-single" ]; then
	ADRESTIA_MAX_ATIME=$ADRESTIA_MIN_ATIME
	ADRESTIA_THREADS=1
fi

ATIME=$ADRESTIA_MIN_ATIME
while [ $ATIME -le $ADRESTIA_MAX_ATIME ]; do
THREADS=
START_THREAD=1
END_THREAD=$ADRESTIA_THREADS
if [ $END_THREAD -gt 32 ]; then
	THREADS=`seq $START_THREAD 3 8`
	THREADS="$THREADS `seq 12 9 32`"
	THREADS="$THREADS `seq 48 31 $END_THREAD`"
elif [ $END_THREAD -gt 8 ]; then
	THREADS=`seq $START_THREAD 2 8`
	THREADS="$THREADS `seq 12 6 $END_THREAD`"
else
	THREADS=`seq $START_THREAD 2 $END_THREAD`
fi
if [ `echo $THREADS | awk '{print $NF}'` -ne $END_THREAD ]; then
	THREADS="$THREADS $END_THREAD"
fi

for NR_THREADS in $THREADS; do
	if [ $NR_THREADS -lt 1 ]; then
		continue
	fi
	mmtests_activity process $NR_THREADS/$END_THREAD
for ITERATION in `seq 1 $ADRESTIA_ITERATIONS`; do
	mmtests_activity iteration $ITERATION
		echo Running $ADRESTIA_TEST $ATIME:$ADRESTIA_STIME threads $NR_THREADS $ITERATION/$ADRESTIA_ITERATIONS
		$TIME_CMD -o $LOGDIR_RESULTS/time-$ATIME-$NR_THREADS-$ITERATION \
			./adrestia -a $ATIME -l $ADRESTIA_LOOPS -s $ADRESTIA_STIME -t $NR_THREADS $ADRESTIA_TEST \
				> $LOGDIR_RESULTS/adrestia-$ATIME-$NR_THREADS-$ITERATION.log
done
done
	((ATIME *= 2))
done

exit $SHELLPACK_SUCCESS
#### Description Linux scheduler load balancer microbenchmark suite
#### Details adrestia-bench 5
