#!/bin/bash

# max time before benchmark program gets killed
max_runtime_sec=3600

# interval in which memory consumption is recorded
mem_poll_interval_ms=50

# maximum number of trials before a run gets aborted
max_trials=3

# configure this for your system
scala_home=

# classpath for ActorFoundry
foundry_home=/home/neverlord/actor-framework/benchmarks/actor_foundry
classpath_foundry="$foundry_home/lib_src/lib/foundry-1.0.jar:$foundry_home/lib_src/classes"

usage="\
usage: $0 USERID BIN_PATH RUNTIME_FILE MEM_USAGE_FILE IMPL BENCH BENCH_ARGS

  USERID:         ID of the user this benchmark should run as
  BIN_PATH:       path to CAF binaries
  RUNTIME_FILE:   output file for runtime
  MEM_USAGE_FILE: output file for memory consumption
  IMPL:           (caf|scala|erlang|foundry|charm|salsa)
  BENCH:          (mixed_case|actor_creation|mailbox_performance|mandelbrot)

"

if [[ $# -le 4 ]]; then
  echo "too few arguments"; echo; echo "$usage"
  exit
fi

if [[ $(id -u) != 0 ]]; then
  echo "need to be root"; echo; echo "$usage"
  exit
fi

if [[ $(uname) == "Darwin" ]]; then
  NumCores=$(/usr/sbin/system_profiler SPHardwareDataType | awk 'tolower($0) ~ /total number of cores/ {print $5};')
else
  NumCores=$(grep "processor" /proc/cpuinfo | wc -l)
fi

cmd=""
username="$1" ; userid=$(id -u $username) ; shift
binpath="$1" ; shift
runtime_out_file="$1" ; shift
mem_usage_out_file="$1" ; shift
impl="$1" ; shift
bench="$1" ; shift

jvm_tuning="-Xmx10240M -Xms32M"
erl_cmd=$(which erl)

salsa_cp=/home/firegurafiku/SalsaLite.jar

case "$impl" in
  caf)
    cmd="$binpath/$bench $@"
    ;;
  charm)
    cmd="$binpath/charm_$bench +p $NumCores $@"
    ;;
  scala)
    scala_lib="$scala_home/lib"
    scala_tuning="-Xbootclasspath/a:$scala_lib/akka-actors.jar:$scala_lib/jline.jar:$scala_lib/scala-actors-migration.jar:$scala_lib/scala-actors.jar:$scala_lib/scala-compiler.jar:$scala_lib/scala-library.jar:$scala_lib/scala-reflect.jar:$scala_lib/scala-swing.jar:$scala_lib/scalap.jar:$scala_lib/typesafe-config.jar -classpath "" -Dscala.home=$scala_home -Dscala.usejavacp=true scala.tools.nsc.MainGenericRunner"
    cmd=" $jvm_tuning $scala_tuning org.caf.scala.$bench $@"
    ;;
  erlang)
    cmd="$erl_cmd -noshell -noinput +P 20000000 -smp enable +S $NumCores:$NumCores -setcookie abc123 -sname benchmark -pa erlang -s $bench start $@ -s init stop"
    ;;
  foundry)
    modified_args=$(echo "_$@" | tr ' ' _)
    echo "modified_args = $modified_args"
    cmd=" -cp $classpath_foundry osl.foundry.FoundryStart osl.examples.caf_benches.$bench boot $modified_args"
    ;;
  salsa)
    cmd=" $jvm_tuning -cp $salsa_cp:$binpath -Dnstages=$NumCores ${bench} $@"
    ;;
  *)
    echo "invalid argument: $impl"; echo; echo "$usage"
    exit 1
    ;;
esac

olddir=$PWD
cd "$binpath"
export JAVA_OPTS="-Xmx40960M"
for trial in $(seq 1 $max_trials); do
  if ./caf_run_bench $userid $max_runtime_sec $mem_poll_interval_ms "$runtime_out_file" "$mem_usage_out_file" $cmd ; then
    cd "$olddir"
    exit 0
  fi
done

echo "benchmark did fail $max_trials times, abort" 1>&2
