#!/bin/bash
# This script installs iozone and whatever
###SHELLPACK preamble iozone-bench 3_428

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required iozone-${VERSION}
###SHELLPACK init_complete

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 $P
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 $P

exit $SHELLPACK_SUCCESS
