3 # ***** BEGIN LICENSE BLOCK *****
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 # The contents of this file are subject to the Mozilla Public License Version
7 # 1.1 (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
9 # http://www.mozilla.org/MPL/
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 # for the specific language governing rights and limitations under the
16 # The Original Code is Waldemar's Perl Utilities.
18 # The Initial Developer of the Original Code is
19 # Netscape Communications Corporation.
20 # Portions created by the Initial Developer are Copyright (C) 2000
21 # the Initial Developer. All Rights Reserved.
24 # Waldemar Horwat <waldemar@acm.org>
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
38 # ***** END LICENSE BLOCK *****
40 # bloattable [-debug] [-source] [-byte n|-obj n|-ref n] <file1> <file2> ... <filen> > <html-file>
42 # file1, file2, ... filen should be successive BloatView files generated from the same run.
43 # Summarize them in an HTML table. Output the HTML to the standard output.
45 # If -debug is set, create a slightly larger html file which is more suitable for debugging this script.
46 # If -source is set, create an html file that prints the html source as the output
47 # If -byte n, -obj n, or -ref n is given, make the page default to showing byte, object, or reference statistics,
48 # respectively, and sort by the nth column (n is zero-based, so the first column has n==0).
50 # See http://lxr.mozilla.org/mozilla/source/xpcom/doc/MemoryTools.html
58 # The generated HTML is almost entirely generated by a script. Only the <HTML>, <HEAD>, and <BODY> elements are explicit
59 # because a <SCRIPT> element cannot officially be a direct descendant of an <HTML> element.
60 # The script itself is almost all generated by an eval of a large string. This allows the script to reproduce itself
61 # when making a new page using document.write's. Re-sorting the page causes it to regenerate itself in this way.
65 # Return the file's modification date.
68 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) =
69 stat $pathName or die "Can't stat '$pathName'";
76 my $fileName = basename
($pathName, "");
77 $fileName =~ s/\..*//;
82 # Convert a raw string into a single-quoted JavaScript string.
83 sub singleQuoteString
($) {
93 # Convert a raw string into a double-quoted JavaScript string.
94 sub doubleQuoteString
($) {
104 # Quote special HTML characters in the string.
116 # Write the generated page to the standard output.
117 # The script source code is read from this file past the __END__ marker
118 # @$scriptData is the JavaScript source for the tables passed to JavaScript. Each entry is one line of JavaScript.
119 # @$persistentScriptData is the same as @scriptData, but persists when the page reloads itself.
120 # If $debug is true, generate the script directly instead of having it eval itself.
121 # If $source is true, generate a script that displays the page's source instead of the page itself.
122 sub generate
(\@\@
$$$$) {
123 my ($scriptData, $persistentScriptData, $debug, $source, $showMode, $sortColumn) = @_;
125 my @scriptSource = <DATA
>;
128 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
131 <SCRIPT type="text/javascript">
134 foreach (@
$scriptData) {print "$_\n";}
137 print "var srcArray = [\n";
138 my @quotedScriptSource = map {
141 # $line =~ s/^\/\/SOURCE\s+//g if $source;
142 $line =~ s/^\/\/.*//g;
144 $line eq "" ?
() : $line
145 } @
$persistentScriptData, @scriptSource;
146 my $lastQuotedLine = pop @quotedScriptSource;
147 foreach (@quotedScriptSource) {print doubleQuoteString
($_), ",\n";}
148 print doubleQuoteString
($lastQuotedLine), "];\n\n";
151 push @quotedScriptSource, $lastQuotedLine;
152 foreach (@quotedScriptSource) {
153 s/<\//<\\\
//g; # This fails if a regexp ends with a '<'. Oh well....
158 print "eval(srcArray.join(\"\\n\"));\n\n";
160 print "showMode = $showMode;\n";
161 print "sortColumn = $sortColumn;\n";
164 function writeQuotedHTML(s) {
165 document.write(quoteHTML(s.toString()).replace(/\n/g, '<BR>\n'));
168 var quotingDocument = {
170 for (var i = 0; i < arguments.length; i++)
171 writeQuotedHTML(arguments[i]);
173 writeln: function () {
174 for (var i = 0; i < arguments.length; i++)
175 writeQuotedHTML(arguments[i]);
176 document.writeln('<BR>');
181 print "showHead(document);\n";
188 print quoteHTML
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n";
189 print quoteHTML
"<HTML>\n";
190 print quoteHTML
"<HEAD>\n";
191 print "<SCRIPT type=\"text/javascript\">showHead(quotingDocument);</SCRIPT>\n";
192 print quoteHTML
"</HEAD>\n\n";
193 print quoteHTML
"<BODY>\n";
194 print "<SCRIPT type=\"text/javascript\">showBody(quotingDocument);</SCRIPT>\n";
195 print quoteHTML
"</BODY>\n";
196 print quoteHTML
"</HTML>\n";
199 print "<SCRIPT type=\"text/javascript\">showBody(document);</SCRIPT>\n";
207 # Read the bloat file into hash table $h. The hash table is indexed by class names;
208 # each entry is a list with the following elements:
209 # bytesAlloc Total number of bytes allocated
210 # bytesNet Total number of bytes allocated but not deallocated
211 # objectsAlloc Total number of objects allocated
212 # objectsNet Total number of objects allocated but not deallocated
213 # refsAlloc Total number of references AddRef'd
214 # refsNet Total number of references AddRef'd but not Released
215 # Except for TOTAL, all hash table entries refer to mutually exclusive data.
216 # $sizes is a hash table indexed by class names. Each entry of that table contains the class's instance size.
217 sub readBloatFile
($\
%\
%) {
218 my ($file, $h, $sizes) = @_;
219 local $_; # Needed for 'while (<FILE>)' below.
221 my $readSomething = 0;
224 if (my ($name, $size, $bytesNet, $objectsAlloc, $objectsNet, $refsAlloc, $refsNet) =
225 /^\s*(?:\d+)\s+([\w:]+)\s+(\d+)\s+(-?\d+)\s+(\d+)\s+(-?\d+)\s*\([^()]*\)\s*(\d+)\s+(-?\d+)\s*\([^()]*\)\s*$/) {
227 if ($name eq "TOTAL") {
229 $bytesAlloc = "undefined";
231 $bytesAlloc = $objectsAlloc * $size;
232 if ($bytesNet != $objectsNet * $size) {
233 print STDERR
"In '$file', class $name bytesNet != objectsNet * size: $bytesNet != $objectsNet * $size\n";
236 print STDERR
"Duplicate entry $name in '$file'\n" if $$h{$name};
237 $$h{$name} = [$bytesAlloc, $bytesNet, $objectsAlloc, $objectsNet, $refsAlloc, $refsNet];
239 my $oldSize = $$sizes{$name};
240 print STDERR
"Mismatch of sizes of class $name: $oldSize and $size\n" if defined($oldSize) && $size ne $oldSize;
241 $$sizes{$name} = $size;
243 } elsif (/^\s*(?:\d+)\s+([\w:]+)\s/) {
244 print STDERR
"Unable to parse '$file' line: $_";
248 print STDERR
"No data in '$file'\n" unless $readSomething;
253 my %sizes; # <class-name> => <instance-size>
254 my %tables; # <file-name> => <bloat-table>; see readBloatFile for format of <bloat-table>
256 # Generate the JavaScript source code for the row named $c. $l can contain the initial entries of the row.
257 sub genTableRowSource
($$) {
261 my $e = $tables{$_}{$c};
262 if (defined($lastE) && !defined($e)) {
264 print STDERR
"Class $c is defined in an earlier file but not in '$_'\n";
267 if (defined $lastE) {
268 for (my $i = 0; $i <= $#$e; $i++) {
270 $l .= ($n eq "undefined" ?
"undefined" : $n - $$lastE[$i]) . ",";
274 $l .= join(",", @
$e) . ", ";
278 $l .= "0,0,0,0,0,0, ";
281 $l .= join(",", @
$lastE);
293 GetOptions
("debug" => \
$debug, "source" => \
$source, "byte=i" => \
$modeOptions[0], "obj=i" => \
$modeOptions[1], "ref=i" => \
$modeOptions[2]);
294 for (my $i = 0; $i != 3; $i++) {
295 my $modeOption = $modeOptions[$i];
297 die "Only one of -byte, -obj, or -ref may be given" if defined $showMode;
298 my $nFileColumns = scalar(@ARGV) + 1;
299 die "-byte, -obj, or -ref column number out of range" if $modeOption < 0 || $modeOption >= 2 + 2*$nFileColumns;
301 if ($modeOption >= 2) {
303 $sortColumn = 2 + $showMode*2;
304 if ($modeOption >= $nFileColumns) {
306 $modeOption -= $nFileColumns;
308 $sortColumn += $modeOption*6;
310 $sortColumn = $modeOption;
314 unless (defined $showMode) {
319 # Read all of the bloat files.
321 unless ($tables{$_}) {
325 readBloatFile
$_, %table, %sizes;
326 $tables{$_} = \
%table;
329 die "No input" unless %sizes;
331 my @scriptData; # JavaScript source for the tables passed to JavaScript. Each entry is one line of JavaScript.
332 my @persistentScriptData; # Same as @scriptData, but persists the page reloads itself.
334 # Print a list of bloat file names.
335 push @persistentScriptData, "var nFiles = " . scalar(@ARGV) . ";";
336 push @persistentScriptData, "var fileTags = [" . join(", ", map {singleQuoteString
substr(fileCoreName
($_), -10)} @ARGV) . "];";
337 push @persistentScriptData, "var fileNames = [" . join(", ", map {singleQuoteString
$_} @ARGV) . "];";
338 push @persistentScriptData, "var fileDates = [" . join(", ", map {singleQuoteString
localtime fileModDate
$_} @ARGV) . "];";
340 # Print the bloat tables.
341 push @persistentScriptData, "var totals = " . genTableRowSource
('"TOTAL", undefined, ', "TOTAL") . ";";
342 push @scriptData, "var classTables = [";
343 delete $sizes{"TOTAL"};
344 my @classes = sort(keys %sizes);
345 for (my $i = 0; $i <= $#classes; $i++) {
346 my $c = $classes[$i];
347 push @scriptData, genTableRowSource
(doubleQuoteString
($c).", ".$sizes{$c}.", ", $c) . ($i == $#classes ?
"];" : ",");
350 generate
(@scriptData, @persistentScriptData, $debug, $source, $showMode, $sortColumn);
354 # The source of the eval'd JavaScript follows.
355 # Comments starting with // that are alone on a line are stripped by the Perl script.
358 // showMode
: 0=bytes
, 1=objects
, 2=references
365 // Sort according to the sortColumn
. Column
0 is sorted alphabetically
in ascending order
.
366 // All other columns are sorted numerically
in descending order
, with column
0 used
for a secondary
sort.
367 // Undefined is always listed
last.
368 function sortCompare
(x
, y
) {
370 var xc
= x
[sortColumn
];
371 var yc
= y
[sortColumn
];
372 if (xc
< yc
|| xc
=== undefined
&& yc
!== undefined
) return 1;
373 if (yc
< xc
|| yc
=== undefined
&& xc
!== undefined
) return -1;
378 if (x0
> y0
|| x0
=== undefined
&& y0
!== undefined
) return 1;
379 if (y0
> x0
|| y0
=== undefined
&& x0
!== undefined
) return -1;
384 // Quote special HTML characters
in the string
.
385 function quoteHTML
(s
) {
386 s
= s
.replace
(/&/g, '&');
387 // Can
't use /</g because HTML interprets '</g
' as ending the script!
388 s = s.replace(/\x3C/g, '<');
389 s = s.replace(/>/g, '>');
390 s = s.replace(/ /g, ' 
;');
395 function writeFileTable(d) {
396 d.writeln('<TABLE border
=1 cellspacing
=1 cellpadding
=0>');
397 d.writeln('<TR
>\n<TH
>Name
</TH>\n<TH>File</TH
>\n<TH
>Date
</TH>\n</TR
>');
398 for (var i = 0; i < nFiles; i++)
399 d.writeln('<TR
>\n<TD
>'+quoteHTML(fileTags[i])+'</TD>\n<TD><TT>'+quoteHTML(fileNames[i])+'</TT
></TD>\n<TD>'+quoteHTML(fileDates[i])+'</TD
>\n</TR
>');
400 d.writeln('</TABLE
>');
404 function writeReloadLink(d, column, s, rowspan) {
405 d.write(rowspan == 1 ? '<TH
>' : '<TH rowspan
='+rowspan+'>');
406 if (column != sortColumn)
407 d.write('<A href
="javascript:reloadSelf('+column+','+showMode+')">');
409 if (column != sortColumn)
414 function writeClassTableRow(d, row, base, modeName) {
416 d.writeln('<TR
>\n<TH
>'+modeName+'</TH
>');
418 d.writeln('<TR
>\n<TD
><A href
="javascript:showRowDetail(\''+row[0]+'\')">'+quoteHTML(row[0])+'</A></TD
>');
420 d.writeln('<TD
class=num
>'+(v === undefined ? '' : v)+'</TD
>');
422 for (var i = 0; i != 2; i++) {
424 for (var j = 0; j <= nFiles; j++) {
433 d.writeln('<TD
class='+style+'>'+(v === undefined ? '' : v)+'</TD
>');
440 function writeClassTable(d) {
441 var base = 2 + showMode*2;
443 // Make a copy because a sort is destructive.
444 var table = classTables.concat();
445 table.sort(sortCompare);
447 d.writeln('<TABLE border
=1 cellspacing
=1 cellpadding
=0>');
450 writeReloadLink(d, 0, 'Class Name
', 2);
451 writeReloadLink(d, 1, 'Instance
<BR
>Size
', 2);
452 d.writeln('<TH colspan
='+(nFiles+1)+'>'+modeNameUpper+'s allocated
</TH
>');
453 d.writeln('<TH colspan
='+(nFiles+1)+'>'+modeNameUpper+'s allocated but
not freed
</TH>\n</TR
>');
455 for (var i = 0; i != 2; i++) {
457 for (var j = 0; j <= nFiles; j++) {
458 writeReloadLink(d, c, j == nFiles ? 'Total
' : quoteHTML(fileTags[j]), 1);
464 writeClassTableRow(d, totals, base, 0);
465 for (var r = 0; r < table.length; r++)
466 writeClassTableRow(d, table[r], base, 0);
468 d.writeln('</TABLE
>');
472 var modeNames = ["byte", "object", "reference"];
473 var modeNamesUpper = ["Byte", "Object", "Reference"];
474 var styleSheet = '<STYLE type
="TEXT/CSS">\n'+
475 'BODY
{background
-color
: #FFFFFF; color: #000000}\n'+
476 '.num {text-align: right}\n'+
477 '.pos {text-align: right; color: #CC0000}\n'+
478 '.neg {text-align: right; color: #009900}\n'+
482 function showHead
(d
) {
483 modeName
= modeNames
[showMode
];
484 modeNameUpper
= modeNamesUpper
[showMode
];
485 d
.writeln
('<TITLE>'+modeNameUpper
+' Bloats</TITLE>');
486 d
.writeln
(styleSheet
);
489 function showBody
(d
) {
490 d
.writeln
('<H1>'+modeNameUpper
+' Bloats</H1>');
493 for (var i
= 0; i
!= 3; i
++)
495 var newSortColumn
= sortColumn
;
497 newSortColumn
= sortColumn
+ (i
-showMode
)*2;
498 d
.write('<INPUT type="button" value="Show '+modeNamesUpper
[i
]+'s" onClick="reloadSelf('+newSortColumn
+','+i
+')">');
500 d
.writeln
('</FORM>');
501 d
.writeln
('<P>The numbers do not include <CODE>malloc</CODE>\'d data such as string contents.</P>');
502 d
.writeln
('<P>Click on a column heading to sort by that column. Click on a class name to see details for that class.</P>');
507 function showRowDetail
(rowName
) {
511 if (rowName
== "TOTAL")
514 for (i
= 0; i
< classTables
.length; i
++)
515 if (rowName
== classTables
[i
][0]) {
516 row
= classTables
[i
];
521 var w
= window
.open("", "ClassTableRowDetails");
524 d
.writeln
('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">');
525 d
.writeln
('<HTML>\n<HEAD>\n<TITLE>'+quoteHTML
(rowName
)+' bloat details</TITLE>');
526 d
.writeln
(styleSheet
);
527 d
.writeln
('</HEAD>\n\n<BODY>');
528 d
.writeln
('<H2>'+quoteHTML
(rowName
)+'</H2>');
529 if (row
[1] !== undefined
)
530 d
.writeln
('<P>Each instance has '+row
[1]+' bytes.</P>');
532 d
.writeln
('<TABLE border=1 cellspacing=1 cellpadding=0>');
533 d
.writeln
('<TR>\n<TH></TH>\n<TH colspan='+(nFiles
+1)+'>Allocated</TH>');
534 d
.writeln
('<TH colspan='+(nFiles
+1)+'>Allocated but not freed</TH>\n</TR>');
535 d
.writeln
('<TR>\n<TH></TH>');
536 for (i
= 0; i
!= 2; i
++)
537 for (var j
= 0; j
<= nFiles
; j
++)
538 d
.writeln
('<TH>'+(j
== nFiles ?
'Total' : quoteHTML
(fileTags
[j
]))+'</TH>');
541 for (i
= 0; i
!= 3; i
++)
542 writeClassTableRow
(d
, row
, 2+i
*2, modeNamesUpper
[i
]+'s');
544 d
.writeln
('</TABLE>\n</BODY>\n</HTML>');
551 function stringSource
(s
) {
552 s
= s
.replace
(/\\/g, '\\\\');
553 s
= s
.replace
(/"/g, '\\"');
554 s
= s
.replace
(/<\//g, '<\\/');
558 function reloadSelf(n,m) {
559 // Need to cache these because globals go away on document.open().
561 var ss = stringSource;
562 var ct = classTables;
566 // Uncomment this and comment the document.open() line above to see the reloaded page's source
.
567 //var w
= window
.open("", "NewDoc");
568 //var d
= w
.document
;
569 //var document
= new Object
;
570 //document
.write = function
() {
571 // for (var i
= 0; i
< arguments
.length; i
++) {
572 // var s
= arguments
[i
].toString
();
573 // s
= s
.replace
(/&/g, '&');
574 // s
= s
.replace
(/\x3C/g, '<');
575 // s
= s
.replace
(/>/g, '>');
576 // s
= s
.replace
(/ /g, ' ');
580 //document
.writeln
= function
() {
581 // for (var i
= 0; i
< arguments
.length; i
++) {
582 // var s
= arguments
[i
].toString
();
583 // s
= s
.replace
(/&/g, '&');
584 // s
= s
.replace
(/\x3C/g, '<');
585 // s
= s
.replace
(/>/g, '>');
586 // s
= s
.replace
(/ /g, ' ');
589 // d
.writeln
('<BR>');
592 document
.writeln
('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">');
593 document
.writeln
('<HTML>\n<HEAD>\n<SCRIPT type="text/javascript">');
595 // Manually copy non
-persistent script data
597 document
.writeln
('var classTables = [];');
599 document
.writeln
('var classTables = [');
600 for (i
= 0; i
< ct
.length; i
++) {
602 document
.write('[' + ss
(row
[0]));
603 for (var j
= 1; j
< row
.length; j
++)
604 document
.write(',' + row
[j
]);
605 document
.writeln
(']' + (i
== ct
.length-1 ?
'];' : ','));
609 document
.writeln
('var srcArray = [');
610 for (i
= 0; i
< sa
.length; i
++) {
611 document
.write(ss
(sa
[i
]));
612 if (i
!= sa
.length-1)
613 document
.writeln
(',');
615 document
.writeln
('];');
616 document
.writeln
('eval(srcArray.join("\\n"));');
617 document
.writeln
('showMode = '+m
+';');
618 document
.writeln
('sortColumn = '+n
+';');
619 document
.writeln
('showHead(document);');
620 document
.writeln
('</SCRIPT>\n</HEAD>\n\n<BODY>\n<SCRIPT type="text/javascript">showBody(document);</SCRIPT>\n</BODY>\n</HTML>');