All pastes #760527 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-11-04 02:14 UTC
rendered paste body
#!/usr/bin/perl 

#use strict;

my $process_id ; 
my @processes ;

##################################################
# Was this program called with an arguement
# Arguements can be a process id or program name
##################################################
if ( (@ARGV == "1" ) && ( $ARGV[0]) =~ m/^\d+$/ )
{
  #
  # we received a process id.  
  #
  chomp ( $process_id=$ARGV[0] );
  print "Analyzing PID: $process_id\n";
} 
elsif ( (@ARGV == "1" ) && ( $ARGV[0] =~ m/^\w+$/ ) )
{
  print "received program name\n";
  #
  # find process id
  #
  chomp $ARGV[0];
  print "Checking for $ARGV[0]\n";
  open( PROCESSES, "pidof $ARGV[0] |" ) || die "No proces found: $!\n";
  @processes=split(" ",<PROCESSES>);
  #
  # Check if more than one process matches
  #
  if ( scalar (@processes) > 1 )
  {
    print "Matches processes: @processes\n";
    print "Which process is the one to watch?\t";
    #
    # Check if they entered one process ID
    #
    if ( scalar ( split(" ",<PROCESSES>) ) > 1 )
    {
      print scalar (<STDIN>);
      print "You aren't getting it.\n**ONE process can be watched**\nExiting\n";      
      exit;
    } else {
      chomp ( $process_id=<STDIN> );
      print "Analyzing PID: $process_id\n";
    }
  } else {
     chomp ( $process_id=$processes[0] );
     print "Analyzing PID: $process_id\n";
  }
}
else 
{
   print "usage: smaps_top.pl [PID|program]\n";
   exit;
}
##################################################
# Check for process
##################################################
if ( -e "/proc/$process_id" )
{
  print "Found: /proc/$process_id\n";
} else {
  print "Not Found: /proc/$process_id\n";
  exit;
}
##################################################
# parse each entery
# load into hash of hashs
##################################################
# Example
# %mem_location = {
#    "bfb52000-bfb55000 rw-p bfb52000 00:00 0" => {
#        Size          => "12 kB",              
#        Rss           => "4 kB",
#        Shared_Clean  => "0 kB",
#        Shared_Dirty  => "0 kB",
#        Private_Clean => "0 kB",
#        Private_Dirty => "4 kB",
#        Referenced    => "4 kB"
#     }
# }
# Dereference:
# $mem_location->"bfb52000-bfb55000 rw-p bfb52000 00:00 0" -> Size;
#
my %mem_location={};
my $mem_address;
open ( SMAPS, "cat /proc/$process_id/smaps |" )|| die; 
while (<SMAPS>){ 
  if ( /^([a-z]|\d)/ ... /^Referenced.*/){ 
    if ( /^([a-z]|\d)?.*/)
    {
       chomp ($mem_address="$_"); 
       print "$mem_address\n";
    } 
    elsif ( /^Size:/ )
    {
      print "size\n";
    }
    elsif ( /^Rss:/ )
    {
    }
    elsif ( /^Shared_Clean:/ )
    {
    }
    elsif ( /^Shared_Dirty:/ )
    {
    }
    elsif ( /^Private_Clean:/ )
    {
    }
    elsif ( /^Private_Dirty:/ )
    {
    }
    elsif ( /^Referenced:/ )
    {
    }
  } 
  
}
close(SMAPS) || die;