#!/bin/bash
P=usemem-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
		;;
	--size)
		USEMEM_WORKLOAD_SIZE=$2
		shift 2
		;;
	--anon-percentage)
		USEMEM_PERCENTAGE_ANON=$2
		shift 2
		;;
	--loops)
		USEMEM_LOOPS=$2
		shift 2
		;;
	--iterations)
		USEMEM_ITERATIONS=$2
		shift 2
		;;
	--min-threads)
		USEMEM_MIN_THREADS=$2
		shift 2
		;;
	--max-threads)
		USEMEM_MAX_THREADS=$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/usemembuild-${VERSION}-installed
fi
if [ ! -d $SHELLPACK_SOURCES/usemembuild-${VERSION}-installed-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-usemembuild -v ${VERSION}  || die usemembuild install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/usemembuild-${VERSION}-installed || die Failed to cd to usemembuild install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo usemembuild installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

MEMTOTAL_ANON=$((USEMEM_WORKLOAD_SIZE*USEMEM_PERCENTAGE_ANON/100))
MEMTOTAL_FILE=$((USEMEM_WORKLOAD_SIZE*(100-USEMEM_PERCENTAGE_ANON)/100))
create_random_file $((MEMTOTAL_FILE+32*1048576)) $SHELLPACK_TEMP/source_file

# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

THREADS=
START_THREAD=$USEMEM_MIN_THREADS
END_THREAD=$USEMEM_MAX_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 $USEMEM_MIN_THREADS ]; then
		continue
	fi
	mmtests_activity process $NR_THREADS/$END_THREAD
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
for ITERATION in `seq 1 $USEMEM_ITERATIONS`; do
	mmtests_activity iteration $ITERATION
		echo Starting usemem with $NR_THREADS threads iteration $ITERATION/$USEMEM_ITERATIONS

		echo "#!/bin/bash
		echo -n > $SHELLPACK_TEMP/usemem.pids

		# Anon
		$SHELLPACK_SOURCES/usemembuild-$VERSION-installed/usemem	\
			-t $NR_THREADS						\
			-j 4096							\
			-r $USEMEM_LOOPS					\
			$((MEMTOTAL_ANON/NR_THREADS)) 2> /dev/null &
		echo \$! >> $SHELLPACK_TEMP/usemem.pids

		# File
		$SHELLPACK_SOURCES/usemembuild-$VERSION-installed/usemem	\
			-t $NR_THREADS						\
			-f $SHELLPACK_TEMP/source_file				\
			-j 4096							\
			-r $USEMEM_LOOPS					\
			--readonly						\
			$((MEMTOTAL_FILE/NR_THREADS)) 2> /dev/null &
		echo \$! >> $SHELLPACK_TEMP/usemem.pids

		for USEMEM_PID in \`cat $SHELLPACK_TEMP/usemem.pids\`; do
			echo Waiting on pid \$USEMEM_PID
			wait \$USEMEM_PID
		done" > $SHELLPACK_TEMP/usemem.sh
		chmod a+x $SHELLPACK_TEMP/usemem.sh
		$TIME_CMD -o $LOGDIR_RESULTS/usemem-$NR_THREADS-$ITERATION $SHELLPACK_TEMP/usemem.sh
		echo
done
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
done

exit $SHELLPACK_SUCCESS

#### Description usemem
#### Details usemem-bench 6
