#!/bin/sh

# Print the size (in bytes) of the heap of the given
# process(es).  If no process (by name or number) is
# given, leos will be targeted.

LIST=${@:-leos saos}
for ITEM in $LIST; do
    case $ITEM in
	[0-9]* ) PIDLIST=$ITEM;;
	*      ) PIDLIST=`pidof $ITEM`;;
    esac
    for PID in $PIDLIST; do
	if [ -d /proc/$PID ] ; then
	    HS=`sed -n -e s/^/0x/ -e 's/-/ 0x/' -e /[[]heap[]]/p \
		</proc/$PID/maps | awk '{print $2 - $1}'`
	    LINK=$(readlink /proc/$PID/exe || root readlink /proc/$PID/exe)
	    PROG=$(basename $LINK)
	    echo "$PROG($PID): $HS"
	fi
    done
done
