3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # This program produces a ``class histogram'' of the live objects, one
8 # line per class, with the total number of objects allocated, and
9 # total number of bytes attributed to those objects.
15 # So we can find TraceMalloc.pm
17 use lib
"$FindBin::Bin";
21 # Collect program options
23 $::opt_types
= "${FindBin::Bin}/types.dat";
25 GetOptions
("help", "types=s");
28 die "usage: histogram.pl [options] <dumpfile>
29 --help Display this message
30 --types=<file> Read type heuristics from <file>";
33 # Initialize type inference juju from the type file specified by
36 TraceMalloc
::init_type_inference
($::opt_types
);
39 # Read the dump file, collecting count and size information for each
40 # object that's detected.
42 # This'll hold a record for each class that we detect
45 sub collect_objects
($) {
47 my $type = $object->{'type'};
49 my $entry = $::Classes
{$type};
51 $entry = $::Classes
{$type} = { '#count#' => 0, '#bytes#' => 0 };
54 $entry->{'#count#'} += 1;
55 $entry->{'#bytes#'} += $object->{'size'};
58 TraceMalloc
::read(\
&collect_objects
);
60 # Print one line per class, sorted with the classes that accumulated
61 # the most bytes first.
62 foreach my $class (sort { $::Classes
{$b}->{'#bytes#'} <=> $::Classes
{$a}->{'#bytes#'} } keys %::Classes
) {
63 print "$class $::Classes{$class}->{'#count#'} $::Classes{$class}->{'#bytes#'}\n";