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

filter: Statementstmt 36.1% 34 / 94 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 64.7% 11 / 17 Podpod 0.0% 0 / 2 totaltotal 23.8% 45 / 189 CC 30 SCAR 55.1
CC
30
Cov
23%
count_svs
506.0
_gen_package_breakdown
30.0
__ANON__
12.0
CRAP 247.2
↑ Summary blib/lib/Devel/MAT/Tool.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::Count 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 CMD => "count";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use constant CMD_DESC => "Count the various kinds of SV";
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use List::Util qw( sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use List::UtilsBy qw( rev_nsort_by );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Struct::Dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 C<Devel::MAT::Tool::Count> - count the various kinds of SV
25  
26 =head1 DESCRIPTION
27  
28 This C<Devel::MAT> tool counts the different kinds of SV in the heap.
29  
30 =cut
31  
32 =head1 COMMANDS
33  
34 =for highlighter
35  
36 =head2 count
37  
38 pmat> count
39 Kind Count (blessed) Bytes (blessed)
40 ARRAY 170 0 15.1 KiB
41 CODE 166 0 20.8 KiB
42  
43 Prints a summary of the count of each type of object.
44  
45 Takes the following named options:
46  
47 =over 4
48  
49 =item --blessed, -b
50  
51 Additionally classify blessed references per package
52  
53 =item --scalars, -S
54  
55 Additionally classify SCALAR SVs according to which fields they have present
56  
57 =item --struct
58  
59 Use the structural size to sum byte counts
60  
61 =item --owned
62  
63 Use the owned size to sum byte counts
64  
65 =back
66  
67 =cut
68  
69 5 ▶use constant CMD_OPTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
70 blessed => { help => "classify blessed references per package",
71 alias => "b" },
72 scalars => { help => "classify SCALARs according to present fields",
73 alias => "S" },
74 struct => { help => "sum SVs by structural size" },
75 owned => { help => "sum SVs by owned size" },
76 5 );
77  
78 5 struct Counts => [qw( svs bytes blessed_svs blessed_bytes )];
79  
80 0 ▶sub run ( $self, $optsref )
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
81 {
82 0 $self->count_svs( $optsref->%* );
83 }
84  
85 0 ▶sub count_svs ( $self, %opts )
Subroutine count_svs: not called CC=22 SCAR=62.3
Pod undocumented
86 {
87 # TODO: consider options for
88 # sorting
89 # filtering
90  
91 my $size_meth = $opts{owned} ? "owned_size" :
92 0 ▶ $opts{struct} ? "structure_size" :
Branch: 2 branches
BranchTrueFalseTotal
$opts{'owned'} ? : 0 0 0
$opts{'struct'} ? : 0 0 0
93 "size";
94  
95 # Options for bin/pmat-counts
96 my $emit_count = $opts{emit_count} //
97 0 ▶ sub ( $kind, $is_blessed, $count ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
98 0 ▶ ( !$is_blessed || $count ) ? $count : ""
Branch: 1 branch
BranchTrueFalseTotal
!$is_blessed || $count ? : 0 0 0
Condition0%0/3
not $is_blessed or $count
l!l&&r!l&&!r
000
MC/DC0%0/2
not $is_blessed or $count
not $is_blessed $count
Truth table0%0/3
not(A) or B
A= not $is_blessedB= $count
ABresult
1X1
011
000
99 0 ▶ };
Condition0%0/3
$opts{"emit_count"} // sub ($kind, $is_blessed, $count) { !$is_blessed || $count ? $count : ""; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"emit_count"} // sub ($kind, $is_blessed, $count) { !$is_blessed || $count ? $count : ""; }
$opts{"emit_count"} sub ($kind, $is_blessed, $count) { !$is_blessed || $count ? $count : ""; }
Truth table0%0/3
A // B
A= $opts{"emit_count"}B= sub ($kind, $is_blessed, $count) { !$is_blessed || $count ? $count : ""; }
ABresult
1X1
011
000
100 my $emit_bytes = $opts{emit_bytes} //
101 0 ▶ sub ( $kind, $is_blessed, $bytes ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
102 0 ▶ ( !$is_blessed || $bytes ) ? Devel::MAT::Cmd->format_bytes( $bytes ) : ""
Branch: 1 branch
BranchTrueFalseTotal
!$is_blessed || $bytes ? : 0 0 0
Condition0%0/3
not $is_blessed or $bytes
l!l&&r!l&&!r
000
MC/DC0%0/2
not $is_blessed or $bytes
not $is_blessed $bytes
Truth table0%0/3
not(A) or B
A= not $is_blessedB= $bytes
ABresult
1X1
011
000
103 0 ▶ };
Condition0%0/3
$opts{"emit_bytes"} // sub ($kind, $is_blessed, $bytes) { !$is_blessed || $bytes ? "Devel::MAT::Cmd"->format_bytes($bytes) : ""; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"emit_bytes"} // sub ($kind, $is_blessed, $bytes) { !$is_blessed || $bytes ? "Devel::MAT::Cmd"->format_bytes($bytes) : ""; }
$opts{"emit_bytes"} sub ($kind, $is_blessed, $bytes) { !$is_blessed || $bytes ? "Devel::MAT::Cmd"->format_bytes($bytes) : ""; }
Truth table0%0/3
A // B
A= $opts{"emit_bytes"}B= sub ($kind, $is_blessed, $bytes) { !$is_blessed || $bytes ? "Devel::MAT::Cmd"->format_bytes($bytes) : ""; }
ABresult
1X1
011
000
104  
105 0 my %counts;
106 my %counts_SCALAR;
107 0 my %counts_per_package;
108  
109 0 foreach my $sv ( $self->df->heap ) {
110 0 my $kind = $sv->type;
111 0 ▶ my $c = $counts{$kind} //= Counts( ( 0 ) x 4 );
Condition0%0/3
$counts{$kind} //= Counts((0) x 4)
l!l&&r!l&&!r
000
MC/DC0%0/2
$counts{$kind} //= Counts((0) x 4)
$counts{$kind} Counts((0) x 4)
Truth table0%0/3
A //= B
A= $counts{$kind}B= Counts((0) x 4)
ABresult
1X1
011
000
112 0 my $bytes = $sv->$size_meth;
113  
114 0 $c->svs++;
115 0 $c->bytes += $bytes;
116  
117 0 ▶ if( $sv->blessed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->blessed) 0 0 0
118 0 $c->blessed_svs++;
119 0 $c->blessed_bytes += $bytes;
120 }
121  
122 0 ▶ if( $opts{scalars} and $sv->isa( "Devel::MAT::SV::SCALAR" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"scalars"} and $sv->isa("Devel::MAT::SV::SCALAR")) 0 0 0
Condition0%0/3
$opts{"scalars"} and $sv->isa("Devel::MAT::SV::SCALAR")
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"scalars"} and $sv->isa("Devel::MAT::SV::SCALAR")
$opts{"scalars"} $sv->isa("Devel::MAT::SV::SCALAR")
Truth table0%0/3
A and B
A= $opts{"scalars"}B= $sv->isa("Devel::MAT::SV::SCALAR")
ABresult
0X0
100
111
123 0 my $desc = $sv->desc;
124  
125 0 ▶ $c = $counts_SCALAR{$desc} //= Counts( ( 0 ) x 4 );
Condition0%0/3
$counts_SCALAR{$desc} //= Counts((0) x 4)
l!l&&r!l&&!r
000
MC/DC0%0/2
$counts_SCALAR{$desc} //= Counts((0) x 4)
$counts_SCALAR{$desc} Counts((0) x 4)
Truth table0%0/3
A //= B
A= $counts_SCALAR{$desc}B= Counts((0) x 4)
ABresult
1X1
011
000
126  
127 0 $c->svs++;
128 0 $c->bytes += $bytes;
129  
130 0 ▶ if( $sv->blessed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->blessed) 0 0 0
131 0 $c->blessed_svs++;
132 0 $c->blessed_bytes += $bytes;
133 }
134 }
135  
136 0 ▶ $opts{blessed} or next;
Branch: 1 branch
BranchTrueFalseTotal
$opts{"blessed"} or next 0 0 0
137  
138 0 ▶ if( $sv->blessed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->blessed) 0 0 0
139 0 ▶ $c = $counts_per_package{ref $sv}{ $sv->blessed->stashname } //= Counts( ( 0 ) x 4 );
Condition0%0/3
$counts_per_package{ref $sv}{$sv->blessed->stashname} //= Counts((0) x 4)
l!l&&r!l&&!r
000
MC/DC0%0/2
$counts_per_package{ref $sv}{$sv->blessed->stashname} //= Counts((0) x 4)
$counts_per_package{ref $sv}{$sv->blessed->stashname} Counts((0) x 4)
Truth table0%0/3
A //= B
A= $counts_per_package{ref $sv}{$sv->blessed->stashname}B= Counts((0) x 4)
ABresult
1X1
011
000
140 0 $c->blessed_svs++;
141 0 $c->blessed_bytes += $bytes;
142 }
143 }
144  
145 0 my @table;
146  
147 0 foreach my $kind ( sort keys %counts ) {
148 0 my $c = $counts{$kind};
149  
150 0 push @table, [ $kind,
151 $emit_count->( $kind, 0, $c->svs ),
152 $emit_count->( $kind, 1, $c->blessed_svs ),
153 $emit_bytes->( $kind, 0, $c->bytes ),
154 $emit_bytes->( $kind, 1, $c->blessed_bytes ) ];
155  
156 0 ▶ push @table, _gen_package_breakdown( $counts_per_package{$_}, $emit_count, $emit_bytes ) if $opts{blessed};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"blessed"} 0 0 0
157  
158 0 ▶ if( $kind eq "SCALAR" and $opts{scalars} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "SCALAR" and $opts{"scalars"}) 0 0 0
Condition0%0/3
$kind eq "SCALAR" and $opts{"scalars"}
!ll&&!rl&&r
000
MC/DC0%0/2
$kind eq "SCALAR" and $opts{"scalars"}
$kind eq "SCALAR" $opts{"scalars"}
Truth table0%0/3
A and B
A= $kind eq "SCALAR"B= $opts{"scalars"}
ABresult
0X0
100
111
159 0 foreach ( sort keys %counts_SCALAR ) {
160 0 my $c = $counts_SCALAR{$_};
161  
162 0 push @table, [ " $_",
163 $emit_count->( $_, 0, $c->svs ),
164 $emit_count->( $_, 1, $c->blessed_svs ),
165 $emit_bytes->( $_, 0, $c->bytes ),
166 $emit_bytes->( $_, 1, $c->blessed_bytes ) ];
167 }
168 }
169 }
170  
171 0 push @table, []; # HR
172  
173 0 my $total = Counts( ( 0 ) x 4 );
174 0 foreach my $method (qw( svs bytes blessed_svs blessed_bytes )) {
175 0 $total->$method = sum map { $_->$method } values %counts;
176 }
177  
178 0 push @table, [ "(total)",
179 $emit_count->( "(total)", 0, $total->svs ),
180 $emit_count->( "(total)", 1, $total->blessed_svs ),
181 $emit_bytes->( "(total)", 0, $total->bytes ),
182 $emit_bytes->( "(total)", 1, $total->blessed_bytes ) ];
183  
184 Devel::MAT::Cmd->print_table( \@table,
185 indent => 2,
186 headings => [ "Kind", "Count", "(blessed)", "Bytes", "(blessed)" ],
187 sep => [ " ", " ", " ", " " ],
188 align => [ undef, "right", "right", "right", "right" ],
189 0 ▶ ( $opts{table_args} || {} )->%*,
Condition0%0/2
$opts{'table_args'} || {}
l!l
00
MC/DC0%0/1
$opts{'table_args'} || {}
$opts{'table_args'}
Truth table0%0/2
A
A= $opts{'table_args'}
Aresult
11
00
190 );
191 }
192  
193 0 ▶sub _gen_package_breakdown ( $counts, $emit_count, $emit_bytes )
Subroutine _gen_package_breakdown: not called CC=5 SCAR=34.0
194 {
195 0 ▶ my @packages = rev_nsort_by { $counts->{$_}->blessed_svs } sort keys %$counts;
Subroutine __ANON__: not called CC=1 SCAR=6.9
196  
197 0 my @ret;
198  
199 my $count;
200 0 ▶ while( @packages ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@packages) 0 0 0
201 0 my $package = shift @packages;
202  
203 push @ret,
204 [
205 " " . Devel::MAT::Cmd->format_symbol( $package ),
206 $emit_count->( $package, 0, 0 ),
207 $emit_count->( $package, 1, $counts->{$package}->blessed_svs ),
208 $emit_bytes->( $package, 0, 0 ),
209 0 $emit_bytes->( $package, 1, $counts->{$package}->blessed_bytes ),
210 ];
211  
212 0 $count++;
213 0 ▶ last if $count >= 10;
Branch: 1 branch
BranchTrueFalseTotal
if $count >= 10 0 0 0
214 }
215  
216 0 my $remaining = Counts( ( 0 ) x 4 );
217 0 foreach my $method (qw( blessed_svs blessed_bytes )) {
218 0 $remaining->$method = sum map { $counts->{$_}->$method } @packages;
219 }
220  
221 0 ▶ push @ret,
Branch: 1 branch
BranchTrueFalseTotal
if @packages 0 0 0
222 [ " " . Devel::MAT::Cmd->format_note( "(others)" ),
223 $emit_count->( "(others)", 0, 0 ),
224 $emit_count->( "(others)", 1, $remaining->blessed_svs ),
225 $emit_bytes->( "(others)", 0, 0 ),
226 $emit_bytes->( "(others)", 1, $remaining->blessed_bytes ),
227 ] if @packages;
228  
229 0 return @ret;
230 }
231  
232 =head1 AUTHOR
233  
234 Paul Evans <leonerd@leonerd.org.uk>
235  
236 =cut
237  
238 5 0x55AA;
↑ Summary blib/lib/Devel/MAT/Tool.pm »