tools/trace-malloc/histogram.pl

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/trace-malloc/histogram.pl	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +#!/usr/bin/perl -w
     1.5 +#
     1.6 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 +
    1.10 +# This program produces a ``class histogram'' of the live objects, one
    1.11 +# line per class, with the total number of objects allocated, and
    1.12 +# total number of bytes attributed to those objects.
    1.13 +
    1.14 +use 5.004;
    1.15 +use strict;
    1.16 +use Getopt::Long;
    1.17 +
    1.18 +# So we can find TraceMalloc.pm
    1.19 +use FindBin;
    1.20 +use lib "$FindBin::Bin";
    1.21 +
    1.22 +use TraceMalloc;
    1.23 +
    1.24 +# Collect program options
    1.25 +$::opt_help = 0;
    1.26 +$::opt_types = "${FindBin::Bin}/types.dat";
    1.27 +
    1.28 +GetOptions("help", "types=s");
    1.29 +
    1.30 +if ($::opt_help) {
    1.31 +    die "usage: histogram.pl [options] <dumpfile>
    1.32 +  --help          Display this message
    1.33 +  --types=<file>  Read type heuristics from <file>";
    1.34 +}
    1.35 +
    1.36 +# Initialize type inference juju from the type file specified by
    1.37 +# ``--types''.
    1.38 +if ($::opt_types) {
    1.39 +    TraceMalloc::init_type_inference($::opt_types);
    1.40 +}
    1.41 +
    1.42 +# Read the dump file, collecting count and size information for each
    1.43 +# object that's detected.
    1.44 +
    1.45 +# This'll hold a record for each class that we detect
    1.46 +$::Classes = { };
    1.47 +
    1.48 +sub collect_objects($) {
    1.49 +    my ($object) = @_;
    1.50 +    my $type = $object->{'type'};
    1.51 +
    1.52 +    my $entry = $::Classes{$type};
    1.53 +    if (! $entry) {
    1.54 +        $entry = $::Classes{$type} = { '#count#' => 0, '#bytes#' => 0 };
    1.55 +    }
    1.56 +
    1.57 +    $entry->{'#count#'} += 1;
    1.58 +    $entry->{'#bytes#'} += $object->{'size'};
    1.59 +}
    1.60 +
    1.61 +TraceMalloc::read(\&collect_objects);
    1.62 +
    1.63 +# Print one line per class, sorted with the classes that accumulated
    1.64 +# the most bytes first.
    1.65 +foreach my $class (sort { $::Classes{$b}->{'#bytes#'} <=> $::Classes{$a}->{'#bytes#'} } keys %::Classes) {
    1.66 +    print "$class $::Classes{$class}->{'#count#'} $::Classes{$class}->{'#bytes#'}\n";
    1.67 +}

mercurial