ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/scripts/mem_usage.pl
Revision: 24
Committed: Tue Jul 26 21:46:39 2011 UTC (8 years, 1 month ago) by gpertea
File size: 1910 byte(s)
Log Message:
Line File contents
1 #!/usr/bin/perl
2 use strict;
3 use Getopt::Std;
4 use FindBin;use lib $FindBin::Bin;
5
6 my $usage = q/Usage:
7 mem_usage.pl <pid>
8 /;
9 umask 0002;
10 getopts('o:') || die($usage."\n");
11 my $pid=$ARGV[0] || die($usage."\n");
12 my $outfile=$Getopt::Std::opt_o;
13 my %cache;
14 if ($outfile) {
15 open(OUTF, '>'.$outfile) || die("Error creating output file $outfile\n");
16 select(OUTF);
17 }
18 # --
19 my $smaps=getMemUsage($pid);
20
21 sub getMemUsage {
22 my $pid=shift;
23 my $name='/proc/'.$pid.'/smaps';
24 open(my $f, $name) || die("Cannot open $name: $!\n");
25 my $l;
26 my %smaps;
27 my ($vma_start, $vma_end, $perms, $file_off, $dev_major, $dev_minor,
28 $inode, $file_name, $is_deleted);
29 while (defined($l=<$f>)) {
30 if ($l=~/([\da-f]+)-([\da-f]+)\s # range
31 ([r\-])([w\-])([x\-])([sp])\s # access mode
32 ([\da-f]+)\s # page offset in file
33 ([\da-f]+):([\da-f]+)\s # device
34 (\d+)\s* # inode
35 (.*?) # file name
36 (\s\(deleted\))?$
37 /xi ) {
38 $vma_start=hex $1;
39 $vma_end=hex $2;
40 unless (exists($cache{$vma_start."\0".$vma_end})) {
41 $cache{$vma_start."\0".$vma_end}=1;
42 $perms=$3.$4.$5.$6;
43 ($file_off,$dev_major,$dev_minor)=(hex($7), hex($8), hex($9));
44 $inode=$10;
45 $file_name=$11;
46 $is_deleted=defined($12);
47 }
48 }
49 elsif ($l=~/^(\w+):\s*(\d+) kB$/) {
50 my $m=lc $1;
51 $m=~s/\s/_/g;
52 $smaps{$m}+=$2;
53 }
54 } # each line in /proc/<pid>/smaps
55 close($f);
56 return \%smaps;
57 }
58
59 foreach my $k (keys(%$smaps)) {
60 printf('%16s:%9s'."\n",$k,fmtInt($smaps{$k}));
61 }
62 # --
63 if ($outfile) {
64 select(STDOUT);
65 close(OUTF);
66 }
67
68 #************ Subroutines **************
69 sub fmtInt {
70 (my $num = shift) =~ s/\G(\d{1,3})(?=(?:\d\d\d)+(?:\.|$))/$1,/g;
71 return $num;
72 }

Properties

Name Value
svn:executable *