1 | #! /usr/bin/env perl |
---|
2 | use strict; |
---|
3 | use File::Basename; |
---|
4 | |
---|
5 | my $total_size = 0; |
---|
6 | my %roots; |
---|
7 | my @all_nodes; |
---|
8 | foreach my $line ( <STDIN> ) { |
---|
9 | chomp($line); |
---|
10 | my @parts = split(/\t/, $line); |
---|
11 | |
---|
12 | my $op = shift(@parts); |
---|
13 | my $state = shift(@parts); |
---|
14 | |
---|
15 | # Only dealing with allocations here... |
---|
16 | if ($op !~ /^M:/) { |
---|
17 | next; |
---|
18 | } |
---|
19 | |
---|
20 | my ($size, $ptr) = ($op =~ /^M: (\d+) 0x(\w+)/); |
---|
21 | my ($time, $maxrss, $tid) = split(/\s+/, $state); |
---|
22 | |
---|
23 | $total_size += $size; |
---|
24 | |
---|
25 | sub level_parts($) { |
---|
26 | my $level = @_[0]; |
---|
27 | my ($file_name, $proc_name, $off, $pc, $relpc) = |
---|
28 | ($level =~ /^(.*) \((.*)\+0x(\w+)\) \[0x(\w+) \(0x(\w+)\)\]/); |
---|
29 | |
---|
30 | return ($file_name, $proc_name, $off, $pc, $relpc); |
---|
31 | } |
---|
32 | |
---|
33 | # Put the top of the stack first. |
---|
34 | @parts = reverse(@parts); |
---|
35 | |
---|
36 | my $parent = \%roots; |
---|
37 | for (my $i = 0; $i < scalar(@parts); ++$i) { |
---|
38 | my $level = $parts[$i]; |
---|
39 | my ($file_name, $proc_name, $off, $pc, $relpc) = level_parts($level); |
---|
40 | |
---|
41 | # Skip this level if we don't even know from what file it came. |
---|
42 | if ($file_name eq '?') { |
---|
43 | next; |
---|
44 | } |
---|
45 | |
---|
46 | # print STDERR "parsed: $file_name, $proc_name, $off, $pc, $relpc\n"; |
---|
47 | |
---|
48 | if (!exists $parent->{$pc}) { |
---|
49 | $parent->{$pc}->{'file_name'} = $file_name; |
---|
50 | $parent->{$pc}->{'proc_name'} = $proc_name; |
---|
51 | $parent->{$pc}->{'off'} = $off; |
---|
52 | $parent->{$pc}->{'pc'} = $pc; |
---|
53 | $parent->{$pc}->{'relpc'} = $relpc; |
---|
54 | |
---|
55 | push(@all_nodes, $parent->{$pc}); |
---|
56 | } |
---|
57 | |
---|
58 | $parent->{$pc}->{'size'} += $size; |
---|
59 | |
---|
60 | my ($next_file_name, $next_proc_name, $next_off, $next_pc, $next_relpc); |
---|
61 | if ($i < scalar(@parts)-1) { |
---|
62 | my $next_level = $parts[$i+1]; |
---|
63 | ($next_file_name, $next_proc_name, $next_off, $next_pc, $next_relpc) = |
---|
64 | level_parts($next_level); |
---|
65 | $parent->{$pc}->{'child_sizes'}->{$next_pc} += $size; |
---|
66 | } |
---|
67 | |
---|
68 | if (!exists $parent->{'children'}) { |
---|
69 | $parent->{'children'} = {}; |
---|
70 | } |
---|
71 | |
---|
72 | $parent = $parent->{'children'}; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | sub format_bytes($) { |
---|
77 | my @sizes = qw( B KB MB GB TB PB ); |
---|
78 | my $size = $_[0]; |
---|
79 | |
---|
80 | my $i = 0; |
---|
81 | while ($size > 1024) { |
---|
82 | $size /= 1024; |
---|
83 | ++$i; |
---|
84 | } |
---|
85 | |
---|
86 | return sprintf("%.3f$sizes[$i]", $size); |
---|
87 | } |
---|
88 | |
---|
89 | printf("digraph \"memlog %s\" {\n", format_bytes($total_size)); |
---|
90 | print("size=\"8,11\"\n"); |
---|
91 | print("node [width=0.375,height=0.25];\n"); |
---|
92 | |
---|
93 | my %cached_names; |
---|
94 | sub get_name($) { |
---|
95 | my $node = $_[0]; |
---|
96 | my $pc = $node->{'pc'}; |
---|
97 | |
---|
98 | if (exists $cached_names{$pc}) { |
---|
99 | return $cached_names{$pc}; |
---|
100 | } |
---|
101 | |
---|
102 | my $ret = ''; |
---|
103 | |
---|
104 | # Prefer the relative offset (that is what we want for shared libraries), but |
---|
105 | # if is not available, use the full offset (which is what we want for the |
---|
106 | # base executable). |
---|
107 | my $exe_off = $node->{'relpc'}; |
---|
108 | if (!$exe_off) { |
---|
109 | $exe_off = $pc; |
---|
110 | } |
---|
111 | |
---|
112 | my $file_name = $node->{'file_name'}; |
---|
113 | my ($func, $loc) = `addr2line -e $file_name -f 0x$exe_off`; |
---|
114 | chomp($func); |
---|
115 | chomp($loc); |
---|
116 | |
---|
117 | if ($func !~ /^\?/) { |
---|
118 | $func =~ s/.*\.//; |
---|
119 | $func = `c++filt '$func'`; |
---|
120 | chomp($func); |
---|
121 | |
---|
122 | $ret .= $func . '\n' . $loc . '\n'; |
---|
123 | } |
---|
124 | |
---|
125 | $ret .= basename($node->{'file_name'}); |
---|
126 | if ($node->{'proc_name'} ne '?') { |
---|
127 | $ret .= ' (' . $node->{'proc_name'} . '+0x' . $node->{'off'} . ')'; |
---|
128 | } |
---|
129 | |
---|
130 | $cached_names{$pc} = $ret; |
---|
131 | return $ret; |
---|
132 | } |
---|
133 | |
---|
134 | foreach my $node (@all_nodes) { |
---|
135 | my $name = get_name($node); |
---|
136 | |
---|
137 | my $local_size = $node->{'size'}; |
---|
138 | |
---|
139 | my $fs = 8.0; |
---|
140 | if ($local_size > 0) { |
---|
141 | $fs = 50.0 * sqrt(abs($local_size * 1.0 / $total_size)); |
---|
142 | } |
---|
143 | |
---|
144 | printf("N%s [label=\"%s\\n%s\", shape=box,fontsize=%.1f%s];\n", |
---|
145 | $node->{'pc'}, $name, format_bytes($local_size), $fs); |
---|
146 | } |
---|
147 | |
---|
148 | foreach my $node (@all_nodes) { |
---|
149 | my $local_size = $node->{'size'}; |
---|
150 | |
---|
151 | foreach my $cpc (keys %{$node->{'child_sizes'}}) { |
---|
152 | my $child_size = $node->{'child_sizes'}->{$cpc}; |
---|
153 | my $frac = $child_size * 1.0 / $local_size; |
---|
154 | |
---|
155 | my $weight = 100.0 * $frac; |
---|
156 | my $style = sprintf("setlinewidth(%f)", 3.0 * $frac); |
---|
157 | |
---|
158 | printf("N%s -> N%s [label=\"%s\", weight=%d, style=\"%s\"];\n", |
---|
159 | $node->{'pc'}, $cpc, format_bytes($child_size), $weight, $style); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | print("}\n"); |
---|
164 | |
---|