#!/bin/bash
# This is the script to run the dbench benchmark
#
# $1 = name
# [$2 = rcfile]

. /etc/autobench.conf || . functions

# Process the arguments ######################################################
if [ $# -lt 1 ]; then
  exit 1
fi

SFS_SCRIPTS_PATH=/root/sfs-tools/scripts

sleep 200

SFS_RC=$2
if [ -z "$SFS_RC" ]; then
	SFS_RC="sfs_rc-fullrun-68k"
fi

SFS_NAME=$1

if [ "$DOPROFILE" ]; then
	touch /root/doprofile
	startprofilers
#	/root/pmcount/pmc_count.py -f /root/pmcount/pmc_POWER5_group  -g1
	OP_PATH=$AUTODIR/sources/oprofile
#	$OP_PATH/opcontrol --vmlinux=/boot/vmlinux-`uname -r`
#	$OP_PATH/opcontrol --setup --vmlinux=/boot/vmlinux-`uname -r` -e PMC6:100000:0:1:1
	SFS_NAME=${SFS_NAME}-PROFILE
else
	rm /root/doprofile
fi

if [ ! "$SFSFS" ]; then
	SFSFS=jfs
fi

getcommand doprofilers
doprofilers install

###############################################################
# Benchmark setup:                                            #
# We take care of loading modules, setting up networks, run	  #
# mkfs on all the filesystems, mounting, exporting and start  #
# nfs deamons                                                 #
###############################################################

/sbin/modprobe lpfcdd
/sbin/modprobe lpfc
$SFS_SCRIPTS_PATH/netup.sh
$SFS_SCRIPTS_PATH/sfs-tune.sh
$SFS_SCRIPTS_PATH/sfs-unexport.sh
/bin/umount /mnt[1-9]*
$SFS_SCRIPTS_PATH/sfs-newfs.sh $SFSFS
$SFS_SCRIPTS_PATH/sfs-mountfs.sh
$SFS_SCRIPTS_PATH/sfs-export.sh
/usr/sbin/rpc.mountd
/usr/sbin/rpc.nfsd 84 

###############################################################
# Run the benchmark                                           #
###############################################################

RUN_NAME=$SFS_NAME-`echo $SFS_RC | sed 's/sfs_rc-//'`
echo $RUN_NAME
rsh -l spec en1host1 "cd benchspec/162.nfsv2/;  bin/sfs_mgr -r result/$SFS_RC -s $RUN_NAME"

################################################################
# DONE:  Benchmark Finished                                    #
# Copy, Unmount, Exit                                          #
################################################################

sleep 100	# Wait until the stats have finish colecting
cp -a /home/spec-sfs3.0/benchspec/162.nfsv2/result/*.${RUN_NAME} $LOGDIR/benchmark
rm -rf /home/spec-sfs3.0/benchspec/162.nfsv2/result/*.${RUN_NAME}

if [ "$DOPROFILE" ]; then
	$OP_PATH/opcontrol --shutdown
fi

$SFS_SCRIPTS_PATH/sfs-unexport.sh
/bin/umount /mnt[1-9]*

#    rsh -l root en1host2 "reboot" &
#    rsh -l root en2host2 "reboot" &
#    rsh -l root en3host2 "reboot" &
#    rsh -l root en4host2 "reboot" &
#    sleep 30
#    killall -9 rsh

stopprofilers
doprofilers report
#doprofilers postprocess

