#!/bin/bash
# This is the script for running SPECcpu
P=speccpu-bench
DEFAULT_VERSION=1.2
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

SPECCPU_DATA_SIZE=ref
SPECCPU_ITERATIONS=3
SPECCPU_TESTS=all

install-depends dmidecode gcc gcc-c++ gcc-fortran

# 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
		;;
	--input-data-size)
		SPECCPU_DATA_SIZE=$2
		shift 2
		;;
	--iterations)
		SPECCPU_ITERATIONS=$2
		shift 2
		;;
	--tests)
		SPECCPU_TESTS=$2
		shift 2
		;;
	--pagesize)
		SPECCPU_PAGESIZE=$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

# Detect bitness
case `uname -m` in
	i?86)
		RUNBITS=32
		;;
	*)
		RUNBITS=64
		;;
esac

# Detect whether it is reportable
REPORTABLE=--reportable
if [ "$TESTS" != "all" -o "$SPECCPU_ITERATIONS" = "1" -o "$SPECCPU_DATA_SIZE" != "ref" ]; then
	REPORTABLE=--noreportable
fi

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

# Prepare system
cd $SHELLPACK_SOURCES/speccpu-${VERSION}-installed > /dev/null || die Failed to cd to speccpu
[ ! -e shrc ] && die No speccpu2006 shrc script
rm -f /tmp/OPiter.*
[ -e result ] && rm -rf result
export MALLOC_CHECK_=0
GENSPECSH="generate-speccpu.sh --bitness ${RUNBITS}"

# Generate SPEC configurations
. shrc
HLINK=--hugepages-newrelink

mkdir -p config
$GENSPECSH > config/gen-m${RUNBITS}base.cfg || die Failed to generate base spec config file
$GENSPECSH > config/gen-m${RUNBITS}default.cfg || die Failed to generate default spec config file
$GENSPECSH > config/gen-m${RUNBITS}transhuge || die Failed to generate transhuge spec config file
$GENSPECSH --hugepages-heaponly > config/gen-m${RUNBITS}huge-heap.cfg || die Failed to generate huge-heap spec config file
$GENSPECSH $HLINK > config/gen-m${RUNBITS}huge-all.cfg || die Failed to generate huge-all spec config file
cp config/gen-m${RUNBITS}base.cfg config/gen-m${RUNBITS}huge-*.cfg $LOGDIR_RESULTS/

$MMTESTS_HUGECTL runspec -c gen-m${RUNBITS}${SPECCPU_PAGESIZE}.cfg \
	$REPORTABLE \
	--tune base \
	--size $SPECCPU_DATA_SIZE \
	--iterations $SPECCPU_ITERATIONS \
	$SPECCPU_TESTS
RETVAL=$?
rm -rf /tmp/OPiter*
mv result/* $LOGDIR_RESULTS/
gzip $LOGDIR_RESULTS/*.log

exit $RETVAL
#### Description Non-conforming speccpu benchmark
#### Details speccpu-bench 22
