Bumping manifests a=b2g-bump
[gecko.git] / tools / trace-malloc / histogram.pl
blob9cdb8bcc5995a81eee483025cf4b61fa564480b7
1 #!/usr/bin/perl -w
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.
11 use 5.004;
12 use strict;
13 use Getopt::Long;
15 # So we can find TraceMalloc.pm
16 use FindBin;
17 use lib "$FindBin::Bin";
19 use TraceMalloc;
21 # Collect program options
22 $::opt_help = 0;
23 $::opt_types = "${FindBin::Bin}/types.dat";
25 GetOptions("help", "types=s");
27 if ($::opt_help) {
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
34 # ``--types''.
35 if ($::opt_types) {
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
43 $::Classes = { };
45 sub collect_objects($) {
46 my ($object) = @_;
47 my $type = $object->{'type'};
49 my $entry = $::Classes{$type};
50 if (! $entry) {
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";