Extract LeakTracer symtable visitor into DBICTest::Util
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / Util / LeakTracer.pm
1 package DBICTest::Util::LeakTracer;
2
3 use warnings;
4 use strict;
5
6 use Carp;
7 use Scalar::Util qw(isweak weaken blessed reftype);
8 use DBIx::Class::_Util qw(refcount hrefaddr refdesc);
9 use DBIx::Class::Optional::Dependencies;
10 use Data::Dumper::Concise;
11 use DBICTest::Util qw( stacktrace visit_namespaces );
12 use constant {
13   CV_TRACING => DBIx::Class::Optional::Dependencies->req_ok_for ('test_leaks_heavy'),
14 };
15
16 use base 'Exporter';
17 our @EXPORT_OK = qw(populate_weakregistry assert_empty_weakregistry visit_refs);
18
19 my $refs_traced = 0;
20 my $leaks_found = 0;
21 my %reg_of_regs;
22
23 sub populate_weakregistry {
24   my ($weak_registry, $target, $note) = @_;
25
26   croak 'Expecting a registry hashref' unless ref $weak_registry eq 'HASH';
27   croak 'Target is not a reference' unless length ref $target;
28
29   my $refaddr = hrefaddr $target;
30
31   # a registry could be fed to itself or another registry via recursive sweeps
32   return $target if $reg_of_regs{$refaddr};
33
34   weaken( $reg_of_regs{ hrefaddr($weak_registry) } = $weak_registry )
35     unless( $reg_of_regs{ hrefaddr($weak_registry) } );
36
37   # an explicit "garbage collection" pass every time we store a ref
38   # if we do not do this the registry will keep growing appearing
39   # as if the traced program is continuously slowly leaking memory
40   for my $reg (values %reg_of_regs) {
41     (defined $reg->{$_}{weakref}) or delete $reg->{$_}
42       for keys %$reg;
43   }
44
45   if (! defined $weak_registry->{$refaddr}{weakref}) {
46     $weak_registry->{$refaddr} = {
47       stacktrace => stacktrace(1),
48       weakref => $target,
49     };
50     weaken( $weak_registry->{$refaddr}{weakref} );
51     $refs_traced++;
52   }
53
54   my $desc = refdesc $target;
55   $weak_registry->{$refaddr}{slot_names}{$desc} = 1;
56   if ($note) {
57     $note =~ s/\s*\Q$desc\E\s*//g;
58     $weak_registry->{$refaddr}{slot_names}{$note} = 1;
59   }
60
61   $target;
62 }
63
64 # Regenerate the slots names on a thread spawn
65 sub CLONE {
66   my @individual_regs = grep { scalar keys %{$_||{}} } values %reg_of_regs;
67   %reg_of_regs = ();
68
69   for my $reg (@individual_regs) {
70     my @live_slots = grep { defined $_->{weakref} } values %$reg
71       or next;
72
73     $reg = {};  # get a fresh hashref in the new thread ctx
74     weaken( $reg_of_regs{hrefaddr($reg)} = $reg );
75
76     for my $slot_info (@live_slots) {
77       my $new_addr = hrefaddr $slot_info->{weakref};
78
79       # replace all slot names
80       $slot_info->{slot_names} = { map {
81         my $name = $_;
82         $name =~ s/\(0x[0-9A-F]+\)/sprintf ('(%s)', $new_addr)/ieg;
83         ($name => 1);
84       } keys %{$slot_info->{slot_names}} };
85
86       $reg->{$new_addr} = $slot_info;
87     }
88   }
89 }
90
91 sub visit_refs {
92   my $args = { (ref $_[0]) ? %{$_[0]} : @_ };
93
94   $args->{seen_refs} ||= {};
95
96   my $visited_cnt = '0E0';
97   for my $i (0 .. $#{$args->{refs}} ) {
98
99     next unless length ref $args->{refs}[$i]; # not-a-ref
100
101     my $addr = hrefaddr $args->{refs}[$i];
102
103     # no diving into weakregistries
104     next if $reg_of_regs{$addr};
105
106     next if $args->{seen_refs}{$addr}++;
107     $visited_cnt++;
108
109     my $r = $args->{refs}[$i];
110
111     $args->{action}->($r) or next;
112
113     # This may end up being necessarry some day, but do not slow things
114     # down for now
115     #if ( defined( my $t = tied($r) ) ) {
116     #  $visited_cnt += visit_refs({ %$args, refs => [ $t ] });
117     #}
118
119     my $type = reftype $r;
120
121     local $@;
122     eval {
123       if ($type eq 'HASH') {
124         $visited_cnt += visit_refs({ %$args, refs => [ map {
125           ( !isweak($r->{$_}) ) ? $r->{$_} : ()
126         } keys %$r ] });
127       }
128       elsif ($type eq 'ARRAY') {
129         $visited_cnt += visit_refs({ %$args, refs => [ map {
130           ( !isweak($r->[$_]) ) ? $r->[$_] : ()
131         } 0..$#$r ] });
132       }
133       elsif ($type eq 'REF' and !isweak($$r)) {
134         $visited_cnt += visit_refs({ %$args, refs => [ $$r ] });
135       }
136       elsif (CV_TRACING and $type eq 'CODE') {
137         $visited_cnt += visit_refs({ %$args, refs => [ map {
138           ( !isweak($_) ) ? $_ : ()
139         } values %{ scalar PadWalker::closed_over($r) } ] }); # scalar due to RT#92269
140       }
141       1;
142     } or warn "Could not descend into @{[ refdesc $r ]}: $@\n";
143   }
144   $visited_cnt;
145 }
146
147 # compiles a list of addresses stored as globals (possibly even catching
148 # class data in the form of method closures), so we can skip them further on
149 sub symtable_referenced_addresses {
150
151   my $refs_per_pkg;
152
153   my $seen_refs = {};
154   visit_namespaces(
155     action => sub {
156
157       no strict 'refs';
158
159       my $pkg = shift;
160
161       # the unless regex at the end skips some dangerous namespaces outright
162       # (but does not prevent descent)
163       $refs_per_pkg->{$pkg} += visit_refs (
164         seen_refs => $seen_refs,
165
166         action => sub { 1 },
167
168         refs => [ map { my $sym = $_;
169           # *{"${pkg}::$sym"}{CODE} won't simply work - MRO-cached CVs are invisible there
170           ( CV_TRACING ? Class::MethodCache::get_cv("${pkg}::$sym") : () ),
171
172           ( defined *{"${pkg}::$sym"}{SCALAR} and length ref ${"${pkg}::$sym"} and ! isweak( ${"${pkg}::$sym"} ) )
173             ? ${"${pkg}::$sym"} : ()
174           ,
175
176           ( map {
177             ( defined *{"${pkg}::$sym"}{$_} and ! isweak(defined *{"${pkg}::$sym"}{$_}) )
178               ? *{"${pkg}::$sym"}{$_}
179               : ()
180           } qw(HASH ARRAY IO GLOB) ),
181
182         } keys %{"${pkg}::"} ],
183       ) unless $pkg =~ /^ (?:
184         DB | next | B | .+? ::::ISA (?: ::CACHE ) | Class::C3
185       ) $/x;
186     }
187   );
188
189 #  use Devel::Dwarn;
190 #  Ddie [ map
191 #    { { $_ => $refs_per_pkg->{$_} } }
192 #    sort
193 #      {$refs_per_pkg->{$a} <=> $refs_per_pkg->{$b} }
194 #      keys %$refs_per_pkg
195 #  ];
196
197   $seen_refs;
198 }
199
200 sub assert_empty_weakregistry {
201   my ($weak_registry, $quiet) = @_;
202
203   Sub::Defer::undefer_all();
204
205   # in case we hooked bless any extra object creation will wreak
206   # havoc during the assert phase
207   local *CORE::GLOBAL::bless;
208   *CORE::GLOBAL::bless = sub { CORE::bless( $_[0], (@_ > 1) ? $_[1] : caller() ) };
209
210   croak 'Expecting a registry hashref' unless ref $weak_registry eq 'HASH';
211
212   defined $weak_registry->{$_}{weakref} or delete $weak_registry->{$_}
213     for keys %$weak_registry;
214
215   return unless keys %$weak_registry;
216
217   my $tb = eval { Test::Builder->new }
218     or croak "Calling assert_empty_weakregistry in $0 without a loaded Test::Builder makes no sense";
219
220   for my $addr (keys %$weak_registry) {
221     $weak_registry->{$addr}{display_name} = join ' | ', (
222       sort
223         { length $a <=> length $b or $a cmp $b }
224         keys %{$weak_registry->{$addr}{slot_names}}
225     );
226
227     $tb->BAILOUT("!!!! WEAK REGISTRY SLOT $weak_registry->{$addr}{display_name} IS NOT A WEAKREF !!!!")
228       if defined $weak_registry->{$addr}{weakref} and ! isweak( $weak_registry->{$addr}{weakref} );
229   }
230
231   # the symtable walk is very expensive
232   # if we are $quiet (running in an END block) we do not really need to be
233   # that thorough - can get by with only %Sub::Quote::QUOTED
234   delete $weak_registry->{$_} for $quiet
235     ? do {
236       my $refs = {};
237       visit_refs (
238         # only look at the closed over stuffs
239         refs => [ grep { length ref $_ } map { values %{$_->[2]} } grep { ref $_ eq 'ARRAY' } values %Sub::Quote::QUOTED ],
240         seen_refs => $refs,
241         action => sub { 1 },
242       );
243       keys %$refs;
244     }
245     : (
246       # full sumtable walk, starting from ::
247       keys %{ symtable_referenced_addresses() }
248     )
249   ;
250
251   for my $addr (sort { $weak_registry->{$a}{display_name} cmp $weak_registry->{$b}{display_name} } keys %$weak_registry) {
252
253     next if ! defined $weak_registry->{$addr}{weakref};
254
255     $leaks_found++ unless $tb->in_todo;
256     $tb->ok (0, "Expected garbage collection of $weak_registry->{$addr}{display_name}");
257
258     my $diag = do {
259       local $Data::Dumper::Maxdepth = 1;
260       sprintf "\n%s (refcnt %d) => %s\n",
261         $weak_registry->{$addr}{display_name},
262         refcount($weak_registry->{$addr}{weakref}),
263         (
264           ref($weak_registry->{$addr}{weakref}) eq 'CODE'
265             and
266           B::svref_2object($weak_registry->{$addr}{weakref})->XSUB
267         ) ? '__XSUB__' : Dumper( $weak_registry->{$addr}{weakref} )
268       ;
269     };
270
271     # FIXME - need to add a circular reference seeker based on the visitor
272     # (will need a bunch of modifications, punting with just a stub for now)
273
274     $diag .= Devel::FindRef::track ($weak_registry->{$addr}{weakref}, 50) . "\n"
275       if ( $ENV{TEST_VERBOSE} && eval { require Devel::FindRef });
276
277     $diag =~ s/^/    /mg;
278
279     if (my $stack = $weak_registry->{$addr}{stacktrace}) {
280       $diag .= "    Reference first seen$stack";
281     }
282
283     $tb->diag($diag);
284
285 #    if ($leaks_found == 1) {
286 #      # using the fh dumper due to intermittent buffering issues
287 #      # in case we decide to exit soon after (possibly via _exit)
288 #      require Devel::MAT::Dumper;
289 #      local $Devel::MAT::Dumper::MAX_STRING = -1;
290 #      open( my $fh, '>:raw', "leaked_${addr}_pid$$.pmat" ) or die $!;
291 #      Devel::MAT::Dumper::dumpfh( $fh );
292 #      close ($fh) or die $!;
293 #
294 #      use POSIX;
295 #      POSIX::_exit(1);
296 #    }
297   }
298
299   if (! $quiet and !$leaks_found and ! $tb->in_todo) {
300     $tb->ok(1, sprintf "No leaks found at %s line %d", (caller())[1,2] );
301   }
302 }
303
304 END {
305   if (
306     $INC{'Test/Builder.pm'}
307       and
308     my $tb = do {
309       local $@;
310       my $t = eval { Test::Builder->new }
311         or warn "Test::Builder->new failed:\n$@\n";
312       $t;
313     }
314   ) {
315     # we check for test passage - a leak may be a part of a TODO
316     if ($leaks_found and !$tb->is_passing) {
317
318       $tb->diag(sprintf
319         "\n\n%s\n%s\n\nInstall Devel::FindRef and re-run the test with set "
320       . '$ENV{TEST_VERBOSE} (prove -v) to see a more detailed leak-report'
321       . "\n\n%s\n%s\n\n", ('#' x 16) x 4
322       ) if ( !$ENV{TEST_VERBOSE} or !$INC{'Devel/FindRef.pm'} );
323
324     }
325     else {
326       $tb->note("Auto checked $refs_traced references for leaks - none detected");
327     }
328
329     # also while we are here and not in plain runmode: make sure we never
330     # loaded any of the strictures XS bullshit (it's a leak in a sense)
331     unless (
332       $ENV{MOO_FATAL_WARNINGS}
333         or
334       # FIXME - SQLT loads strictures explicitly, /facedesk
335       # remove this INC check when 0fb58589 and 45287c815 are rectified
336       $INC{'SQL/Translator.pm'}
337         or
338       DBICTest::RunMode->is_plain
339     ) {
340       for (qw(indirect multidimensional bareword::filehandles)) {
341         exists $INC{ Module::Runtime::module_notional_filename($_) }
342           and
343         $tb->ok(0, "$_ load should not have been attempted!!!" )
344       }
345     }
346   }
347 }
348
349 1;