tools/trace-malloc/histogram.pl

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 #!/usr/bin/perl -w
     2 #
     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>";
    31 }
    33 # Initialize type inference juju from the type file specified by
    34 # ``--types''.
    35 if ($::opt_types) {
    36     TraceMalloc::init_type_inference($::opt_types);
    37 }
    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 };
    52     }
    54     $entry->{'#count#'} += 1;
    55     $entry->{'#bytes#'} += $object->{'size'};
    56 }
    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";
    64 }

mercurial