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