DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#!/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/.

#
# Process output of TraceMallocDumpAllocations() to produce a table
# that attributes memory to the allocators using call stack.
#

use 5.004;
use strict;

# A table of all ancestors. Key is function name, value is an
# array of ancestors, each attributed with a number of calls and
# the amount of memory allocated.
my %Ancestors;

# Ibid, for descendants.
my %Descendants;

# A table that keeps the total amount of memory allocated by each
# function
my %Totals;
$Totals{".root"} = { "#memory#" => 0, "#calls#" => 0 };

# A table that maps the long ugly function name to a unique number so
# that the HTML we generate isn't too fat
my %Ids;
my $NextId = 0;

$Ids{".root"} = ++$NextId;


LINE: while (<>) {
    # The line'll look like:
    #
    #  0x4000a008     16  PR_Malloc+16; nsMemoryImpl::Alloc(unsigned int)+12; ...

    # Ignore any lines that don't start with an address
    next LINE unless /^0x/;

    # Parse it
    my ($address, $size, $rest) = /^(0x\S*)\s*(\d+)\s*(.*)$/;
    my @stack = reverse(split /; /, $rest);

    # Accumulate at the root
    $Totals{".root"}->{"#memory#"} += $size;
    ++$Totals{".root"}->{"#calls#"};

    my $caller = ".root";
    foreach my $callee (@stack) {
        # Strip the offset from the callsite information. I don't
        # think we care.
        $callee =~ s/\+\d+$//g;

        # Accumulate the total for the callee
        if (! $Totals{$callee}) {
            $Totals{$callee} = { "#memory#" => 0, "#calls#" => 0 };
        }

        $Totals{$callee}->{"#memory#"} += $size;
        ++$Totals{$callee}->{"#calls#"};

        # Descendants
        my $descendants = $Descendants{$caller};
        if (! $descendants) {
            $descendants = $Descendants{$caller} = [ ];
        }

        # Manage the list of descendants
        {
            my $wasInserted = 0;
          DESCENDANT: foreach my $item (@$descendants) {
                if ($item->{"#name#"} eq $callee) {
                    $item->{"#memory#"} += $size;
                    ++$item->{"#calls#"};
                    $wasInserted = 1;
                    last DESCENDANT;
                }
            }

            if (! $wasInserted) {
                $descendants->[@$descendants] = {
                    "#name#"   => $callee,
                    "#memory#" => $size,
                    "#calls#"  => 1
                };
            }
        }

        # Ancestors
        my $ancestors = $Ancestors{$callee};
        if (! $ancestors) {
            $ancestors = $Ancestors{$callee} = [ ];
        }

        # Manage the list of ancestors
        {
            my $wasInserted = 0;
          ANCESTOR: foreach my $item (@$ancestors) {
                if ($item->{"#name#"} eq $caller) {
                    $item->{"#memory#"} += $size;
                    ++$item->{"#calls#"};
                    $wasInserted = 1;
                    last ANCESTOR;
                }
            }

            if (! $wasInserted) {
                $ancestors->[@$ancestors] = {
                    "#name#"   => $caller,
                    "#memory#" => $size,
                    "#calls#"  => 1
                };
            }
        }

        # Make a new "id", if necessary
        if (! $Ids{$callee}) {
            $Ids{$callee} = ++$NextId;
        }

        # On to the next one...
        $caller = $callee;
    }
}


# Change the manky looking callsite into a pretty function; strip argument
# types and offset information.
sub pretty($) {
    $_ = $_[0];
    s/&/&amp;/g;
    s/</&lt;/g;
    s/>/&gt;/g;

    if (/([^\(]*)(\(.*\))/) {
        return $1 . "()";
    }
    else {
        return $_[0];
    }
}

# Dump a web page!
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n";
print "<html><head>\n";
print "<title>Live Bloat Blame</title>\n";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"blame.css\">\n";
print "</head>\n";
print "<body>\n";

# At most 100 rows per table so as not to kill the browser.
my $maxrows = 100;

print "<table>\n";
print "<thead><tr><td>Function</td><td>Ancestors</td><td>Descendants</td></tr></thead>\n";

foreach my $node (sort(keys(%Ids))) {
    print "<tr>\n";

    # Print the current node
    {
        my ($memory, $calls) =
            ($Totals{$node}->{"#memory#"},
             $Totals{$node}->{"#calls#"});

        my $pretty = pretty($node);
        print "  <td><a name=\"$Ids{$node}\">$pretty&nbsp;$memory&nbsp;($calls)</a></td>\n";
    }

    # Ancestors, sorted descending by amount of memory allocated
    print "  <td>\n";
    my $ancestors = $Ancestors{$node};
    if ($ancestors) {
        foreach my $ancestor (sort { $b->{"#memory#"} <=> $a->{"#memory#"} } @$ancestors) {
            my ($name, $memory, $calls) =
                ($ancestor->{"#name#"},
                 $ancestor->{"#memory#"},
                 $ancestor->{"#calls#"});

            my $pretty = pretty($name);

            print "    <a href=\"#$Ids{$name}\">$pretty</a>&nbsp;$memory&nbsp;($calls)<br>\n";
        }
    }

    print "  </td>\n";

    # Descendants, sorted descending by amount of memory allocated
    print "  <td>\n";
    my $descendants = $Descendants{$node};
    if ($descendants) {
        foreach my $descendant (sort { $b->{"#memory#"} <=> $a->{"#memory#"} } @$descendants) {
            my ($name, $memory, $calls) =
                ($descendant->{"#name#"},
                 $descendant->{"#memory#"},
                 $descendant->{"#calls#"});

            my $pretty = pretty($name);

            print "    <a href=\"#$Ids{$name}\">$pretty</a>&nbsp;$memory&nbsp;($calls)<br>\n";
        }
    }
    print "  </td></tr>\n";

    if (--$maxrows == 0) {
        print "</table>\n";
        print "<table>\n";
        print "<thead><tr><td>Function</td><td>Ancestors</td><td>Descendants</td></tr></thead>\n";
        $maxrows = 100;
    }
}

# Footer
print "</table>\n";
print "</body></html>\n";