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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#!/usr/bin/perl
#
# Blame callstacks for each memory allocation.
# Similar to memprof.pl, will also try to filter out unuseful stacks.
# TODO: better describe how these tools differ.
#
# Usage:
#
# memtrace.pl <logfile>
#
# logfile -- The memwatcher.logXXXX file to summarize.
#
#
#
# Sample output:
#
# 41,975,368 77.64% f:\sp\vctools\crt_bld\self_x86\crt\src\malloc.c (163): malloc
# 2,097,152 3.88% c:\src\chrome1\src\webkit\pending\frameloader.cpp (3300): WebCore::FrameLoader::committedLoad
# 1,572,864 2.91% c:\src\chrome1\src\webkit\port\bridge\v8bridge.cpp (214): WebCore::V8Bridge::evaluate
# 1,572,864 2.91% c:\src\chrome1\src\webkit\glue\webframeloaderclient_impl.cc (1071): WebFrameLoaderClient::committedLoad
# 1,572,864 2.91% c:\src\chrome1\src\v8\src\ast.h (1181): v8::internal::Visitor::Visit
#
#
#
sub process_raw($) {
my $file = shift;
my %leaks = ();
my $location_bytes = 0;
my $location_hits = 0;
my $location_blame = "";
my $location_last = "";
my $contains_load_lib = 0;
my $total_bytes = 0;
open (LOGFILE, "$file") or die("could not open $file");
while(<LOGFILE>) {
my $line = $_;
#print "$line";
chomp($line);
if ($line =~ m/([0-9]*) bytes, ([0-9]*) items/) {
#print "START\n";
# Dump "prior" frame here
if ($location_bytes > 0) {
#print("GOTLEAK: $location_bytes ($location_hits) $location_blame\n");
if ($location_blame eq "") {
$location_blame = $location_last;
}
if (!$contains_load_lib) {
$leaks{$location_blame} += $location_bytes;
}
$location_bytes = 0;
$location_blame = "";
$contains_load_lib = 0;
}
#print("stackframe " . $1 . ", " . $2 . "\n");
$location_hits = $2;
$location_bytes = $1;
}
elsif ($line =~ m/Total Bytes:[ ]*([0-9]*)/) {
$total_bytes += $1;
}
elsif ($line =~ m/LoadLibrary/) {
# skip these, they contain false positives.
$contains_load_lib = 1;
next;
}
elsif ($line =~ m/=============/) {
next;
}
elsif ($line =~ m/Untracking untracked/) {
next;
}
elsif ($line =~ m/[ ]*([a-z]:\\[a-z]*\\[a-zA-Z_\\0-9\.]*) /) {
my $filename = $1;
if ($filename =~ m/memory_watcher/) {
next;
}
if ($filename =~ m/skmemory_stdlib.cpp/) {
next;
}
if ($filename =~ m/stringimpl.cpp/) {
next;
}
if ($filename =~ m/stringbuffer.h/) {
next;
}
if ($filename =~ m/fastmalloc.h/) {
next;
}
if ($filename =~ m/microsoft visual studio 8/) {
next;
}
if ($filename =~ m/platformsdk_win2008_6_1/) {
next;
}
if ($location_blame eq "") {
# use this to blame the line
$location_blame = $line;
# use this to blame the file.
# $location_blame = $filename;
#print("blaming $location_blame\n");
}
} else {
# print("junk: " . $line . "\n");
if (! ($line =~ m/GetModuleFileNameA/) ) {
$location_last = $line;
}
}
}
# now dump our hash table
my $sum = 0;
my @keys = sort { $leaks{$b} <=> $leaks{$a} }keys %leaks;
for ($i=0; $i<@keys; $i++) {
my $key = @keys[$i];
if (0 == $total_bytes) { $total_bytes = 1; }
printf "%11s\t%3.2f%%\t%s\n", comma_print($leaks{$key}), (100* $leaks{$key} / $total_bytes), $key;
$sum += $leaks{$key};
}
printf("TOTAL: %s\n", comma_print($sum));
}
# Insert commas into an integer after each three digits for printing.
sub comma_print {
my $num = "$_[0]";
$num =~ s/(\d{1,3}?)(?=(\d{3})+$)/$1,/g;
return $num;
}
# ----- Main ------------------------------------------------
# Get the command line argument
my $filename = shift;
# Process the file.
process_raw($filename);
|