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

TASKSET_SERVER=
TASKSET_CLIENT=
SERVER_HOST=127.0.0.1
if [ "$REMOTE_SERVER_HOST" != "" ]; then
	SERVER_HOST=$REMOTE_SERVER_HOST
fi

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
TASKSET_ALL=
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
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--bind-pinned)
		CPUA=`numactl --hardware | grep ^node | grep cpus: | head -1 | awk '{print $4}'`
		TASKSET_SERVER="taskset -c $CPUA"
		TASKSET_CLIENT="taskset -c $CPUA"
		TASKSET_ALL="taskset -c $CPUA"
		shift
		;;
	--bind-cross-node)
		CPUA=`numactl --hardware | grep ^node | grep cpus: | head -1 | awk '{print $4}'`
		CPUB=`numactl --hardware | grep ^node | grep cpus: | tail -1 | awk '{print $NF}'`
		TASKSET_SERVER="taskset -c $CPUA"
		TASKSET_CLIENT="taskset -c $CPUB"
		TASKSET_ALL="taskset -c $CPUA,$CPUB"
		shift
		;;
	--bind-cross-socket)
		CPUA=`numactl --hardware | grep ^node | grep cpus: | head -1 | awk '{print $4}'`
		CPUB=`list-cpu-siblings.pl $CPUA cores 0 | awk -F , '{print $1}'`
		TASKSET_SERVER="taskset -c $CPUA"
		TASKSET_CLIENT="taskset -c $CPUB"
		TASKSET_ALL="taskset -c $CPUA,$CPUB"
		shift
		;;
	--bind-cross-ht)
		CPUA=`numactl --hardware | grep ^node | grep cpus: | head -1 | awk '{print $4}'`
		CPUB=`list-cpu-siblings.pl $CPUA threads 0 | awk -F , '{print $1}'`
		if [ "$CPUB" = "" ]; then
			echo ERROR: Could not identify HT thread for CPU $CPUA
			exit $SHELLPACK_ERROR
		fi
		TASKSET_SERVER="taskset -c $CPUA"
		TASKSET_CLIENT="taskset -c $CPUB"
		TASKSET_ALL="taskset -c $CPUA,$CPUB"
		shift
		;;
	*)
		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/netpipe-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/netpipe-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-netpipe -v ${VERSION}  || die netpipe install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/netpipe-${VERSION}-installed || die Failed to cd to netpipe install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo netpipe installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

export REMOTE_SERVER_SCRIPT=$SCRIPTDIR/shellpacks/shellpack-bench-netpipe
case $SERVERSIDE_COMMAND in
start)
	echo Killing old servers
	killall NPtcp

	echo Starting server
	mmtests_activity netpipe-$SERVERSIDE_NAME
	monitor_pre_hook $LOGDIR_RESULTS netpipe-$SERVERSIDE_NAME
	NETROOT=$SHELLPACK_SOURCES/netpipe-${VERSION}-installed
	nohup $TASKSET_SERVER $NETROOT/NPtcp 0<&- &>/dev/null &
	sleep 5
	exit $SHELLPACK_SUCCESS
	;;
stop)
	monitor_post_hook $LOGDIR_RESULTS netpipe-$SERVERSIDE_NAME
	echo Killing old server
	killall NPtcp
	exit $SHELLPACK_SUCCESS
	;;
flush)
	exit $SHELLPACK_SUCCESS
	;;
none)
	echo Killing old servers
	killall NPtcp

	echo Starting server
	NETROOT=$SHELLPACK_SOURCES/netpipe-${VERSION}-installed 
	$TASKSET_SERVER $NETROOT/NPtcp &
	sleep 2
	;;
*)
	die Unrecognised server-side command
	;;
esac

echo $PROTOCOL > $LOGDIR_RESULTS/protocols
EXTRA=
if [ "$PROTOCOL" = "UDP_STREAM" ]; then
	EXTRA="-P 15895"
fi

mmtests_server_ctl start --serverside-name netpipe-server
mmtests_activity netpipe
monitor_pre_hook $LOGDIR_RESULTS netpipe
	eval $TASKSET_CLIENT $NETROOT/NPtcp -h $SERVER_HOST	\
		-o $LOGDIR_RESULTS/netpipe.log			\
			|| die Failed to run netpipe
monitor_post_hook $LOGDIR_RESULTS netpipe
mmtests_server_ctl stop --serverside-name netpipe-server
killall NPtcp

exit $SHELLPACK_SUCCESS
#### Description netpipe
#### Details netpipe-bench 10
