#!/bin/bash

# Needed for consistent float format
export LC_ALL=C

if [[ "$JAVA_HOME" != "" ]]; then
  JAVA_HOME="${JAVA_HOME}/bin/"
fi

if [[ "$JETTY_HOME" == "" ]]; then
  JETTY_HOME=/tmp/jetty-home-11.0.7
fi

if [[ "$COUNT" == "" ]]; then
  COUNT=20
fi

RSS_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
RSS1_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
RSS2_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
OUT_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
CODE_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
CODECACHE_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMETHODS_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
JCMD_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
TOTAL_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMCOMPILED_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMCODE_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMTOTAL_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMTIME_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMBAILOUT_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMC1TIME_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
NMC2TIME_FILE=$(mktemp evol_method_deps.XXXXXXXXXX)
# Delete temporary files on exit
trap '{ rm -f -- "${RSS_FILE}" "${RSS1_FILE}" "${RSS2_FILE}" "${OUT_FILE}" "${CODE_FILE}" "${CODECACHE_FILE}" "${NMETHODS_FILE}" "${JCMD_FILE}" "${TOTAL_FILE}" "${NMCOMPILED_FILE}" "${NMCODE_FILE}" "${NMTOTAL_FILE}" "${NMTIME_FILE}" "${NMBAILOUT_FILE}" "${NMC1TIME_FILE}" "${NMC2TIME_FILE}"; }' EXIT

for (( i=1; i<=${COUNT}; i++ )); do
  ${JAVA_HOME}java -XX:+CITime -XX:-UseLargePages -XX:-PrintWarnings -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockCommercialFeatures -XX:+PerfDataSaveToFile -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics -XX:StartFlightRecording=dumponexit=true,filename=rwrecording.jfr ${JAVA_OPTS} -Djetty.home=${JETTY_HOME} -jar ${JETTY_HOME}/start.jar --module=http > ${OUT_FILE} 2>&1 &

  sleep 3

  pmap -x `pgrep -f jetty` | tail -1 | awk '{print $4}' >> ${RSS_FILE}

  sleep 10

  pmap -x `pgrep -f jetty` | tail -1 | awk '{print $4}' >> ${RSS1_FILE}

  ${JAVA_HOME}jcmd `pgrep -f jetty` System.trim_native_heap > /dev/null 2>&1
  pmap -x `pgrep -f jetty` | tail -1 | awk '{print $4}' >> ${RSS2_FILE}

  # ${JAVA_HOME}jcmd `pgrep -f jetty` Compiler.codecache > ${JCMD_FILE}

  kill $!
  wait $!

  awk '/^- +Code / { i = match($0, "committed=([0-9]+)", a); if (i > 0) print a[1] >>"'${CODE_FILE}'";}'\
'/^Total: / { i = match($0, "committed=([0-9]+)", a); if (i > 0) print a[1] >>"'${TOTAL_FILE}'";}'\
'/Total compiled methods/ { i = match($0, " ([0-9]+) ", a); if (i > 0) print a[1] >>"'${NMCOMPILED_FILE}'";}'\
'/nmethod code size/ { i = match($0, " ([0-9]+) ", a); if (i > 0) print a[1] >>"'${NMCODE_FILE}'";}'\
'/nmethod total size/ { i = match($0, " ([0-9]+) ", a); if (i > 0) print a[1] >>"'${NMTOTAL_FILE}'";}'\
'/Total compilation time/ { i = match($0, " ([0-9]+.[0-9]+) s", a); if (i > 0) print a[1] >>"'${NMTIME_FILE}'";}'\
'/C1 Compile Time/ { i = match($0, " ([0-9]+.[0-9]+) s", a); if (i > 0) print a[1] >>"'${NMC1TIME_FILE}'";}'\
'/C2 Compile Time/ { i = match($0, " ([0-9]+.[0-9]+) s", a); if (i > 0) print a[1] >>"'${NMC2TIME_FILE}'";}'\
'/Bailed out compilation/ { i = match($0, " ([0-9]+.[0-9]+) s", a); if (i > 0) print a[1] >>"'${NMBAILOUT_FILE}'";}' < ${OUT_FILE}

  # awk 'BEGIN {max_used=0;} /max_used/ { i = match($0, "max_used=([0-9]+)Kb", a); if (i > 0) max_used+=a[1]; } END {print max_used;}' < ${JCMD_FILE} >> ${CODECACHE_FILE}
  # awk 'BEGIN {max_used=0;} /total_blobs/ { i = match($0, "nmethods=([0-9]+) ", a); if (i > 0) max_used+=a[1]; } END {print max_used;}' < ${JCMD_FILE} >> ${NMETHODS_FILE}

done

awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("RSS/3s    (pmap   %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, min, max, int(sum / count));}' < ${RSS_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("RSS/10s   (pmap   %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, min, max, int(sum / count));}' < ${RSS1_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("RSS/trim  (pmap   %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, min, max, int(sum / count));}' < ${RSS2_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("Total     (NMT    %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, int(min/1024), int(max/1024), int((sum / count)/1024));}' < ${TOTAL_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("Codecache (NMT    %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, int(min/1024), int(max/1024), int((sum / count)/1024));}' < ${CODE_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("NM time   (CITime %d measurments): min=%.4fs  max=%.4fs  avarage=%.4fs\n", count, min, max, (sum / count));}' < ${NMTIME_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("NM C1time (CITime %d measurments): min=%.4fs  max=%.4fs  avarage=%.4fs\n", count, min, max, (sum / count));}' < ${NMC1TIME_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("NM C2time (CITime %d measurments): min=%.4fs  max=%.4fs  avarage=%.4fs\n", count, min, max, (sum / count));}' < ${NMC2TIME_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("NM bailo. (CITime %d measurments): min=%.4fs  max=%.4fs  avarage=%.4fs\n", count, min, max, (sum / count));}' < ${NMBAILOUT_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("NM total  (CITime %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, int(min/1024), int(max/1024), int((sum / count)/1024));}' < ${NMTOTAL_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("NM code   (CITime %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, int(min/1024), int(max/1024), int((sum / count)/1024));}' < ${NMCODE_FILE}
awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("NM comp.  (CITime %d measurments): min=%6d   max=%6d   avarage=%6d\n", count, int(min), int(max), int((sum / count)));}' < ${NMCOMPILED_FILE}

# awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("Codecache (jcmd   %d measurments): min=%6dkb max=%6dkb avarage=%6dkb\n", count, min, max, int(sum / count));}' < ${CODECACHE_FILE}
# awk 'BEGIN {max=0; sum=0; count=0; min=(2^PREC);} { sum+=$1; if ($1 > max) {max=$1;} if ($1 < min) {min=$1;} count++; } END {printf("Nmethods  (jcmd   %d measurments): min=%6d   max=%6d   avarage=%6d\n", count, min, max, int(sum / count));}' < ${NMETHODS_FILE}
