#!/usr/bin/env bash

set -e

trap cleanup SIGINT
trap cleanup SIGHUP

MOD_PATH="/var/lib/dkms/advligorts-rt-bench/kernel-"`uname -r`"-x86_64/module/advligorts-rt-bench.ko"

#echo "MOD_PATH: " $MOD_PATH
#MOD_PATH=`pwd`"/advligo-rt-bench.ko" #TODO: Remove


#
# Start Util functions
#


function cleanup() {
  echo "Caught signal and ignoring, wait for banchmark to complete..."
}

print_help() {
    echo "USAGE cpu-isolator-benchmark.sh [OPTIONS]"
    echo ""
    echo "    OPTIONS:"
    echo "        -h : Show this help screen"
    echo "        -t <seconds> : The number of seconds to run the benchmark for. (Default 30)"
    echo "        -q : Quite output, raw output is not displayed. A return of 0 means pass, else failure detected"
    echo "        -c <core> : The index of the core you would like to benchmark (Default isolator picks lowest free core)"
    echo "        -r <threshold_ns> : If this is set any measured latency over this value will be recorded and emitted at the end of the benchmark"
    echo "        -s <cbuf_size>     : When the -r option is used values are stored in a cbuf, this parameter will set that cbuf size (Default 100)"
    echo "        -f <format>        : When the -r option is used latencies and time offset into benchmark are stored. This parameter can change the time format. 0 -> sec, 1 -> us (Default 0)"

    echo ""
}


#
# Start Main Code
#

test_time_s=30
quite=0
core=-1
record_thresh_ns=-1
record_thresh_sz=-1
record_thresh_format=-1
# Parse Args
while getopts hqt:c:r:s:f: flag
do
    case "${flag}" in
        h) (print_help) && exit 0;;
        t) test_time_s=${OPTARG};;
        q) quite=1;;
        c) core=${OPTARG};;
        r) record_thresh_ns=${OPTARG};;
        s) record_thresh_sz=${OPTARG};;
        f) record_thresh_format=${OPTARG};;
    esac
done


KERN_ARGS=""

if (( $core != -1 )); then
    KERN_ARGS="$KERN_ARGS MP_CPU_ID=$core"
fi

if (( $record_thresh_ns > 0 )); then
    KERN_ARGS="$KERN_ARGS MP_HIGH_THRESH_NS=$record_thresh_ns"
fi

if (( $record_thresh_sz > 0 )); then
    KERN_ARGS="$KERN_ARGS MP_HIGH_THRESH_CBUF_SIZE=$record_thresh_sz"
fi

if (( $record_thresh_format > 0 )); then
    KERN_ARGS="$KERN_ARGS MP_HIGH_THRESH_FORMAT=$record_thresh_format"
fi

# Clear dmesg output
sudo dmesg -c > /dev/null

set +e
echo "Inserting benchmark module..."
sudo insmod $MOD_PATH $KERN_ARGS
if (( $? != 0 )); then
    sudo dmesg -t
    exit 1
fi
set -e

echo "Done inserting module..."
echo ""

DOTS=0
CNT=0

for (( c=0; c<$test_time_s; c++ ))
do
    printf "\r%sRunning benchmark, please don't interrupt. Seconds left: %s" "$(tput el)" "$((test_time_s-c))" 
    sleep 1
done
echo ""

echo "Removing benchmark module..."
sudo rmmod $MOD_PATH
echo ""

if [[ $quite == 0 ]]; then

OUTPUT=`sudo dmesg -t`

    if [[ $OUTPUT == *"BENCHMARK PASS"* ]]; then
        echo "*********************************"
        echo "*****                       *****"
        echo "*****    Benchmark: PASS    *****"
        echo "*****                       *****"
        echo "*********************************"
        echo ""
        echo "$OUTPUT"
        exit 1
    else
        echo "*********************************"
        echo "*****                       *****"
        echo "*****    Benchmark: FAIL    *****"
        echo "*****                       *****"
        echo "*********************************"
        echo ""
        echo "$OUTPUT"
        exit 0
    fi

fi


