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