Impressum bloattable.pl
Sprache: Shell
|
|
#!/usr/bin/perl -w
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# bloattable [-debug] [-source] [-byte n|-obj n|-ref n] <file1> <file2> ... <filen> > <html-file>
#
# file1, file2, ... filen should be successive BloatView files generated from the same run.
# Summarize them in an HTML table. Output the HTML to the standard output.
#
# If -debug is set, create a slightly larger html file which is more suitable for debugging this script.
# If -source is set, create an html file that prints the html source as the output
# If -byte n, -obj n, or -ref n is given, make the page default to showing byte, object, or reference statistics,
# respectively, and sort by the nth column (n is zero-based, so the first column has n==0).
#
# See http://lxr.mozilla.org/mozilla/source/xpcom/doc/MemoryTools.html
use 5.004;
use strict;
use diagnostics;
use File::Basename;
use Getopt::Long;
# The generated HTML is almost entirely generated by a script. Only the <HTML>, <HEAD>, and <BODY> elements are explicit
# because a <SCRIPT> element cannot officially be a direct descendant of an <HTML> element.
# The script itself is almost all generated by an eval of a large string. This allows the script to reproduce itself
# when making a new page using document.write's. Re-sorting the page causes it to regenerate itself in this way.
# Return the file's modification date.
sub fileModDate($) {
my ($pathName) = @_;
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) =
stat $pathName or die "Can't stat '$pathName'";
return $mtime;
}
sub fileCoreName($) {
my ($pathName) = @_;
my $fileName = basename($pathName, "");
$fileName =~ s/\..*//;
return $fileName;
}
# Convert a raw string into a single-quoted JavaScript string.
sub singleQuoteString($) {
local ($_) = @_;
s/\\/\\\\/g;
s/'/\\'/g;
s/\n/\\n/g;
s/<\//<\\\//g;
return "'$_'";
}
# Convert a raw string into a double-quoted JavaScript string.
sub doubleQuoteString($) {
local ($_) = @_;
s/\\/\\\\/g;
s/"/\\"/g;
s/\n/\\n/g;
s/<\//<\\\//g;
return "\"$_\"";
}
# Quote special HTML characters in the string.
sub quoteHTML($) {
local ($_) = @_;
s/\&/&/g;
s/</</g;
s/>/>/g;
s/ / /g;
s/\n/<BR>\n/g;
return $_;
}
# Write the generated page to the standard output.
# The script source code is read from this file past the __END__ marker
# @$scriptData is the JavaScript source for the tables passed to JavaScript. Each entry is one line of JavaScript.
# @$persistentScriptData is the same as @scriptData, but persists when the page reloads itself.
# If $debug is true, generate the script directly instead of having it eval itself.
# If $source is true, generate a script that displays the page's source instead of the page itself.
sub generate(\@\@$$$$) {
my ($scriptData, $persistentScriptData, $debug, $source, $showMode, $sortColumn) = @_;
my @scriptSource = <DATA>;
chomp @scriptSource;
print <<'EOS';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<SCRIPT type="text/javascript">
EOS
foreach (@$scriptData) {print "$_\n";}
print "\n";
print "var srcArray = [\n";
my @quotedScriptSource = map {
my $line = $_;
$line =~ s/^\s+//g;
# $line =~ s/^\/\/SOURCE\s+//g if $source;
$line =~ s/^\/\/.*//g;
$line =~ s/\s+$//g;
$line eq "" ? () : $line
} @$persistentScriptData, @scriptSource;
my $lastQuotedLine = pop @quotedScriptSource;
foreach (@quotedScriptSource) {print doubleQuoteString($_), ",\n";}
print doubleQuoteString($lastQuotedLine), "];\n\n";
if ($debug) {
push @quotedScriptSource, $lastQuotedLine;
foreach (@quotedScriptSource) {
s/<\//<\\\//g; # This fails if a regexp ends with a '<'. Oh well....
print "$_\n";
}
print "\n";
} else {
print "eval(srcArray.join(\"\\n\"));\n\n";
}
print "showMode = $showMode;\n";
print "sortColumn = $sortColumn;\n";
if ($source) {
print <<'EOS';
function writeQuotedHTML(s) {
document.write(quoteHTML(s.toString()).replace(/\n/g, ' \n'));
}
var quotingDocument = {
write: function () {
for (var i = 0; i < arguments.length; i++)
writeQuotedHTML(arguments[i]);
},
writeln: function () {
for (var i = 0; i < arguments.length; i++)
writeQuotedHTML(arguments[i]);
document.writeln(' ');
}
};
EOS
} else {
print "showHead(document);\n";
}
print "\n";
print "\n\n";
print "\n";
if ($source) {
print "" ;
print quoteHTML "-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n";
print quoteHTML "\n";
print quoteHTML "\n";
print "\n";
print quoteHTML "\n\n";
print quoteHTML "\n";
print "\n";
print quoteHTML "\n";
print quoteHTML "\n";
print "\n";
} else {
print "\n";
}
print "\n";
print " | |