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

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

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

COMMON="-E -C 0 -D 30000 -B 10000 -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
exec            exec			-B 10
exit            exit_10			-B 10
exit            exit_100		-B 100
exit            exit_1000		-B 1000 -C 50
fork            fork_10			-B 10
fork            fork_100		-B 100  -C 100
fork            fork_1000		-B 1000 -C 50
pthread_create  pthread_8		-B 8
pthread_create  pthread_32		-B 32
pthread_create  pthread_128		-B 128
pthread_create  pthread_512		-B 512
sigaction       sigaction		-I 100
signal          signal			-I 1000
sigprocmask     sigprocmask		-I 200
system          system			-I 1000000
EOT

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