[EMAIL PROTECTED] writes:

> En op 4 oktober 2002 sprak Michael G Schwern:
> > The problem is when you put those two next to each other, one
> > promising a friendly interface, one a bare-metal interface,
> > it confuses the intent of the module. Is it for Joe End User
> > or is it for Joe Core Hacker?
> 
> A module to report memory usage of any process (not just yourself),
> is do-able: on some Unices you might trample thru /proc file system;
> on Windows, the Performance Registry, and so on. But has it been done?
> I searched CPAN and could not find such a module.

Not a module, but a function which should work on FreeBSD and Linux:

=item currmem([$pid])

=for category System

Return ($mem, $realmem) of the current process or process $pid, if $pid
is given.

=cut

sub currmem {
    my $pid = shift || $$;
    if (open(MAP, "/proc/$pid/map")) { # FreeBSD
        my $mem = 0;
        my $realmem = 0;
        while(<MAP>) {
            my(@l) = split /\s+/;
            my $delta = (hex($l[1])-hex($l[0]));
            $mem += $delta;
            if ($l[11] ne 'vnode') {
                $realmem += $delta;
            }
        }
        close MAP;
        ($mem, $realmem);
    } elsif (open(MAP, "/proc/$pid/maps")) { # Linux
        my $mem = 0;
        my $realmem = 0;
        while(<MAP>) {
            my(@l) = split /\s+/;
            my($start,$end) = split /-/, $l[0];
            my $delta = (hex($end)-hex($start));
            $mem += $delta;
            if (!defined $l[5] || $l[5] eq '') {
                $realmem += $delta;
            }
        }
        close MAP;
        ($mem, $realmem);
    } else {
        undef;
    }
}
__END__

> And, if you were to
> write such a module, what should you call it?
> 
> I would find such a module generally useful in that it gives a sysadmin
> view of a Perl process (is it a memory pig compared to a Java process,
> say). Admittedly, such a module may not be very useful in figuring out
> how much memory a perl data structure is using, but I don't see how
> sbrk is any better in that regard. For a more fine-grained view, you
> need hooks into Perl internals (such as the Perl malloc).
> 

Regards,
        Slaven

-- 
Slaven Rezic - [EMAIL PROTECTED]
    BBBike - route planner for cyclists in Berlin
    WWW version:     http://www.bbbike.de
    Perl/Tk version: http://bbbike.sourceforge.net

Reply via email to