Perlbal/AIO.pm

filter: Statementstmt 48.9% 72 / 147 Branchbran 34.0% 17 / 50 Conditioncond 4.3% 1 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 67.7% 21 / 31 Podpod 0.0% 0 / 16 totaltotal 39.5% 111 / 281 CC 36 SCAR 23.2
CC
36
Cov
41%
get_aio_stats
30.0
aio_channel_cond_run
30.0
aio_channel_push
26.4
CRAP 10.2
« Perlbal/SocketSSL.pm ↑ Summary Perlbal/Socket.pm »
1 # AIO abstraction layer
2 #
3 # Copyright 2004, Danga Interactive, Inc.
4 # Copyright 2005-2007, Six Apart, Ltd.
5  
6 package Perlbal::AIO;
7  
8 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use POSIX qw(ENOENT EACCES EBADF);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Fcntl qw(SEEK_CUR SEEK_SET SEEK_END O_RDWR O_CREAT O_TRUNC);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Try and use IO::AIO, if it's around.
13 22 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=1.2
14 0 ▶ $Perlbal::OPTMOD_IO_AIO = eval "use IO::AIO 1.6 (); 1;";
Subroutine BEGIN: called CC=1 SCAR=1.2
15 }
16  
17 22 ▶END {
Subroutine END: called CC=2 SCAR=7.6
18 22 ▶ IO::AIO::max_parallel(0)
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::OPTMOD_IO_AIO 0 22 22
19 if $Perlbal::OPTMOD_IO_AIO;
20 }
21  
22 22 $Perlbal::AIO_MODE = "none";
23 22 ▶$Perlbal::AIO_MODE = "ioaio" if $Perlbal::OPTMOD_IO_AIO;
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::OPTMOD_IO_AIO 0 22 22
24  
25 ############################################################################
26 # AIO functions available to callers
27 ############################################################################
28  
29 1 ▶sub aio_rename {
Subroutine aio_rename: called CC=1 SCAR=0.2
Pod undocumented
30 1 my ($srcpath, $dstpath, $user_cb) = @_;
31 1 ▶ aio_channel_push(get_chan($srcpath), $user_cb, sub {
Subroutine __ANON__: called CC=3 SCAR=11.8
32 1 my $cb = shift;
33  
34 1 ▶ if ($Perlbal::AIO_MODE eq "ioaio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::AIO_MODE eq 'ioaio') { } 0 1 1
35 0 IO::AIO::aio_rename($srcpath, $dstpath, $cb);
36 } else {
37 1 my $rv = rename($srcpath, $dstpath);
38 1 ▶ $rv = $rv ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 1 0 1
39 1 $cb->($rv);
40 }
41 1 });
42 }
43  
44 46 ▶sub aio_readahead {
Subroutine aio_readahead: called CC=1 SCAR=0.5
Pod undocumented
45 46 my ($fh, $offset, $length, $user_cb) = @_;
46  
47 46 ▶ aio_channel_push(get_chan(), $user_cb, sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
48 46 my $cb = shift;
49 # $fh could end up closed.
50 46 ▶ if ($Perlbal::AIO_MODE eq "ioaio" && defined fileno($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::AIO_MODE eq 'ioaio' and defined fileno $fh) { } 0 46 46
Condition33%1/3
$Perlbal::AIO_MODE eq "ioaio" and defined fileno $fh
!ll&&!rl&&r
4600
MC/DC0%0/2
$Perlbal::AIO_MODE eq "ioaio" and defined fileno $fh
$Perlbal::AIO_MODE eq "ioaio" defined fileno $fh
Truth table33%1/3
A and B
A= $Perlbal::AIO_MODE eq "ioaio"B= defined fileno $fh
ABresult
0X0
100
111
51 0 IO::AIO::aio_readahead($fh, $offset, $length, $cb);
52 } else {
53 46 $cb->();
54 }
55 46 });
56 }
57  
58 85 ▶sub aio_stat {
Subroutine aio_stat: called CC=1 SCAR=0.1
Pod undocumented
59 85 my ($file, $user_cb) = @_;
60  
61 85 ▶ aio_channel_push(get_chan($file), $user_cb, sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
62 85 my $cb = shift;
63 85 ▶ if ($Perlbal::AIO_MODE eq "ioaio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::AIO_MODE eq 'ioaio') { } 0 85 85
64 0 IO::AIO::aio_stat($file, $cb);
65 } else {
66 85 stat($file);
67 85 $cb->();
68 }
69 85 });
70 }
71  
72 90 ▶sub aio_open {
Subroutine aio_open: called CC=1 SCAR=0.0
Pod undocumented
73 90 my ($file, $flags, $mode, $user_cb) = @_;
74  
75 90 ▶ aio_channel_push(get_chan($file), $user_cb, sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
76 90 my $cb = shift;
77  
78 90 ▶ if ($Perlbal::AIO_MODE eq "ioaio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::AIO_MODE eq 'ioaio') { } 0 90 90
79 0 IO::AIO::aio_open($file, $flags, $mode, $cb);
80 } else {
81 90 my $fh;
82 90 my $rv = sysopen($fh, $file, $flags, $mode);
83 90 ▶ $cb->($rv ? $fh : undef);
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 88 2 90
84 }
85 90 });
86 }
87  
88 32 ▶sub aio_unlink {
Subroutine aio_unlink: called CC=1 SCAR=0.0
Pod undocumented
89 32 my ($file, $user_cb) = @_;
90 32 ▶ aio_channel_push(get_chan($file), $user_cb, sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
91 32 my $cb = shift;
92  
93 32 ▶ if ($Perlbal::AIO_MODE eq "ioaio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::AIO_MODE eq 'ioaio') { } 0 32 32
94 0 IO::AIO::aio_unlink($file, $cb);
95 } else {
96 32 my $rv = unlink($file);
97 32 ▶ $rv = $rv ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 31 1 32
98 32 $cb->($rv);
99 }
100 32 });
101 }
102  
103 77 ▶sub aio_write {
Subroutine aio_write: called CC=2 SCAR=7.1
Pod undocumented
104 # 0 1 2 3(data) 4
105 77 my ($fh, $offset, $length, undef, $user_cb) = @_;
106 77 ▶ return no_fh($user_cb) unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 77 77
107 77 my $alist = \@_;
108  
109 77 ▶ aio_channel_push(get_chan(), $user_cb, sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
110 77 my $cb = shift;
111 77 ▶ if ($Perlbal::AIO_MODE eq "ioaio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::AIO_MODE eq 'ioaio') { } 0 77 77
112 0 IO::AIO::aio_write($fh, $offset, $length, $alist->[3], 0, $cb);
113 } else {
114 77 my $old_off = sysseek($fh, 0, SEEK_CUR);
115 77 sysseek($fh, $offset, 0);
116 77 my $rv = syswrite($fh, $alist->[3], $length, 0);
117 77 sysseek($fh, $old_off, SEEK_SET);
118 77 $cb->($rv);
119 }
120 77 });
121 }
122  
123 0 ▶sub aio_read {
Subroutine aio_read: not called CC=2 SCAR=17.9
Pod undocumented
124 # 0 1 2 3(data) 4
125 0 my ($fh, $offset, $length, undef, $user_cb) = @_;
126 0 ▶ return no_fh($user_cb) unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
127 0 my $alist = \@_;
128  
129 0 ▶ aio_channel_push(get_chan(), $user_cb, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
130 0 my $cb = shift;
131 0 ▶ if ($Perlbal::AIO_MODE eq "ioaio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perlbal::AIO_MODE eq 'ioaio') { } 0 0 0
132 0 IO::AIO::aio_read($fh, $offset, $length, $alist->[3], 0, $cb);
133 } else {
134 0 my $old_off = sysseek($fh, 0, SEEK_CUR);
135 0 sysseek($fh, $offset, 0);
136 0 my $rv = sysread($fh, $alist->[3], $length, 0);
137 0 sysseek($fh, $old_off, SEEK_SET);
138 0 $cb->($rv);
139 }
140 0 });
141 }
142  
143 ############################################################################
144 # AIO channel stuff
145 # prevents all AIO threads from being consumed by requests for same
146 # failing/overloaded disk by isolating them into separate 'channels' in
147 # parent process and not dispatching more than the max in-flight count
148 # allows. think of a channel as a named queue. or in reality, a disk.
149 ############################################################################
150  
151 22 my %chan_outstanding; # $channel_name -> $num_in_flight
152 my %chan_pending; # $channel_name -> [ [$subref, $cb], .... ]
153 22 my %chan_hitmaxdepth; # $channel_name -> $times_enqueued (not dispatched immediately)
154 22 my %chan_submitct; # $channel_name -> $times_submitted (total AIO requests for this channel)
155 22 my $use_aio_chans = 0; # keep them off for now, until mogstored code is ready to use them
156 22 my $file_to_chan_hook; # coderef that returns $chan_name given a $filename
157  
158 my %chan_concurrency; # $channel_name -> concurrency per channel
159 # (cache. definitive version via function call)
160  
161 0 ▶sub get_aio_stats {
Subroutine get_aio_stats: not called CC=5 SCAR=34.0
Pod undocumented
162 0 my $ret = {};
163 0 foreach my $c (keys %chan_outstanding) {
164 $ret->{$c} = {
165 cur_running => $chan_outstanding{$c},
166 ctr_queued => $chan_hitmaxdepth{$c} || 0,
167 0 ▶ ctr_total => $chan_submitct{$c},
Condition0%0/2
$chan_hitmaxdepth{$c} || 0
l!l
00
MC/DC0%0/1
$chan_hitmaxdepth{$c} || 0
$chan_hitmaxdepth{$c}
Truth table0%0/2
A
A= $chan_hitmaxdepth{$c}
Aresult
11
00
168 };
169 }
170  
171 0 foreach my $c (keys %chan_pending) {
172 0 ▶ my $rec = $ret->{$c} ||= {};
Condition0%0/2
$ret->{$c} ||= {}
l!l
00
MC/DC0%0/1
$ret->{$c} ||= {}
$ret->{$c}
Truth table0%0/2
A
A= $ret->{$c}
Aresult
11
00
173 0 $rec->{cur_queued} = scalar @{$chan_pending{$c}};
174 }
175  
176 0 return $ret;
177 }
178  
179 # (external API). set trans hook, but also enables AIO channels.
180 0 ▶sub set_file_to_chan_hook {
Subroutine set_file_to_chan_hook: not called CC=1 SCAR=6.9
Pod undocumented
181 0 $file_to_chan_hook = shift; # coderef that returns $chan_name given a $filename
182 0 $use_aio_chans = 1;
183 }
184  
185 # internal API:
186 331 ▶sub aio_channel_push {
Subroutine aio_channel_push: called CC=6 SCAR=32.7
Pod undocumented
187 331 my ($chan, $user_cb, $action) = @_;
188  
189 # if we were to do it immediately, bypassing AIO channels (future option?)
190 331 ▶ unless ($use_aio_chans) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_aio_chans) 331 0 331
191 331 $action->($user_cb);
192 331 return;
193 }
194  
195 # IO::AIO/etc only take one callback. so we wrap the user
196 # (caller) function with our own that first calls theirs, then
197 # does our bookkeeping and queue management afterwards.
198 0 ▶ my $wrapped_cb = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 $user_cb->(@_);
200 0 $chan_outstanding{$chan}--;
201 0 aio_channel_cond_run($chan);
202 0 };
203  
204 # in case this is the first time this queue has been used, init stuff:
205 0 ▶ my $chanpend = ($chan_pending{$chan} ||= []);
Condition0%0/2
$chan_pending{$chan} ||= []
l!l
00
MC/DC0%0/1
$chan_pending{$chan} ||= []
$chan_pending{$chan}
Truth table0%0/2
A
A= $chan_pending{$chan}
Aresult
11
00
206 0 ▶ $chan_outstanding{$chan} ||= 0;
Condition0%0/2
$chan_outstanding{$chan} ||= 0
l!l
00
MC/DC0%0/1
$chan_outstanding{$chan} ||= 0
$chan_outstanding{$chan}
Truth table0%0/2
A
A= $chan_outstanding{$chan}
Aresult
11
00
207 0 $chan_submitct{$chan}++;
208  
209 0 ▶ my $max_out = $chan_concurrency{$chan} ||= aio_chan_max_concurrent($chan);
Condition0%0/3
$chan_concurrency{$chan} ||= aio_chan_max_concurrent($chan)
l!l&&r!l&&!r
000
MC/DC0%0/2
$chan_concurrency{$chan} ||= aio_chan_max_concurrent($chan)
$chan_concurrency{$chan} aio_chan_max_concurrent($chan)
Truth table0%0/3
A ||= B
A= $chan_concurrency{$chan}B= aio_chan_max_concurrent($chan)
ABresult
1X1
011
000
210  
211 0 ▶ if ($chan_outstanding{$chan} < $max_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chan_outstanding{$chan} < $max_out) { } 0 0 0
212 0 $chan_outstanding{$chan}++;
213 0 $action->($wrapped_cb);
214 0 return;
215 } else {
216 # too deep. enqueue.
217 0 $chan_hitmaxdepth{$chan}++;
218 0 push @$chanpend, [$action, $wrapped_cb];
219 }
220 }
221  
222 0 ▶sub aio_chan_max_concurrent {
Subroutine aio_chan_max_concurrent: not called CC=2 SCAR=17.9
Pod undocumented
223 0 my ($chan) = @_;
224 0 ▶ return 100 if $chan eq '[default]';
Branch: 1 branch
BranchTrueFalseTotal
if $chan eq "[default]" 0 0 0
225 0 return 10;
226 }
227  
228 0 ▶sub aio_channel_cond_run {
Subroutine aio_channel_cond_run: not called CC=5 SCAR=34.0
Pod undocumented
229 0 my ($chan) = @_;
230  
231 0 ▶ my $chanpend = $chan_pending{$chan} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $chanpend = $chan_pending{$chan} or return 0 0 0
232 0 ▶ my $max_out = $chan_concurrency{$chan} ||= aio_chan_max_concurrent($chan);
Condition0%0/3
$chan_concurrency{$chan} ||= aio_chan_max_concurrent($chan)
l!l&&r!l&&!r
000
MC/DC0%0/2
$chan_concurrency{$chan} ||= aio_chan_max_concurrent($chan)
$chan_concurrency{$chan} aio_chan_max_concurrent($chan)
Truth table0%0/3
A ||= B
A= $chan_concurrency{$chan}B= aio_chan_max_concurrent($chan)
ABresult
1X1
011
000
233  
234 0 my $job;
235 0 ▶ while ($chan_outstanding{$chan} < $max_out && ($job = shift @$chanpend)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chan_outstanding{$chan} < $max_out and $job = shift @$chanpend) 0 0 0
Condition0%0/3
$chan_outstanding{$chan} < $max_out and $job = shift @$chanpend
!ll&&!rl&&r
000
MC/DC0%0/2
$chan_outstanding{$chan} < $max_out and $job = shift @$chanpend
$chan_outstanding{$chan} < $max_out $job = shift @$chanpend
Truth table0%0/3
A and B
A= $chan_outstanding{$chan} < $max_outB= $job = shift @$chanpend
ABresult
0X0
100
111
236 0 $chan_outstanding{$chan}++;
237 0 $job->[0]->($job->[1]);
238 }
239 }
240  
241 22 my $next_chan;
242 0 ▶sub set_channel {
Subroutine set_channel: not called CC=1 SCAR=6.9
Pod undocumented
243 0 $next_chan = shift;
244 }
245  
246 63 ▶sub set_file_for_channel {
Subroutine set_file_for_channel: called CC=2 SCAR=7.6
Pod undocumented
247 63 my ($file) = @_;
248 63 ▶ if ($file_to_chan_hook) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_to_chan_hook) { } 0 63 63
249 0 $next_chan = $file_to_chan_hook->($file);
250 } else {
251 63 $next_chan = undef;
252 }
253 }
254  
255 # gets currently-set channel, then clears it. or if none set,
256 # lets registered hook set the channel name from the optional
257 # $file parameter. the default channel, '[default]' has no limits
258 331 ▶sub get_chan {
Subroutine get_chan: called CC=4 SCAR=26.2
Pod undocumented
259 331 ▶ return undef unless $use_aio_chans;
Branch: 1 branch
BranchTrueFalseTotal
unless $use_aio_chans 331 0 331
260 0 my ($file) = @_;
261 0 ▶ set_file_for_channel($file) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 0 0
262  
263 0 ▶ if (my $chan = $next_chan) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chan = $next_chan) 0 0 0
264 0 $next_chan = undef;
265 0 return $chan;
266 }
267  
268 0 return "[default]";
269 }
270  
271 ############################################################################
272 # misc util functions
273 ############################################################################
274  
275 0 ▶sub _fh_of_fd_mode {
Subroutine _fh_of_fd_mode: not called CC=3 SCAR=24.8
276 0 my ($fd, $mode) = @_;
277 0 ▶ return undef unless defined $fd && $fd >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and $fd >= 0 0 0 0
Condition0%0/3
defined $fd and $fd >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table0%0/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
278  
279 #TODO: use the write MODE for the given $mode;
280 0 my $fh = IO::Handle->new_from_fd($fd, 'r+');
281 0 my $num = fileno($fh);
282 0 return $fh;
283 }
284  
285 0 ▶sub no_fh {
Subroutine no_fh: not called CC=2 SCAR=17.9
Pod undocumented
286 0 my $cb = shift;
287  
288 0 my $i = 1;
289 0 my $stack_trace = "";
290 0 ▶ while (my ($pkg, $filename, $line, $subroutine, $hasargs,
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
291 $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller($i++)) {
292 0 $stack_trace .= " at $filename:$line $subroutine\n";
293 }
294  
295 0 Perlbal::log("crit", "Undef \$fh: $stack_trace");
296 0 $cb->(undef);
297 0 return undef;
298 }
299  
300 22 1;
« Perlbal/SocketSSL.pm ↑ Summary Perlbal/Socket.pm »