#!/bin/sh
# - runtime
# - nr_threads
# - bs

## Copy a file, converting and formatting according to the operands.

[ -n "$mount_points" ] || { echo "empty mount_points, disk failed?" >&2; exit; }

. $LKP_SRC/lib/reproduce-log.sh

get_time_ms()
{
	echo $(($(date +%s%N)/1000000))
}

: ${runtime:=600}
[ -n "$bs" ] && opt_bs="bs=$bs"

t=$(get_time_ms)

files=
pid=
for i in $(seq ${nr_threads:-1})
do
	for mnt in $mount_points
	do
		file=$mnt/zero-$i
		files="$files $file "
		rm -f $file
		log_eval "timeout -s KILL $runtime  dd $opt_bs if=/dev/zero of=$file status=noxfer &"
		[ -n "$BASH_VERSION" ] && disown
		pid="${pid} $!"
	done
done

ps aux | grep dd
echo $pid > $TMP/pid-dd

tt=$(get_time_ms)
startup_time=$((tt - t))
echo "startup_time_ms: $startup_time"
startup_time_s=$((startup_time / 1000))

log_cmd sleep $((runtime > startup_time_s ? runtime - startup_time_s : 0))

t=$(get_time_ms)
pkill -9 --exact dd --pidfile $TMP/pid-dd
log_echo killall -9 dd
pkill -9 --exact dd --pidfile $TMP/pid-dd
pkill -9 --exact dd --pidfile $TMP/pid-dd
tt=$(get_time_ms)
echo "kill_time_ms: $((tt - t))"

chown lkp.lkp $TMP/pid-dd
mv $TMP/pid-dd  $RESULT_ROOT/pid
ls -li $files > $RESULT_ROOT/ls-files
truncate --size=0 $files
rm -f $files

t=$(get_time_ms)
echo "rm_time_ms: $((t - tt))"
