Skip to content

Instantly share code, notes, and snippets.

@mutsune
Created December 6, 2018 03:42
Show Gist options
  • Save mutsune/dcd2929bf1059dc10949be40eac92036 to your computer and use it in GitHub Desktop.
Save mutsune/dcd2929bf1059dc10949be40eac92036 to your computer and use it in GitHub Desktop.
Number of GC events and GC time **per day**
#!/usr/bin/env bash
set -e
# Java process ID
PID="$(pgrep java)"
# convert `ps -o etime` output format to second
function psetime_converter() {
echo "$(cat -)" | awk '
BEGIN { FS = ":" }
{
if (NF == 2) {
print $1*60 + $2
} else if (NF == 3) {
split($1, a, "-");
if (a[2] != "" ) {
print ((a[1]*24+a[2])*60 + $2) * 60 + $3;
} else {
print ($1*60 + $2) * 60 + $3;
}
}
}
'
}
# get running time
RUNNING_TIME="$(ps -o etime= -p "${PID}" | psetime_converter)"
RUNNING_TIME_DAY="$(echo "${RUNNING_TIME} / (60 * 60 * 24)" | bc -l)"
# extract jstat statistics
## YGC: Minor GC (New generation) count
## YGCT: Minor GC (New generation) time
## FGC: Full GC (Old generation) count
## FGCT: Full GC (Old generation) time
## GCT: All GC time
JSTAT_RESULT="$(sudo jstat -gcutil "${PID}")"
echo "${JSTAT_RESULT}"
IFS=" " read -r -a JSTAT_GC_VALUES <<< "$(echo "${JSTAT_RESULT}" | tail -n 1 | awk '{print $7,$8,$9,$10,$11}')"
# formatting
function average_stat() {
echo "$1 ${RUNNING_TIME_DAY}" | awk '{print sprintf("%.2f", $1 / $2)}'
}
# print statistics
echo "Averaged GC info"
echo -e "YGC:\t $(average_stat "${JSTAT_GC_VALUES[0]}")"
echo -e "YGCT:\t $(average_stat "${JSTAT_GC_VALUES[1]}")"
echo -e "FGC:\t $(average_stat "${JSTAT_GC_VALUES[2]}")"
echo -e "FGCT:\t $(average_stat "${JSTAT_GC_VALUES[3]}")"
echo -e "GCT:\t $(average_stat "${JSTAT_GC_VALUES[4]}")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment