"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "gmemusage-0.2/testmem.pl" of archive gmemusage-0.2.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 #!/usr/bin/perl -w
    2 #
    3 # an SGI gmemusage(1)-like interface to the process table.
    4 #
    5 # Excluded processes
    6 #
    7 @ExcludedProcesses = ( "/proc/0" , "/proc/2" , "/proc/3" ) ;
    8 %ProcSize = {} ;
    9 %ProcRSS = {} ;
   10 %nProc = {} ;
   11 $TotProcs = 0 ;
   12 $ls = "/bin/ls" ;
   13 #
   14 # Determine if a value is present in an array.
   15 #
   16 sub in
   17 {
   18     $val = shift @_ ;
   19     foreach $i ( @_ )
   20     {
   21 	return 1 if $val eq $i ;
   22     }
   23     return 0 ;
   24 }
   25 #
   26 # Extract info about running processes.
   27 #
   28 sub GetInfo
   29 {
   30     %ProcSize = {} ;
   31     %ProcRSS = {} ;
   32     $TotProcs = 0 ;
   33     %nProc = {} ;
   34     foreach $proc ( `$ls -d /proc/[0-9]*` )
   35     {
   36 	chop $proc ;
   37 	if ( in ( $proc , @ExcludedProcesses ) )
   38 	{
   39 	    next ;
   40 	}
   41 	$TotProcs++ ;
   42 	open ( PROC , "$proc/status" ) or next ;
   43 	while ( <PROC> )
   44 	{
   45 	    if ( /^Name:\s(.*)$/ )
   46 	    {
   47 		$ProcName = $1 ;
   48 		# Do this to avoid unnecessary warnings from perl. can remove
   49 		# later once we zap the -w .
   50 		if ( ! defined $ProcSize { $ProcName } )
   51 		{
   52 		    $ProcSize { $ProcName } = $ProcRSS { $ProcName } = 0 ;
   53 		}
   54 	    }
   55 	    if ( /^VmSize:\s*(.*)\s*kB/ )
   56 	    {
   57 		$ProcSize = $1 ;
   58 	    }
   59 	    if ( /^VmRSS:\s*(.*)\s*kB/ )
   60 	    {
   61 		$ProcRSS = $1 ;
   62 	    }
   63 	}
   64 	close PROC ;
   65 	$ProcSize { $ProcName } += $ProcSize ;
   66 	$ProcRSS { $ProcName } += $ProcRSS ;
   67 	$nProc { $ProcName }++ ;
   68     }
   69 }
   70 #
   71 # Main()
   72 #
   73 while ( 1 )
   74 {
   75     printf "%-20s%10s%10s%10s\n" , "Name" , "nProcs" , "totSize" , "totRSS" ;
   76     GetInfo ;
   77     foreach $i ( keys %nProc )
   78     {
   79 	printf ( "%-20s%10d%10d%10d\n" , $i , $nProc { $i } ,
   80 		$ProcSize { $i } , $ProcRSS { $i } ) ;
   81     }
   82     sleep 5 ;
   83 }
   84 exit 0 ;