#!/bin/bash
# - media
# - fstype
# - directio
# - test
# - duration

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/reproduce-log.sh

cd $BENCHMARK_ROOT/fxmark || die "can not enter $BENCHMARK_ROOT/fxmark"

: ${duration:=50}

loopdev=$(losetup -f)

# create a small virtual block device to speed up the test
setup_test_block()
{
	dev=$partition

	echo "$fstype" | grep -q -e "^ext" || local mkfs_opts="-f"
	umount $dev 2>/dev/null

	# "$ mkfs -t f2fs -q -f /dev/sda2" will output "WDC WD30EZRX-00S0A80"
	# and it will be shown as " WDC WD30EZRX-00S0A80^@^@" in rt/fxmark,
	# the "^@^@" lead to no fxmark.json under rt, thus redirect mkfs's output
	mkfs -t ${fstype%%_*} -q $mkfs_opts $dev >/dev/null || return

	mkdir -p /fs/$dev
	mount -t ${fstype%%_*} $dev /fs/$dev || return

	dd if=/dev/zero of=/fs/$dev/test_block bs=100M count=1 || return
	losetup $loopdev /fs/$dev/test_block || return

	partition=$loopdev
}

umount_test_block()
{
	[ "$dev" ] && {
		losetup -d $loopdev
		umount $dev
	}
}

# fxmark can not be run until we change the source to fit the target machine
# TODO: support more media type: nvme,mem
# after setup_config, the changes are like:
# $ git diff
# diff --git a/bin/run-fxmark.py b/bin/run-fxmark.py
# index 2df42a5..ec3b5df 100755
# --- a/bin/run-fxmark.py
# +++ b/bin/run-fxmark.py
# @@ -528,6 +528,9 @@ if __name__ == "__main__":
#          #  ("*", "*", "*", str(cpupol.PHYSICAL_CHIPS * cpupol.CORE_PER_CHIP), "*"))
#      ]
#
# +    Runner.HDDDEV="/dev/sdc"
# +    Runner.SSDDEV="/dev/sdc"
# +    run_config = [(Runner.CORE_FINE_GRAIN, PerfMon.LEVEL_LOW, ("hdd", "ext4", "DWAL", "*", "bufferedio")),]
#      confirm_media_path()
#      for c in run_config:
#          runner = Runner(c[0], c[1], c[2])
setup_config()
{
	partition="${partitions%% *}"
	[ "$partition" ] || return

	# some tests create testfiles on host's device, e.g when we run test
	# DWTL on ivb44, DWTL creates 24 testfiles on /dev/sdb1, each testfile
	# is 16G. DWTL need a lot of time to finish, it always timeout.
	# testfile's size is determined by the host device, the default test
	# device is hdd/ssd in lkp, testfiles's size will be too large.
	# setup_test_block creates a small device to speed up tests.
	[ "$test" = "MWUM" -o "$test" = "MWUL" -o "$test" = "DWTL" ] && {
		setup_test_block || return
	}

	# install the filebench command
	[[ "$test" =~ "filebench" ]] && {
		ln -s $BENCHMARK_ROOT/fxmark/filebench/bin/filebench /usr/bin/filebench
	}

	sed -i "s/self.DURATION      = 30/self.DURATION      = $duration/" bin/run-fxmark.py

	sed -i "/    confirm_media_path()/i\\
    Runner.HDDDEV = \"$partition\"\n\
    Runner.SSDDEV = \"$partition\"\n\
    run_config = [(Runner.CORE_COARSE_GRAIN, PerfMon.LEVEL_LOW, (\"$media\", \"$fstype\", \"$test\", \"*\", \"$directio\")),]"  bin/run-fxmark.py

	sed -i "/    (opts, args) = parser.parse_args()/i\\
    parser.add_option(\"--directio\", help=\"directio\")" bin/run-*bench.py
}

do_backup()
{
	cp ./bin/run-fxmark.py ./bin/run-fxmark.py.bak
	cp ./bin/run-dbench.py ./bin/run-dbench.py.bak
	cp ./bin/run-filebench.py ./bin/run-filebench.py.bak
}

do_recover()
{
	cp ./bin/run-fxmark.py.bak ./bin/run-fxmark.py
	cp ./bin/run-dbench.py.bak ./bin/run-dbench.py
	cp ./bin/run-filebench.py.bak ./bin/run-filebench.py
}

run()
{
	echo -e "Y\nY" | ./bin/run-fxmark.py
}

sleep 5

do_backup

setup_config || die "failed to setup configuration"

# run-fxmark.py has been changed much, save a copy for check
cp ./bin/run-fxmark.py	$TMP_RESULT_ROOT/

run

umount_test_block
do_recover
