Hi Monks!

This small snippet is just an example of what your mighty
/proc filesystem in Linux can do for you. They report the
vsize (allocated virtual memory) of your perl process inclusive
all data it allocated and the number of pages your process
uses.

Find out the VMEM (virtual memory) size your current perl
process has grabbed, find out the number of pages it uses:
open( STAT , "</proc/$$/stat" ) or die "Unable to open stat file"; @stat = split /\s+/ , <STAT>; close( STAT ); print sprintf "Vsize: %3.2f MB\n", $stat[22]/1048576; print "RSS : $stat[23] pages\n";
Of course you may just get the byte number for vsize and so
on. These values help me a lot to prevent the process from
trashing (watching these values relatively to /proc/meminfo).
There is more to this /proc/your_proc_id/stat thing. Just
check out 'man 5 proc'.

Replies are listed 'Best First'.
Re: /proc Gimmicks (Linux specific)
by Zaxo (Archbishop) on Jul 16, 2001 at 05:08 UTC

    /proc/$$/ can be replaced by the fixed path /proc/self/ . The self entry is a rather magical symlink.

    Additionally, /proc/self/status is a human-readable form of stat which gives names to the data, all set to make a hash from. Here's the command line version:

    $ perl -e 'open STATUS, "< /proc/self/status"; my $status = {map {split /:\s*/} <STATUS>}; close(STATUS);print "$_ => $status->{$_}" for keys %$status' Groups => 501 101 102 SigCgt => 0000000000000000 SigIgn => 8000000000000000 State => R (running) Name => perl Uid => 501 501 501 501 CapPrm => 0000000000000000 CapEff => 0000000000000000 SigPnd => 0000000000000000 VmExe => 488 kB FDSize => 256 PPid => 1341 CapInh => 0000000000000000 Gid => 501 501 501 501 VmStk => 20 kB VmLib => 1240 kB VmRSS => 1148 kB SigBlk => 0000000000000000 Pid => 10847 VmLck => 0 kB VmData => 316 kB TracerPid => 0 VmSize => 2280 kB

    There are lots of treasures in Linux /proc . That makes ordinary filesystem access a viable alternative to the Proc:: family of CPAN modules.

    After Compline,
    Zaxo

Re: /proc Gimmicks (Linux specific)
by premchai21 (Curate) on Jul 16, 2001 at 13:26 UTC
    One rather rude thing root can do through /proc is usurp any process's STDOUT. I tried it on my box, opening up two gnome-terminals, one su'd, one not. Found out the PID of the non-su'd bash (189), then used the su'd one + /proc to get the fd for the STDOUT of the bash, using stty and a Perl script to put keypresses in immediately (the Perl was to map backspaces correctly). So I started typing (slowly, for effect):
    blueroom:/proc/189/fd# perl del-to-bs > 1 You have violated Money Guzzler Corp.'s TOS. Your session will be terminated in four seconds. Four... Three... Two... One... ^D blueroom:/proc/189/fd# cd / blueroom:/# kill -KILL 189 blueroom:/# deluser hosed
    ... and it worked too. I can only wonder what would happen if you injected random data into a pipe...