blib/lib/File/Locate/Iterator/FileMap.pm

filter: Statementstmt 94.3% 67 / 71 Branchbran 70.0% 7 / 10 Conditioncond 80.0% 4 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 3 totaltotal 86.9% 93 / 107 CC 10 SCAR 7.9
CC
10
Cov
91%
get
4.0
__ANON__
3.1
_have_mmap_layer
3.0
CRAP 2.2
« blib/lib/Iterator/Locate.pm ↑ Summary Iterator.xs »
1 # Copyright 2009, 2010, 2011, 2014, 2018, 2019 Kevin Ryde.
2 #
3 # This file is part of File-Locate-Iterator.
4 #
5 # File-Locate-Iterator is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option)
8 # any later version.
9 #
10 # File-Locate-Iterator is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with File-Locate-Iterator; see the file COPYING. Failing that, go to
17 # <http://www.gnu.org/licenses/>.
18  
19 package File::Locate::Iterator::FileMap;
20 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # uncomment this to run the ### lines
26 #use Devel::Comments;
27  
28 4 our $VERSION = 28;
29  
30 4 our %cache;
31  
32 43 ▶sub _key {
Subroutine _key: called CC=1 SCAR=0.0
33 43 my ($fh) = @_;
34 43 my ($dev, $ino, undef, undef, undef, undef, undef, $size) = stat ($fh);
35 43 return "$dev,$ino,$size,".tell($fh);
36 }
37 9 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod undocumented
38 9 my ($class, $fh) = @_;
39 9 return $cache{_key($fh)};
40 }
41  
42 # return a FileMap object which is $fh mmapped
43 34 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod undocumented
44 34 my ($class, $fh) = @_;
45  
46 34 my $key = _key($fh);
47 ### cache get: "$fh, $key, size=".(-s $fh)
48 34 ▶ return ($cache{$key} || do {
Condition100%3/3
$File::Locate::Iterator::FileMap::cache{$key} || do { require File::Map; 'File::Map'->VERSION('0.38'); require PerlIO::Layers; require Scalar::Util; PerlIO::Layers::query_handle($fh, 'mappable') or croak('Handle not mappable'); my $self = bless({'key', $key, 'mmap', undef}, $class); my $tell = tell $fh; if ($tell < 0) { croak("Cannot tell() file position: $!"); } ; File::Map::map_handle($self->{'mmap'}, $fh, '<', $tell); File::Map::advise($self->{'mmap'}, 'sequential'); builtin::weaken($File::Locate::Iterator::FileMap::cache{$key} = $self); $self }
l!l&&r!l&&!r
2293
MC/DC0%0/2
$File::Locate::Iterator::FileMap::cache{$key} || do { require File::Map; 'File::Map'->VERSION('0.38'); require PerlIO::Layers; require Scalar::Util; PerlIO::Layers::query_handle($fh, 'mappable') or croak('Handle not mappable'); my $self = bless({'key', $key, 'mmap', undef}, $class); my $tell = tell $fh; if ($tell < 0) { croak("Cannot tell() file position: $!"); } ; File::Map::map_handle($self->{'mmap'}, $fh, '<', $tell); File::Map::advise($self->{'mmap'}, 'sequential'); builtin::weaken($File::Locate::Iterator::FileMap::cache{$key} = $self); $self }
$File::Locate::Iterator::FileMap::cache{$key} do { require File::Map; 'File::Map'->VERSION('0.38'); require PerlIO::Layers; require Scalar::Util; PerlIO::Layers::query_handle($fh, 'mappable') or croak('Handle not mappable'); my $self = bless({'key', $key, 'mmap', undef}, $class); my $tell = tell $fh; if ($tell < 0) { croak("Cannot tell() file position: $!"); } ; File::Map::map_handle($self->{'mmap'}, $fh, '<', $tell); File::Map::advise($self->{'mmap'}, 'sequential'); builtin::weaken($File::Locate::Iterator::FileMap::cache{$key} = $self); $self }
Truth table66%2/3
A || B
A= $File::Locate::Iterator::FileMap::cache{$key}B= do { require File::Map; 'File::Map'->VERSION('0.38'); require PerlIO::Layers; require Scalar::Util; PerlIO::Layers::query_handle($fh, 'mappable') or croak('Handle not mappable'); my $self = bless({'key', $key, 'mmap', undef}, $class); my $tell = tell $fh; if ($tell < 0) { croak("Cannot tell() file position: $!"); } ; File::Map::map_handle($self->{'mmap'}, $fh, '<', $tell); File::Map::advise($self->{'mmap'}, 'sequential'); builtin::weaken($File::Locate::Iterator::FileMap::cache{$key} = $self); $self }
ABresult
1X1
011
000
49 32 require File::Map;
50 # File::Map->VERSION('0.35'); # for binary handled properly, maybe
51 32 File::Map->VERSION('0.38'); # for tainting
52 32 require PerlIO::Layers;
53 32 require Scalar::Util;
54  
55 32 ▶ PerlIO::Layers::query_handle ($fh, 'mappable')
Branch: 1 branch
BranchTrueFalseTotal
PerlIO::Layers::query_handle($fh, "mappable") or croak("Handle not mappable") 29 3 32
56 or croak "Handle not mappable";
57  
58 29 my $self = bless { key => $key,
59 mmap => undef,
60 }, $class;
61  
62 29 my $tell = tell($fh);
63 29 ▶ if ($tell < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tell < 0) 0 29 29
64 # assume if tell() doesn't work then $fh is not mmappable, or in any
65 # case don't know where the current position is to map
66 0 croak "Cannot tell() file position: $!";
67 }
68  
69 # File::Map 0.38 does tainting itself
70 #
71 # # induce taint on the mmap -- seems to cause segvs though
72 # read $fh, $self->{'mmap'}, 0;
73 # use Devel::Peek;
74 # Dump ($self->{'mmap'});
75 #
76 # # crib: must taint before mapping, doesn't work afterwards
77 # require Taint::Util;
78 # Taint::Util::taint($self->{'mmap'});
79  
80 29 File::Map::map_handle ($self->{'mmap'}, $fh, '<', $tell);
81 29 File::Map::advise ($self->{'mmap'}, 'sequential');
82  
83 29 Scalar::Util::weaken ($cache{$key} = $self);
84 29 $self;
85 });
86 }
87 # return a scalar ref to the mmapped string
88 29 ▶sub mmap_ref {
Subroutine mmap_ref: called CC=1 SCAR=0.0
Pod undocumented
89 29 my ($self) = @_;
90 29 return \($self->{'mmap'});
91 }
92 29 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
93 29 my ($self) = @_;
94 29 delete $cache{$self->{'key'}};
95 }
96  
97 4 ▶use constant::defer _PAGESIZE => sub {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=3 SCAR=11.4
98 4 require POSIX;
99 4 ▶ my $pagesize = eval { POSIX::sysconf (POSIX::_SC_PAGESIZE()) } || -1;
Condition50%1/2
eval { do { POSIX::sysconf(&POSIX::_SC_PAGESIZE()) } } || -1
l!l
40
MC/DC0%0/1
eval { do { POSIX::sysconf(&POSIX::_SC_PAGESIZE()) } } || -1
eval { do { POSIX::sysconf(&POSIX::_SC_PAGESIZE()) } }
Truth table50%1/2
A
A= eval { do { POSIX::sysconf(&POSIX::_SC_PAGESIZE()) } }
Aresult
11
00
100 4 ▶ return ($pagesize > 0 ? $pagesize : 1024);
Branch: 1 branch
BranchTrueFalseTotal
$pagesize > 0 ? : 4 0 4
101 4 };
102  
103 # return the total bytes used by mmaps here plus prospective further $space
104 10 ▶sub _total_space {
Subroutine _total_space: called CC=2 SCAR=7.6
105 10 my ($space) = @_;
106 ### total space of: $space, values(%cache)
107 10 $space = _round_up_pagesize($space);
108 10 foreach my $self (values %cache) {
109 0 $space += _round_up_pagesize (length (${$self->mmap_ref}));
110 }
111 10 return $space;
112 }
113 12 ▶sub _round_up_pagesize {
Subroutine _round_up_pagesize: called CC=1 SCAR=0.0
114 12 my ($n) = @_;
115  
116 12 my $pagesize = _PAGESIZE();
117 12 return $pagesize * int (($n + $pagesize - 1) / $pagesize);
118 }
119  
120 #-----------------------------------------------------------------------------
121  
122 # return true if $fh has an ":mmap" layer
123 4 ▶sub _have_mmap_layer {
Subroutine _have_mmap_layer: called CC=3 SCAR=11.0
124 4 my ($fh) = @_;
125 4 my $ret;
126 4 eval {
127 4 require PerlIO; # new in perl 5.8
128 4 foreach my $layer (PerlIO::get_layers ($fh)) {
129 11 ▶ if ($layer eq 'mmap') { $ret = 1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($layer eq "mmap") 1 10 11
130 }
131 };
132 4 return $ret;
133 }
134  
135 # return true if mmapping $fh would be an excessive cumulative size
136 8 ▶sub _mmap_size_excessive {
Subroutine _mmap_size_excessive: called CC=2 SCAR=7.1
137 8 my ($fh) = @_;
138 8 ▶ if (File::Locate::Iterator::FileMap->find($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Locate::Iterator::FileMap"->find($fh)) 0 8 8
139 # if already mapped then not excessive
140 0 return 0;
141 }
142  
143 # in 32-bits this is 4G*(1/4)*(1/5) which is 200Mb
144 8 require Config;
145 my $limit
146 8 = (2 ** (8 * $Config::Config{'ptrsize'})) # eg. 2^32 bytes addr space
147 * 0.25 # perhaps only 1/2 or 1/4 of it usable for data
148 * 0.2; # then don't go past 1/5 of that usable space
149  
150 8 my $prosp = File::Locate::Iterator::FileMap::_total_space (-s $fh);
151 ### mmap size limit: $limit
152 ### file size: -s $fh
153 ### for new total: $prosp
154 8 return ($prosp > $limit);
155 }
156  
157 4 1;
158 __END__
« blib/lib/Iterator/Locate.pm ↑ Summary Iterator.xs »