#!/bin/bash
# tiobench installer
P=tiobench-install
DEFAULT_VERSION=0.3.3
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
WEB_LOCATION=https://10gbps-io.dl.sourceforge.net/project/tiobench/tiobench
MIRROR_LOCATION="$WEBROOT/tiobench/"

# 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
		;;
	*)
		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

# Unconditionally fetch the tar to find out the real version number
TARFILE=tiobench-${VERSION}.tar.gz
sources_fetch $WEB_LOCATION/$VERSION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $WEB_LOCATION_ALT/$VERSION/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf tiobench-${VERSION}.tar.gz failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

# Rename directory to something we expect.
DST_DIR=`tar tf $TARFILE | head -n 1 | awk -F / '{print $1}'`
mv $DST_DIR tiobench-${VERSION}-installed
pushd tiobench-${VERSION}-installed > /dev/null || die Failed to rename tar

# Build
pushd $SHELLPACK_SOURCES/tiobench-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done

# Patch the stupid thing for low cpu efficiency
echo "--- tiobench.pl-orig	2012-02-17 08:51:54.915428315 +0000
+++ tiobench.pl	2012-02-17 09:08:27.004613193 +0000
@@ -167,9 +167,13 @@
                   100 * ( \$stat_data{\$identifier}{\$thread}{\$size}{\$block}{\$field}{'utime'} +
                   \$stat_data{\$identifier}{\$thread}{\$size}{\$block}{\$field}{'stime'} ) / 
                   \$stat_data{\$identifier}{\$thread}{\$size}{\$block}{\$field}{'time'};
+               my \$cpur = \$stat_data{\$identifier}{\$thread}{\$size}{\$block}{\$field}{'cpu'}/100;
+               if (\$cpur == 0) {
+                  \$cpur = 0.01;
+               }
                \$stat_data{\$identifier}{\$thread}{\$size}{\$block}{\$field}{'cpueff'} =
                   (\$stat_data{\$identifier}{\$thread}{\$size}{\$block}{\$field}{'rate'} /
-                  (\$stat_data{\$identifier}{\$thread}{\$size}{\$block}{\$field}{'cpu'}/100));
+                  (\$cpur));
             }
          }
       }" | patch -p0

# Patch it for aligned_alloc if available
grep -q aligned_alloc /usr/include/stdlib.h 2> /dev/null
if [ $? -eq 0 ]; then
	echo "--- tiotest.c.orig	2013-06-02 10:58:23.721553954 +0100
+++ tiotest.c	2013-06-02 10:58:38.015644836 +0100
@@ -22,6 +22,7 @@
 
 #include \"tiotest.h\"
 #include \"crc32.h\"
+#include <unistd.h>
 
 static const char* versionStr = \"tiotest v0.3.3 (C) 1999-2000 Mika Kuoppala <miku@iki.fi>\";
 
@@ -31,16 +32,6 @@
 */
 ArgumentOptions args;
 
-static void * aligned_alloc(ssize_t size)
-{
-	caddr_t a;
-	a = mmap((caddr_t )0, size, 
-	         PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
-	if (a == MAP_FAILED)
-		return NULL;
-	return a;
-}
-
 static int aligned_free(caddr_t a, ssize_t size)
 {
 	return munmap(a, size);
@@ -281,7 +272,7 @@
 		pthread_attr_setscope(&(d->threads[i].thread_attr),
 				      PTHREAD_SCOPE_SYSTEM);
 
-		d->threads[i].buffer = aligned_alloc( d->threads[i].blockSize );
+		d->threads[i].buffer = aligned_alloc( getpagesize(), d->threads[i].blockSize );
 		if( d->threads[i].buffer == NULL )
 		{
 			perror(\"Error allocating memory\");" | patch -p0
fi

make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

echo tiobench installed successfully
#### Description tiobench
#### Details tiobench 24
