#! /usr/bin/env bash
#
#  top 
#
#  Outputs one line per active process as follows:
# 
#           <pid> <vsize bytes> <rss in bytes> <%cpu> <cmdline>

. `dirname $0`/../broctl-config.sh

cmd_linux='top -b -n 1 | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$5, \$6, \$9, \$12)}"'
cmd_freebsd='top -u -b all | tail -n +5 | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$6, \$7, \$11, \$12)}"'
cmd_freebsd_nonsmp='top -u -b all | tail -n +5 | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$6, \$7, \$10, \$11)}"'
cmd_openbsd='top -u -b all | tail -n +5 | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$5, \$6, \$10, \$11)}"'
cmd_netbsd='top -u -b all | tail -n +5 | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$5, \$6, \$10, \$11)}"'

# top on Mac OS X is different.  It doesn't give CPU utilization until the
# second sample so we are getting two samples with zero delay between them.
# The awk command removes the first sample from the output and converts the
# CPU percentages to integers.  The sed command removes the trailing + or -
# signs that sometimes occur with the memory statistics.
cmd_darwin='top -l 2 -s0 -stats pid,vprvt,rsize,cpu,command | awk -v c=0 "{ if(\$1 == \"PID\") c++; else if(c>1) printf(\"%d %s %s %d %s\n\", \$1, \$2, \$3, \$4, \$5)}" | sed "s/[+-] / /g"'

eval cmd="\$cmd_${os}"

if [ "${os}" = "freebsd" ]; then
   # Top's output looks different on non-SMP FreeBSD machines.
   top -u -b all | grep -q "STATE  *C  *TIME" || cmd="$cmd_freebsd_nonsmp"
fi

if [ -z "$cmd" ]; then
    echo "unrecognized os: ${os}" >&2
    exit 1
fi

unset LINES
unset COLUMNS

eval $cmd | awk -v start_field=2 -v end_field=3 -v def_factor=1024 -f "${helperdir}/to-bytes.awk"

