#!/bin/ksh
##############################################################
# Original : C. Talandier for ESOPA
# Contact  : opatlod@locean-ipsl.upmc.fr
#
# It is called by the assessment.ksh script for a 
# given configuration 
#
# INPUT ARGS: 6
#     - zxcm   : (0/1) mon${zconf}_err file (do/do not) exist"
#     - zxrs   : (0/1) Ref memory_size.txt file (do/do not) exist"
#     - zxrm   : (0/1) Ref mon${zconf}_err file (do/do not) exist"
#     - zconf  : configuration name"
#     - zout   : output file name"
#     - ztag   : tag name to which are compared files"
#
# WORK: It aims to :
#
#      - Treat output timing from the sx8brodie target for 
#        mon, mpi & omp runs
#     *- 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 [-a] [-b] [-d] [-c] [-o] [-t]"
  echo "Options: These are optional argument"
  echo " -a : (0/1) mon${zconf}_err file (do/do not) exist"
  echo " -b : (0/1) Ref memory_size.txt file (do/do not) exist"
  echo " -d : (0/1) Ref mon${zconf}_err file (do/do not) exist"
  echo " -c : configuration name"
  echo " -o : output file name"
  echo " -t : tag name to which are compared files"
  echo " their values are not taken)"
  exit 1
}

# Initialization
zxcm=1
zxrs=1
zxrm=1
zconf=
zout=
ztag=

# Get arguments
while getopts a:b:d:c:o:t: opt
do
  case "$opt" in
    a) zxcm="$OPTARG";;
    b) zxrs="$OPTARG";;
    d) zxrm="$OPTARG";;
    c) zconf="$OPTARG";;
    o) zout="$OPTARG";;
    t) ztag="$OPTARG";;
    *) help_MEM_size;;
  esac
done

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

zfilecs='mon/LONG/memory_size.txt' ;  zfilers='mon/LONG/REF/memory_size.txt'
zfilecm="mon/LONG/mon${zconf}_err"  ;  zfilerm="mon/LONG/REF/mon${zconf}_err"

# If reference version files exist
if [ ${evaltag} == 0 -a ${zxrm} == 0 ] ;  then
    # Get values from the reference tag
    Mo_stack_ms=$( (awk '/Stack/ {print $NF/(1024*1024) }' ${zfilers} ) )
    Mo_stati_ms=$( (awk '/Stati/ {print $NF/(1024*1024) }' ${zfilers} ) )
    if [ ${zxcm} == 0 ] ; then Mo_max_used=$( (awk '/resident/ {print $6/1024}' ${zfilerm} ) ) ; fi

    # Print: Variable long name | current version values | reference version values | variations
    echo "                                                 " >> ${zout}
    echo "                                      Version    " >> ${zout}
    echo "             Memory          Current /       \ ${ztag}        Variation " >> ${zout}
    awk ' /Stack/ { val=$NF/(1024*1024) } END {printf("%-9s %-16s %8.1f      |   %8.1f     |    %5.1f %\n ", " ", "Stack     (Mo) :", val, inck, ((val-inck)/inck)*100 ) }' inck=${Mo_stack_ms} ${zfilecs} >> ${zout}
    awk ' /Stati/ { val=$NF/(1024*1024) } END {printf("%-8s %-16s %8.1f      |   %8.1f     |    %5.1f %\n ", " ", "Static    (Mo) :", val, inti, ((val-inti)/inti)*100 ) }' inti=${Mo_stati_ms} ${zfilecs} >> ${zout}
    if [ ${zxcm} == 0 ] ; then awk ' /resident/ { val=$6/1024 } END {printf("%-8s %-16s %8.1f      |   %8.1f     |    %5.1f %\n ", " ", "Max. used (Mo) :", val, inus, ((val-inus)/inus)*100 ) }' inus=${Mo_max_used} ${zfilecm} >> ${zout} ; fi
else
    # Print: Variable long name | current version values
    echo "                                                 " >> ${zout}
    echo "             Memory          Current version "     >> ${zout}
    awk ' /Stack/  {printf("%-9s %-.16s %11.1f\n ", " ", "Stack     (Mo) :", $NF/(1024*1024) ) }' ${zfilecs} >> ${zout}
    awk ' /Stati/  {printf("%-8s %-.16s %11.1f\n ", " ", "Static    (Mo) :", $NF/(1024*1024) ) }' ${zfilecs} >> ${zout}
    if [ ${zxcm} == 0 ] ; then awk ' /resident/ {printf("%-8s %-.16s %11.1f\n ", " ", "Max. used (Mo) :", $6/1024 ) }' ${zfilecm} >> ${zout} ; fi
fi
