| 1 | package Test::NoLeaks; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use POSIX qw/sysconf _SC_PAGESIZE/; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Test::Builder; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Test::More; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 6 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 6 | our @EXPORT = qw/test_noleaks/; ## no critic (ProhibitAutomaticExportation) | |||||||||||||||||||||||||||||||||
| 14 | 6 | our @EXPORT_OK = qw/noleaks/; | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | Test::NoLeaks - Memory and file descriptor leak detector | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | use Test::NoLeaks; | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | test_noleaks ( | ||||||||||||||||||||||||||||||||||
| 25 | code => sub{ | ||||||||||||||||||||||||||||||||||
| 26 | # code that might leak | ||||||||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||||||||
| 28 | track_memory => 1, | ||||||||||||||||||||||||||||||||||
| 29 | track_fds => 1, | ||||||||||||||||||||||||||||||||||
| 30 | passes => 2, | ||||||||||||||||||||||||||||||||||
| 31 | warmup_passes => 1, | ||||||||||||||||||||||||||||||||||
| 32 | tolerate_hits => 0, | ||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | Sample output: | ||||||||||||||||||||||||||||||||||
| 36 | # pass 1, leaked: 225280 bytes 0 file descriptors | ||||||||||||||||||||||||||||||||||
| 37 | # pass 36, leaked: 135168 bytes 0 file descriptors | ||||||||||||||||||||||||||||||||||
| 38 | # pass 52, leaked: 319488 bytes 0 file descriptors | ||||||||||||||||||||||||||||||||||
| 39 | # pass 84, leaked: 135168 bytes 0 file descriptors | ||||||||||||||||||||||||||||||||||
| 40 | # pass 98, leaked: 155648 bytes 0 file descriptors | ||||||||||||||||||||||||||||||||||
| 41 | not ok 1214 - Leaked 970752 bytes (5 hits) 0 file descriptors | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | test_noleaks ( | ||||||||||||||||||||||||||||||||||
| 44 | code => sub { ... }, | ||||||||||||||||||||||||||||||||||
| 45 | track_memory => 1, | ||||||||||||||||||||||||||||||||||
| 46 | passes => 2, | ||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | # old-school way | ||||||||||||||||||||||||||||||||||
| 50 | use Test::More; | ||||||||||||||||||||||||||||||||||
| 51 | use Test::NoLeaks qw/noleaks/; | ||||||||||||||||||||||||||||||||||
| 52 | ok noleaks( | ||||||||||||||||||||||||||||||||||
| 53 | code => sub { ... }, | ||||||||||||||||||||||||||||||||||
| 54 | track_memory => ..., | ||||||||||||||||||||||||||||||||||
| 55 | track_fds => ..., | ||||||||||||||||||||||||||||||||||
| 56 | passes => ..., | ||||||||||||||||||||||||||||||||||
| 57 | warmup_passes => ..., | ||||||||||||||||||||||||||||||||||
| 58 | ), "non-leaked code description"; | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | It is hard to track memory leaks. There are a lot of perl modules (e.g. | ||||||||||||||||||||||||||||||||||
| 63 | L<Test::LeakTrace>), that try to B<detect> and B<point> leaks. Unfortunately, | ||||||||||||||||||||||||||||||||||
| 64 | they do not always work, and they are rather limited because they are not | ||||||||||||||||||||||||||||||||||
| 65 | able to detect leaks in XS-code or external libraries. | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | Instead of examining perl internals, this module offers a bit naive empirical | ||||||||||||||||||||||||||||||||||
| 68 | approach: let the suspicious code to be launched in infinite loop | ||||||||||||||||||||||||||||||||||
| 69 | some time and watch (via tools like C<top>)if the memory consumption by | ||||||||||||||||||||||||||||||||||
| 70 | perl process increses over time. If it does, while it is expected to | ||||||||||||||||||||||||||||||||||
| 71 | be constant (stabilized), then, surely, there are leaks. | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | This approach is able only to B<detect> and not able to B<point> them. The | ||||||||||||||||||||||||||||||||||
| 74 | module C<Test::NoLeaks> implements the general idea of the approach, which | ||||||||||||||||||||||||||||||||||
| 75 | might be enough in many cases. | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | =head1 INTERFACE | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | =head3 C<< test_noleaks >> | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | =head3 C<< noleaks >> | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | The mandatory hash has the following members | ||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | =over 2 | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | =item * C<code> | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | Suspicious for leaks subroutine, that will be executed multiple times. | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | =item * C<track_memory> | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | =item * C<track_fds> | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | Track memory or file descriptor leaks. At leas one of them should be | ||||||||||||||||||||||||||||||||||
| 96 | specified. | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | In B<Unices>, every socket is file descriptor too, so, C<track_fds> | ||||||||||||||||||||||||||||||||||
| 99 | will be able to track unclosed sockets, i.e. network connections. | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | =item * C<passes> | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | How many times C<code> should be executed. If memory leak is too small, | ||||||||||||||||||||||||||||||||||
| 104 | number of passes should be large enough to trigger additional pages | ||||||||||||||||||||||||||||||||||
| 105 | allocation for perl process, and the leak will be detected. | ||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | Page size is 4kb on linux, so, if C<code> leaks 4 bytes on every | ||||||||||||||||||||||||||||||||||
| 108 | pass, then C<1024> passes should be specified. | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | In general, the more passes are specified, the more chance to | ||||||||||||||||||||||||||||||||||
| 111 | detect possible leaks. | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | It is good idea to initally define C<passes> to some large number, | ||||||||||||||||||||||||||||||||||
| 114 | e.g. C<10_000> to be sure, that the suspicious code leaks, but then | ||||||||||||||||||||||||||||||||||
| 115 | decrease to some smaller number, enough to produce test fail report, | ||||||||||||||||||||||||||||||||||
| 116 | i.e. enough to produces 3-5 memory hits (additional pages allocations). | ||||||||||||||||||||||||||||||||||
| 117 | This will speed up tests execution and will save CO2 atmospheric | ||||||||||||||||||||||||||||||||||
| 118 | emissions a little bit. | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | Default value is C<100>. Minimal value is C<2>. | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | =item * C<warmup_passes> | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | How many times the C<code> should be executed before module starts | ||||||||||||||||||||||||||||||||||
| 125 | tracking resources consumption on executing the C<code> C<passes> | ||||||||||||||||||||||||||||||||||
| 126 | times. | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | If you have caches, memoizes etc., then C<warmup_passes> is your | ||||||||||||||||||||||||||||||||||
| 129 | friend. | ||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | Default value is C<0>. | ||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | =item * C<tolerate_hits> | ||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | How many passes, which considered leaked, should be ingnored, i.e. | ||||||||||||||||||||||||||||||||||
| 136 | maximal number of possible false leak reports. | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | Even if your code has no leaks, it might cause perl interpreter | ||||||||||||||||||||||||||||||||||
| 139 | allocate additional memory pages, e.g. due to memory fragmentation. | ||||||||||||||||||||||||||||||||||
| 140 | Those allocations are legal, and should not be treated as leaks. | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | Use this B<only> when memory leaks are already fixed, but there | ||||||||||||||||||||||||||||||||||
| 143 | are still false leak reports from C<test_leak>. This value expected | ||||||||||||||||||||||||||||||||||
| 144 | to be small enough, i.e. C<1> or C<2>. For additional assurance, please, | ||||||||||||||||||||||||||||||||||
| 145 | increase C<passes> value, if C<tolarate_hits> is non-zero. | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | Default value is C<0>. | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | =back | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | =head1 MEMORY LEAKS TESTING TECHNIQUES | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | C<Test::NoLeaks> can be used to test web applications for memory leaks. | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | Let's consider you have the following suspicious code | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | sub might_leak { | ||||||||||||||||||||||||||||||||||
| 158 | my $t = Test::Mojo->new('MyApp'); | ||||||||||||||||||||||||||||||||||
| 159 | $t->post_ok('/search.json' => form => {q => 'Perl'}) | ||||||||||||||||||||||||||||||||||
| 160 | ->status_is(200); | ||||||||||||||||||||||||||||||||||
| 161 | ...; | ||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | test_noleaks ( | ||||||||||||||||||||||||||||||||||
| 165 | code => \&might_leak, | ||||||||||||||||||||||||||||||||||
| 166 | track_memory => 1, | ||||||||||||||||||||||||||||||||||
| 167 | track_fds => 1, | ||||||||||||||||||||||||||||||||||
| 168 | passes => 1000, | ||||||||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | The C<might_leak> subroutine isn't optimal for leak detection, because it | ||||||||||||||||||||||||||||||||||
| 172 | mixes infrastructure-related code (application) with request code. Let's | ||||||||||||||||||||||||||||||||||
| 173 | consider, that there is a leak: every request creates some data and puts | ||||||||||||||||||||||||||||||||||
| 174 | it into application, but forgets to do clean up. As soon as the application | ||||||||||||||||||||||||||||||||||
| 175 | is re-created on every pass, the leaked data might be destroyed together | ||||||||||||||||||||||||||||||||||
| 176 | with the application, and leak might remain undetected. | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | So, the code under test should look much more production like, i.e. | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | my $t = Test::Mojo->new('MyApp'); | ||||||||||||||||||||||||||||||||||
| 181 | ok($t); | ||||||||||||||||||||||||||||||||||
| 182 | sub might_leak { | ||||||||||||||||||||||||||||||||||
| 183 | $t->post_ok('/search.json' => form => {q => 'Perl'}) | ||||||||||||||||||||||||||||||||||
| 184 | ->status_is(200); | ||||||||||||||||||||||||||||||||||
| 185 | ...; | ||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | That way web-application is created only once, and leaks will be tracked | ||||||||||||||||||||||||||||||||||
| 189 | on request-related code. | ||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||
| 191 | Anyway, C<might_leak> still wrong, because it unintentionally leaks due to | ||||||||||||||||||||||||||||||||||
| 192 | use of direct or indirect L<Test::More> functions, like C<ok> or | ||||||||||||||||||||||||||||||||||
| 193 | C<post_ok>. They should not be used; if you still need to assert, that | ||||||||||||||||||||||||||||||||||
| 194 | C<might_leak> works propertly, you can use C<BAIL_OUT> subroutine, | ||||||||||||||||||||||||||||||||||
| 195 | to cancel any further testing, e.g. | ||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||
| 197 | sub might_leak { | ||||||||||||||||||||||||||||||||||
| 198 | my $got = some_function_might_leak; | ||||||||||||||||||||||||||||||||||
| 199 | my $expected = "some_value"; | ||||||||||||||||||||||||||||||||||
| 200 | BAIL_OUT('some_function_might_leak does not work propertly!') | ||||||||||||||||||||||||||||||||||
| 201 | unless $got eq $expected; | ||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | Please, B<do not> use C<test_noleaks> more then once per test file. Consider | ||||||||||||||||||||||||||||||||||
| 207 | the following example: | ||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||
| 209 | # (A) | ||||||||||||||||||||||||||||||||||
| 210 | test_noleaks( | ||||||||||||||||||||||||||||||||||
| 211 | code => &does_not_leak_but_consumes_large_amount_of_memory, | ||||||||||||||||||||||||||||||||||
| 212 | ..., | ||||||||||||||||||||||||||||||||||
| 213 | ) | ||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||
| 215 | # (B) | ||||||||||||||||||||||||||||||||||
| 216 | test_noleaks( | ||||||||||||||||||||||||||||||||||
| 217 | code => &leaks_but_consumes_small_amount_of_memory, | ||||||||||||||||||||||||||||||||||
| 218 | ... | ||||||||||||||||||||||||||||||||||
| 219 | ) | ||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||
| 221 | In A-case OS already allocated large amount of memory for Perl interpreter. | ||||||||||||||||||||||||||||||||||
| 222 | In case-B perl might just re-use them, without allocating new ones, and | ||||||||||||||||||||||||||||||||||
| 223 | this will be false negative, i.e. memory leak might B<not> be reported. | ||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||
| 226 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||
| 228 | =over 2 | ||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||
| 230 | =item * Currently it works propertly only on B<Linux> | ||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||
| 232 | Patches or pull requests to support other OSes are welcome. | ||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||
| 234 | =item * The module will not work propertly in B<fork>ed child | ||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||
| 236 | It seems a little bit strange to use C<test_noleaks> or | ||||||||||||||||||||||||||||||||||
| 237 | C<noleaks> in forked child, but if you really need that, please, | ||||||||||||||||||||||||||||||||||
| 238 | send PR. | ||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||
| 240 | =back | ||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||
| 242 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||
| 244 | L<Test::MemoryGrowth> | ||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||
| 248 | 6 | my $PAGE_SIZE; | |||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||
| 250 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||
| 251 | 6 | ▶ | no strict "subs"; ## no critic (ProhibitNoStrict ProhibitProlongedStrictureOverride) | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||
| 253 | 6 | ▶ | $PAGE_SIZE = sysconf(_SC_PAGESIZE) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 254 | or die("page size cannot be determined, Test::NoLeaks cannot be used"); | ||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||
| 256 | 6 | ▶ | open(my $statm, '<', '/proc/self/statm') ## no critic (RequireBriefOpen) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 257 | or die("couldn't access /proc/self/status : $!"); | ||||||||||||||||||||||||||||||||||
| 258 | 30081 | ▶ | *_platform_mem_size = sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 259 | 30081 | my $line = <$statm>; | |||||||||||||||||||||||||||||||||
| 260 | 30081 | seek($statm, 0, 0); | |||||||||||||||||||||||||||||||||
| 261 | 30081 | my ($pages) = (split / /, $line)[0]; | |||||||||||||||||||||||||||||||||
| 262 | 30081 | return $pages * $PAGE_SIZE; | |||||||||||||||||||||||||||||||||
| 263 | 6 | }; | |||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||
| 265 | 6 | my $fd_dir = '/proc/self/fd'; | |||||||||||||||||||||||||||||||||
| 266 | 6 | ▶ | opendir(my $dh, $fd_dir) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 267 | or die "can't opendir $fd_dir: $!"; | ||||||||||||||||||||||||||||||||||
| 268 | 30074 | ▶ | *_platform_fds = sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 269 | 30074 | my $open_fd_count = () = readdir($dh); | |||||||||||||||||||||||||||||||||
| 270 | 30074 | rewinddir($dh); | |||||||||||||||||||||||||||||||||
| 271 | 30074 | return $open_fd_count; | |||||||||||||||||||||||||||||||||
| 272 | 6 | }; | |||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||
| 275 | 12 | ▶ | sub _noleaks { | ||||||||||||||||||||||||||||||||
|
Subroutine
_noleaks: called CC=20 SCAR=30.5
| |||||||||||||||||||||||||||||||||||
| 276 | 12 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||
| 278 | # check arguments | ||||||||||||||||||||||||||||||||||
| 279 | 12 | my $code = $args{code}; | |||||||||||||||||||||||||||||||||
| 280 | 12 | ▶ | die("code argument (CODEREF) isn't provided") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$code and ref $code eq "CODE"
MC/DC0%0/2
$code and ref $code eq "CODE"
$code
ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
| |||||||||||||||||||||||||||||||||||
| 281 | if (!$code || !(ref($code) eq 'CODE')); | ||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||
| 283 | 12 | my $track_memory = $args{'track_memory'}; | |||||||||||||||||||||||||||||||||
| 284 | 12 | my $track_fds = $args{'track_fds'}; | |||||||||||||||||||||||||||||||||
| 285 | 12 | ▶ | die("don't know what to track (i.e. no 'track_memory' nor 'track_fds' are specified)") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$track_memory or $track_fds
MC/DC0%0/2
$track_memory or $track_fds
$track_memory
$track_fds
Truth table66%2/3
A or B
A= $track_memoryB= $track_fds
| |||||||||||||||||||||||||||||||||||
| 286 | if (!$track_memory && !$track_fds); | ||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||
| 288 | 12 | ▶ | my $passes = $args{passes} || 100; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'passes'} || 100
MC/DC0%0/1
$args{'passes'} || 100
$args{'passes'}
Truth table50%1/2
A
A= $args{'passes'}
| |||||||||||||||||||||||||||||||||||
| 289 | 12 | ▶ | die("passes count too small (should be at least 2)") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 290 | if $passes < 2; | ||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | 12 | ▶ | my $warmup_passes = $args{warmup_passes} || 0; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'warmup_passes'} || 0
MC/DC100%1/1
$args{'warmup_passes'} || 0
$args{'warmup_passes'}
Truth table100%2/2
A
A= $args{'warmup_passes'}
| |||||||||||||||||||||||||||||||||||
| 293 | 12 | ▶ | die("warmup_passes count too small (should be non-negative)") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 294 | if $passes < 0; | ||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | # warm-up phase | ||||||||||||||||||||||||||||||||||
| 297 | # a) warm up code | ||||||||||||||||||||||||||||||||||
| 298 | 12 | $code->() for (1 .. $warmup_passes); | |||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | # b) warm-up package itself, as it might cause additional memory (re) allocations | ||||||||||||||||||||||||||||||||||
| 301 | # (ignore results) | ||||||||||||||||||||||||||||||||||
| 302 | 12 | ▶ | _platform_mem_size if $track_memory; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 303 | 12 | ▶ | _platform_fds if $track_fds; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 304 | 12 | my @leaked_at = map { [0, 0] } (1 .. $passes); # index: pass, value array[$mem_leak, $fds_leak] | |||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||
| 306 | # pre-allocate all variables, including those, which are used in cycle only | ||||||||||||||||||||||||||||||||||
| 307 | 12 | my ($total_mem_leak, $total_fds_leak, $memory_hits) = (0, 0, 0); | |||||||||||||||||||||||||||||||||
| 308 | 12 | my ($mem_t0, $fds_t0, $mem_t1, $fds_t1) = (0, 0, 0, 0); | |||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | # execution phase | ||||||||||||||||||||||||||||||||||
| 311 | 12 | for my $pass (0 .. $passes - 1) { | |||||||||||||||||||||||||||||||||
| 312 | 15042 | ▶ | $mem_t0 = _platform_mem_size if $track_memory; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 313 | 15042 | ▶ | $fds_t0 = _platform_fds if $track_fds; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 314 | 15042 | $code->(); | |||||||||||||||||||||||||||||||||
| 315 | 15042 | ▶ | $mem_t1 = _platform_mem_size if $track_memory; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 316 | 15042 | ▶ | $fds_t1 = _platform_fds if $track_fds; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||
| 318 | 15042 | my $leaked_mem = $mem_t1 - $mem_t0; | |||||||||||||||||||||||||||||||||
| 319 | 15042 | ▶ | $leaked_mem = 0 if ($leaked_mem < 0); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | 15042 | my $leaked_fds = $fds_t1 - $fds_t0; | |||||||||||||||||||||||||||||||||
| 322 | 15042 | ▶ | $leaked_fds = 0 if ($leaked_fds < 0); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||
| 324 | 15042 | $leaked_at[$pass]->[0] = $leaked_mem; | |||||||||||||||||||||||||||||||||
| 325 | 15042 | $leaked_at[$pass]->[1] = $leaked_fds; | |||||||||||||||||||||||||||||||||
| 326 | 15042 | $total_mem_leak += $leaked_mem; | |||||||||||||||||||||||||||||||||
| 327 | 15042 | $total_fds_leak += $leaked_fds; | |||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||
| 329 | 15042 | ▶ | $memory_hits++ if ($leaked_mem > 0); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | 12 | return ($total_mem_leak, $total_fds_leak, $memory_hits, \@leaked_at); | |||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||
| 335 | 11 | ▶ | sub noleaks(%) { ## no critic (ProhibitSubroutinePrototypes) | ||||||||||||||||||||||||||||||||
|
Subroutine
noleaks: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 336 | 11 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||
| 338 | 11 | my ($mem, $fds, $mem_hits) = _noleaks(%args); | |||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||
| 340 | 11 | ▶ | my $tolerate_hits = $args{tolerate_hits} || 0; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'tolerate_hits'} || 0
MC/DC100%1/1
$args{'tolerate_hits'} || 0
$args{'tolerate_hits'}
Truth table100%2/2
A
A= $args{'tolerate_hits'}
| |||||||||||||||||||||||||||||||||||
| 341 | 11 | my $track_memory = $args{'track_memory'}; | |||||||||||||||||||||||||||||||||
| 342 | 11 | my $track_fds = $args{'track_fds'}; | |||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||
| 344 | 11 | ▶ | my $has_fd_leaks = $track_fds && ($fds > 0); | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$track_fds && $fds > 0
MC/DC100%2/2
$track_fds && $fds > 0
$track_fds
$fds > 0
Truth table100%3/3
A && B
A= $track_fdsB= $fds > 0
| |||||||||||||||||||||||||||||||||||
| 345 | 11 | ▶ | my $has_mem_leaks = $track_memory && ($mem > 0) && ($mem_hits > $tolerate_hits); | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
$track_memory && $mem > 0 && $mem_hits > $tolerate_hits
$track_memory and $mem > 0
MC/DC100%3/3
$track_memory && $mem > 0 && $mem_hits > $tolerate_hits
$track_memory
$mem > 0
$mem_hits > $tolerate_hits
Truth table100%4/4
A and B && C
A= $track_memoryB= $mem > 0C= $mem_hits > $tolerate_hits
| |||||||||||||||||||||||||||||||||||
| 346 | 11 | ▶ | return !($has_fd_leaks || $has_mem_leaks); | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$has_fd_leaks || $has_mem_leaks
MC/DC100%2/2
$has_fd_leaks || $has_mem_leaks
$has_fd_leaks
$has_mem_leaks
Truth table100%3/3
A || B
A= $has_fd_leaksB= $has_mem_leaks
| |||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||
| 349 | 1 | ▶ | sub test_noleaks(%) { ## no critic (ProhibitSubroutinePrototypes) | ||||||||||||||||||||||||||||||||
|
Subroutine
test_noleaks: called CC=14 SCAR=42.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 350 | 1 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 351 | 1 | my ($mem, $fds, $mem_hits, $details) = _noleaks(%args); | |||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | 1 | ▶ | my $tolerate_hits = $args{tolerate_hits} || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'tolerate_hits'} || 0
MC/DC0%0/1
$args{'tolerate_hits'} || 0
$args{'tolerate_hits'}
Truth table50%1/2
A
A= $args{'tolerate_hits'}
| |||||||||||||||||||||||||||||||||||
| 354 | 1 | my $track_memory = $args{'track_memory'}; | |||||||||||||||||||||||||||||||||
| 355 | 1 | my $track_fds = $args{'track_fds'}; | |||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||
| 357 | 1 | ▶ | my $has_fd_leaks = $track_fds && ($fds > 0); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$track_fds && $fds > 0
MC/DC0%0/2
$track_fds && $fds > 0
$track_fds
$fds > 0
Truth table33%1/3
A && B
A= $track_fdsB= $fds > 0
| |||||||||||||||||||||||||||||||||||
| 358 | 1 | ▶ | my $has_mem_leaks = $track_memory && ($mem > 0) && ($mem_hits > $tolerate_hits); | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
$track_memory && $mem > 0 && $mem_hits > $tolerate_hits
$track_memory and $mem > 0
MC/DC0%0/3
$track_memory && $mem > 0 && $mem_hits > $tolerate_hits
$track_memory
$mem > 0
$mem_hits > $tolerate_hits
Truth table25%1/4
A and B && C
A= $track_memoryB= $mem > 0C= $mem_hits > $tolerate_hits
| |||||||||||||||||||||||||||||||||||
| 359 | 1 | ▶ | my $has_leaks = $has_fd_leaks || $has_mem_leaks; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$has_fd_leaks || $has_mem_leaks
MC/DC0%0/2
$has_fd_leaks || $has_mem_leaks
$has_fd_leaks
$has_mem_leaks
Truth table33%1/3
A || B
A= $has_fd_leaksB= $has_mem_leaks
| |||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||
| 361 | 1 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||||||||
| 362 | 1 | ▶ | if (!$has_leaks) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 363 | 1 | pass("no leaks have been found"); | |||||||||||||||||||||||||||||||||
| 364 | } else { | ||||||||||||||||||||||||||||||||||
| 365 | 0 | ▶ | my $summary = "Leaked " . ($track_memory ? "$mem bytes ($mem_hits hits) " : "") . ($track_fds ? "$fds file descriptors" : ""); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||
| 367 | 0 | my @lines; | |||||||||||||||||||||||||||||||||
| 368 | 0 | for my $pass (1 .. @$details) { | |||||||||||||||||||||||||||||||||
| 369 | 0 | my $v = $details->[$pass - 1]; | |||||||||||||||||||||||||||||||||
| 370 | 0 | my ($mem, $fds) = @$v; | |||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | if ($mem || $fds) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mem or $fds
MC/DC0%0/2
$mem or $fds
$mem
$fds
Truth table0%0/3
A or B
A= $memB= $fds
| |||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | my $line = "pass $pass, leaked: " . ($track_memory ? $mem . " bytes " : "") . ($track_fds ? $fds . " file descriptors" : ""); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 373 | 0 | push @lines, $line; | |||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||
| 376 | 0 | my $report = join("\n", @lines); | |||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||
| 378 | 0 | note($report); | |||||||||||||||||||||||||||||||||
| 379 | 0 | fail("$summary"); | |||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||
| 381 | 1 | return; | |||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||
| 384 | =head1 SOURCE CODE | ||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||
| 386 | L<GitHub|https://github.com/binary-com/perl-Test-NoLeaks> | ||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||
| 388 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||
| 390 | binary.com, C<< <perl at binary.com> >> | ||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||
| 392 | =head1 BUGS | ||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||
| 394 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||||||||
| 395 | L<https://github.com/binary-com/perl-Test-NoLeaks/issues>. | ||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||
| 397 | =cut | ||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||
| 399 | 6 | 1; | |||||||||||||||||||||||||||||||||