#!/bin/bash
# This script installs iozone and whatever
P=iozone-bench
DEFAULT_VERSION=3_428
. $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
		;;
	*)
		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/iozone-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/iozone-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-iozone -v ${VERSION}  || die iozone install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/iozone-${VERSION}-installed || die Failed to cd to iozone install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo iozone installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

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

if [ -z "$IOZONE_MAX_SIZE" ]; then
	IOZONE_MAX_SIZE=$((MEMTOTAL_BYTES*2))
fi

if [ -z "$IOZONE_MIN_SIZE" ]; then
	IOZONE_MIN_SIZE=$((IOZONE_MAX_SIZE/16))
fi

if [ -z "$IOZONE_MIN_BLK_SIZE" ]; then
	if [ -z "$IOZONE_MAX_BLK_SIZE" ]; then
		IOZONE_MAX_BLK_SIZE=4096
	fi
	IOZONE_MIN_BLK_SIZE="$IOZONE_MAX_BLK_SIZE"
elif [ -z "$IOZONE_MAX_BLK_SIZE" ]; then
	IOZONE_MAX_BLK_SIZE="$IOZONE_MIN_BLK_SIZE"
fi

IOZONE_PARAM=""
if [ "$IOZONE_FSYNC" = "yes" ]; then
	IOZONE_PARAM="$IOZONE_PARAM -e"
fi

if [ "$IOZONE_DIO" = "yes" ]; then
	IOZONE_PARAM="$IOZONE_PARAM -I"
fi

if [ -n "$IOZONE_NODE" ]; then
	MMTESTS_NUMA_POLICY=fullbind_single_instance_node
else
	MMTESTS_NUMA_POLICY=interleave
fi
set_mmtests_numactl $IOZONE_NODE $NUMNODES

monitor_pre_hook $LOGDIR_RESULTS
for (( ITERATION = 1; ITERATION <= IOZONE_ITERATIONS; ITERATION++ )); do
	mmtests_activity iteration $ITERATION/$IOZONE_ITERATIONS
	$MMTESTS_NUMACTL src/current/iozone -a -n $((IOZONE_MIN_SIZE/1024)) \
		-g $((IOZONE_MAX_SIZE/1024)) -y $((IOZONE_MIN_BLK_SIZE/1024)) \
		-q $((IOZONE_MAX_BLK_SIZE/1024)) -i 0 -i 1 -i 2 -i 3 \
		$IOZONE_PARAM -f $SHELLPACK_DATA/iozone_file 2>&1 | \
		tee $LOGDIR_RESULTS/iozone-$ITERATION.log ||
		die "Failed to run iozone"
done
monitor_post_hook $LOGDIR_RESULTS

exit $SHELLPACK_SUCCESS
#### Description iozone
#### Details iozone-bench 17
