#!/bin/bash
# tiobench installer
###SHELLPACK preamble tiobench-install 0.3.3
WEB_LOCATION=http://garr.dl.sourceforge.net/project/tiobench/tiobench/
MIRROR_LOCATION="$WEBROOT/tiobench/"

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd

###SHELLPACK sources_fetch $VERSION/tiobench-${VERSION}.tar.gz tiobench-${VERSION}-installed

# Build
###SHELLPACK build_start tiobench-${VERSION}-installed

# 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

###SHELLPACK make

echo tiobench installed successfully
