-
Notifications
You must be signed in to change notification settings - Fork 3
/
peakmem.pl
executable file
·87 lines (73 loc) · 1.83 KB
/
peakmem.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl -w
# from http://tstarling.com/blog/2010/06/measuring-memory-usage-with-strace
# changes:
# - show only max mem, and use \r to animate display
# - better x64 detection ... Tim's version uses
# if ( `uname -m` eq 'x86_64' ) {
# to spot x64, but this will always fail since the uname output will
# include a \n
# - watch sub-processes too
my $cmd;
$machine = `uname -m`;
chomp($machine);
if ( $machine eq 'x86_64' ) {
$cmd = 'strace -f -e trace=mmap,munmap,brk ';
} else {
$cmd = 'strace -f -e trace=mmap,mmap2,munmap,brk ';
}
for my $arg (@ARGV) {
$arg =~ s/'/'\\''/g;
$cmd .= " '$arg'";
}
$cmd .= ' 2>&1 >/dev/null';
open( PIPE, "$cmd|" ) or die "Cannot execute command \"$cmd\"\n";
my $currentSize = 0;
my $maxSize = 0;
my %maps;
my %topOfData;
my ($addr, $length, $prot, $flags, $fd, $pgoffset);
my $newTop;
my $v;
my $error = '';
# turn on output flushing after every write
$|++;
while ( <PIPE> ) {
$pid = 0;
if ( /^\[pid (\d+)\] / ) {
$pid = $1;
}
if ( /mmap2?\((.*)\) = (\w+)/ ) {
$v = $pid . $2;
@params = split( /, ?/, $1 );
($addr, $length, $prot, $flags, $fd, $pgoffset) = @params;
if ( $addr eq 'NULL' && $fd == -1 ) {
$maps{$v} = $length;
$currentSize += $length;
}
} elsif ( /munmap\((\w+),/ ) {
$v = $pid . $1;
if ( defined( $maps{$v} ) ) {
$currentSize -= $maps{$v};
undef $maps{$v};
}
} elsif ( /brk\((\w+)\)\s*= (\w+)/ ) {
$newTop = hex( $2 );
if ( hex( $1 ) == 0 or !defined( $topOfData{$pid} ) ) {
$topOfData{$pid} = $newTop;
} else {
$currentSize += $newTop - $topOfData{$pid};
$topOfData{$pid} = $newTop;
}
} else {
$error .= $_;
}
if ( int( ( $currentSize - $maxSize ) / 1048576 ) > 0 ) {
$maxSize = $currentSize;
printf( "\r%d MB", $maxSize / 1048576 );
}
}
printf( "\r%d MB \n", $maxSize / 1048576 );
close( PIPE );
if ( $? ) {
print $error;
}