blib/lib/Devel/MAT/Tool/Sizes.pm

filter: Statementstmt 51.7% 73 / 141 Branchbran 7.6% 2 / 26 Conditioncond 14.8% 4 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 62.8% 22 / 35 Podpod 16.6% 1 / 6 totaltotal 40.6% 102 / 251 CC 28 SCAR 36.4
CC
28
Cov
41%
list_largest_svs
132.0
run
90.0
init_ui
6.0
CRAP 38.2
« blib/lib/Devel/MAT/Tool/Find.pm ↑ Summary blib/lib/Devel/MAT/SV.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2013-2026 -- leonerd@leonerd.org.uk
5  
6 package Devel::MAT::Tool::Sizes 0.56;
7  
8 5 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use base qw( Devel::MAT::Tool );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use constant FOR_UI => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use List::Util qw( sum0 );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use List::UtilsBy qw( rev_nsort_by );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 C<Devel::MAT::Tool::Sizes> - calculate sizes of SV structures
23  
24 =head1 DESCRIPTION
25  
26 This C<Devel::MAT> tool calculates the sizes of the structures around SVs.
27 The individual size of each individual SV is given by the C<size> method,
28 though in several cases SVs can be considered to be part of larger structures
29 of a combined aggregate size. This tool calculates those sizes and adds them
30 to the UI.
31  
32 The structural size is calculated from the basic size of the SV, added to
33 which for various types is:
34  
35 =over 2
36  
37 =item ARRAY
38  
39 Arrays add the basic size of every non-mortal element SV.
40  
41 =item HASH
42  
43 Hashes add the basic size of every non-mortal value SV.
44  
45 =item CODE
46  
47 Codes add the basic size of their padlist and constant value, and all their
48 padnames, pads, constants and globrefs.
49  
50 =back
51  
52 The owned size is calculated by starting at the given SV and accumulating the
53 set of every strong outref whose refcount is 1. This is the set of all SVs the
54 original directly owns.
55  
56 =cut
57  
58 0 ▶sub init_ui ( $self, $ui )
Subroutine init_ui: not called CC=2 SCAR=17.9
Pod documented
59 {
60 0 my %size_tooltip = (
61 SV => "Display the size of each SV individually",
62 Structure => "Display the size of SVs including its internal structure",
63 Owned => "Display the size of SVs including all owned referrents",
64 );
65  
66 $ui->provides_radiobutton_set(
67 map {
68 0 ▶ my $size = $_ eq "SV" ? "size" : "\L${_}_size";
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'SV' ? : 0 0 0
69  
70 0 $ui->register_icon(
71 name => "size-$_",
72 svg => "icons/size-$_.svg",
73 );
74  
75 {
76 text => $_,
77 icon => "size-$_",
78 tooltip => $size_tooltip{$_},
79 0 ▶ code => sub ( @ ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 $ui->set_svlist_column_values(
81 column => Devel::MAT::UI->COLUMN_SIZE,
82 0 ▶ from => sub ( $sv ) { $sv->$size },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 );
84 },
85 }
86 0 } qw( SV Structure Owned )
87 );
88 }
89  
90 =head1 SV METHODS
91  
92 =for highlighter language=perl
93  
94 This tool adds the following SV methods.
95  
96 =head2 structure_set
97  
98 @svs = $sv->structure_set;
99  
100 Returns the total set of the SV's structure.
101  
102 =head2 structure_size
103  
104 $size = $sv->structure_size;
105  
106 Returns the size, in bytes, of the structure that the SV contains.
107  
108 =cut
109  
110 # Most SVs' structual set is just themself
111 0 ▶sub Devel::MAT::SV::structure_set ( $sv ) { $sv }
Subroutine structure_set: not called CC=1 SCAR=6.9
Pod undocumented
112  
113 # ARRAY structure includes the element SVs
114 2 ▶sub Devel::MAT::SV::ARRAY::structure_set ( $av )
Subroutine structure_set: called CC=2 SCAR=7.6
115 {
116 2 ▶ my @svs = ( $av, grep { $_ && !$_->immortal } $av->elems );
Condition33%1/3
$_ && !$_->immortal
!ll&&!rl&&r
006
MC/DC0%0/2
$_ && !$_->immortal
$_ !$_->immortal
Truth table33%1/3
A && not(B)
A= $_B= !$_->immortal
ABresult
0X0
100
111
117 2 return @svs;
118 }
119  
120 # HASH structure includes the value SVs
121 0 ▶sub Devel::MAT::SV::HASH::structure_set ( $hv )
Subroutine structure_set: not called CC=2 SCAR=17.9
122 {
123 0 ▶ my @svs = ( $hv, grep { $_ && !$_->immortal } $hv->values );
Condition0%0/3
$_ && !$_->immortal
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && !$_->immortal
$_ !$_->immortal
Truth table0%0/3
A && not(B)
A= $_B= !$_->immortal
ABresult
0X0
100
111
124 0 return @svs;
125 }
126  
127 # CODE structure includes PADLIST, PADNAMES, PADs, and all pad name and pad SVs
128 0 ▶sub Devel::MAT::SV::CODE::structure_set ( $cv )
Subroutine structure_set: not called CC=2 SCAR=17.9
129 {
130 0 ▶ my @svs = ( $cv, grep { $_ && !$_->immortal }
Condition0%0/3
$_ && !$_->immortal
!ll&&!rl&&r
000
MC/DC0%0/2
$_ && !$_->immortal
$_ !$_->immortal
Truth table0%0/3
A && not(B)
A= $_B= !$_->immortal
ABresult
0X0
100
111
131 $cv->padlist, $cv->padnames_av, $cv->pads,
132 $cv->constval, $cv->constants, $cv->globrefs );
133 0 return @svs;
134 }
135  
136 1 ▶sub Devel::MAT::SV::structure_size ( $sv )
Subroutine structure_size: called CC=1 SCAR=0.0
Pod undocumented
137 {
138 1 return sum0 map { $_->size } $sv->structure_set
139 }
140  
141 =head2 owned_set
142  
143 @svs = $sv->owned_set;
144  
145 Returns the set of every SV owned by the given one.
146  
147 =head2 owned_size
148  
149 $size = $sv->owned_size;
150  
151 Returns the total size, in bytes, of the SVs owned by the given one.
152  
153 =cut
154  
155 2 ▶sub Devel::MAT::SV::owned_set ( $sv )
Subroutine owned_set: called CC=4 SCAR=14.3
Pod undocumented
156 {
157 2 my @more = ( $sv );
158  
159 2 my %seen;
160 my @owned;
161  
162 2 ▶ while( @more ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@more) 14 2 16
163 14 my $next = pop @more;
164 14 push @owned, $next;
165  
166 14 $seen{$next->addr}++;
167 12 ▶ push @more, grep { !$seen{$_->addr} and
Condition33%2/6
!($seen{$_->addr} || $_->immortal) && $_->refcnt == 1
!ll&&!rl&&r
0012
$seen{$_->addr} || $_->immortal
l!l&&r!l&&!r
0012
MC/DC0%0/3
!($seen{$_->addr} || $_->immortal) && $_->refcnt == 1
$seen{$_->addr} $_->immortal $_->refcnt == 1
Truth table25%1/4
not(A || B) && C
A= $seen{$_->addr}B= $_->immortalC= $_->refcnt == 1
ABCresult
1XX0
01X0
0000
0011
168 !$_->immortal and
169 14 $_->refcnt == 1 } map { $_->sv } $next->outrefs_strong;
170 }
171 2 return @owned;
172 }
173  
174 1 ▶sub Devel::MAT::SV::owned_size ( $sv )
Subroutine owned_size: called CC=2 SCAR=8.1
Pod undocumented
175 {
176 1 ▶ return $sv->{tool_sizes_owned} //= sum0 map { $_->size } $sv->owned_set;
Condition33%1/3
$sv->{"tool_sizes_owned"} //= &sum0(map({$_->size;} $sv->owned_set))
l!l&&r!l&&!r
010
MC/DC0%0/2
$sv->{"tool_sizes_owned"} //= &sum0(map({$_->size;} $sv->owned_set))
$sv->{"tool_sizes_owned"} &sum0(map({$_->size;} $sv->owned_set))
Truth table33%1/3
A //= B
A= $sv->{"tool_sizes_owned"}B= &sum0(map({$_->size;} $sv->owned_set))
ABresult
1X1
011
000
177 }
178  
179 =head1 COMMANDS
180  
181 =for highlighter
182  
183 =cut
184  
185 =head2 size
186  
187 Prints the sizes of a given SV
188  
189 pmat> size defstash
190 STASH(61) at 0x556e47243e10=defstash consumes:
191 2.1 KiB directly
192 11.2 KiB structurally
193 54.2 KiB including owned referrants
194  
195 =cut
196  
197 5 ▶use constant CMD => "size";
Subroutine BEGIN: called CC=1 SCAR=0.0
198 5 ▶use constant CMD_DESC => "Show the size of a given SV";
Subroutine BEGIN: called CC=1 SCAR=0.0
199  
200 5 ▶use constant CMD_ARGS_SV => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 0 ▶sub run ( $self, $sv )
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
203 {
204 0 Devel::MAT::Cmd->printf( "%s consumes:\n",
205 Devel::MAT::Cmd->format_sv( $sv )
206 );
207  
208 0 Devel::MAT::Cmd->printf( " %s directly\n",
209 Devel::MAT::Cmd->format_bytes( $sv->size )
210 );
211 0 Devel::MAT::Cmd->printf( " %s structurally\n",
212 Devel::MAT::Cmd->format_bytes( $sv->structure_size )
213 );
214 0 Devel::MAT::Cmd->printf( " %s including owned referrants\n",
215 Devel::MAT::Cmd->format_bytes( $sv->owned_size )
216 );
217 }
218  
219 package # hide
220 Devel::MAT::Tool::Sizes::_largest;
221 5 ▶use base qw( Devel::MAT::Tool );
Subroutine BEGIN: called CC=1 SCAR=0.0
222  
223 =head2 largest
224  
225 pmat> largest -owned
226 STASH(61) at 0x55e4317dfe10: 54.2 KiB: of which
227 | GLOB(%*) at 0x55e43180be60: 16.9 KiB: of which
228 | | STASH(40) at 0x55e43180bdd0: 16.7 KiB
229 | | GLOB(&*) at 0x55e4318ad330: 2.8 KiB
230 | | others: 15.0 KiB
231 | GLOB(%*) at 0x55e4317fdf28: 4.1 KiB: of which
232 | | STASH(34) at 0x55e4317fdf40: 4.0 KiB bytes
233 ...
234  
235 Finds and prints the largest SVs by size. The 5 largest SVs are shown.
236  
237 If counting sizes in a way that includes referred SVs, a tree is printed
238 showing the 3 largest SVs within these, and of those the 2 largest referred
239 SVs again. This should help identify large memory occupiers.
240  
241 Takes the following named options:
242  
243 =over 4
244  
245 =item --struct
246  
247 Count SVs using the structural size.
248  
249 =item --owned
250  
251 Count SVs using the owned size.
252  
253 =back
254  
255 By default, only the individual SV size is counted.
256  
257 =cut
258  
259 5 ▶use constant CMD => "largest";
Subroutine BEGIN: called CC=1 SCAR=0.0
260 5 ▶use constant CMD_DESC => "Find the largest SVs by size";
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 5 ▶use Heap;
Subroutine BEGIN: called CC=1 SCAR=0.0
263 5 ▶use List::UtilsBy qw( max_by );
Subroutine BEGIN: called CC=1 SCAR=0.0
264  
265 5 my %seen;
266  
267 0 ▶sub list_largest_svs ( $svlist, $metric, $indent, @counts )
Subroutine list_largest_svs: not called CC=11 SCAR=48.8
268 {
269 0 ▶ my $method = $metric ? "${metric}_size" : "size";
Branch: 1 branch
BranchTrueFalseTotal
$metric ? : 0 0 0
270  
271 0 my $heap = Heap::Fibonacci->new;
272 0 $heap->add( Devel::MAT::Tool::Sizes::_Elem->new( $_->$method, $_ ) ) for @$svlist;
273  
274 0 my $count = shift @counts;
275 0 ▶ while( $count-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 0 0 0
276 0 ▶ my $topelem = $heap->extract_top or last;
Branch: 1 branch
BranchTrueFalseTotal
my $topelem = $heap->extract_top or last 0 0 0
277 0 my $largest = $topelem->sv;
278  
279 0 $seen{$largest->addr}++;
280  
281 0 Devel::MAT::Cmd->printf( "$indent%s: %s",
282 Devel::MAT::Cmd->format_sv( $largest ),
283 Devel::MAT::Cmd->format_bytes( $largest->$method ),
284 );
285  
286 0 ▶ if( !defined $metric or !@counts ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $metric and @counts) 0 0 0
Condition0%0/3
defined $metric and @counts
!ll&&!rl&&r
000
MC/DC0%0/2
defined $metric and @counts
defined $metric @counts
Truth table0%0/3
A and B
A= defined $metricB= @counts
ABresult
0X0
100
111
287 0 Devel::MAT::Cmd->printf( "\n" );
288 0 next;
289 }
290  
291 0 my $set_method = "${metric}_set";
292 0 my @set = $largest->$set_method;
293 0 shift @set; # SV itself is always first
294  
295 0 ▶ if( !@set ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@set) 0 0 0
296 0 Devel::MAT::Cmd->printf( "\n" );
297 0 next;
298 }
299  
300 0 Devel::MAT::Cmd->printf( ": of which\n" );
301 0 list_largest_svs( \@set, $metric, "${indent} | ", @counts );
302  
303 0 $seen{$_->addr}++ for @set;
304 }
305  
306 0 my $others = 0;
307 0 $others += $_->size for grep { !$seen{$_->addr} } @$svlist;
308  
309 0 ▶ if( $others ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($others) 0 0 0
310 0 Devel::MAT::Cmd->printf( "$indent%s: %s\n",
311 Devel::MAT::Cmd->format_note( "others" ),
312 Devel::MAT::Cmd->format_bytes( $others ),
313 );
314 }
315 }
316  
317 5 package Devel::MAT::Tool::Sizes::_Elem {
318 0 ▶ sub new ( $class, $val, $sv ) { bless [ $val, $sv ], $class }
Subroutine new: not called CC=1 SCAR=6.9
319  
320 0 ▶ sub sv ( $self ) { return $self->[1]; }
Subroutine sv: not called CC=1 SCAR=6.9
321 0 ▶ sub heap ( $self, @args ) { $self->[2] = shift @args if @args; return $self->[2] }
Subroutine heap: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
322  
323 0 ▶ sub cmp ( $self, $other ) { return $other->[0] <=> $self->[0] }
Subroutine cmp: not called CC=1 SCAR=6.9
324 }
325  
326 5 ▶use constant CMD_OPTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
327 struct => { help => "count SVs by structural size" },
328 owned => { help => "count SVs by owned size" },
329 5 );
330  
331 5 ▶use constant CMD_ARGS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
332 { name => "count", help => "how many items to display",
333 repeated => 1 },
334 5 );
335  
336 0 ▶sub run ( $self, $optsref, @counts )
Subroutine run: not called CC=9 SCAR=45.0
337 {
338 0 my %opts = $optsref->%*;
339  
340 0 ▶ $counts[0] //= 5;
Condition0%0/2
$counts[0] //= 5
l!l
00
MC/DC0%0/1
$counts[0] //= 5
$counts[0]
Truth table0%0/2
A
A= $counts[0]
Aresult
11
00
341 0 ▶ $counts[1] //= 3;
Condition0%0/2
$counts[1] //= 3
l!l
00
MC/DC0%0/1
$counts[1] //= 3
$counts[1]
Truth table0%0/2
A
A= $counts[1]
Aresult
11
00
342 0 ▶ $counts[2] //= 2;
Condition0%0/2
$counts[2] //= 2
l!l
00
MC/DC0%0/1
$counts[2] //= 2
$counts[2]
Truth table0%0/2
A
A= $counts[2]
Aresult
11
00
343  
344 0 my $df = $self->df;
345  
346 0 my $METRIC;
347 0 ▶ $METRIC = "structure" if $opts{struct};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"struct"} 0 0 0
348 0 ▶ $METRIC = "owned" if $opts{owned};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"owned"} 0 0 0
349  
350 0 my @svs = $df->heap;
351  
352 0 ▶ my $method = $METRIC ? "${METRIC}_size" : "size";
Branch: 1 branch
BranchTrueFalseTotal
$METRIC ? : 0 0 0
353  
354 0 my $heap_total = scalar @svs;
355 0 my $count = 0;
356 0 foreach my $sv ( @svs ) {
357 0 $count++;
358 0 ▶ $self->report_progress( sprintf "Calculating sizes in %d of %d (%.2f%%)",
Branch: 1 branch
BranchTrueFalseTotal
if $count % 20000 == 0 0 0 0
359 $count, $heap_total, 100*$count / $heap_total ) if $count % 20000 == 0;
360 0 $sv->$method;
361 }
362 0 $self->report_progress();
363  
364 0 undef %seen;
365 0 list_largest_svs( \@svs, $METRIC, "", @counts );
366 }
367  
368 =head1 AUTHOR
369  
370 Paul Evans <leonerd@leonerd.org.uk>
371  
372 =cut
373  
374 5 0x55AA;
« blib/lib/Devel/MAT/Tool/Find.pm ↑ Summary blib/lib/Devel/MAT/SV.pm »