#!/bin/bash
# This script installs fio and runs a fio job

P=fio-bench
DEFAULT_VERSION=2.2.13
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
FIO_CMD_OPTIONS=

# 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
		;;
	--cmdline)
		FIO_CMD_OPTIONS=$2
		shift 2
		;;
	--type)
		FIO_TEST_TYPE=$2
		shift 2
		;;
	--max_jobs)
		FIO_MAX_JOBS=$2
		shift 2
		;;
	--runtime)
		FIO_RUNTIME=$2
		shift 2
		;;
	--size)
		FIO_SIZE=$2
		shift 2
		;;
	--ioengine)
		FIO_IOENGINE=$2
		shift 2
		;;
	--direct)
		FIO_DIRECT=$2
		shift 2
		;;
	--iodepth)
		FIO_IODEPTH=$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/fio-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/fio-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-fio -v ${VERSION}  || die fio install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/fio-${VERSION}-installed || die Failed to cd to fio install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo fio installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

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

FIO_IO_PATTERNS="read write rw randread randwrite randrw"

if [ "$FIO_CMD_OPTIONS" != "" ]; then
	monitor_pre_hook $LOGDIR_RESULTS $SIZE
	ln -s $TESTDISK_DIR /tmp/fio-$$

	./fio --directory="$TESTDISK_DIR" --minimal \
		--write_lat_log $LOGDIR_RESULTS/fio $FIO_CMD_OPTIONS 2>&1 |
		tee $LOGDIR_RESULTS/fio.log || \
		die Failed to have fun with fio

	rm -rf $TESTDISK_DIR/*
	rm /tmp/fio-$$
	monitor_post_hook $LOGDIR_RESULTS $SIZE
	exit $SHELLPACK_SUCCESS
fi

# default values for scaling test
FIO_MAX_JOBS=${FIO_MAX_JOBS:-12}
FIO_RUNTIME=${FIO_RUNTIME:-30}
FIO_SIZE=${FIO_SIZE:-5G}
FIO_IOENGINE=${FIO_IOENGINE:-libaio}
FIO_DIRECT=${FIO_DIRECT:-1}
FIO_IODEPTH=${FIO_IODEPTH:-1}

case $FIO_TEST_TYPE in
scaling)
	DIR_NR=${#TESTDISK_DIRS[*]}
	DIRS=`echo ${TESTDISK_DIRS[*]} | tr " " ":"`
	monitor_pre_hook $LOGDIR_RESULTS $FIO_READWRITE
	for FIO_RW in $FIO_IO_PATTERNS; do
		for FIO_NUMJOBS in `seq $DIR_NR $DIR_NR $NUMCPUS`; do
			if [ $FIO_NUMJOBS -gt $FIO_MAX_JOBS ]; then
				break;
			fi
			./fio --directory=$DIRS --rw=$FIO_RW \
				--name=fio-scaling --size=$FIO_SIZE --numjobs=$FIO_NUMJOBS \
				--ioengine=$FIO_IOENGINE --group_reporting \
				--direct=$FIO_DIRECT --iodepth=$FIO_IODEPTH \
				--runtime=$FIO_RUNTIME 2>&1 | \
				tee -a $LOGDIR_RESULTS/fio-$FIO_TEST_TYPE-$FIO_RW \
					|| die Failed to have fun with fio
		done
	done
	monitor_post_hook $LOGDIR_RESULTS $FIO_READWRITE
	;;
*)
	die Unrecognised test type $FIO_TEST_TYPE
	;;
esac

exit $SHELLPACK_SUCCESS
#### Description Flexible IO tester
#### Details fio-bench 14
