#!/bin/bash
# StarPU --- Runtime system for heterogeneous multicore architectures.
#
# Copyright (C) 2014-2021  Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
# Copyright (C) 2014       Université Joseph Fourier
#
# StarPU is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at
# your option) any later version.
#
# StarPU is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU Lesser General Public License in COPYING.LGPL for more details.
#
# Script for giving statistical analysis of the paje trace

set -e # fail fast

# File names
basename="$PWD"
outputfile="starpu_paje_state_stats.csv"
r_script="$(dirname $(which $0))/starpu_paje_state_stats.R"
r_input=""

# Command line arguments
range="0:-1"
name="All"
verbose=0
inputfiles=""

help_script()
{
cat << EOF
Give statistical analysis of the paje trace

$0 [ options ] paje.trace [paje.trace2 ...]

Options:
   -r      To fix range x1:x2 ("-1" for infinity)
   -n      To choose a certain state
   -v      Print output to command line
   -h      Show this message

Examples:

$0 example.native.trace

$0 -r 100:300 -n FetchingInput -v example.native.trace example.simgrid.trace

Report bugs to <starpu-devel@inria.fr>
EOF
}

if [ "$1" = "--version" ] ; then
    echo "$PROGNAME (StarPU) 1.3.8"
    exit 0
fi

if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "" ] ; then
    help_script
    exit 0
fi

while getopts "r:n:vh" opt; do
  case $opt in
     r)
      range="$OPTARG"
      ;;
     n)
      name="$OPTARG"
      ;;
     v)
      verbose=1
      ;;
     h)
      help_script
      exit 4
      ;;
    \?)
      echo "Invalid option: -$OPTARG"
      help_script
      exit 3
      ;;
  esac
done

# Reading files that need to be analyzed
shift $((OPTIND - 1))
inputfiles=$@
if [[ $# < 1 ]]; then
    echo "Error!"
    help_script
    exit 2
fi

# Getting range
range1=$(eval echo $range | cut -d: -f1)
range2=$(eval echo $range | cut -d: -f2)

#####################################
# Transforming input files into .csv
for file in $inputfiles; do
    if [ ! -s $file ]
	then
	echo "Error: file $file does not exist!"
	exit 5
    fi
# Sorting traces
    grep -e '^\(\(%\)\|\(\(0\|1\|2\|3\|4\|5\|6\|7\)\>\)\)' $file > start.trace
    grep -e '^\(\(%\)\|\(\(0\|1\|2\|3\|4\|5\|6\|7\)\>\)\)' -v $file > end.trace
    sort -s -V --key=2,2 end.trace > endSorted.trace
    if grep -q start_profiling endSorted.trace
    then
	echo Using start_profiling/stop_profiling trace selection.
	sed -ne '/start_profiling/,/stop_profiling/p' < endSorted.trace > endSorted2.trace
    else
	cp endSorted.trace endSorted2.trace
    fi
    cat start.trace endSorted2.trace > outputSorted.trace

# Transferring to .csv
    pj_dump -n outputSorted.trace > $file.csv
    perl -i -ne 'print if /^State/' $file.csv

    r_input=$(eval echo "$r_input $file.csv")
done

#####################################
# Running R file to get actual results
Rscript $r_script $range1 $range2 $name $outputfile $r_input

# If verbose then write results to stdout
if [[ $verbose == 1 ]]; then
    column -s, -t $outputfile
fi

# Cleanup: delete temporary files
rm -f outputSorted.trace
rm -f start.trace
rm -f end.trace
rm -f endSorted.trace
rm -f endSorted2.trace
