3
# Copyright (C) 2007 Apple Inc. All rights reserved.
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
9
# 1. Redistributions of source code must retain the above copyright
10
# notice, this list of conditions and the following disclaimer.
11
# 2. Redistributions in binary form must reproduce the above copyright
12
# notice, this list of conditions and the following disclaimer in the
13
# documentation and/or other materials provided with the distribution.
14
# 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15
# its contributors may be used to endorse or promote products derived
16
# from this software without specific prior written permission.
18
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
# Parses the callstacks in a file with malloc_history formatted content, sorting
30
# based on total number of bytes allocated, and filtering based on command-line
44
"Usage: " . basename($0) . " [options] malloc_history.txt\n" .
45
" --grep-regexp Include only call stacks that match this regular expression.\n" .
46
" --byte-minimum Include only call stacks with allocation sizes >= this value.\n" .
47
" --merge-regexp Merge all call stacks that match this regular expression.\n" .
48
" --merge-depth Merge all call stacks that match at this stack depth and above.\n";
52
my @mergeRegexps = ();
54
my $getOptionsResult = GetOptions(
55
"grep-regexp:s" => \$grepRegexp,
56
"byte-minimum:i" => \$byteMinimum,
57
"merge-regexp:s" => \@mergeRegexps,
58
"merge-depth:i" => \$mergeDepth
60
die $usage if (!$getOptionsResult || !scalar(@ARGV));
63
foreach my $fileName (@ARGV) {
64
open FILE, "<$fileName" or die "bad file: $fileName";
70
my $byteCountTotal = 0;
72
for (my $i = 0; $i < @lines; $i++) {
73
my $line = $lines[$i];
74
my ($callCount, $byteCount);
76
# First try malloc_history format
77
# 6 calls for 664 bytes thread_ffffffff |0x0 | start
78
($callCount, $byteCount) = ($line =~ /(\d+) calls for (\d+) bytes/);
80
# Then try leaks format
81
# Leak: 0x0ac3ca40 size=48
82
# 0x00020001 0x00000001 0x00000000 0x00000000 ................
83
# Call stack: [thread ffffffff]: | 0x0 | start
84
if (!$callCount || !$byteCount) {
86
($byteCount) = ($line =~ /Leak: [x[:xdigit:]]* size=(\d+)/);
89
while (!($line =~ "Call stack: ")) {
96
# Then try LeakFinder format
97
# --------------- Key: 213813, 84 bytes ---------
98
# c:\cygwin\home\buildbot\webkit\opensource\webcore\rendering\renderarena.cpp(78): WebCore::RenderArena::allocate
99
# c:\cygwin\home\buildbot\webkit\opensource\webcore\rendering\renderobject.cpp(82): WebCore::RenderObject::operator new
100
if (!$callCount || !$byteCount) {
102
($byteCount) = ($line =~ /Key: (?:\d+), (\d+) bytes/);
104
$line = $lines[++$i];
106
while ($lines[$i+1] !~ /^(?:-|\d)/) {
107
if ($line =~ /\): (.*)$/) {
110
unshift(@tempStack, $call);
112
$line = $lines[++$i];
114
$line = join(" | ", @tempStack);
119
next if (!$callCount || !$byteCount);
121
$byteCountTotal += $byteCount;
123
next if ($grepRegexp && !($line =~ $grepRegexp));
125
my $callstackBegin = 0;
127
# count stack frames backwards from end of callstack
128
$callstackBegin = length($line);
129
for (my $pipeCount = 0; $pipeCount < $mergeDepth; $pipeCount++) {
130
my $rindexResult = rindex($line, "|", $callstackBegin - 1);
131
last if $rindexResult == -1;
132
$callstackBegin = $rindexResult;
135
# start at beginning of callstack
136
$callstackBegin = index($line, "|");
139
my $callstack = substr($line, $callstackBegin + 2); # + 2 skips "| "
140
for my $regexp (@mergeRegexps) {
141
if ($callstack =~ $regexp) {
142
$callstack = $regexp . "\n";
147
if (!$callstacks{$callstack}) {
148
$callstacks{$callstack} = {"callCount" => 0, "byteCount" => 0};
151
$callstacks{$callstack}{"callCount"} += $callCount;
152
$callstacks{$callstack}{"byteCount"} += $byteCount;
155
my $byteCountTotalReported = 0;
156
for my $callstack (sort { $callstacks{$b}{"byteCount"} <=> $callstacks{$a}{"byteCount"} } keys %callstacks) {
157
my $callCount = $callstacks{$callstack}{"callCount"};
158
my $byteCount = $callstacks{$callstack}{"byteCount"};
159
last if ($byteMinimum && $byteCount < $byteMinimum);
161
$byteCountTotalReported += $byteCount;
162
print commify($callCount) . " calls for " . commify($byteCount) . " bytes: $callstack\n";
165
print "total: " . commify($byteCountTotalReported) . " bytes (" . commify($byteCountTotal - $byteCountTotalReported) . " bytes excluded).\n";
171
# Copied from perldoc -- please excuse the style
175
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;