#!/bin/bash

# TODO: add benchmark specific options here
#ELF="$1"
#
#run-elf $ELF

# benchmark-run target elf cache-size lines

set -e

LOGDIR="$ICLANG_ROOT/benchmarks/logs"
mkdir -p $LOGDIR
mkdir -p $LOGDIR/binaries

# Uninstrumented
# Does not perform any transformations but does go trough the pipeline
function nacho-naive() {
    cache=$1
    lines=$2
    on_duration=$3
    checkpoint_period=$4
	run-elf \
        -p custom_cache_plugin.so \
        -a hash-method=0 \
        -a cache-size=$cache \
        -a cache-lines=$lines \
        -a custom-cache-log-file=$LOGDIR/$name-$compile_config-run-nacho_naive \
        -a enable-pw-bit=0 \
        -a enable-stack-tracking=0 \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        $elf
}

function nacho-pw() {
    cache=$1
    lines=$2
    on_duration=$3
    checkpoint_period=$4
	run-elf \
        -p custom_cache_plugin.so \
        -a hash-method=0 \
        -a cache-size=$cache \
        -a cache-lines=$lines \
        -a custom-cache-log-file=$LOGDIR/$name-$compile_config-run-nacho_pw \
        -a enable-pw-bit=1 \
        -a enable-stack-tracking=0 \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        $elf
}

# Stack tracking only at checkpoint
function nacho-pw-st() {
    cache=$1
    lines=$2
    on_duration=$3
    checkpoint_period=$4
	run-elf \
        -p custom_cache_plugin.so \
        -a hash-method=0 \
        -a cache-size=$cache \
        -a cache-lines=$lines \
        -a custom-cache-log-file=$LOGDIR/$name-$compile_config-run-nacho_pw_st \
        -a enable-pw-bit=1 \
        -a enable-stack-tracking=1 \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        $elf
}


# Continious stack tracking
function nacho-pw-stcont() {
    cache=$1
    lines=$2
    on_duration=$3
    checkpoint_period=$4
	run-elf \
        -p custom_cache_plugin.so \
        -a hash-method=0 \
        -a cache-size=$cache \
        -a cache-lines=$lines \
        -a custom-cache-log-file=$LOGDIR/$name-$compile_config-run-nacho_pw_stcont \
        -a enable-pw-bit=1 \
        -a enable-stack-tracking=2 \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        $elf
}

# Continious stack tracking
function nacho-stcont() {
    cache=$1
    lines=$2
    on_duration=$3
    checkpoint_period=$4
	run-elf \
        -p custom_cache_plugin.so \
        -a hash-method=0 \
        -a cache-size=$cache \
        -a cache-lines=$lines \
        -a custom-cache-log-file=$LOGDIR/$name-$compile_config-run-nacho_stcont \
        -a enable-pw-bit=0 \
        -a enable-stack-tracking=2 \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        $elf
}

function nacho-clank() {
    cache=$1
    lines=$2
    on_duration=$3
    checkpoint_period=$4
	run-elf \
        -p custom_cache_plugin.so \
        -a hash-method=0 \
        -a cache-size=$cache \
        -a cache-lines=$lines \
        -a custom-cache-log-file=$LOGDIR/$name-$compile_config-run-nacho_clank \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        -a enable-stack-tracking=2 \
        -a enable-pw-bit=0 \
        -a enable-oracle=1 \
        $elf
}

function clank() {
    on_duration=$1
    checkpoint_period=$2
	run-elf \
        -p simple_war_detect_plugin.so \
        -a clank-log-file=$LOGDIR/$name-$compile_config-run-clank \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        $elf
}


function prowl() {
    cache=$1
    on_duration=$3
    checkpoint_period=$4
	run-elf -p custom_cache_plugin.so \
        -a hash-method=1 \
        -a cache-size=$cache \
        -a cache-lines=2 \
        -a custom-cache-log-file=$LOGDIR/$name-$compile_config-run-prowl \
        -a on-duration=$on_duration \
        -a checkpoint-period=$checkpoint_period \
        $elf
}

function plain-c() {
	run-elf \
        -p memory_stats_plugin.so \
        -a memory-stats-log-file=$LOGDIR/$name-$compile_config-run-plain_c \
        $elf
}

function help() {
    echo "Usage: benchmark-run [target]"
    echo ""

    # Other
    echo ""
    echo "other:"
    echo "  help       print this help message"
}

# Arguments
target="$1"
elf="$2"
compile_config="$3"

if [ -z "$compile_config" ]; then
    compile_config="unknown"
fi

# Vars
name=$(basename $elf .elf)

# Check for all the arguments
if [ "$#" -lt 2 ]; then
    echo "Please provide all arguments"
    help
    exit 1
fi

# Run a composit target with arguemts
# Replace + with space should be the correct argument
comp_target=${target//"+"/" "}

echo "Running target: $comp_target"
$comp_target $elf $bench_name

# Copy the elf file to the binaries directory
cp "$elf" "$LOGDIR/binaries/$compile_config-$elf"

exit 0

# Not a target
echo "Target '$target' does not exist"
help
exit 1

