| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use v5.14; |
| 4 |
use warnings; |
| 5 |
use feature qw( say ); |
| 6 |
use utf8; |
| 7 |
|
| 8 |
use Devel::MAT; |
| 9 |
use Devel::MAT::Cmd::Terminal; |
| 10 |
use List::Util qw( any ); |
| 11 |
use List::UtilsBy qw( nsort_by ); |
| 12 |
|
| 13 |
# We're drawing pretty graphs with line drawing |
| 14 |
STDOUT->binmode( ":encoding(UTF-8)" ); |
| 15 |
|
| 16 |
my $progress = ( -t STDERR ) ? |
| 17 |
sub { print STDERR "\r\e[K" . ( shift // "" ); } : |
| 18 |
undef; |
| 19 |
|
| 20 |
my $pmatA = Devel::MAT->load( my $fileA = ( $ARGV[0] // die "Need dumpfile A\n" ), |
| 21 |
progress => $progress, |
| 22 |
); |
| 23 |
my $pmatB = Devel::MAT->load( my $fileB = ( $ARGV[1] // die "Need dumpfile B\n" ), |
| 24 |
progress => $progress, |
| 25 |
); |
| 26 |
|
| 27 |
$progress->( "Sorting,.." ) if $progress; |
| 28 |
|
| 29 |
my @svsA = nsort_by { $_->addr } $pmatA->dumpfile->heap; |
| 30 |
my @svsB = nsort_by { $_->addr } $pmatB->dumpfile->heap; |
| 31 |
|
| 32 |
$progress->() if $progress; |
| 33 |
|
| 34 |
my $countC = 0; |
| 35 |
|
| 36 |
my @onlyA; |
| 37 |
my @onlyB; |
| 38 |
|
| 39 |
while( @svsA && @svsB ) { |
| 40 |
my $svA = $svsA[0]; |
| 41 |
my $svB = $svsB[0]; |
| 42 |
|
| 43 |
my $addrA = $svA->addr; |
| 44 |
my $addrB = $svB->addr; |
| 45 |
|
| 46 |
if( $addrA < $addrB ) { |
| 47 |
push @onlyA, $svA; |
| 48 |
shift @svsA; |
| 49 |
} |
| 50 |
elsif( $addrB < $addrA ) { |
| 51 |
push @onlyB, $svB; |
| 52 |
shift @svsB; |
| 53 |
} |
| 54 |
else { |
| 55 |
# common - no print |
| 56 |
$countC++; |
| 57 |
shift @svsA; |
| 58 |
shift @svsB; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
push @onlyA, @svsA; |
| 63 |
push @onlyB, @svsB; |
| 64 |
|
| 65 |
my %notesA; |
| 66 |
my %notesB; |
| 67 |
|
| 68 |
sub add_notes |
| 69 |
{ |
| 70 |
my ( $svs, $notes, $pmat ) = @_; |
| 71 |
my %addrs = map { $_->addr => 1 } @$svs; |
| 72 |
|
| 73 |
foreach my $sv ( $pmat->dumpfile->heap ) { |
| 74 |
next unless $sv->type eq "STASH"; |
| 75 |
my $stash = $sv; |
| 76 |
|
| 77 |
foreach my $field (qw( mro_isa mro_linearcurrent )) { |
| 78 |
my $sv = $stash->$field or next; |
| 79 |
$addrs{ $sv->addr } or next; |
| 80 |
|
| 81 |
$notes->{ $sv->addr } = "$field of " . Devel::MAT::Cmd->format_symbol( $stash->stashname, $stash ); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
add_notes \@onlyA, \%notesA, $pmatA; |
| 87 |
add_notes \@onlyB, \%notesB, $pmatB; |
| 88 |
|
| 89 |
sub svtrees_from_set |
| 90 |
{ |
| 91 |
my @svs = @_; |
| 92 |
|
| 93 |
# In general the set of SVs and their cross-linkages are not yet suitable |
| 94 |
# to print in a simple tree, because of cycles and multiple paths. We have |
| 95 |
# to reduce the linkages down to something more well-behaved. |
| 96 |
|
| 97 |
my %svs_by_addr = map { $_->addr => $_ } @svs; |
| 98 |
|
| 99 |
my %sv_outrefs; # {$addr} => [other svs here that it refers to] |
| 100 |
foreach my $sv ( @svs ) { |
| 101 |
$sv_outrefs{ $sv->addr } = []; |
| 102 |
|
| 103 |
foreach my $ref ( $sv->outrefs ) { |
| 104 |
next unless $svs_by_addr{ $ref->sv->addr }; |
| 105 |
push $sv_outrefs{ $sv->addr }->@*, $ref->sv; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
my %sv_trees; # {$addr} => [$sv, other SV trees it refers to] |
| 110 |
my %seen; # {$addr} => bool |
| 111 |
my %toplevel; # {$addr} => bool |
| 112 |
|
| 113 |
foreach my $origsv ( @svs ) { |
| 114 |
my @queue = $origsv; |
| 115 |
while( @queue ) { |
| 116 |
my $sv = shift @queue; |
| 117 |
my $addr = $sv->addr; |
| 118 |
|
| 119 |
if( !$sv_trees{ $addr } ) { |
| 120 |
$toplevel{ $addr }++; |
| 121 |
} |
| 122 |
|
| 123 |
$seen{ $addr }++; |
| 124 |
|
| 125 |
my $node = $sv_trees{ $addr } //= [ $sv ]; |
| 126 |
|
| 127 |
my @new_outrefs = grep { !$seen{ $_->addr }++ } $sv_outrefs{ $addr }->@*; |
| 128 |
|
| 129 |
foreach my $outref ( nsort_by { $_->addr } @new_outrefs ) { |
| 130 |
push @queue, $outref; |
| 131 |
push $node->@*, $sv_trees{ $outref->addr } //= [ $outref ]; |
| 132 |
|
| 133 |
delete $toplevel{ $outref->addr }; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return @sv_trees{ sort { $a <=> $b } keys %toplevel }; |
| 139 |
} |
| 140 |
|
| 141 |
our $Indent = ""; |
| 142 |
sub print_svtree |
| 143 |
{ |
| 144 |
my ( $tree, $leader0, $leader1, $notes ) = @_; |
| 145 |
my ( $sv, @subtrees ) = @$tree; |
| 146 |
|
| 147 |
my $note = $notes->{ $sv->addr } ? " (" . $notes->{ $sv->addr } . ")" : ""; |
| 148 |
|
| 149 |
Devel::MAT::Cmd->printf( " %s%s%s%s\n", |
| 150 |
$Indent, |
| 151 |
$leader0, |
| 152 |
Devel::MAT::Cmd->format_sv( $sv ), |
| 153 |
$note, |
| 154 |
); |
| 155 |
|
| 156 |
return unless @subtrees; |
| 157 |
|
| 158 |
local $Indent = "$Indent$leader1"; |
| 159 |
|
| 160 |
my $final_subtree = pop @subtrees; |
| 161 |
{ |
| 162 |
print_svtree( $_, "├─ ", "│ ", $notes ) for @subtrees; |
| 163 |
} |
| 164 |
{ |
| 165 |
print_svtree( $final_subtree, "└─ ", " ", $notes ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
print "\n"; |
| 170 |
printf "%d unique to %s:\n", scalar @onlyA, $fileA; |
| 171 |
my @treesA = svtrees_from_set @onlyA; |
| 172 |
print_svtree $_, "- ", " ", \%notesA for @treesA; |
| 173 |
|
| 174 |
print "\n"; |
| 175 |
printf "%d unique to %s:\n", scalar @onlyB, $fileB; |
| 176 |
my @treesB = svtrees_from_set @onlyB; |
| 177 |
print_svtree $_, "+ ", " ", \%notesB for @treesB; |
| 178 |
|
| 179 |
print "\n"; |
| 180 |
printf "%d common\n", $countC; |
| 181 |
|
| 182 |
=head1 NAME |
| 183 |
|
| 184 |
pmat-diff - print a list of SVs unique to each of two given files |
| 185 |
|
| 186 |
=head1 SYNOPSIS |
| 187 |
|
| 188 |
$ pmat-diff test-1.pmat test-1-after.pmat |
| 189 |
|
| 190 |
=head1 DESCRIPTION |
| 191 |
|
| 192 |
Given two F<.pmat> files, compares them and prints a list of SVs unique to |
| 193 |
each, and a count of those found common to both. This is only useful if the |
| 194 |
two files were generated by the same process, usually at similar times, such |
| 195 |
as either side of a memory leak test as created by C<Test::MemoryGrowth>. |
| 196 |
|
| 197 |
For each file, the SVs unique to it are gathered up into a forest of trees by |
| 198 |
reference, because in typical usage patterns it usually ends up that several |
| 199 |
SVs are all referenced by one container. It often helps when tracking down |
| 200 |
memory leaks to focus on those outer containers, rather than the inner SVs |
| 201 |
they contain. |
| 202 |
|
| 203 |
=head1 AUTHOR |
| 204 |
|
| 205 |
Paul Evans <[email protected]> |
| 206 |
|
| 207 |
=cut |