#!/bin/bash
# For simplicity, run all as root with gcc 4.6.3 under kernel 3.7:
#   NO fp:
#     perf
#     perf-PEBS
#   Special:
#     gprof (with OPTLVL=-O1)
#   WITH fp:
#     operf (with fp)
#     oprofile (with fp, needs root)
#     perf-fp
# All once with ADDCFLAGS="-DINNERLOOP=1"
#     once with ADDCFLAGS="-DINNERLOOP=1 -DREVERSE"
#     once with no flags
REPS=100
mkdir val1_forward val1_reverse val1_original

# FORWARD

# perf and perf-PEBS use same executable (without frame pointers).
make PRF=perf ADDCFLAGS="-DINNERLOOP=1" val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=perf val1test
done
mv val1_perf.txt    val1_forward
for looper in $(seq 1 ${REPS}); do
  make PRF=perf PERFEVENT=cpu-cycles:pp val1test
done
mv val1             val1_forward/val1_perf
mv val1.i           val1_forward/val1_perf.i
mv val1_objdump.txt val1_forward/val1_perf_objdump.txt
mv val1_perf.txt    val1_forward/val1_perf-PEBS.txt

make PRF=gprof OPTLVL="-O1" ADDCFLAGS="-DINNERLOOP=1" val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=gprof OPTLVL="-O1" val1test
done
mv val1             val1_forward/val1_gprof
mv val1.i           val1_forward/val1_gprof.i
mv val1_objdump.txt val1_forward/val1_gprof_objdump.txt
mv val1_gprof.txt   val1_forward

# operf and oprofile use same executable (with frame pointers).
# Add perf-fp.
make PRF=operf ADDCFLAGS="-DINNERLOOP=1" val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=operf val1test
done
mv val1_operf.txt   val1_forward
for looper in $(seq 1 ${REPS}); do
  make PRF=oprofile val1test
done
mv val1_oprofile.txt  val1_forward
for looper in $(seq 1 ${REPS}); do
  make PRF=perf val1test
done
mv val1_perf.txt      val1_forward/val1_perf-fp.txt
mv val1               val1_forward/val1_oprofile
mv val1.i             val1_forward/val1_oprofile.i
mv val1_objdump.txt   val1_forward/val1_oprofile_objdump.txt

# REVERSE (clone and hack)
# Replace "-DINNERLOOP=1" with "-DINNERLOOP=1 -DREVERSE"
# Replace _forward with _reverse

# perf and perf-PEBS use same executable (without frame pointers).
make PRF=perf ADDCFLAGS="-DINNERLOOP=1 -DREVERSE" val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=perf val1test
done
mv val1_perf.txt    val1_reverse
for looper in $(seq 1 ${REPS}); do
  make PRF=perf PERFEVENT=cpu-cycles:pp val1test
done
mv val1             val1_reverse/val1_perf
mv val1.i           val1_reverse/val1_perf.i
mv val1_objdump.txt val1_reverse/val1_perf_objdump.txt
mv val1_perf.txt    val1_reverse/val1_perf-PEBS.txt

make PRF=gprof OPTLVL="-O1" ADDCFLAGS="-DINNERLOOP=1 -DREVERSE" val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=gprof OPTLVL="-O1" val1test
done
mv val1             val1_reverse/val1_gprof
mv val1.i           val1_reverse/val1_gprof.i
mv val1_objdump.txt val1_reverse/val1_gprof_objdump.txt
mv val1_gprof.txt   val1_reverse

# operf and oprofile use same executable (with frame pointers).
# Add perf-fp.
make PRF=operf ADDCFLAGS="-DINNERLOOP=1 -DREVERSE" val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=operf val1test
done
mv val1_operf.txt   val1_reverse
for looper in $(seq 1 ${REPS}); do
  make PRF=oprofile val1test
done
mv val1_oprofile.txt  val1_reverse
for looper in $(seq 1 ${REPS}); do
  make PRF=perf val1test
done
mv val1_perf.txt      val1_reverse/val1_perf-fp.txt
mv val1               val1_reverse/val1_oprofile
mv val1.i             val1_reverse/val1_oprofile.i
mv val1_objdump.txt   val1_reverse/val1_oprofile_objdump.txt

# ORIGINAL (clone and hack)
# Replace ADDCFLAGS="-DINNERLOOP=1" with nothing.
# Replace _forward with _original

# perf and perf-PEBS use same executable (without frame pointers).
make PRF=perf  val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=perf val1test
done
mv val1_perf.txt    val1_original
for looper in $(seq 1 ${REPS}); do
  make PRF=perf PERFEVENT=cpu-cycles:pp val1test
done
mv val1             val1_original/val1_perf
mv val1.i           val1_original/val1_perf.i
mv val1_objdump.txt val1_original/val1_perf_objdump.txt
mv val1_perf.txt    val1_original/val1_perf-PEBS.txt

make PRF=gprof OPTLVL="-O1"  val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=gprof OPTLVL="-O1" val1test
done
mv val1             val1_original/val1_gprof
mv val1.i           val1_original/val1_gprof.i
mv val1_objdump.txt val1_original/val1_gprof_objdump.txt
mv val1_gprof.txt   val1_original

# operf and oprofile use same executable (with frame pointers).
# Add perf-fp.
make PRF=operf  val1 val1.i val1_objdump.txt
for looper in $(seq 1 ${REPS}); do
  make PRF=operf val1test
done
mv val1_operf.txt   val1_original
for looper in $(seq 1 ${REPS}); do
  make PRF=oprofile val1test
done
mv val1_oprofile.txt  val1_original
for looper in $(seq 1 ${REPS}); do
  make PRF=perf val1test
done
mv val1_perf.txt      val1_original/val1_perf-fp.txt
mv val1               val1_original/val1_oprofile
mv val1.i             val1_original/val1_oprofile.i
mv val1_objdump.txt   val1_original/val1_oprofile_objdump.txt
