#!/bin/bash
#make graph support script
#COPYRIGHT (C) HITACHI,LTD. 2006
#Created by S.Moriya<satoru.moriya.br@hitachi.com

TXT_FILE="$2_txt"
STAT_FILE="$2_stat"
DATA_FILE="$2_data"
TMP_FILE="kstrax_tmp"
TMP_FILE_2="kstrax_tmp_2"
NR_SYSCALLS=285

function usage() {
    echo ""
    echo "This command display a graph"
    echo "Usage:"
    echo "      kstrax-graph [opton] filename"
    echo ""
    echo "<OPTION>"
    echo "ALL      display all statistics information(count, average, total)"
    echo "CNT      dislpay count of syscall (only top 20)"
    echo "CNT+     display count and average time of syscall (only count top 20)"
    echo "CNT++    display count,average and total time of syscall (only count top 20)"
    echo "AVE      display average time of syscall (only top 20)"
    echo "AVE+     display average time and count of syscall (only average top 20)"
    echo "AVE++    display average, total time and count of syscall (only average top 20)"
    echo "TOTAL    display total time of syscall (only top 20)"
    echo "TOTAL+   display total time and count of syscall (only total top 20)"
    echo "TOTAL++  display total, count and average time of syscall (only total top 20)"
    echo "EXEC     display the time series information of syscall"
    echo "EXEC-C   display the time series information of syscall (only count top 20)"
    echo ""
}

function print_stats_graph() {
    local INDEX=0
    local X_TICS=$(cut -f 2 -d \  $1)
    cp /usr/sbin/$2 $TMP_FILE
    for ARG in ${X_TICS[@]}
    do
      cat $TMP_FILE | sed -e s%@x$INDEX@%$ARG%g > $TMP_FILE_2
      cp $TMP_FILE_2 $TMP_FILE
      INDEX=`expr $INDEX + 1`
    done
    cat $TMP_FILE | sed -e s%@datafile@%$1%g > $TMP_FILE_2
    gnuplot $TMP_FILE_2
    rm $TMP_FILE $TMP_FILE_2
}

if [ $# -eq 0 ];then
    usage
else
    case $1 in
	"ALL")
	    kstrax -c $2 -g 
	    cut -f 1-4,7 -d \  $STAT_FILE > $DATA_FILE
	    cat /usr/sbin/kstrax-graph-all.gp | sed -e s%@datafile@%$DATA_FILE%g > $TMP_FILE
	    gnuplot $TMP_FILE
	    rm $TMP_FILE
	    ;;
	"CNT")
	    GP_SCRIPT=kstrax-graph-count.gp
	    kstrax -c $2 -g 
	    cut -f 1-3 -d \  $STAT_FILE | sort -n -r -k 3 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"CNT+")
	    GP_SCRIPT=kstrax-graph-count+.gp
	    kstrax -c $2 -g 
	    cut -f 1-4 -d \  $STAT_FILE | sort -n -r -k 3 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"CNT++")
	    GP_SCRIPT=kstrax-graph-count++.gp
	    kstrax -c $2 -g 
	    cut -f 1-4,7 -d \  $STAT_FILE | sort -n -r -k 3 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"AVE")
	    GP_SCRIPT=kstrax-graph-average.gp
	    kstrax -c $2 -g 
	    cut -f 1,2,4 -d \  $STAT_FILE | sort -n -r -k 3 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"AVE+")
	    GP_SCRIPT=kstrax-graph-average+.gp
	    kstrax -c $2 -g 
	    cut -f 1-4 -d \  $STAT_FILE | sort -n -r -k 4 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"AVE++")
	    GP_SCRIPT=kstrax-graph-average++.gp
	    kstrax -c $2 -g 
	    cut -f 1-4,7 -d \  $STAT_FILE | sort -n -r -k 4 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"TOTAL")
	    GP_SCRIPT=kstrax-graph-total.gp
	    kstrax -c $2 -g 
	    cut -f 1,2,7 -d \  $STAT_FILE | sort -n -r -k 3 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"TOTAL+")
	    GP_SCRIPT=kstrax-graph-total+.gp
	    kstrax -c $2 -g 
	    cut -f 1-3,7 -d \  $STAT_FILE | sort -n -r -k 4 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"TOTAL++")
	    GP_SCRIPT=kstrax-graph-total++.gp
	    kstrax -c $2 -g 
	    cut -f 1-4,7 -d \  $STAT_FILE | sort -n -r -k 5 | head -n 20 > $DATA_FILE
	    print_stats_graph $DATA_FILE $GP_SCRIPT
	    rm $STAT_FILE $DATA_FILE
	    ;;
	"EXEC")
	    GP_SCRIPT=kstrax-graph-exec.gp
	    kstrax -t $2 -g 
	    grep -v ^name $TXT_FILE | grep -v ^count |\
		grep -v ^total | grep -v ^average > $DATA_FILE
	    cat /usr/sbin/kstrax-graph-exec.gp |\
	    sed -e s%@datafile@%$DATA_FILE%g > $TMP_FILE
	    gnuplot $TMP_FILE
	    rm $TMP_FILE $DATA_FILE
	    ;;
	"EXEC-C")
	    GP_SCRIPT=kstrax-graph-exec-c.gp
	    kstrax -t $2 -g
	    ARG_LIST=$(grep ^count $TXT_FILE | cut -f 2-$NR_SYSCALLS -d \ )
	    COUNT=2
	    for ARG in ${ARG_LIST[@]}
	    do
	      echo "$ARG $COUNT" >> $TMP_FILE
	      COUNT=`expr $COUNT + 1`
	    done
	    ARG_LIST=$(sort -n -r $TMP_FILE | head -n 20 | cut -f 2 -d \ )
	    CUT_ARGS=`echo $ARG_LIST | sed -e s%\ %,%g`
	    grep -v ^count $TXT_FILE | grep -v ^total | grep -v ^average |\
		cut -f 1,$CUT_ARGS -d \  > $DATA_FILE
	    COUNT=0
	    DATA=$(grep ^name $DATA_FILE | cut -f 2-21 -d \  )
	    cp /usr/sbin/$GP_SCRIPT $TMP_FILE
	    for ARG in ${DATA[@]}
	    do
	      cat $TMP_FILE | sed -e s%@x$COUNT@%$ARG%g > $TMP_FILE_2
	      cp $TMP_FILE_2 $TMP_FILE
	      COUNT=`expr $COUNT + 1`
	    done
	    cat $TMP_FILE | sed -e s%@datafile@%$DATA_FILE%g > $TMP_FILE_2
	    gnuplot $TMP_FILE_2
	    rm $TMP_FILE $TMP_FILE_2 $DATA_FILE $TXT_FILE
#	    grep -v ^name $TXT_FILE | grep -v ^count |\
#		grep -v ^total | grep -v ^average > $DATA_FILE
#	    cat /usr/sbin/kstrax-graph-exec.gp |\
#	    sed -e s%@datafile@%$DATA_FILE%g -e s%@graph@%$GRAPH_FILE%g > $TMP_FILE
#	    gnuplot $TMP_FILE
#	    rm $TMP_FILE $DATA_FILE
	    ;;
	*)
	    echo "unknown option"
    esac
fi
