#!/bin/bash
# This script installs fsmark and runs the regression tests

P=fsmark-bench
DEFAULT_VERSION=3.3
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
MIN_THREADS=1
MAX_THREADS=1
NUM_SUB_DIRECTORIES=100
NUM_FILES_PER_ITERATION=50000
NUM_ITERATIONS=63
FILESIZE=0

# 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
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--min-threads)
		MIN_THREADS=$2
		shift 2
		;;
	--max-threads)
		MAX_THREADS=$2
		shift 2
		;;
	--filesize)
		FILESIZE=$2
		shift 2
		;;
	--nr-files-per-iteration)
		NUM_FILES_PER_ITERATION=$2
		shift 2
		;;
	--nr-sub-directories)
		NUM_SUB_DIRECTORIES=$2
		shift 2
		;;
	--iterations)
		NUM_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/fsmark-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/fsmark-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-fsmark -v ${VERSION}  || die fsmark install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/fsmark-${VERSION}-installed || die Failed to cd to fsmark install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo fsmark installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

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

ln -s $SHELLPACK_TEMP /tmp/fsmark-$$

THREADS=
START_THREAD=$MIN_THREADS
END_THREAD=$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
	mmtests_activity process $NR_THREADS/$END_THREAD
	PARAM=
	for THREAD in `seq 1 $NR_THREADS`; do
		mkdir -p /tmp/fsmark-$$/$THREAD
		PARAM="$PARAM -d /tmp/fsmark-$$/$THREAD"
	done

	echo ./fs_mark \
		$PARAM \
		-D $NUM_SUB_DIRECTORIES \
		-n $(($NUM_FILES_PER_ITERATION/$NR_THREADS)) \
		-L $NUM_ITERATIONS \
		-t $NR_THREADS \
		-S0 \
		-s $FILESIZE | tee $LOGDIR_RESULTS/fsmark-cmd-${NR_THREADS}.log

	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	eval ./fs_mark \
		$PARAM \
		-D $NUM_SUB_DIRECTORIES \
		-n $(($NUM_FILES_PER_ITERATION/$NR_THREADS)) \
		-L $NUM_ITERATIONS \
		-t $NR_THREADS \
		-S0 \
		-s $FILESIZE \
			2>&1 | tee $LOGDIR_RESULTS/fsmark-${NR_THREADS}.log \
				|| die Failed to run fsmark
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
	rm -rf $SHELLPACK_TEMP/*
done

rm /tmp/fsmark-$$

exit $SHELLPACK_SUCCESS
#### Description fsmark
#### Details fsmark-bench 33
