| 1 | package Plack::Middleware::Debug::RefCounts; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | our $AUTHORITY = 'cpan:GSG'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Reference count debugging for Plack apps | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = 'v0.910.1'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use v5.10.1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use parent 'Plack::Middleware::Debug::Base'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Devel::Gladiator; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Env qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | $PLACK_MW_DEBUG_REFCOUNTS_DUMP_RE | ||||||||||||||||||||||||||||||||||||||||||
| 17 | $PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Scalar::Util qw( refaddr ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use namespace::clean; # don't export the above | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod use Plack::Middleware::Debug::RefCounts; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod enable 'Debug', panels => [ 'RefCounts', @any_other_panels ]; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod This module aims to provide debugging tools to help identify memory leaks. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod It uses L<Devel::Gladiator> to compare reference counts at the beginning and end | ||||||||||||||||||||||||||||||||||||||||||
| 34 | #pod of requests. | ||||||||||||||||||||||||||||||||||||||||||
| 35 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 36 | #pod To get the most out of this module, you should: | ||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod =item 1. Run you application with a single worker process. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 42 | #pod The middleware attempts not to unduly accumulate references. As such, it tracks | ||||||||||||||||||||||||||||||||||||||||||
| 43 | #pod references counts in a simple package variable (L</Arena_Refs>), which does not | ||||||||||||||||||||||||||||||||||||||||||
| 44 | #pod scale to multiple processes. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod =item 2. Identify what's growing unexpectedly, I<then> dive in. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod See the explanation under L</PLACK_MW_DEBUG_REFCOUNTS_DUMP_RE>. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 50 | #pod Generally, just be aware that you're potentially looking at B<A LOT> of | ||||||||||||||||||||||||||||||||||||||||||
| 51 | #pod information, and trying to debug it takes up a lot of resources. System | ||||||||||||||||||||||||||||||||||||||||||
| 52 | #pod errors may occur if you're too aggressive. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 54 | #pod =item 3. Repeat tests to make sure they are consistently leaking memory. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 56 | #pod Objects can be loaded the first time you load a specific web page, and increase | ||||||||||||||||||||||||||||||||||||||||||
| 57 | #pod memory usage. The key is that they don't continue to increase memory after | ||||||||||||||||||||||||||||||||||||||||||
| 58 | #pod repeated hits. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 60 | #pod Preloading data prior to forking can help with this problem, but it can be hard | ||||||||||||||||||||||||||||||||||||||||||
| 61 | #pod to capture every single object or singleton that needs to be loaded. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 63 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 64 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 65 | #pod =head1 ENVIRONMENT VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 66 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 67 | #pod =head2 PLACK_MW_DEBUG_REFCOUNTS_DUMP_RE | ||||||||||||||||||||||||||||||||||||||||||
| 68 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 69 | #pod A regex to be matched against changing counts in L</calculate_arena_refs>. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | #pod If a variable's ref type (or class) matches it, the variable will be dumped to | ||||||||||||||||||||||||||||||||||||||||||
| 71 | #pod C<STDERR>. Only newly-discovered variables are dumped. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 73 | #pod B<WARNING:> Dumping certain variables may crash your process, because there is | ||||||||||||||||||||||||||||||||||||||||||
| 74 | #pod so much to dump. Look at the ref counts first to figure out what you want to | ||||||||||||||||||||||||||||||||||||||||||
| 75 | #pod dump, and try to work around any bizarre behaviors. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 77 | #pod =head2 PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP | ||||||||||||||||||||||||||||||||||||||||||
| 78 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 79 | #pod A boolean, defaulting to C<0>. | ||||||||||||||||||||||||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 81 | #pod If the PSGI application supports cleanup and this variable is true, then ref | ||||||||||||||||||||||||||||||||||||||||||
| 82 | #pod counting will happen during cleanup. This prevents rendering this refcount | ||||||||||||||||||||||||||||||||||||||||||
| 83 | #pod information in the debug panel. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 85 | #pod =head1 PACKAGE VARIABLES | ||||||||||||||||||||||||||||||||||||||||||
| 86 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 87 | #pod =head2 Arena_Refs | ||||||||||||||||||||||||||||||||||||||||||
| 88 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 89 | #pod This stores all of the types and memory locations of every variable, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | #pod except C<SCALAR>s and C<REF>s. Data is captured at the end of each dispatch. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 92 | #pod B<NOTE> this is just a package variable - debugging memory works best with a | ||||||||||||||||||||||||||||||||||||||||||
| 93 | #pod single worker anyway. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 95 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | our $Arena_Refs = {}; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | #pod =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 101 | #pod =head2 run | ||||||||||||||||||||||||||||||||||||||||||
| 102 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 103 | #pod The standard debug middleware interface. Runs the reference count comparison | ||||||||||||||||||||||||||||||||||||||||||
| 104 | #pod as late as possible (ie. during cleanup if supported). | ||||||||||||||||||||||||||||||||||||||||||
| 105 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 106 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # A T:MT template version of compare_arena_counts | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | my $refcount_html_template = __PACKAGE__->build_template(<<'EOTMPL'); | |||||||||||||||||||||||||||||||||||||||||
| 110 | <table> | ||||||||||||||||||||||||||||||||||||||||||
| 111 | <thead> | ||||||||||||||||||||||||||||||||||||||||||
| 112 | <tr> | ||||||||||||||||||||||||||||||||||||||||||
| 113 | <th style="text-align: right;">Prev</th> | ||||||||||||||||||||||||||||||||||||||||||
| 114 | <th style="text-align: right;">Diff</th> | ||||||||||||||||||||||||||||||||||||||||||
| 115 | <th style="text-align: right;">Now</th> | ||||||||||||||||||||||||||||||||||||||||||
| 116 | <th>Class/Type</th> | ||||||||||||||||||||||||||||||||||||||||||
| 117 | </tr> | ||||||||||||||||||||||||||||||||||||||||||
| 118 | </thead> | ||||||||||||||||||||||||||||||||||||||||||
| 119 | <tbody> | ||||||||||||||||||||||||||||||||||||||||||
| 120 | % my $diff_list = $_[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 121 | % foreach my $key (sort keys %$diff_list) { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | % my ($diff, $count_a, $count_b) = @{ $diff_list->{$key} }; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | % next unless $diff; | ||||||||||||||||||||||||||||||||||||||||||
| 124 | <tr style="background-color: <%= $diff > 0 ? '#8f8' : '#f88' %>;"> | ||||||||||||||||||||||||||||||||||||||||||
| 125 | <td style="text-align: right;"><%= $count_a %></td> | ||||||||||||||||||||||||||||||||||||||||||
| 126 | <td style="text-align: right;"><%= sprintf "%+-d", $diff %></td> | ||||||||||||||||||||||||||||||||||||||||||
| 127 | <td style="text-align: right;"><%= $count_b %></td> | ||||||||||||||||||||||||||||||||||||||||||
| 128 | <td><%= $key %></td> | ||||||||||||||||||||||||||||||||||||||||||
| 129 | </tr> | ||||||||||||||||||||||||||||||||||||||||||
| 130 | % } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | </tbody> | ||||||||||||||||||||||||||||||||||||||||||
| 132 | </table> | ||||||||||||||||||||||||||||||||||||||||||
| 133 | EOTMPL | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 4 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 4 | my ($self, $env, $panel) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | ▶ | if ($env->{'psgix.cleanup'} && $PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$env->{"psgix.cleanup"} and $Plack::Middleware::Debug::RefCounts::PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP
MC/DC50%1/2
$env->{"psgix.cleanup"} and $Plack::Middleware::Debug::RefCounts::PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP
$env->{"psgix.cleanup"}
$Plack::Middleware::Debug::RefCounts::PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP
Truth table66%2/3
A and B
A= $env->{"psgix.cleanup"}B= $Plack::Middleware::Debug::RefCounts::PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $panel->content(<<'EOP'); | |||||||||||||||||||||||||||||||||||||||||
| 141 | Because $PLACK_MW_DEBUG_REFCOUNTS_ON_CLEANUP is true, refcounts are being | ||||||||||||||||||||||||||||||||||||||||||
| 142 | tabulated <em>after</em> rendering. See the STDERR for details. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | EOP | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | push @{ $env->{'psgix.cleanup.handlers'} }, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | $self->update_arena_counts; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | my ($is_first, $diff_list) = $self->update_arena_counts; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 3 | ▶ | if ($is_first) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | $panel->content(<<'EOP'); | |||||||||||||||||||||||||||||||||||||||||
| 152 | This was the first load. Make sure you only have one process and load | ||||||||||||||||||||||||||||||||||||||||||
| 153 | another page to see refcounts. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | EOP | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | elsif (!$diff_list) { | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | $panel->content('No changes \o/'); | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | $panel->content( | |||||||||||||||||||||||||||||||||||||||||
| 161 | $self->render($refcount_html_template, $diff_list) | ||||||||||||||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | #pod =head2 update_arena_counts | ||||||||||||||||||||||||||||||||||||||||||
| 170 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 171 | #pod ($is_first, \%diff_list) = $self->update_arena_counts; | ||||||||||||||||||||||||||||||||||||||||||
| 172 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 173 | #pod Updates the arena counts and returns a boolean indicating whether this is the | ||||||||||||||||||||||||||||||||||||||||||
| 174 | #pod first runthrough and a diff of hashes via L</compare_arena_counts>. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 176 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 8 | ▶ | sub update_arena_counts { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_arena_counts: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | my $is_first = !%$Arena_Refs; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | my $diff_list = $self->calculate_arena_refs; | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 8 | ▶ | if (!$is_first) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 6 | $self->compare_arena_counts($diff_list); | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 8 | return $is_first, $diff_list; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | #pod =head2 calculate_arena_refs | ||||||||||||||||||||||||||||||||||||||||||
| 191 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 192 | #pod \%diff_list = $self->calculate_arena_refs; | ||||||||||||||||||||||||||||||||||||||||||
| 193 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 194 | #pod Walks the arena (of Perl variables) via L<Devel::Gladiator/walk_arena>, and | ||||||||||||||||||||||||||||||||||||||||||
| 195 | #pod catalogs all non-SCALAR/REFs into ref types and memory locations. Returns a | ||||||||||||||||||||||||||||||||||||||||||
| 196 | #pod diff list hashref. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 198 | #pod I<After> the first (initializing) run, if L</PLACK_MW_DEBUG_REFCOUNTS_DUMP_RE> | ||||||||||||||||||||||||||||||||||||||||||
| 199 | #pod is set, newly discovered matching variables will be dumped to C<STDERR>. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 201 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 8 | ▶ | sub calculate_arena_refs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
calculate_arena_refs: called CC=19 SCAR=29.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 8 | my $dump_re = $PLACK_MW_DEBUG_REFCOUNTS_DUMP_RE; | |||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # To save on memory with this memory-intensive operation, we operate against | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # $Arena_Ref as the sole storage mechanism for variable type/addresses. The $all | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # variable has the arena, but it's strictly a pointer without any memory usage. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 8 | ▶ | $dump_re = undef unless %$Arena_Refs; # don't dump the first run | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | # refs start out "deleted", until they are found again | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 8 | $Arena_Refs->{$_} = -1 for keys %$Arena_Refs; | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | # This creates string address lists of all of the existing arena variables. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # This is much cleaner and memory-friendly than storing real refs. | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 8 | my $all = Devel::Gladiator::walk_arena(); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 8 | foreach my $it (@$all) { | |||||||||||||||||||||||||||||||||||||||||
| 220 | 914582 | my $type = ref $it; | |||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | # There are so many of these that even cataloging the memory addresses | ||||||||||||||||||||||||||||||||||||||||||
| 223 | # of these is enough to cause an OOM in some systems. | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 914582 | ▶ | next if $type eq 'SCALAR' || $type eq 'REF'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "SCALAR" or $type eq "REF"
MC/DC100%2/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR"
$type eq "REF"
Truth table100%3/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # Get the pointer address | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 181802 | my $addr = sprintf '%x', refaddr $it; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 181802 | my $id = "$type/$addr"; | |||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 181802 | ▶ | unless ($Arena_Refs->{$id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | # New ref | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 46642 | ▶ | if ($dump_re && $type =~ /$dump_re/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$dump_re and $type =~ /$dump_re/
MC/DC100%2/2
$dump_re and $type =~ /$dump_re/
$dump_re
$type =~ /$dump_re/
Truth table100%3/3
A and B
A= $dump_reB= $type =~ /$dump_re/
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | # Sometimes this dies. If so, just move on to the next one. | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | local $Data::Dumper::Maxdepth = 2; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | print STDERR "+$id = ".Dumper($it); | |||||||||||||||||||||||||||||||||||||||||
| 237 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | print STDERR "+$id > ERROR: $@"; | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # either equalize to 0 for an existing ref, or go to 1 for a new one | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 181802 | $Arena_Refs->{$id}++; | |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 181802 | $it = undef; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 8 | $all = undef; | |||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 8 | my %diff_list; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 8 | foreach my $id (keys %$Arena_Refs) { | |||||||||||||||||||||||||||||||||||||||||
| 252 | 182036 | my ($type, $addr) = split m!/!, $id, 2; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 182036 | my $cmp = $Arena_Refs->{$id}; | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # Process the diff list | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 182036 | ▶ | $diff_list{$type} //= [0,0,0]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$diff_list{$type} //= [0, 0, 0]
MC/DC100%1/1
$diff_list{$type} //= [0, 0, 0]
$diff_list{$type}
Truth table100%2/2
A
A= $diff_list{$type}
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 182036 | $diff_list{$type}[0] += $cmp; # diff | |||||||||||||||||||||||||||||||||||||||||
| 258 | 182036 | ▶ | $diff_list{$type}[1]++ unless $cmp == 1; # count_a | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 182036 | ▶ | $diff_list{$type}[2]++ unless $cmp == -1; # count_b | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | # Also dump the removed refs, if requested | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 182036 | ▶ | if ($dump_re && $type =~ /$dump_re/ && $cmp == -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$dump_re and $type =~ /$dump_re/ and $cmp == -1
$dump_re and $type =~ /$dump_re/
MC/DC0%0/3
$dump_re and $type =~ /$dump_re/ and $cmp == -1
$dump_re
$type =~ /$dump_re/
$cmp == -1
Truth table75%3/4
A and B and C
A= $dump_reB= $type =~ /$dump_re/C= $cmp == -1
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | say STDERR "-$id"; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | # Remove any deleted references | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 182036 | ▶ | delete $Arena_Refs->{$id} if $cmp == -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 8 | ▶ | say STDERR '' if $dump_re; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 8 | return \%diff_list; | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | #pod =head2 compare_arena_counts | ||||||||||||||||||||||||||||||||||||||||||
| 276 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 277 | #pod @lines = $self->compare_arena_counts(\%diff_list); | ||||||||||||||||||||||||||||||||||||||||||
| 278 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 279 | #pod Using a diff list from L</calculate_arena_refs>, this displays the new ref | ||||||||||||||||||||||||||||||||||||||||||
| 280 | #pod counts on STDERR, and returns those displayed lines. | ||||||||||||||||||||||||||||||||||||||||||
| 281 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 282 | #pod Anything listed here has either shrunk or grown the variables within the arena. | ||||||||||||||||||||||||||||||||||||||||||
| 283 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 284 | #pod Example output: | ||||||||||||||||||||||||||||||||||||||||||
| 285 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 286 | #pod === Reference growth counts === | ||||||||||||||||||||||||||||||||||||||||||
| 287 | #pod +4 (diff) => 4 (now) => Class::MOP::Class::Immutable::Moose::Meta::Class | ||||||||||||||||||||||||||||||||||||||||||
| 288 | #pod +1 (diff) => 1 (now) => Class::MOP::Method::Wrapped | ||||||||||||||||||||||||||||||||||||||||||
| 289 | #pod +12 (diff) => 19 (now) => DBD::mysql::st_mem | ||||||||||||||||||||||||||||||||||||||||||
| 290 | #pod +24 (diff) => 38 (now) => DBI::st | ||||||||||||||||||||||||||||||||||||||||||
| 291 | #pod +1 (diff) => 1 (now) => Data::Visitor::Callback | ||||||||||||||||||||||||||||||||||||||||||
| 292 | #pod +4 (diff) => 4 (now) => DateTime | ||||||||||||||||||||||||||||||||||||||||||
| 293 | #pod +1 (diff) => 1 (now) => DateTime::TimeZone::America::New_York | ||||||||||||||||||||||||||||||||||||||||||
| 294 | #pod +1 (diff) => 1 (now) => Devel::StackTrace | ||||||||||||||||||||||||||||||||||||||||||
| 295 | #pod +1 (diff) => 1 (now) => FCGI | ||||||||||||||||||||||||||||||||||||||||||
| 296 | #pod +3 (diff) => 3 (now) => FCGI::Stream | ||||||||||||||||||||||||||||||||||||||||||
| 297 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 298 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | 6 | ▶ | sub compare_arena_counts { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compare_arena_counts: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 6 | my ($self, $diff_list) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 6 | my @lines = ( "=== Reference growth counts ===\n" ); | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 6 | foreach my $key (sort keys %$diff_list) { | |||||||||||||||||||||||||||||||||||||||||
| 306 | 199 | my ($diff, $count_a, $count_b) = @{ $diff_list->{$key} }; | |||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 199 | ▶ | next unless $diff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 26 | push @lines, sprintf "%+-5d (diff) => %7d (now) => %-s\n", $diff, $count_b, $key; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 6 | print STDERR $_ for @lines, "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 6 | return @lines; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 317 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 318 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 319 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 320 | #pod =item L<Devel::Gladiator> | ||||||||||||||||||||||||||||||||||||||||||
| 321 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 322 | #pod The tool used for leak hunting. | ||||||||||||||||||||||||||||||||||||||||||
| 323 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 324 | #pod =item L<Plack::Middleware::Debug> | ||||||||||||||||||||||||||||||||||||||||||
| 325 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 326 | #pod General debugging framework. | ||||||||||||||||||||||||||||||||||||||||||
| 327 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 328 | #pod =item L<Plack::Middleware::Debug::Memory> | ||||||||||||||||||||||||||||||||||||||||||
| 329 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 330 | #pod Monitors RSS, which is not particularly helpful for tracking down memory leaks. | ||||||||||||||||||||||||||||||||||||||||||
| 331 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 332 | #pod =item L<Plack::Middleware::MemoryUsage> | ||||||||||||||||||||||||||||||||||||||||||
| 333 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 334 | #pod As of writing, is broken by a 2015 bug in L<B::Size2> | ||||||||||||||||||||||||||||||||||||||||||
| 335 | #pod (and neither module has been updated since 2014). | ||||||||||||||||||||||||||||||||||||||||||
| 336 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 337 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 338 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 339 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | __END__ | ||||||||||||||||||||||||||||||||||||||||||