#!/bin/bash
##
## specjbb script version 0.07
## Change history:
##     0.02 - changed $SYSTEM.MAP to $SYSTEM_MAP
##          - changed to use do_profile correctly
##     0.03 - remove symbolic link results if it exists at the beginning
##            of each invocation to better recover from a previously
##            aborted run.
##     0.04 - point all JVM related output to 
##            $LOGDIR/benchmark/java.out.$RUN_NUMBER
##          - use -h test for symbolic link existence test
##     0.05 - the previous of method of creating SPECjbb_config.props
##            and SPECjbb.props files with the current parameters seems
##            to intermittantly fail, so changing method of generating
##            these files to something hopefully more reliable.
##     0.06 - Copying SPECjbb_config.props and SPECjbb.props files
##            to benchmark folder to aid in diagnosis of bad runs.
##     0.07 - Ported to the DATS version of autobench
##
## This script runs the SPECjbb2000 benchmark within the autobench framework
##
# reqparam value The heap size to use for the JVM (in MB)
# reqparam value The number of cpus
# reqparam value The number of the starting warehouse
# reqparam value How many warehouses to increment by (must be 1 for run to be reportable)
# reqparam value The number of the ending warehouse
# reqparam value How long to ramp up (in seconds) (must be 30 for a run to be reportable)
# reqparam value How long to measure (in seconds) (must be 120 for a run to be reportable)
#

. /etc/autobench.conf || . functions

# Process the arguments ######################################################
if [ "$#" -lt "7" ]
then
	echo Syntax Error: Insufficient number of parameters
	echo Usage: $0 JVMHeapSizeMB MaxCPUs StartWH WHInc EndWH RampUp MTime  
	exit 1
fi

# Install support files ######################################################
if [ ! \( -d $AUTODIR/sources/specjbb2000 -a -f $AUTODIR/sources/specjbb2000/jbb.jar \) ]; then
  echo installing SpecJBB support files...
  $AUTODIR/scripts/benchmarks/install/specjbb || exit 2
  echo done installing SpecJBB support files
fi

if [ "`uname -m`" == "ppc64" ]; then
	getcommand ppc_64-bit_userspace
	ppc_64-bit_userspace;
fi;

getcommand doprofilers
doprofilers install

# Prepare the benchmark ######################################################

# ugly ifdef-type thing
if [ "`uname -m`" != "ppc64" ]; then
	export LD_ASSUME_KERNEL=2.4
fi

# Figure out what version of java to run, and any arguments...
JAVA=$(expand_command "\$javabin")
if [ -z "$JAVA" -o "$JAVA" = '$javabin' ]; then
    JAVA=$AUTODIR/sources/IBMJava2/jre/bin/java
fi
JAVAARGS=$(expand_command "\$javaargs")
if [ "$JAVAARGS" = '$javaargs' ]; then
  JAVAARGS=""
fi
JAVAARGS="$JAVAARGS -verbose:gc"
echo "JAVA=$JAVA" >> $LOGDIR/benchmark/java.out.$RUN_NUMBER
echo "JAVAARGS=$JAVAARGS" >> $LOGDIR/benchmark/java.out.$RUN_NUMBER
# The above means that the user can define $javabin and $javaargs in the .dat
# file, and they'll be used here

CLASSPATH=./jbb.jar:./jbb_no_precompile.jar:./check.jar:./reporter.jar:$CLASSPATH
echo "-cp=$CLASSPATH" >> $LOGDIR/benchmark/java.out.$RUN_NUMBER
export CLASSPATH
$JAVA -fullversion 2>&1 | tee -a $LOGDIR/benchmark/java.out.$RUN_NUMBER

echo Current RUN_NUMBER is $RUN_NUMBER

pushd $AUTODIR/sources/SPECjbb2000 > /dev/null

# Fill the arguments into the properties files.
cat SPECjbb_config.props.master | sed \
    -e "s/\(config.sw.JVMheapInitial\)=.*$/\1=$1/" \
    -e "s/\(config.sw.JVMheapMax\)=.*$/\1=$1/" \
    -e "s/-ms[0-9]*m/-ms$1m/" \
    -e "s/-mx[0-9]*m/-mx$1m/" \
    -e "s/\(config.hw.ncpu\)=.*$/\1=$2/" \
  > SPECjbb_config.props
cp SPECjbb_config.props $LOGDIR/benchmark/SPECjbb_config.props.$RUN_NUMBER

cat SPECjbb.props.master | sed \
    -e "s/\(input.starting_number_warehouses\)=.*$/\1=$3/" \
    -e "s/\(input.increment_number_warehouses\)=.*$/\1=$4/" \
    -e "s/\(input.ending_number_warehouses\)=.*$/\1=$5/" \
    -e "s/\(input.ramp_up_seconds\)=.*$/\1=$6/" \
    -e "s/\(input.measurement_seconds\)=.*$/\1=$7/" \
  > SPECjbb.props
cp SPECjbb.props $LOGDIR/benchmark/SPECjbb.props.$RUN_NUMBER

# Create a symlink between results to $LOGDIR
if [ -h results ]; then
    rm results
fi
ln -s $LOGDIR/benchmark results

# Setup the profiling actions 
watchfor "Timing Measurement ended" 'stopprofilers $count ; doprofilers report $count ; echo $count > $TMPDIR/max-profiles'
watchfor "Timing Measurement began" startprofilers '$count'

watchfor "WARNING!!! WARNING!!!  In order for the profiler to be started and stopped at the correct points, the output of this benchmark must be piped through the stream filter.  This is done automatically if the benchmark is called from within the run script. WARNING!!! WARNING!!!" echo

# Run the benchmark ##########################################################
echo -n "running test: "
echo "$JAVA $JAVAARGS -ms$1m -mx$1m spec.jbb.JBBmain -propfile SPECjbb.props"

$JAVA $JAVAARGS -ms$1m -mx$1m spec.jbb.JBBmain -propfile SPECjbb.props 2>&1   | tee -a $LOGDIR/benchmark/java.out.$RUN_NUMBER

# Cleanup ####################################################################
NUM_PROFILES=`cat $TMPDIR/max-profiles 2> /dev/null`
if [ -z "$NUM_PROFILES" ]; then
  NUM_PROFILES=0
fi

for ((loop_variable=1; $loop_variable<=$NUM_PROFILES; loop_variable++)); do
  doprofilers postprocess $loop_variable
done
rm $TMPDIR/max-profiles 2> /dev/null

# Remove symlink of results to $LOGDIR
rm SPECjbb.props SPECjbb_config.props 2> /dev/null
rm results 2> /dev/null
popd > /dev/null
