#!/bin/bash
P=swappiness-bench
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--size)
		WORKLOAD_SIZE=$2
		shift 2
		;;
	--anon-size)
		PERCENTAGE_ANON=$2
		shift 2
		;;
	--min-swappiness)
		MIN_SWAPPINESS=$2
		shift 2
		;;
	--max-swappiness)
		MAX_SWAPPINESS=$2
		shift 2
		;;
	--steps)
		STEPS=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/usemembuild-${VERSION}-installed
fi
if [ ! -d $SHELLPACK_SOURCES/usemembuild-${VERSION}-installed-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-usemembuild -v ${VERSION}  || die usemembuild install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/usemembuild-${VERSION}-installed || die Failed to cd to usemembuild install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo usemembuild installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

function create_sparse_file() {
	dd if=/dev/zero of=$SHELLPACK_TEMP/$1 bs=32768 count=0 seek=$(($2/32768+1))
}

function create_populated_file() {
	dd if=/dev/zero of=$SHELLPACK_TEMP/$1 bs=32768 count=$(($2/32768+1))
}

ORIGINAL_SWAPPINESS=`cat /proc/sys/vm/swappiness`
MEMTOTAL_ANON=$((WORKLOAD_SIZE*PERCENTAGE_ANON/100))
MEMTOTAL_FILE=$((WORKLOAD_SIZE*(100-PERCENTAGE_ANON)/100))

echo Anon footprint $((MEMTOTAL_ANON/1048576))MB | tee $LOGDIR_RESULTS/anon-footprint
echo File footprint $((MEMTOTAL_FILE/1048576))MB | tee $LOGDIR_RESULTS/file-footprint
echo $ORIGINAL_SWAPPINESS > /proc/sys/vm/swappiness || die Failed to write swappiness
echo Creating file
create_populated_file workfile-1 $MEMTOTAL_FILE

USEMEM_LOOPS=150

THREADS="$MIN_SWAPPINESS"
THREAD_DIFF=$(($MAX_SWAPPINESS-$MIN_SWAPPINESS))
if [ "$THREAD_DIFF" -lt $STEPS ]; then
	THREADS=`seq $MIN_SWAPPINESS $MAX_SWAPPINESS`
else
	for ITER in `seq 1 $(($STEPS-1))`; do
		THREADS="$THREADS $((THREAD_DIFF/$STEPS*$ITER))"
	done
	THREADS="$THREADS $MAX_SWAPPINESS"
fi
for NR_THREADS in $THREADS; do
	mmtests_activity process $NR_THREADS/$END_THREAD
	SWAPPINESS=$NR_THREADS

	mmtests_activity swappiness-$SWAPPINESS
	echo Executing test with swappiness $SWAPPINESS
	echo $SWAPPINESS > /proc/sys/vm/swappiness || die Failed to write swappiness

	unbuffer vmstat -n 1 > $LOGDIR_RESULTS/vmstat-$SWAPPINESS &
	PID_VMSTAT=$!

	echo "#!/bin/bash
	# File
	echo -n > $SHELLPACK_TEMP/usemem.pids
	$SHELLPACK_SOURCES/usemembuild-$VERSION-installed/usemem	\
		-f $SHELLPACK_TEMP/workfile-1				\
		-j 4096							\
		-r $USEMEM_LOOPS					\
		--readonly						\
		$MEMTOTAL_FILE 2> /dev/null &
	echo \$! >> $SHELLPACK_TEMP/usemem.pids

	# Anon
	$SHELLPACK_SOURCES/usemembuild-$VERSION-installed/usemem	\
		-j 4096							\
		-r $USEMEM_LOOPS					\
		$MEMTOTAL_ANON 2> /dev/null &
	echo \$! >> $SHELLPACK_TEMP/usemem.pids

	for USEMEM_PID in \`cat $SHELLPACK_TEMP/usemem.pids\`; do
		echo Waiting on pid \$USEMEM_PID
		wait \$USEMEM_PID
	done" > $SHELLPACK_TEMP/usemem.sh
	chmod a+x $SHELLPACK_TEMP/usemem.sh
	$TIME_CMD -o $LOGDIR_RESULTS/usemem-$SWAPPINESS $SHELLPACK_TEMP/usemem.sh
	cat $LOGDIR_RESULTS/usemem-$SWAPPINESS

	kill $PID_VMSTAT
done

echo Restoring swappiness value of $ORIGINAL_SWAPPINESS
echo $ORIGINAL_SWAPPINESS > /proc/sys/vm/swappiness

exit $SHELLPACK_SUCCESS

#### Description swappiness
#### Details swappiness-bench 11
