#! /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 | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$6, \$7, \$11, \$12)}"'
cmd_freebsd_nonsmp='top -u -b all | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$6, \$7, \$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
# grep command at the end removes the first sample from the output.
cmd_darwin='top -l 2 -s0 | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$12, \$11, \$3, \$2)}" | sed "s/\+//g" | egrep  -A 1000 "^1.*launchd"'
cmd_netbsd='top -b -u  | awk "/^ *[0-9]+ /{printf(\"%d %s %s %d %s\\n\", \$1, \$5, \$6, \$10, \$11)}"'

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

unset LINES
unset COLUMNS

echo 0
eval $cmd | awk -f ${helperdir}/to-bytes.awk
echo ~~~
