#!/bin/bash
# This script whatevers vmscale
P=vmscale-bench
DEFAULT_VERSION=1
. $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
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--cases)
		VMSCALE_CASES=$2
		shift 2
		;;
	--use-perf)
		VMSCALE_USE_PERF=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

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

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

cd $SHELLPACK_SOURCES/vmscale-${VERSION}-installed || die Failed to cd to vmscale directory
VMSCALE_CASES=`echo $VMSCALE_CASES | sed -e 's/,/ /g'`

echo Unlimiting virtual memory usage
ulimit -v unlimited

echo -n > $LOGDIR_RESULTS/cases
for CASE in $VMSCALE_CASES; do
	mmtests_activity $CASE

	# Prepare for compatability with how vmscale works without using the
	# run script as it does more setup than mmtests needs to deal with
	rm -rf /tmp/vm-scalability
	rm -rf $SHELLPACK_TEMP/*
	ln -s $SHELLPACK_TEMP /tmp/vm-scalability

	echo $CASE | tee -a $LOGDIR_RESULTS/cases
	monitor_pre_hook  $LOGDIR_RESULTS $CASE
	if [ "$VMSCALE_USE_PERF" = "yes" ]; then
		$TIME_CMD "./run case-$CASE" 2>$LOGDIR_RESULTS/$CASE.time | tee $LOGDIR_RESULTS/${CASE}.log
		mv `hostname`-* $LOGDIR_RESULTS/$CASE-raw
	else
		$TIME_CMD "./case-$CASE" 2>$LOGDIR_RESULTS/$CASE.time | tee $LOGDIR_RESULTS/${CASE}.log
	fi
	monitor_post_hook $LOGDIR_RESULTS $CASE
done

exit $SHELLPACK_SUCCESS
#### Description vm scalability test suite
#### Details vmscale-bench 33
