#!/bin/bash
# cyclictest
P=cyclictest-bench
DEFAULT_VERSION=v1.3
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

CYCLICTEST_BACKGROUND=none

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
TASKSET_ALL=
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
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--duration)
		CYCLICTEST_DURATION=$2
		shift 2
		;;
	--background)
		CYCLICTEST_BACKGROUND=$2
		shift 2
		;;
	--affinity)
		CYCLICTEST_AFFINITY=yes
		shift
		;;
	*)
		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/rttestbuild-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/rttestbuild-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-rttestbuild -v ${VERSION}  || die rttestbuild install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/rttestbuild-${VERSION}-installed || die Failed to cd to rttestbuild install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo rttestbuild installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh
cd $SHELLPACK_SOURCES/rttestbuild-${VERSION}-installed || die "Failed to change to rttest directory"

if [ "$CYCLICTEST_AFFINITY" = "yes" ]; then
	CYCLICTEST_AFFINITY_PARAM=-a
fi

BACKGROUND_PID=
case $CYCLICTEST_BACKGROUND in
hackbench)
	./hackbench -P -g $NUMCPUS -l 200000000 &
	BACKGROUND_PID=$!
	echo "Background hackbench $PID"
	;;
none)
	echo "No background task specified"
	;;
*)
	die "Unrecognised background task $CYCLICTEST_BACKGROUND"
	;;
esac
monitor_pre_hook $LOGDIR_RESULTS
echo Starting cyclictest threads
./cyclictest --mlockall -p99 --threads $CYCLICTEST_AFFINITY_PARAM \
	--distance=0 --duration=$CYCLICTEST_DURATION --quiet 2>&1 | \
	tee $LOGDIR_RESULTS/cyclictest.log
if [ "$BACKGROUND_PID" != "" ]; then
	shutdown_pid $CYCLICTEST_BACKGROUND $BACKGROUND_PID
fi
monitor_post_hook $LOGDIR_RESULTS
exit $SHELLPACK_SUCCESS
#### Description cyclictest
#### Details cyclictest-bench 15
