#!/bin/bash
# An annoying number of bugs are reported as a result of copying a large
# number of files or untarring a large object. The exact problem varies
# but the symtoms of random stalls are common. Simple benchmark of this
# problem
P=largecopy-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/largecopy.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 -q $SRCTAR || die Failed to download source tar
if [ "$SRCTAR_EXTRA" != "" ]; then
	echo Downloading extra source tar: $SRCTAR_EXTRA
	wget -q $SRCTAR_EXTRA || die Failed to download source tar
fi
monitor_post_hook $LOGDIR_RESULTS download
CURRENTTIME=`date +%s`
echo "1:Time to download tar:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largecopy.result

monitor_pre_hook $LOGDIR_RESULTS unpack
STARTTIME=`date +%s`
echo Unpacking tarfile
mkdir 0
tar -C 0/ -xf `basename $SRCTAR` || die Failed to unpack tar
if [ "$SRCTAR_EXTRA" != "" ]; then
	echo Unpacking extra tar: $SRCTAR_EXTRA
	tar -C 0/ -xf `basename $SRCTAR_EXTRA` || die Failed to unpack extra tar
fi
monitor_post_hook $LOGDIR_RESULTS unpack
CURRENTTIME=`date +%s`
echo "2:Time to unpack tar:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largecopy.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
echo Making $NR_COPIES copies, source size ${SRCSIZE}M
while [ $IN_PROGRESS -lt $NR_COPIES ]; do
	NR_ACTIVE=0
	for PID in `cat copy.pids`; do
		if [ "`ps h --pid $PID`" != "" ]; then
			NR_ACTIVE=$((NR_ACTIVE+1))
		fi
	done

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

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

DIRECTORIES=`seq 0 $((NR_COPIES-1))`
if [ "$LARGECOPY_DEEP_DIRECTORIES" = "yes" ]; then
	echo Stacking directories for maximum depth
	MAX=1
	ROOT=0
	DIRECTORIES=0
	for COPY in `seq 1 $((NR_COPIES-1))`; do
		for LINE in `find $ROOT -mindepth $MAX -type d`; do
			DEPTH=`perl -e "\\$string = \"$LINE\"; print (\\$string =~ tr/\///)"`
			if [ $DEPTH -gt $MAX ]; then
				DEEPEST=$LINE
				MAX=$DEPTH
			fi
		done
		mv $COPY $DEEPEST 2> /dev/null
		if [ $MAX -gt 25 -o $? -ne 0 ]; then
			ROOT=$COPY
			MAX=1
			DIRECTORIES="$DIRECTORIES $ROOT"
			echo Starting new root $ROOT
		fi
	done
fi

monitor_pre_hook $LOGDIR_RESULTS createtar
STARTTIME=`date +%s`
echo Creating tar
tar -czf gianttar.tar.gz $DIRECTORIES
ls -lh gianttar.tar.gz
monitor_post_hook $LOGDIR_RESULTS createtar
CURRENTTIME=`date +%s`
echo "4:Time to create tarfile:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largecopy.result

monitor_pre_hook $LOGDIR_RESULTS deletesource
STARTTIME=`date +%s`
echo Deleting source directories
rm -rf $DIRECTORIES
monitor_post_hook $LOGDIR_RESULTS deletesource
CURRENTTIME=`date +%s`
echo "5:Time to delete source directories:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largecopy.result

monitor_pre_hook $LOGDIR_RESULTS expandtar
STARTTIME=`date +%s`
echo Expanding tar
tar -xf gianttar.tar.gz
monitor_post_hook $LOGDIR_RESULTS expandtar
CURRENTTIME=`date +%s`
echo "6:Time to expand tar:$CURRENTTIME:$((CURRENTTIME-STARTTIME))" >> $LOGDIR_RESULTS/largecopy.result

echo Deleting work
rm -rf $DIRECTORIES gianttar.tar.gz `basename $SRCTAR`
#### Description Large copy and untar
#### Details largecopy-bench 8
