#!/bin/sh
#
# Copyright (C) 2006 Holger Levsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

if [ "$1" = "config" ]; then
	echo "graph_title VM by Process"
	echo 'graph_args --base 1024k'
	echo 'graph_vlabel VM size'
	echo 'graph_category system'
	echo "graph_info Shows contribution of each process to VM size"

	ps auxww | perl -e '
		$junk = <>;
		while (<>)
		{
			@a = split;
			$proc = $a[10];
			$proc =~ s|.*/||;
			$proc =~ s/:.*//;
			$proc =~ tr/[]//d;
			$proc =~ tr/A-Za-z0-9/_/c;
			$vsz = $a[4];
			$total{$proc} += $vsz;
		}
		my $stack = 0;
		sub draw() { return $stack++ ? "STACK" : "AREA" }
		print map
		{
			"$_.label $_\n" .
			"$_.min 0\n" .
			"$_.draw " . draw() . "\n" .
			"$_.cdef $_,1024,*\n"
		}
		sort keys %total;
		'
	exit 0
else
	ps auxww | perl -e '
		$junk = <>;
		while (<>)
		{
			@a = split;
			$proc = $a[10];
			$proc =~ s|.*/||;
			$proc =~ s/:.*//;
			$proc =~ tr/[]//d;
			$proc =~ tr/A-Za-z0-9/_/c;
			$vsz = $a[4];
			$total{$proc} += $vsz;
		}
		print map {"$_.value $total{$_}\n"} sort keys %total'
fi
