#!/bin/sh
# - iterations

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

cd $BENCHMARK_ROOT/stutter || die "no $BENCHMARK_ROOT/stutter"

set_memtotal_bytes()
{
	local default_memory_bytes=$(to_byte "8G")

	# get available memory(gb)
	# root@lkp-ivb-d02 ~# free -g
	#              total        used        free      shared  buff/cache   available
	#Mem:          7.7Gi       305Mi       6.4Gi       8.0Mi       991Mi       6.3Gi
	#Swap:            0B          0B          0B

	local available_memory_gb=$(free -g | sed -n "2, 1p" | awk '{print $7}')
	local memory_bytes=$(to_byte ${available_memory_gb}G)
	# limit the memory to speed up the test
	if [ $memory_bytes -gt $default_memory_bytes ]; then
		memory_bytes=$default_memory_bytes
	fi
	log_eval export MEMTOTAL_BYTES=$memory_bytes
}

set_memtotal_bytes

log_eval export ITERATIONS=$iterations
log_eval export LOGDIR_RESULTS=$RESULT_ROOT/log_result

log_cmd mkdir -p $LOGDIR_RESULTS

[ -z "$mount_points" ] && exit
log_eval export TESTDISK_DIR="${mount_points%% *}"

log_cmd ./run.sh

# This case reuqires disk_size > (system memory * 2 * 2), but it seems lkp has
# not an existed filter to handle it, so here is to check whether there is a
# 'No space left on device' error
ret=0
grep 'No space left on device' $LOGDIR_RESULTS/*.log && ret=1

if [ $ret -ne 0 ]; then
	echo "stutter: fail"
else
	echo "stutter: pass"
fi
