#! /bin/sh

# Copyright 1997, 1998, 1999, 2001 Alexandre Oliva <oliva@lsd.ic.unicamp.br>

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Runs the Guarana performance test program

# The following program names are assumed:
#  jg: Kaffe+Guarana with JIT
#  jk: Kaffe with JIT
#  ig: Kaffe+Guarana without JIT
#  ik: Kaffe without JIT

# usage: ./run

rm -rf perftest.$$
trap 'rm -rf perftest.$$' 1 2 15
# This is only used if guarana/kaffe was not configured --with-stats
timecmd () {
    cmdname=$1; shift
    { { time -p ${1+"$@"}; } |
      awk '/JIT/ { jittime += $4; next; }
	   END {
	     if (jittime) print "'"$cmdname"'-JIT: " jittime/1000.0;
	   }'; } 2>&1 |
    awk '/^user [0-9.]*$/ { usertime=0+$2; next; }
	 /^(real|sys) [0-9.]*$/ { next; }
	 $1 == "'"$cmdname"'-JIT:" { jitline=$0; jittime=$2; next; }
    	 { print; }
	 END {
	     print "'"$cmdname"': " usertime;
	     if (jitline) {
	         print jitline;
		 print "'"$cmdname"'-diff: " usertime-jittime;
	     }
	 }' >&2
}
    
argstopass=""
repeat=1
while test $# -gt 0; do
    case "$1" in
    --time) argstopass="$1 $2"; shift;;
    --tolerance) argstopass="$1 $2"; shift;;
    --repeat) repeat=$2; shift;;
    *) echo $0: invalid argument: $1 >&2; exec false;;
    esac
    shift
done

errstatus=0

while repeat=`expr $repeat - 1`; test "$repeat" -ge 0; do
    if test -n "$JAVA"; then
        echo "$JAVA" performance Current build
        case "$CLASSPATH" in *guarana.jar*)
	    echo "$JAVA" nullMetaPerformance Current build and nullMetaPerformance
	    echo "$JAVA" logMetaPerformance Current build and logMetaPerformance
	  ;;
	esac
    else
	echo jk performance Kaffe JIT without Guarana
	echo jg performance Kaffe JIT with Guarana
	echo ik performance Kaffe intrp without Guarana
	echo ig performance Kaffe intrp with Guarana
	echo jg nullMetaPerformance Kaffe JIT with Guarana and nullMetaPerformance
	echo ig nullMetaPerformance Kaffe intrp with Guarana and nullMetaPerformance
	echo jg logMetaPerformance Kaffe JIT with Guarana and MetaLogger
	echo ig logMetaPerformance Kaffe intrp with Guarana and MetaLogger
    fi | { 
	while read java class comment; do
	    if $java -version > /dev/null 2>&1; then
		echo "$comment" >&2
		$java $class $argstopass `$java performance --list |
					  egrep -v '^getTime$'` || errstatus=$?
		if test x"$class" = x"performance"; then
		    mkdir perftest.$$
		    if $java -help 2>&1 | grep vmstats > /dev/null; then
			# -ms 1m is needed for gctime and totals to be
			# printed, particularly on intrp
			$java -ms 1m \
			    -vmstats gctime-scan,gctime-sweep,jittime \
			    at.dms.kjc.Main -d perftest.$$ \
			    performance.java 2>&1 |
			  awk >&2 '
/#/ { next; }
/^gctime/ { gctime += $2; }
/^jittime/ { jittime += $2; }
/^TOTAL/ { totaltime += $2; }
END {
  if (totaltime != "") print "compile: " totaltime;
  if (jittime != "") print "compile-jit: " jittime;
  if (gctime != "") print "compile-gc: " gctime;
  if (totaltime != "") print "compile-diff: " totaltime - jittime - gctime;
}' || errstatus=$? 
		    else
			case "$comment" in
			*JIT* | Current*) verbosejit=-verbosejit;;
			*) verbosejit=;;
			esac
			timecmd compile $java $verbosejit at.dms.kjc.Main -d perftest.$$ performance.java || errstatus=$?
		    fi
		    rm -rf perftest.$$
		fi
		echo >&2
	    fi
	done
        exit $errstatus
    } || errstatus=$?
done

exit $errstatus
