#!/bin/bash
P=libmicro-smallbatches-bench
DEFAULT_VERSION=1
. $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
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		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/libmicro-smallbatches-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/libmicro-smallbatches-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-libmicro-smallbatches -v ${VERSION}  || die libmicro-smallbatches install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/libmicro-smallbatches-${VERSION}-installed || die Failed to cd to libmicro-smallbatches install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo libmicro-smallbatches installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

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

cd $SHELLPACK_SOURCES/libmicro-smallbatches-${VERSION}-installed || die Failed to cd to libmicro directory

COMMON="-E -C 0 -D 30000 -B 100 -L -S -W"

while read A B C; do
	monitor_pre_hook $LOGDIR_RESULTS $B
	if [ -f bin/$A ]; then
		eval bin/$A $COMMON -N "$B" $C 2>&1 > $LOGDIR_RESULTS/$B.log
	fi
	monitor_post_hook $LOGDIR_RESULTS $B
done <<EOT
write		write_t100k	-s 100k			-f $TFILE
writev		writev_t10k	-s 10k		-f $TFILE
writev		writev_t100k	-s 100k			-f $TFILE
memset	memset_1m	-s 1m
memset	memset_10m	-s 10m
memset	memsetP2_10m	-s 10m -P 2
malloc	mallocT2_100k	-s 100k -g 10 -T 2
memcpy	memcpy_1m	-s 1m
memcpy	memcpy_10m	-s 10m
mmap	mmap_wz128k	-l 128k -w	-f /dev/zero
mmap	mmap_wa128k	-l 128k -w	-f MAP_ANON
munmap	unmap_wz128k	-l 128k	-w	-f /dev/zero
munmap	unmap_wa128k	-l 128k	-w	-f MAP_ANON
mprotect	mprot_tw4m	-l 4m -w -t -f /dev/zero
cascade_mutex	c_mutex_10	-T 10
cascade_mutex	c_mutex_200	-T 200
cascade_lockf	c_lockf_10 -P 10
cascade_lockf	c_lockf_200 -P 200
cascade_fcntl	c_fcntl_10 -P 10
cascade_fcntl	c_fcntl_200 -P 200
cascade_cond	c_cond_10	-T 10
cascade_cond	c_cond_200	-T 200
connection	connection
connection	conn_accept -a
connection	conn_connect -c
close_tcp	close_tcp
poll	poll_1000	-n 1000
poll	poll_w1000	-n 1000	-w 100
select	select_1000	-n 1000
select	select_w1000	-n 1000 -w 100
exit	exit
EOT

exit $SHELLPACK_SUCCESS
#### Description libmicro-smallbatches
#### Details libmicro-smallbatches-bench 5
