| 1 | package Dash::Leak; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.008008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Dash::Leak - Track memory allocation | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Quick summary of what the module does. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | BEGIN { | ||||||||||||||||||||||||||||
| 21 | # enables operation of Dash::Leak, leaksz is a nop without this | ||||||||||||||||||||||||||||
| 22 | $ENV{DEBUG_MEM} = 1; | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | use Dash::Leak; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||
| 28 | leaksz "block label"; | ||||||||||||||||||||||||||||
| 29 | # some code, that may leak | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | # If your code leaked, you'll be noticed about change | ||||||||||||||||||||||||||||
| 32 | # of process vsize after leaving block | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | leaksz "tests begin"; | ||||||||||||||||||||||||||||
| 35 | some_operation($arg); | ||||||||||||||||||||||||||||
| 36 | leaksz "some_operation", sub { | ||||||||||||||||||||||||||||
| 37 | warn sprintf "We leaked after some_operation($arg) by %+d kilobytes;", shift | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | another_operation(); | ||||||||||||||||||||||||||||
| 40 | leaksz "another_operation"; | ||||||||||||||||||||||||||||
| 41 | # ... | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | use Dash::Leak sub { ... }; # Will call this cb for every alloc, instead of warn | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Export of this module is "virtual", by using L<Devel::Declare>. | ||||||||||||||||||||||||||||
| 48 | When C<$ENV{DEBUG_MEM}> is true, it will work, when false, this opcodes will be ignored, like with C<leaksz ... if 0>; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head2 leaksz $label, [$cb->($delta)] | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Starts tracking current block. | ||||||||||||||||||||||||||||
| 55 | If something changed since last note, notice will be warned. | ||||||||||||||||||||||||||||
| 56 | If callback is passed, it will be invoked instead of warn. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | use Devel::Declare (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | use Guard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub sz(); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=15.0
| |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | if ($^O eq 'freebsd') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | 0 | require BSD::Process; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | *sz = sub () { BSD::Process->new->{size} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | elsif ($^O eq 'linux') { | ||||||||||||||||||||||||||||
| 71 | 4 | require Proc::ProcessTable; | |||||||||||||||||||||||||||
| 72 | 4 | ▶ | *sz = sub () { (map { $_->{size} } grep { $_->{pid} == $$ } @{Proc::ProcessTable->new->table})[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 0 | require Win32::Process::Info; | |||||||||||||||||||||||||||
| 75 | 0 | Win32::Process::Info->import( 'WMI' ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $pi = Win32::Process::Info->new; | |||||||||||||||||||||||||||
| 78 | 0 | $pi->Set( elapsed_in_seconds => 0 ); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | *sz = sub () { $pi->GetProcInfo( { no_user_info => 1 }, $$ )->[0]->{PrivatePageCount} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | our $cmem = 0; | |||||||||||||||||||||||||||
| 85 | 4 | our $SUBNAME = 'leaksz'; | |||||||||||||||||||||||||||
| 86 | 4 | our $idx; | |||||||||||||||||||||||||||
| 87 | 4 | our $OUT = 0; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 90 | 4 | ▶ | if ($ENV{DEBUG_MEM}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | my $debug = $ENV{DEBUG_MEM}; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | *DEBUG = sub () { $debug }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 2 | *DEBUG = sub () { 0 }; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | our $FIRST; | |||||||||||||||||||||||||||
| 99 | 4 | our %CBS; | |||||||||||||||||||||||||||
| 100 | 4 | ▶ | sub import{ | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 101 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 102 | 4 | my $caller = caller; | |||||||||||||||||||||||||||
| 103 | 4 | ▶ | my $cb = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 4 | check("use $class from @{[ (caller)[1,2] ]}",$cb ? $cb : ()) if DEBUG; | |||||||||||||||||||||||||||
| 105 | 4 | if (DEBUG and $cb) { | |||||||||||||||||||||||||||
| 106 | $FIRST ||= $cb; | ||||||||||||||||||||||||||||
| 107 | $CBS{$caller} = $cb; | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | Devel::Declare->setup_for( | ||||||||||||||||||||||||||||
| 110 | 4 | $caller, | |||||||||||||||||||||||||||
| 111 | { $SUBNAME => { const => \&parse } } | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 3 | *{$caller.'::'.$SUBNAME } = sub() { DEBUG }; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 132 | ▶ | sub check(@) { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=10 SCAR=23.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 4 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 134 | my $cb; | |||||||||||||||||||||||||||
| 122 | 134 | ▶ | $cb = pop if @_ > 1 and UNIVERSAL::isa( $_[-1], 'CODE' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ > 1 and UNIVERSAL::isa($_[-1], "CODE")
MC/DC50%1/2
@_ > 1 and UNIVERSAL::isa($_[-1], "CODE")
@_ > 1
UNIVERSAL::isa($_[-1], "CODE")
Truth table66%2/3
A and B
A= @_ > 1B= UNIVERSAL::isa($_[-1], "CODE")
| |||||||||||||||||||||||||||||
| 123 | 134 | my $op = "@_"; | |||||||||||||||||||||||||||
| 124 | 132 | my $mem = sz / 1024; | |||||||||||||||||||||||||||
| 125 | 132 | my $delta = $mem - $cmem; | |||||||||||||||||||||||||||
| 126 | 132 | ▶ | if ($delta != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 4 | $cmem = $mem; | |||||||||||||||||||||||||||
| 128 | 4 | ▶ | if ($cb) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 3 | ▶ | $cb->($delta,$OUT ? 'out' : 'in' ,$op); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||
| 131 | 1 | my ($caller,$file,$line) = (caller($OUT))[0..2]; | |||||||||||||||||||||||||||
| 132 | 1 | ▶ | if (exists $CBS{$caller}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $CBS{$caller}->($delta, $OUT ? 'out' : 'in' ,$op); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||||||||||
| 135 | 1 | ▶ | warn sprintf "%s %s: %+dk at %s line %s\n",($OUT ? '<-' : '->'),$op,$delta,$file,$line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 132 | ▶ | return 1 if $OUT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 66 | ▶ | return guard { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 141 | 66 | local $OUT = 1; | |||||||||||||||||||||||||||
| 142 | 66 | ▶ | check($op,$cb ? $cb : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 66 | }; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 4 | my $offset = $_[1]; | |||||||||||||||||||||||||||
| 149 | 4 | $offset += Devel::Declare::toke_move_past_token($offset); | |||||||||||||||||||||||||||
| 150 | 4 | my $linestr = Devel::Declare::get_linestr(); | |||||||||||||||||||||||||||
| 151 | 4 | substr($linestr,$offset,0) = 'and my $__leaksz_'.++$idx.'__ = '.__PACKAGE__.'::check'; | |||||||||||||||||||||||||||
| 152 | 4 | Devel::Declare::set_linestr($linestr); | |||||||||||||||||||||||||||
| 153 | 4 | return; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 157 | 4 | DEBUG and check("Finishing", $FIRST ? $FIRST : ()); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =over 4 | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item * Thanks to knevgen (L<http://github.com/knevgen>) for linux version patch | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =back | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Mons Anderson, C<< <mons at cpan.org> >> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Copyright 2010 Mons Anderson, all rights reserved. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 177 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 4 | 1; # End of Dash::Leak | |||||||||||||||||||||||||||