This *might* help, its a start, needs refinement
#!/usr/bin/perl --
use strict; use warnings;
Main( @ARGV );
exit( 0 );
sub Main {
@_ = $$ if @_ and $_[0] eq 'self' ;
@_ or die "\nUsage: $0 pid\nUsage: $0 self\n";
print mems(@_),"\n";
}
use constant GOTPROC => !!-f "/proc/$$/statm";
use File::Which qw/ which /;
use vars qw' $GOTPS $GOTPSLIST $GOTTASKLIST $GOTMEMSTAT ';
BEGIN {
$GOTPS = which('ps');
$GOTPSLIST = which('pslist.exe');
$GOTTASKLIST = which('tasklist.exe');
$GOTMEMSTAT = which('memstat.exe');
}
sub mems {
die "pid must be a number, not ((($_[0])))" unless $_[0] =~ /^\-?\
+d+\Z/;
GOTPROC and goto &procMem;
$GOTPSLIST and goto &pslistMem;
$GOTTASKLIST and goto &tasklistMem;
#~ $GOTMEMSTAT and goto &memstatMem; # unfinished
$GOTPS and goto &psMem;
die "NO SUITABLE MEMORY FUNCTION FOUND";
}
sub memstatMem {
my( $pid ) = @_;
my $mem = qx{$GOTMEMSTAT -p $pid};
chomp $mem;
return $mem;
}
sub tasklistMem {
my( $pid ) = @_;
#~ /nh no headers
my @lines = qx[$GOTTASKLIST /nh /fi "pid eq $pid"];
chomp @lines;
#~ Image Name PID Session Name Session# Mem U
+sage
#~ ========================= ====== ================ ======== ========
+====
#~ perl.exe 888 0 101,0
+08 K
my %ps;
@ps{'ImageName',
'PID',
'SessionName',
'Session#',
'Mem' } = grep length, split /\s+/, $lines[-1];
return "Mem: $ps{Mem}";
}
sub procMem {
my( $pid ) = @_;
my $mem = `cat /proc/$pid/statm`;
$mem =~ s/^(\d+).*/$1/s;
return "proc{ $mem }";
}
sub psMem {
my( $pid ) = @_;
my @lines = qx{$GOTPS -o vsize,rss -p $pid };
chomp @lines;
foreach my $line (@lines) {
my ( $vsize, $rss) = split /\s+/, $line;
return "VM: $vsize RSS: $rss ";
}
return;
}
sub pslistMem {
my( $pid ) = @_;
my @lines = qx{$GOTPSLIST -m $pid 2>NUL};
chomp @lines;
#~ Name Pid VM WS Priv Priv Pk Faults
+NonP Page
#~ perl 1052 25348 7220 4504 4512 1977
+ 2 33
my %ps;
@ps{'Name',
'Pid',
'VM',
'WS',
'Priv',
'Priv Pk',
'Faults',
'NonP',
'Page' } = grep length, split /\s+/, $lines[-1];
#~ Pri Priority
#~ Thd Number of Threads
#~ Hnd Number of Handles
#~ VM Virtual Memory
#~ WS Working Set ------ "Mem Usage" in taskmanager
#~ Priv Private Virtual Memory ------ "VM Size" in taskman
+ager
#~ Priv Pk Private Virtual Memory Peak
#~ Faults Page Faults
#~ NonP Non-Paged Pool
#~ Page Paged Pool
#~ Cswtch Context Switches
#~ use DDS; Dump(\@lines , \%ps);
return "WVM: $ps{VM} { WS: $ps{WS} VM: $ps{Priv} } ";
}
#~ https://en.wikipedia.org/wiki/Resident_Set_Size
#~ https://en.wikipedia.org/wiki/Working_set_size
#~ https://en.wikipedia.org/wiki/Virtual_memory
|