rendered paste body#!/usr/bin/perl -w
use strict;
my $process_id ;
my @processes ;
my $index;
##################################################
# 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;
}
my %mem_location={};
my $mem_address;
while ( 1 )
{
##################################################
# parse each entery
# load into hash of hashs
##################################################
# Example
# %mem_location = {
# "bfb52000-bfb55000 rw-p bfb52000 00:00 0" => {
# Size => "12B",
# Rss => "4",
# Shared_Clean => "0",
# Shared_Dirty => "0",
# Private_Clean => "0",
# Private_Dirty => "4",
# Referenced => "4"
# }
# }
# Dereference:
# $mem_location->"bfb52000-bfb55000 rw-p bfb52000 00:00 0" -> Size;
#
open ( SMAPS, "cat /proc/$process_id/smaps |" )|| die;
while (<SMAPS>){
if ( /^([a-f]|\d+)/ ... /^Referenced.*/){
#if ( /^([a-z]|\d)?.*/)
#{
# chomp ($mem_address="$_");
# print "$mem_address\n";
# }
if( /^([a-f]|\d+)/ ){
chomp ( $index="$_" );
}
elsif ( /^Size:\W+(\d+)\W+kB/ )
{
$mem_location{$index}{Size}="$1";
}
elsif ( /^Rss:\W+(\d+)\W+kB/ )
{
$mem_location{$index}{Rss}="$1";
}
elsif ( /^Shared_Clean:\W+(\d+)\W+kB/ )
{
$mem_location{$index}{Shared_Clean}="$1";
}
elsif ( /^Shared_Dirty:\W+(\d+)\W+kB/ )
{
$mem_location{$index}{Shared_Dirty}="$1";
}
elsif ( /^Private_Clean:\W+(\d+)\W+kB/ )
{
$mem_location{$index}{Private_Clean}="$1";
}
elsif ( /^Private_Dirty:\W+(\d+)\W+kB/ )
{
$mem_location{$index}{Private_Dirty}="$1";
}
elsif ( /^Referenced:\W+(\d+)\W+kB/ )
{
$mem_location{$index}{Referenced}="$1";
}
}
#print "$index\t$mem_location{$index}{Size}\t$mem_location{$index}{Rss}\t$mem_location{$index}{Shared_Clean}\t$mem_location{$index}{Shared_Dirty}\t$mem_location{$index}{Private_Clean}\t$mem_location{$index}{Private_Dirty}\t$mem_location{$index}{Referenced}\n";
}
close(SMAPS) || die;
print "Memory_Segment Perm Size Rss Shar_C Share_D Priv_C Priv_D Referenced\n";
foreach my $key (sort hashValueAscendingNum ( keys %mem_location) )
{
printf "%.22s %7d %5d %7d %7d %6d %7d %7d\n",
$key,
$mem_location{$key}{Size},
$mem_location{$index}{Rss},
$mem_location{$key}{Shared_Clean},
$mem_location{$index}{Shared_Dirty},
$mem_location{$key}{Private_Clean},
$mem_location{$key}{Private_Dirty},
$mem_location{$key}{Referenced};
}
sleep 1;
print " " x 80*25; # clear screen
}
sub hashValueAscendingNum {
$mem_location{$b}{Size} <=> $mem_location{$a}{Size};
}