#!/bin/bash
P=syscall-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=
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/syscall
fi
if [ ! -d $SHELLPACK_SOURCES/syscall-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-syscall -v ${VERSION}  || die syscall install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/syscall-${VERSION}-installed || die Failed to cd to syscall install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo syscall installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

echo Executing syscall microbenchmark
monitor_pre_hook $LOGDIR_RESULTS
$TASKSET_SERVER ./syscall | tee $LOGDIR_RESULTS/syscall.log
monitor_post_hook $LOGDIR_RESULTS

exit $SHELLPACK_SUCCESS
#### Description syscall
#### Details syscall-bench 26
