nosapp_log_title() {
  echo "NOS CPU User (jiffies), NOS CPU Kernel (jiffies), NOS Total Size (kB), NOS Peak Size (kB), NOS Resident Size (kB), NOS Resident Peak (kB)"
}

nosapp_log() {
  # NOTE1: Testing shows pidof is ~500 times slower than checking a cached pid is valid
  # NOTE2: This makes the *incorrect* assumption that a pid could not be re-used by kernel after NOSApp crashes,
  #        but this is a very low probability event. Could check inside /proc/[pid]/comm, but that slows us down
  #        again....
  if [ ! -e "$NOSAPP_PROCDIR" ]; then
    local app pid
    if app=$(readlink -f /usr/bin/NOSApp) && pid=$(pgrep "$app"); then
      NOSAPP_PROCDIR=/proc/$pid
    fi
  fi

  local MEMPEAK=$(sed -n 's/VmPeak.* \([0-9]*\) .*/\1/p' $NOSAPP_PROCDIR/status)
  local RSSPEAK=$(sed -n 's/VmHWM.* \([0-9]*\) .*/\1/p' $NOSAPP_PROCDIR/status)

  # F14=utime(jiffies) F15=stime(jiffies) F23=vsize(bytes), F24=rss(pages).  Refer "man proc".
  awk '{ print $14 "," $15 "," ($23/1024) ",'"$MEMPEAK"'," ($24*4) ",'"$RSSPEAK"'" }' $NOSAPP_PROCDIR/stat
}
