#!/bin/bash
# An annoying number of bugs are reported as a result of dd'ing a large
# file. Uses the same parameters as largedd for convenience
P=largedd-bench
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
SRCTAR=
SRCTAR_EXTRA=
TARGETSIZE_MB=
NUM_CPU=$(grep -c '^processor' /proc/cpuinfo)
if [ "$LARGECOPY_THREADS" = "" ]; then
	LARGECOPY_THREADS=$NUM_CPU
fi

# Basic argument parser
while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--srctar)
		SRCTAR=$2
		shift 2
		;;
	--srctar-extra)
		SRCTAR_EXTRA=$2
		shift 2
		;;
	--targetsize)
		TARGETSIZE_MB=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

STARTTIME=`date +%s`
echo "0:Start time:$STARTTIME:0" > $LOGDIR_RESULTS/largedd.result

monitor_pre_hook $LOGDIR_RESULTS download
STARTTIME=`date +%s`
echo Downloading source tar: $SRCTAR
cd $TESTDISK_DIR || die Failed to change to temp directory
wget -O 0 -q $SRCTAR
if [ $? -ne 0 ]; then
	echo Failed to download source tar, creating dummy

	# This will create a file that will need 32 copies
	dd if=/dev/zero of=0 ibs=4096 count=$((TARGETSIZE_MB*8)) ||
		die Failed to create dummy source file
fi

# Download extra source tar if available
if [ "$SRCTAR_EXTRA" != "" ]; then
	echo Downloading extra source tar: $SRCTAR_EXTRA
	wget -O 1 -q $SRCTAR_EXTRA || die Failed to download source tar
	cat 0 1 > 0.tmp
	mv 0.tmp 0
fi
monitor_post_hook $LOGDIR_RESULTS download
CURRENTTIME=`date +%s`
echo "1:Time to download tar:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largedd.result

monitor_pre_hook $LOGDIR_RESULTS copy
STARTTIME=`date +%s`
SRCSIZE=`du -BM 0 | tail -1 | awk '{print $1}' | sed -e 's/M//'`
NR_COPIES=$((TARGETSIZE_MB/SRCSIZE))
IN_PROGRESS=1
DIRECTORIES=`seq 0 $((NR_COPIES-1))`
echo Making $NR_COPIES copies via dd, source size ${SRCSIZE}M
while [ $IN_PROGRESS -lt $NR_COPIES ]; do
	NR_ACTIVE=0
	for PID in `cat dd.pids 2> /dev/null`; do
		if [ "`ps h --pid $PID`" != "" ]; then
			NR_ACTIVE=$((NR_ACTIVE+1))
		fi
	done

	if [ $NR_ACTIVE -lt $LARGECOPY_THREADS ]; then
		dd if=0 of=$IN_PROGRESS &
		PID=$!
		echo $PID >> dd.pids
		IN_PROGRESS=$((IN_PROGRESS+1))
		echo o Started copy pid $PID
	fi
	sleep 1
done

echo Waiting on completion
for PID in `cat dd.pids`; do
	echo -n "o $PID"
	while [ "`ps h --pid $PID`" != "" ]; do
		sleep 2
	done
	echo
done
monitor_post_hook $LOGDIR_RESULTS dd
CURRENTTIME=`date +%s`
echo "3:Time to dd source files:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largedd.result

monitor_pre_hook $LOGDIR_RESULTS deletesource
STARTTIME=`date +%s`
echo Deleting source files
rm -rf $DIRECTORIES
monitor_post_hook $LOGDIR_RESULTS deletesource
CURRENTTIME=`date +%s`
echo "5:Time to delete source:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largedd.result
#### Description Large dd
#### Details largedd-bench 12
