|
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/. |
|
6 |
|
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. |
|
10 |
|
11 use 5.004; |
|
12 use strict; |
|
13 use Getopt::Long; |
|
14 |
|
15 # So we can find TraceMalloc.pm |
|
16 use FindBin; |
|
17 use lib "$FindBin::Bin"; |
|
18 |
|
19 use TraceMalloc; |
|
20 |
|
21 # Collect program options |
|
22 $::opt_help = 0; |
|
23 $::opt_types = "${FindBin::Bin}/types.dat"; |
|
24 |
|
25 GetOptions("help", "types=s"); |
|
26 |
|
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 } |
|
32 |
|
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 } |
|
38 |
|
39 # Read the dump file, collecting count and size information for each |
|
40 # object that's detected. |
|
41 |
|
42 # This'll hold a record for each class that we detect |
|
43 $::Classes = { }; |
|
44 |
|
45 sub collect_objects($) { |
|
46 my ($object) = @_; |
|
47 my $type = $object->{'type'}; |
|
48 |
|
49 my $entry = $::Classes{$type}; |
|
50 if (! $entry) { |
|
51 $entry = $::Classes{$type} = { '#count#' => 0, '#bytes#' => 0 }; |
|
52 } |
|
53 |
|
54 $entry->{'#count#'} += 1; |
|
55 $entry->{'#bytes#'} += $object->{'size'}; |
|
56 } |
|
57 |
|
58 TraceMalloc::read(\&collect_objects); |
|
59 |
|
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 } |