#!/bin/bash

. run_profile.txt
. $base_dir/plans/commands/common.fns

# Check needed vars from run_profile.txt
[ ! -d "$run_dir" ] && echo "ERROR: $run_dir does not exist" && exit -10

SUT=`hostname -s`
log_dir="$run_dir/test_output"
mkdir -p $log_dir

# use $FIOFILES for fio files
FIOFILES=/root/tmp
mkdir -p $FIOFILES

echo "Running fio on SUT: $SUT"
export PATH=/usr/local/bin:$PATH
runit="/usr/bin/time fio"

# non-extensive combinations of fio parameters/arguments/options
# (old run_fio.sh file)
#
# test matrix:
# ioengine=sync libaio posixaio mmap splice sg
# mem=malloc shm mmap
# direct=bool|buffered=bool (mut-ex.)
# sync=bool
# ioscheduler= {wait}

TIMESECS=60
FIXEDOPTS="--timeout $TIMESECS --runtime=$TIMESECS --numjobs=3 --verify=crc32 --directory=$FIOFILES --size=100M --bsrange=4K-256K --iodepth=8 --end_fsync=1 --rw=randrw --rwmixread=65 --rwmixwrite=35 --unlink=0"
## --minimal

# variances:  ioengine=str; mem=str; direct=bool|buffered=bool; sync=bool;

echo "= 1 = 1 = 1 = 1 = 1 = 1 = 1 = 1 = 1 = 1 = 1 = 1 ="
specs="--ioengine=sync --mem=malloc --buffered=1 --sync=0"
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run1.log"

echo "= 2 = 2 = 2 = 2 = 2 = 2 = 2 = 2 = 2 = 2 = 2 = 2 ="
specs="--ioengine=sync --mem=mmap   --buffered=0 --sync=1"
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run2.log"

echo "= 3 = 3 = 3 = 3 = 3 = 3 = 3 = 3 = 3 = 3 = 3 = 3 ="
specs="--ioengine=libaio --mem=malloc --buffered=0 --sync=1"
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run3.log"

echo "= 4 = 4 = 4 = 4 = 4 = 4 = 4 = 4 = 4 = 4 = 4 = 4 ="
specs="--ioengine=libaio --mem=shm    --buffered=1 --sync=0" 
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run4.log"

echo "= 5 = 5 = 5 = 5 = 5 = 5 = 5 = 5 = 5 = 5 = 5 = 5 ="
specs="--ioengine=posixaio --mem=malloc --buffered=1 --sync=0"
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run5.log"

echo "= 6 = 6 = 6 = 6 = 6 = 6 = 6 = 6 = 6 = 6 = 6 = 6 ="
specs="--ioengine=posixaio --mem=shm    --buffered=0 --sync=1"
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run6.log"

echo "= 7 = 7 = 7 = 7 = 7 = 7 = 7 = 7 = 7 = 7 = 7 = 7 ="
specs="--ioengine=mmap --mem=malloc --buffered=0 --sync=1"
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run7.log"

echo "= 8 = 8 = 8 = 8 = 8 = 8 = 8 = 8 = 8 = 8 = 8 = 8 ="
specs="--ioengine=mmap --mem=mmap   --buffered=1 --sync=0"
run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run8.log"

# splice hangs, runs forever, never completes.
#echo "= 9 = 9 = 9 = 9 = 9 = 9 = 9 = 9 = 9 = 9 = 9 = 9 ="
#specs="--ioengine=splice --mem=malloc --buffered=1 --sync=0"
#run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run9.log"

#echo "= 10 = 10 = 10 = 10 = 10 = 10 = 10 = 10 = 10 = 10 = 10 = 10 ="
#specs="--ioengine=splice --mem=mmap   --buffered=0 --sync=1"
#run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run10.log"

# sg wants a block device target:
#echo "= 11 = 11 = 11 = 11 = 11 = 11 = 11 = 11 = 11 = 11 = 11 = 11 ="
#specs="--ioengine=sg --mem=malloc --buffered=1 --sync=0"
#run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run11.log"

#echo "= 12 = 12 = 12 = 12 = 12 = 12 = 12 = 12 = 12 = 12 = 12 = 12 ="
#specs="--ioengine=sg --mem=mmap   --buffered=0 --sync=1"
#run_test "fio" "$runit $FIXEDOPTS $specs" "$log_dir" "fio-run12.log"

# maybe print some selective log output:
# $all_pkgs_dir/posixtestsuite/posixtest-summary.sh

exit 0
