# This script compiles the benchmark program with each of the different
# mutex implementations selected, and runs it with various combinations
# of number-of-mutexes and number-of-threads.  Three runs are performed
# and the median results are reported.

# For cross-compilation, set prefix to the compiler name prefix and
# set compile_only.
# To run previously-[cross]-compiled executables, set no_compile.
prefix=arm-linux-gnu-
compile_only=1
#no_compile=1
#prefix=
#compile_only=
no_compile=

if [ ! ${compile_only} ]
then

  case $# in
  1) iters=$1 ;;
  *) echo "Usage: $0 num_iters"
     exit 1
  esac

fi

echo "Method           Objs    Threads   Runtime   Errors?"

for method in NO_LOCKING SPINLOCK YIELD FUTEX PTHREADS
do
  echo "----------------------------------------------------"

  exe="mutex_perf_${method}"

  if [ ! ${no_compile} ]
  then
    ${prefix}g++ "-DUSE_${method}" -O3 -W -Wall -o ${exe} main.cc extern.cc -lpthread
  fi

  if [ ${compile_only} ]
  then
    continue
  fi

  for num_objs in 1 10007
  do
    for num_threads in 1 2 32
    do

      printf "%-14s  %5d      %5d  " ${method} ${num_objs} ${num_threads}

      rm -f runtimes
      errors=""
      for run in 1 2 3
      do
        /usr/bin/time --quiet -a -o runtimes -f '%e' \
            "./${exe}" ${num_objs} ${num_threads} ${iters}
        if [ $? -ne 0 ]
        then
          errors="${errors}*"
        fi
      done
      median_time=`sort -n runtimes | head -2 | tail -1`
      printf "%8.2f    %s\n" "${median_time}" "${errors}"

    done
  done
#fi
done

