#!/bin/bash
# This is the script for running the hackbench benchmark
P=hackbench-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
iterations=1
CONN_NAME=
CONN_TYPE=
HACKBENCH_TYPE=process

# 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
		;;
	--pipes)
		CONN_TYPE="-pipe"
		CONN_NAME=pipes
		shift
		;;
	--sockets)
		CONN_TYPE=
		CONN_NAME=sockets
		shift
		;;
	--min-groups)
		HACKBENCH_MIN_GROUPS=$2
		shift 2
		;;
	--max-groups)
		HACKBENCH_MAX_GROUPS=$2
		shift 2
		;;
	--iterations)
		HACKBENCH_ITERATIONS=$2
		shift 2
		;;
	--type)
		HACKBENCH_TYPE=$2
		shift 2
		;;
	--loops)
		HACKBENCH_LOOPS=$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/hackbench
fi
if [ ! -d $SHELLPACK_SOURCES/hackbench-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-hackbench -v ${VERSION}  || die hackbench install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/hackbench-${VERSION}-installed || die Failed to cd to hackbench install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo hackbench installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

pushd $SHELLPACK_SOURCES/hackbench-${VERSION}-installed > /dev/null
RESULTSLOG=$LOGDIR_RESULTS/hackbench

# Ensure we don't hit the open file descriptor ulimit when running
# hackbench-threads-*. " * 2" adds a bit of headroom for marvin's
# processes, e.g. monitors.
ulimit -n $((HACKBENCH_MAX_GROUPS * 40 * 2))

THREADS=
START_THREAD=$HACKBENCH_MIN_GROUPS
if [ "$MMTESTS_THREAD_CUTOFF" != "" ]; then
	echo Forcing HACKBENCH_MAX_GROUPS to $MMTESTS_THREAD_CUTOFF
	HACKBENCH_MAX_GROUPS=$MMTESTS_THREAD_CUTOFF
fi
END_THREAD=$HACKBENCH_MAX_GROUPS
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 $HACKBENCH_MIN_GROUPS ]; then
		continue
	fi
	mmtests_activity process $NR_THREADS/$END_THREAD
	mmtests_activity $CONN_NAME-$NR_THREADS
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS

for ITERATION in `seq 1 $HACKBENCH_ITERATIONS`; do
	mmtests_activity iteration $ITERATION
		echo Running $CONN_NAME $NR_THREADS groups iteration $ITERATION/$HACKBENCH_ITERATIONS
		echo pwd: `pwd`
		echo cmd: hackbench $CONN_TYPE $NR_THREADS $HACKBENCH_TYPE $HACKBENCH_LOOPS
		$TIME_CMD -o $LOGDIR_RESULTS/time-$NR_THREADS-$ITERATION  \
			./hackbench $CONN_TYPE $NR_THREADS $HACKBENCH_TYPE $HACKBENCH_LOOPS \
				> $LOGDIR_RESULTS/hackbench-$NR_THREADS-$ITERATION

		# Failure likely due to open file or pid limitations
		if [ $? -ne 0 ]; then
			monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
			rm $LOGDIR_RESULTS/hackbench-$NR_THREADS-$ITERATION
			exit $SHELLPACK_SUCCESS
		fi
sync
done
	
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
done

exit $RETVAL
#### Description Run the HackBench benchmark
#### Details hackbench-bench 37
