My stab at finding memory used by processes

From: Brett
Date: Mon Oct 06 2003 - 13:59:08 EST


This perl script just goes through /proc/maps, finding inodes numbered with 0 and adds those sizes. Seems to over-report things with our servers, probably because it counts all pages even if they're copy on write, don't know how to get around that.

Any comments are welcome. #!/usr/bin/perl -w
use strict;
use File::Glob ':glob';

my $procregex = qr /^(\S+)-(\S+) \S+ \S+ \S+ (\S+)/;

sub process_mapsfile {
my ($path) = @_;

open(MAPSFD, $path);

my $totalsize = 0;
while(<MAPSFD>)
{
if (!/$procregex/)
{
#print "Couldn't match\n$_\n";
next;
}
else
{
#print "Matched\n$_\n";
}

#print "line = $_";
my $start = int(hex($1));
my $end = int(hex($2));
my $inode = int($3);
#print "inode = $inode, end = $end, start = $start\n";
if($inode == 0)
{
my $size = $end - $start;
$totalsize += $size;
}
}

close(MAPSFD);
return $totalsize;
}


my $totalsize = 0;
my @files = </proc/*>;
foreach my $file (@files)
{
# if we have a pid file, parse maps file
if($file =~ /\/proc\/\d+/)
{
$totalsize += process_mapsfile($file . "/maps");
}
}

print "size = $totalsize\n";