1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#!/usr/bin/perl -w
#
# an SGI gmemusage(1)-like interface to the process table.
#
# Excluded processes
#
@ExcludedProcesses = ( "/proc/0" , "/proc/2" , "/proc/3" ) ;
%ProcSize = {} ;
%ProcRSS = {} ;
%nProc = {} ;
$TotProcs = 0 ;
$ls = "/bin/ls" ;
#
# Determine if a value is present in an array.
#
sub in
{
$val = shift @_ ;
foreach $i ( @_ )
{
return 1 if $val eq $i ;
}
return 0 ;
}
#
# Extract info about running processes.
#
sub GetInfo
{
%ProcSize = {} ;
%ProcRSS = {} ;
$TotProcs = 0 ;
%nProc = {} ;
foreach $proc ( `$ls -d /proc/[0-9]*` )
{
chop $proc ;
if ( in ( $proc , @ExcludedProcesses ) )
{
next ;
}
$TotProcs++ ;
open ( PROC , "$proc/status" ) or next ;
while ( <PROC> )
{
if ( /^Name:\s(.*)$/ )
{
$ProcName = $1 ;
# Do this to avoid unnecessary warnings from perl. can remove
# later once we zap the -w .
if ( ! defined $ProcSize { $ProcName } )
{
$ProcSize { $ProcName } = $ProcRSS { $ProcName } = 0 ;
}
}
if ( /^VmSize:\s*(.*)\s*kB/ )
{
$ProcSize = $1 ;
}
if ( /^VmRSS:\s*(.*)\s*kB/ )
{
$ProcRSS = $1 ;
}
}
close PROC ;
$ProcSize { $ProcName } += $ProcSize ;
$ProcRSS { $ProcName } += $ProcRSS ;
$nProc { $ProcName }++ ;
}
}
#
# Main()
#
while ( 1 )
{
printf "%-20s%10s%10s%10s\n" , "Name" , "nProcs" , "totSize" , "totRSS" ;
GetInfo ;
foreach $i ( keys %nProc )
{
printf ( "%-20s%10d%10d%10d\n" , $i , $nProc { $i } ,
$ProcSize { $i } , $ProcRSS { $i } ) ;
}
sleep 5 ;
}
exit 0 ;
|