#!/bin/ksh
# $Id$
##############################################################
# Original : C. Talandier for NEMO team
# Contact  : nemo_st@locean-ipsl.upmc.fr
#
# It is called by the assessment.ksh script for a 
# given configuration 
#
# INPUT ARGS: 6
#     - zrunt  : run type to treat
#     - zfile1 : mon run timing input file name
#     - zfile2 : mpi run timing input file name
#     - zout   : output file name
#     - ztag   : tag name to which are compared files
#     - zxis   : (0/1) reference timing files (do/do not) exist
#
# WORK: It aims to :
#
#      - Treat output timing from the aix target for 
#        mon & mpi run 
#     *- compare it to a reference version 
#
# * this step is performed only if a reference tag 
#   is specified with the -t option and if files exist.
#   See the Makefile under ./config/NVTK directory
#
##############################################################
#set -xv
##############################################################

# Get the name of the present script
nscript=$( (basename $0) )

#
# help_CPU_time() To print help
#
function help_CPU_time
{
  echo "Usage: $nscript [-r] [-a] [-b] [-o] [-t] [-x]"
  echo "Options: These are optional argument"
  echo " -r : run type to treat"
  echo " -a : current version timing input file name"
  echo " -b : reference version timing input file name"
  echo " -o : output file name"
  echo " -t : tag name to which are compared files"
  echo " -x : (0/1) reference timing files (do/do not) exist"
  echo " their values are not taken)"
  exit 1
}

# Initialization
zrunt=
zfile1=
zfile2=
zout=
ztag=
zxis=1

# Get arguments
while getopts r:a:b:o:t:x: opt
do
  case "$opt" in
    r) zrunt="$OPTARG";;
    a) zfile1="$OPTARG";;
    b) zfile2="$OPTARG";;
    o) zout="$OPTARG";;
    t) ztag="$OPTARG";;
    x) zxis="$OPTARG";;
    *) help_CPU_time;;
  esac
done

# Check if results must be compare to the reference version
test -n "${ztag}"
evaltag=$?

case "${zrunt}" in

     'mon' )

           # OUTPUT TIMING OF THE MON RUN
           ##############################
           
           if [ ${evaltag} -a ${zxis} == 0 ] ; then
               # Get values from the reference tag
               zreal=$( awk '/wall clock time/           {print $6}' ${zfile2} )
               zuser=$( awk '/Total amount of time in u/ {print $9}' ${zfile2} )
               zmemo=$( awk '/Maximum resident set size/ {print $6/1024}' ${zfile2} )
               zuzra=$( awk '/Utilization rate/          {print $4}' ${zfile2} )
               # Print: Variable long name | current version values | reference version values | variations
               echo "                                                 Current version     ${ztag} version       Variation " >> ${zout}
               echo "                                                                 " >> ${zout}
               awk '/wall clock time/            { creal=$6      ; printf("%-6s %.33s %8s %12.2f    | %12.2f      |     %7.2f % \n", " ", $0, "(sec) :"    , $6  , invar, ((creal-invar)/invar)*100 ) }' invar=${zreal} ${zfile1} >> ${zout}
               awk '/Total amount of time in u/  { cuser=$9      ; printf("%-6s %.34s %7s %12.2f    | %12.2f      |     %7.2f % \n", " ", $0, "(sec) :"    , $9  , invar, ((cuser-invar)/invar)*100 ) }' invar=${zuser} ${zfile1} >> ${zout}
               awk '/Maximum resident set size/  { cmflo=$6/1024 ; printf("%-6s %.26s %15s %12.2f    | %12.2f      |     %7.2f % \n", " ", $0, "(Mo) :"     ,cmflo, invar, ((cmflo-invar)/invar)*100 ) }' invar=${zmemo} ${zfile1} >> ${zout}
               awk '/Utilization rate/           { cuzra=$4      ; printf("%-5s %.24s %18s %12.2f    | %12.2f      |     %7.2f % \n", " ", $0, "~100% (%) :", $4  , invar, ((cuzra-invar)/invar)*100 ) }' invar=${zuzra} ${zfile1} >> ${zout}
           else
               # Print: Variable long name | current version values
               echo "                                                 Current version " >> ${zout}
               echo "                                                                 " >> ${zout}
               awk '/wall clock time/            {printf("%-6s %.33s %8s %9.2f \n", " ", $0, "(sec) :"    , $6      ) }' ${zfile1} >> ${zout}
               awk '/Total amount of time in u/  {printf("%-6s %.34s %7s %9.2f \n", " ", $0, "(sec) :"    , $9      ) }' ${zfile1} >> ${zout}
               awk '/Maximum resident set size/  {printf("%-6s %.26s %15s %9.2f \n", " ", $0, "(Mo) :"     , $6/1024) }' ${zfile1} >> ${zout}
               awk '/Utilization rate/           {printf("%-5s %.24s %18s %9.2f \n", " ", $0, "~100% (%) :", $4     ) }' ${zfile1} >> ${zout}
           fi 

           ;;

     'mpi' )
           
           # OUTPUT TIMING OF THE MPI RUN
           ##############################
           
           if [ ${evaltag} -a ${zxis} == 0 ] ; then
               # Get values from the reference tag
               awk '/wall clock time/           {print}' ${zfile2} > temp1
               awk '/Maximum resident set size/ {print}' ${zfile2} > temp2
               awk '/Utilization rate/          {print}' ${zfile2} > temp3
           
               zreal=$( awk '/wall clock time/           {cum += $6} END {printf("%f", cum/NR) }'        temp1 )
               zuser=$( awk '/Total amount of time in u/ {cum += $9} END {printf("%f", cum )   }'        ${zfile2})
               zmemo=$( awk '/Maximum resident set size/ {cum += $6} END {printf("%f", cum/(1024*NR)) }' temp2 )
               zuzra=$( awk '/Utilization rate/          {cum += $4} END {printf("%f", cum/NR) }'        temp3 )
           
               # Get values from the current version
               awk '/wall clock time/           {print}' ${zfile1} > temp1
               awk '/Maximum resident set size/ {print}' ${zfile1} > temp2
               awk '/Utilization rate/          {print}' ${zfile1} > temp3
           
               echo "                                                 Current version     ${ztag} version       Variation " >> ${zout}
               echo "                                                                                                     " >> ${zout}
               awk '/wall clock time/           {cum += $6} END {printf("%-7s %-39s %1s %11.2f    |     %12.2f   |     %7.2f %\n" , " ", "Execution time (wall clock time)  (sec)", ":", cum/NR       , invar, ((cum/NR-invar)/invar)*100  ) }' invar=${zreal} temp1     >> ${zout}
               awk '/Total amount of time in u/ {cum += $9} END {printf("%-7s %-39s %1s %11.2f    |     %12.2f   |     %7.2f %\n" , " ", "Total amount of time in user mode (sec)", ":", cum          , invar, ((cum-invar)/invar)*100  ) }'    invar=${zuser} ${zfile1} >> ${zout}
               awk '/Maximum resident set size/ {cum += $6} END {printf("%-7s %-25s %15s %12.2f   |     %12.2f   |     %7.2f %\n", " ", "Maximum resident set size", "(Mo)  :"        , cum/(1024*NR), invar, ((cum/(1024*NR)-invar)/invar)*100  ) }'  invar=${zmemo} temp2     >> ${zout}
               awk '/Utilization rate/          {cum += $4} END {printf("%-7s %-16s %24s %12.2f   |     %12.2f   |     %7.2f %\n", " ", "Utilization rate", "(%)   :"                 , cum/NR       , invar, ((cum/NR-invar)/invar)*100  ) }' invar=${zuzra} temp3     >> ${zout}
           
               echo "                                                                                                     " >> ${zout}
           
               \rm temp*
           
           else
               # Get values from the current version
               awk '/wall clock time/           {print}' ${zfile1} > temp1
               awk '/Maximum resident set size/ {print}' ${zfile1} > temp2
               awk '/Utilization rate/          {print}' ${zfile1} > temp3
           
               echo "                                                 Current version " >> ${zout}
               echo "                                                                 " >> ${zout}
               awk '/wall clock time/           {cum += $6} END {printf("%-7s %-39s %1s %9.2f\n" , " ", "Execution time (wall clock time)  (sec)", ":", cum/NR ) }' temp1     >> ${zout}
               awk '/Total amount of time in u/ {cum += $9} END {printf("%-7s %-39s %1s %9.2f\n" , " ", "Total amount of time in user mode (sec)", ":", cum ) }'    ${zfile1} >> ${zout}
               awk '/Maximum resident set size/ {cum += $6} END {printf("%-7s %-25s %15s %9.2f\n", " ", "Maximum resident set size", "(Mo)  :", cum/(1024*NR) ) }'  temp2     >> ${zout}
               awk '/Utilization rate/          {cum += $4} END {printf("%-7s %-16s %24s %9.2f\n", " ", "Utilization rate", "(%)   :", cum/NR ) }'                  temp3     >> ${zout}
           
               echo "                                                                                                     " >> ${zout}
           
               \rm temp*
           fi

           ;;
esac
