blib/lib/Linux/Event/Loop.pm

filter: Statementstmt 73.6% 310 / 421 Branchbran 63.2% 124 / 196 Conditioncond 43.7% 35 / 80 MC/DCmcdc 18.7% 9 / 48 Subroutinesub 77.7% 35 / 45 Podpod 100% 9 / 9 totaltotal 65.3% 522 / 799 CC 142 SCAR 41.0
CC
142
Cov
67%
fork
237.1
_fork_child_drop
30.0
_drain_deferred_forks
20.4
CRAP 60.4
« blib/lib/Linux/Event/_Socket/Connection.pm ↑ Summary bench/run-reactor-comparison.pl »
1 package Linux::Event::Loop;
2 181 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 181 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 181 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 181 our $VERSION = '0.120';
7  
8 181 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 181 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 181 ▶use Hash::Util::FieldHash qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 181 ▶use Scalar::Util qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 181 ▶use utf8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 181 require XSLoader;
15 181 XSLoader::load(__PACKAGE__, $VERSION);
16  
17 181 require Linux::Event::Loop::Introspection;
18 181 our @ISA = ('Linux::Event::Loop::Introspection');
19  
20 181 fieldhash my %DEFER_STATE;
21 181 fieldhash my %DEFER_FORK_STATE;
22 181 our $FORK_DRIVER_FRAME;
23 181 my $DEFER_CALLBACK_BATCH = 1024;
24  
25 # Native drivers are private XS entry points. The public Perl boundary does
26 # not retain XS coderefs across interpreter clones.
27  
28 19 ▶sub _fork_write_all ($fh, $bytes) {
Subroutine _fork_write_all: called CC=6 SCAR=19.0
29 19 my $offset = 0;
30 19 ▶ while ($offset < length($bytes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $bytes) 19 19 38
31 19 my $written = syswrite($fh, $bytes, length($bytes) - $offset, $offset);
32 19 ▶ next if !defined($written) && $! == Errno::EINTR();
Branch: 1 branch
BranchTrueFalseTotal
if not defined $written and $! == 4 0 19 19
Condition33%1/3
not defined $written and $! == 4
!ll&&!rl&&r
1900
MC/DC0%0/2
not defined $written and $! == 4
not defined $written $! == 4
Truth table33%1/3
not(A) and B
A= not defined $writtenB= $! == 4
ABresult
0X0
100
111
33 19 ▶ die "fork(): handshake write failed: $!\n" if !defined $written;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $written 0 19 19
34 19 ▶ die "fork(): handshake write returned zero bytes\n" if !$written;
Branch: 1 branch
BranchTrueFalseTotal
unless $written 0 19 19
35 19 $offset += $written;
36 }
37 19 return;
38 }
39  
40 22 ▶sub _fork_read_exact ($fh, $length) {
Subroutine _fork_read_exact: called CC=6 SCAR=18.8
41 22 my $bytes = '';
42 22 ▶ while (length($bytes) < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes < $length) 22 22 44
43 22 my $chunk = '';
44 22 my $read = sysread($fh, $chunk, $length - length($bytes));
45 22 ▶ next if !defined($read) && $! == Errno::EINTR();
Branch: 1 branch
BranchTrueFalseTotal
if not defined $read and $! == 4 0 22 22
Condition33%1/3
not defined $read and $! == 4
!ll&&!rl&&r
2200
MC/DC0%0/2
not defined $read and $! == 4
not defined $read $! == 4
Truth table33%1/3
not(A) and B
A= not defined $readB= $! == 4
ABresult
0X0
100
111
46 22 ▶ die "fork(): handshake read failed: $!\n" if !defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 22 22
47 22 ▶ return undef if !$read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 22 22
48 22 $bytes .= $chunk;
49 }
50 22 return $bytes;
51 }
52  
53 0 ▶sub _fork_child_failure ($fh, $error) {
Subroutine _fork_child_failure: not called CC=3 SCAR=24.8
54 0 $error = "$error";
55 0 ▶ $error = "child reconstruction failed\n" if $error eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "" 0 0 0
56 0 ▶ utf8::encode($error) if utf8::is_utf8($error);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($error) 0 0 0
57 0 $error = substr($error, 0, 1_048_576);
58 0 eval { _fork_write_all($fh, 'E' . pack('N', length($error)) . $error); 1 };
59 0 require POSIX;
60 0 POSIX::_exit(255);
61 }
62  
63 0 ▶sub _fork_child_reset_deferred ($self) {
Subroutine _fork_child_reset_deferred: not called CC=2 SCAR=17.9
64 0 my $state = delete $DEFER_STATE{$self};
65 0 ▶ $state->_fork_child_drop if $state;
Branch: 1 branch
BranchTrueFalseTotal
if $state 0 0 0
66 0 return;
67 }
68  
69 27 ▶sub fork ($self, %option) {
Subroutine fork: called CC=42 SCAR=54.7
Pod documented
70 27 $self->_assert_owner_native('fork');
71 27 ▶ croak 'fork(): Loop must be quiescent and cannot fork during dispatch'
Branch: 1 branch
BranchTrueFalseTotal
if $self->running 1 26 27
72 if $self->running;
73  
74 26 my %known = map { $_ => 1 } qw(share clone move);
75 26 my @unknown = sort grep { !$known{$_} } keys %option;
76 26 ▶ croak 'fork(): unknown options: ' . join(', ', @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 25 26
77  
78 25 my %disposition;
79 my %selected;
80 25 for my $mode (qw(share clone move)) {
81 73 ▶ my $list = exists($option{$mode}) ? $option{$mode} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $option{$mode} ? : 17 56 73
82 73 ▶ croak "fork(): $mode must be an array reference" if ref($list) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $list ne "ARRAY" 1 72 73
83 72 for my $object (@$list) {
84 16 ▶ croak "fork(): $mode entries must be resource objects"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 0 16 16
85 if !blessed($object);
86 16 my $id = refaddr($object);
87 croak 'fork(): a resource may appear in only one disposition list'
88 16 ▶ if $selected{$id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $selected{$id}++ 1 15 16
89 15 ▶ croak "fork(): $mode resource is not current in this Loop"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has($object) 0 15 15
90 if !$self->has($object);
91 15 $disposition{$id} = $mode;
92 }
93 }
94  
95 23 my $objects = $self->objects;
96 23 for my $object (@$objects) {
97 18 ▶ my $mode = $disposition{ refaddr($object) } // 'drop';
Condition100%2/2
$disposition{builtin::refaddr($object)} // "drop"
l!l
144
MC/DC100%1/1
$disposition{builtin::refaddr($object)} // "drop"
$disposition{builtin::refaddr($object)}
Truth table100%2/2
A
A= $disposition{builtin::refaddr($object)}
Aresult
11
00
98 18 ▶ croak 'fork(): resource does not implement fork disposition hooks'
Branch: 1 branch
BranchTrueFalseTotal
unless $object->can("_fork_preflight") and $object->can("_fork_child_drop") 0 18 18
Condition33%1/3
$object->can("_fork_preflight") and $object->can("_fork_child_drop")
!ll&&!rl&&r
0018
MC/DC0%0/2
$object->can("_fork_preflight") and $object->can("_fork_child_drop")
$object->can("_fork_preflight") $object->can("_fork_child_drop")
Truth table33%1/3
A and B
A= $object->can("_fork_preflight")B= $object->can("_fork_child_drop")
ABresult
0X0
100
111
99 if !$object->can('_fork_preflight') || !$object->can('_fork_child_drop');
100 18 $object->_fork_preflight($mode, $self);
101 15 ▶ croak "fork(): resource does not implement child '$mode' disposition"
Branch: 1 branch
BranchTrueFalseTotal
if $mode ne "drop" and not $object->can("_fork_child_$mode") 0 15 15
Condition66%2/3
$mode ne "drop" and not $object->can("_fork_child_$mode")
!ll&&!rl&&r
4110
MC/DC0%0/2
$mode ne "drop" and not $object->can("_fork_child_$mode")
$mode ne "drop" not $object->can("_fork_child_$mode")
Truth table66%2/3
A and not(B)
A= $mode ne "drop"B= not $object->can("_fork_child_$mode")
ABresult
0X0
100
111
102 if $mode ne 'drop' && !$object->can("_fork_child_$mode");
103 15 ▶ croak 'fork(): moved resource does not implement parent disposition'
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "move" and not $object->can("_fork_parent_move") 0 15 15
Condition66%2/3
$mode eq "move" and not $object->can("_fork_parent_move")
!ll&&!rl&&r
870
MC/DC0%0/2
$mode eq "move" and not $object->can("_fork_parent_move")
$mode eq "move" not $object->can("_fork_parent_move")
Truth table66%2/3
A and not(B)
A= $mode eq "move"B= not $object->can("_fork_parent_move")
ABresult
0X0
100
111
104 if $mode eq 'move' && !$object->can('_fork_parent_move');
105 }
106  
107 20 require Linux::Event::_Resolver;
108 20 Linux::Event::_Resolver->_fork_prepare_loop($self);
109  
110 20 require Socket;
111 20 ▶ socketpair(my $parent_channel, my $child_channel,
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $parent_channel, my $child_channel, &Socket::AF_UNIX(), &Socket::SOCK_STREAM(), &Socket::PF_UNSPEC() or croak("fork(): socketpair failed: $!") 20 0 20
112 Socket::AF_UNIX(), Socket::SOCK_STREAM(), Socket::PF_UNSPEC())
113 or croak "fork(): socketpair failed: $!";
114  
115 20 my $pid = CORE::fork();
116 20 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 20 20
117 0 my $errno = 0 + $!;
118 0 close $parent_channel;
119 0 close $child_channel;
120 0 $! = $errno;
121 0 return undef;
122 }
123  
124 20 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 20 20
125 0 close $parent_channel;
126 0 my $ok = eval {
127 0 $self->_fork_child_reset;
128 0 $self->reset_stats;
129 0 $self->_fork_child_reset_deferred;
130 0 $self->_fork_child_reset_deferred_forks;
131 0 require Linux::Event::Kernel::Signal;
132 0 Linux::Event::Kernel::Signal->_fork_child_drop_loop($self);
133  
134 0 for my $object (@$objects) {
135 0 ▶ my $mode = $disposition{ refaddr($object) } // 'drop';
Condition0%0/2
$disposition{builtin::refaddr($object)} // "drop"
l!l
00
MC/DC0%0/1
$disposition{builtin::refaddr($object)} // "drop"
$disposition{builtin::refaddr($object)}
Truth table0%0/2
A
A= $disposition{builtin::refaddr($object)}
Aresult
11
00
136 0 ▶ my $method = $mode eq 'drop'
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'drop' ? : 0 0 0
137 ? '_fork_child_drop' : "_fork_child_$mode";
138 0 $object->$method($self);
139 }
140 0 1;
141 };
142 0 ▶ _fork_child_failure($child_channel, $@) if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
143  
144 0 my $ready = eval { _fork_write_all($child_channel, 'R'); 1 };
145 0 ▶ _fork_child_failure($child_channel, $@) if !$ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $ready 0 0 0
146 0 my $commit = eval { _fork_read_exact($child_channel, 1) };
147 0 ▶ if ($@ || !defined($commit) || $commit ne 'C') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $commit or $commit ne "C") 0 0 0
Condition0%0/6
$@ or not defined $commit or $commit ne "C"
l!l&&r!l&&!r
000
$@ or not defined $commit
l!l&&r!l&&!r
000
MC/DC0%0/3
$@ or not defined $commit or $commit ne "C"
$@ not defined $commit $commit ne "C"
Truth table0%0/4
A or not(B) or C
A= $@B= not defined $commitC= $commit ne "C"
ABCresult
1XX1
01X1
0011
0000
148 0 require POSIX;
149 0 POSIX::_exit(255);
150 }
151 0 close $child_channel;
152 0 return 0;
153 }
154  
155 20 close $child_channel;
156 20 my $status = eval { _fork_read_exact($parent_channel, 1) };
157 20 ▶ if ($@ || !defined $status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $status) 0 20 20
Condition33%1/3
$@ or not defined $status
l!l&&r!l&&!r
0020
MC/DC0%0/2
$@ or not defined $status
$@ not defined $status
Truth table33%1/3
A or not(B)
A= $@B= not defined $status
ABresult
1X1
011
000
158 0 ▶ my $error = $@ || "fork(): child reconstruction channel closed\n";
Condition0%0/2
$@ || "fork(): child reconstruction channel closed\n"
l!l
00
MC/DC0%0/1
$@ || "fork(): child reconstruction channel closed\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
159 0 waitpid($pid, 0);
160 0 close $parent_channel;
161 0 die $error;
162 }
163 20 ▶ if ($status eq 'E') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "E") 1 19 20
164 1 my $length_bytes = _fork_read_exact($parent_channel, 4);
165 1 ▶ my $length = defined($length_bytes) ? unpack('N', $length_bytes) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $length_bytes ? : 1 0 1
166 1 ▶ my $message = $length ? _fork_read_exact($parent_channel, $length) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 1 0 1
167 1 waitpid($pid, 0);
168 1 close $parent_channel;
169 1 ▶ $message //= 'child reconstruction failed';
Condition50%1/2
$message //= "child reconstruction failed"
l!l
10
MC/DC0%0/1
$message //= "child reconstruction failed"
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
170 1 croak "fork(): $message";
171 }
172 19 ▶ if ($status ne 'R') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status ne "R") 0 19 19
173 0 waitpid($pid, 0);
174 0 close $parent_channel;
175 0 croak 'fork(): invalid child reconstruction handshake';
176 }
177  
178 19 my $moved = eval {
179 19 for my $object (@$objects) {
180 13 ▶ next if ($disposition{ refaddr($object) } // '') ne 'move';
Branch: 1 branch
BranchTrueFalseTotal
if ($disposition{builtin::refaddr($object)} // "") ne "move" 7 6 13
Condition100%2/2
$disposition{builtin::refaddr($object)} // ""
l!l
94
MC/DC100%1/1
$disposition{builtin::refaddr($object)} // ""
$disposition{builtin::refaddr($object)}
Truth table100%2/2
A
A= $disposition{builtin::refaddr($object)}
Aresult
11
00
181 6 $object->_fork_parent_move($pid);
182 }
183 19 1;
184 };
185 19 ▶ if (!$moved) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($moved) 0 19 19
186 0 ▶ my $error = $@ || "fork(): parent move disposition failed\n";
Condition0%0/2
$@ || "fork(): parent move disposition failed\n"
l!l
00
MC/DC0%0/1
$@ || "fork(): parent move disposition failed\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
187 0 eval { _fork_write_all($parent_channel, 'A'); 1 };
188 0 waitpid($pid, 0);
189 0 close $parent_channel;
190 0 die $error;
191 }
192  
193 19 _fork_write_all($parent_channel, 'C');
194 19 close $parent_channel;
195 19 return $pid;
196 }
197  
198 203201 ▶sub add ($self, $object) {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
199 203201 ▶ croak 'add(): object must support loop attachment'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) and $object->can("_attach_to_loop") 1 203200 203201
Condition66%2/3
builtin::blessed($object) and $object->can("_attach_to_loop")
!ll&&!rl&&r
01203200
MC/DC50%1/2
builtin::blessed($object) and $object->can("_attach_to_loop")
builtin::blessed($object) $object->can("_attach_to_loop")
Truth table66%2/3
A and B
A= builtin::blessed($object)B= $object->can("_attach_to_loop")
ABresult
0X0
100
111
200 if !blessed($object) || !$object->can('_attach_to_loop');
201 203200 $object->_attach_to_loop($self);
202 203192 return $object;
203 }
204 1055 ▶sub _defer_state ($self) {
Subroutine _defer_state: called CC=4 SCAR=16.4
205 1055 ▶ return $DEFER_STATE{$self} if $DEFER_STATE{$self};
Branch: 1 branch
BranchTrueFalseTotal
if $DEFER_STATE{$self} 1037 18 1055
206  
207 18 require Linux::Event::Kernel::Event;
208 18 my $fd = Linux::Event::Kernel::Event::_new_fd();
209 18 my $state = bless {
210 fd => $fd,
211 queue => [],
212 pending => 0,
213 signaled => 0,
214 }, 'Linux::Event::Loop::_DeferService';
215  
216 18 my $ok = eval {
217 $self->watch(
218 fd => $fd,
219 _internal => 1,
220 25 ▶ read => sub { $state->_dispatch },
Subroutine __ANON__: called CC=1 SCAR=0.0
221 0 ▶ error => sub { die "Linux::Event defer event source failed\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 18 no_args => 1,
223 lean => 1,
224 );
225 18 1;
226 };
227 18 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 18 18
228 0 my $error = $@;
229 0 my $close_fd = delete $state->{fd};
230 0 ▶ eval { Linux::Event::Kernel::Event::_close_fd($close_fd); 1 }
Branch: 1 branch
BranchTrueFalseTotal
if defined $close_fd 0 0 0
231 if defined $close_fd;
232 0 die $error;
233 }
234  
235 18 $DEFER_STATE{$self} = $state;
236 18 return $state;
237 }
238  
239 1056 ▶sub defer ($self, $callback) {
Subroutine defer: called CC=2 SCAR=6.9
Pod documented
240 1056 $self->_assert_owner_native('defer');
241 1056 ▶ croak 'defer(): callback must be a coderef' if ref($callback) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $callback ne "CODE" 1 1055 1056
242 1055 my $state = $self->_defer_state;
243 1055 my $deferred = bless {
244 state => $state,
245 callback => $callback,
246 active => 1,
247 }, 'Linux::Event::_Deferred';
248 1055 weaken($deferred->{state});
249 1055 $state->_enqueue($deferred);
250 1055 return $deferred;
251 }
252  
253 25 ▶sub _deferred_count ($self) {
Subroutine _deferred_count: called CC=2 SCAR=6.9
254 25 my $state = $DEFER_STATE{$self};
255 25 ▶ return $state ? $state->{pending} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 7 18 25
256 }
257  
258 25 ▶sub _deferred_fd ($self) {
Subroutine _deferred_fd: called CC=2 SCAR=6.9
259 25 my $state = $DEFER_STATE{$self};
260 25 ▶ return $state ? $state->{fd} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 7 18 25
261 }
262  
263  
264 # A deferred fork cannot execute in a normal defer() callback: that callback
265 # still runs inside the native driver. The defer eventfd only asks the driver
266 # to exit. We perform the managed fork after its XS frame has unwound.
267 13 ▶sub defer_fork ($self, %option) {
Subroutine defer_fork: called CC=10 SCAR=23.6
Pod documented
268 13 $self->_assert_owner_native('defer_fork');
269  
270 13 my %known = map { $_ => 1 }
271 qw(share clone move on_parent on_child on_error);
272 13 my @unknown = sort grep { !$known{$_} } keys %option;
273 13 ▶ croak 'defer_fork(): unknown options: ' . join(', ', @unknown)
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 12 13
274 if @unknown;
275  
276 12 for my $mode (qw(share clone move)) {
277 36 ▶ next if !exists $option{$mode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{$mode} 32 4 36
278 croak "defer_fork(): $mode must be an array reference"
279 4 ▶ if ref($option{$mode}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $option{$mode} ne "ARRAY" 1 3 4
280 }
281 11 for my $callback (qw(on_parent on_child on_error)) {
282 31 ▶ next if !exists $option{$callback};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option{$callback} 13 18 31
283 croak "defer_fork(): $callback must be a coderef"
284 18 ▶ if ref($option{$callback}) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $option{$callback} ne "CODE" 1 17 18
285 }
286  
287 10 ▶ my $state = $DEFER_FORK_STATE{$self} //= { queue => [], pending => 0 };
Condition100%2/2
$DEFER_FORK_STATE{$self} //= {"queue", [], "pending", 0}
l!l
19
MC/DC100%1/1
$DEFER_FORK_STATE{$self} //= {"queue", [], "pending", 0}
$DEFER_FORK_STATE{$self}
Truth table100%2/2
A
A= $DEFER_FORK_STATE{$self}
Aresult
11
00
288 my $request = bless {
289 state => $state,
290 3 options => { map { $_ => $option{$_} }
291 30 grep { exists $option{$_} } qw(share clone move) },
292 on_parent => delete $option{on_parent},
293 on_child => delete $option{on_child},
294 on_error => delete $option{on_error},
295 10 active => 1,
296 }, 'Linux::Event::_DeferredFork';
297 10 weaken($request->{state});
298 10 push @{ $state->{queue} }, $request;
299 10 $state->{pending}++;
300  
301 # A wakeup also handles requests scheduled while no driver is running,
302 # and makes requests queued by on_parent wait for another Loop turn.
303 10 my $weak_loop = $self;
304 10 my $weak_request = $request;
305 10 weaken($weak_loop);
306 10 weaken($weak_request);
307 10 my $ok = eval {
308 7 ▶ $self->defer(sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
309 7 ▶ my $loop = $weak_loop // return;
Condition50%1/2
$weak_loop // (return)
l!l
70
MC/DC0%0/1
$weak_loop // (return)
$weak_loop
Truth table50%1/2
A
A= $weak_loop
Aresult
11
00
310 7 ▶ my $pending = $weak_request // return;
Condition50%1/2
$weak_request // (return)
l!l
70
MC/DC0%0/1
$weak_request // (return)
$weak_request
Truth table50%1/2
A
A= $weak_request
Aresult
11
00
311 $loop->_stop_native
312 7 ▶ if $pending->{active} && $loop->running;
Branch: 1 branch
BranchTrueFalseTotal
if $pending->{"active"} and $loop->running 5 2 7
Condition66%2/3
$pending->{"active"} and $loop->running
!ll&&!rl&&r
205
MC/DC50%1/2
$pending->{"active"} and $loop->running
$pending->{"active"} $loop->running
Truth table66%2/3
A and B
A= $pending->{"active"}B= $loop->running
ABresult
0X0
100
111
313 10 });
314 10 1;
315 };
316 10 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 10 10
317 0 my $error = $@;
318 0 pop @{ $state->{queue} };
319 0 $state->{pending}--;
320 0 $request->{active} = 0;
321 0 delete @$request{qw(options on_parent on_child on_error)};
322 0 die $error;
323 }
324 10 return $request;
325 }
326  
327 0 ▶sub _fork_child_reset_deferred_forks ($self) {
Subroutine _fork_child_reset_deferred_forks: not called CC=4 SCAR=30.0
328 0 my $state = delete $DEFER_FORK_STATE{$self};
329 0 ▶ return if !$state;
Branch: 1 branch
BranchTrueFalseTotal
unless $state 0 0 0
330 0 for my $request (@{ $state->{queue} }) {
331 0 ▶ next if !$request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 0 0
332 0 $request->{active} = 0;
333 0 delete @$request{qw(options on_parent on_child on_error)};
334 }
335 0 $state->{queue} = [];
336 0 $state->{pending} = 0;
337 0 return;
338 }
339  
340 # Driver frames are dynamically scoped, never stored on a Loop. In particular,
341 # this avoids retaining Loop-owned objects across Perl ithread cloning.
342 269 ▶sub _current_fork_driver ($self) {
Subroutine _current_fork_driver: called CC=3 SCAR=11.3
343 269 my $frame = $FORK_DRIVER_FRAME;
344 269 ▶ while ($frame) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame) 266 3 269
345 266 ▶ return $frame if refaddr($frame->{loop}) == refaddr($self);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($frame->{"loop"}) == builtin::refaddr($self) 266 0 266
346 0 $frame = $frame->{parent};
347 }
348 3 return undef;
349 }
350  
351 661 ▶sub _drain_deferred_forks ($self) {
Subroutine _drain_deferred_forks: called CC=16 SCAR=30.1
352 661 my $state = $DEFER_FORK_STATE{$self};
353 661 ▶ return 0 if !$state || !$state->{pending};
Branch: 1 branch
BranchTrueFalseTotal
unless $state and $state->{"pending"} 653 8 661
Condition100%3/3
$state and $state->{"pending"}
!ll&&!rl&&r
65038
MC/DC100%2/2
$state and $state->{"pending"}
$state $state->{"pending"}
Truth table100%3/3
A and B
A= $stateB= $state->{"pending"}
ABresult
0X0
100
111
354  
355 # Freeze the eligible set: requests added from completion callbacks wait
356 # for the next driver turn, rather than recursively forking here.
357 8 my $eligible = scalar @{ $state->{queue} };
358 8 my $processed = 0;
359 8 for (1 .. $eligible) {
360 9 my $driver = $self->_current_fork_driver;
361 9 ▶ last if $driver && $driver->{user_stop};
Branch: 1 branch
BranchTrueFalseTotal
if $driver and $driver->{"user_stop"} 0 9 9
Condition33%1/3
$driver and $driver->{"user_stop"}
!ll&&!rl&&r
090
MC/DC0%0/2
$driver and $driver->{"user_stop"}
$driver $driver->{"user_stop"}
Truth table33%1/3
A and B
A= $driverB= $driver->{"user_stop"}
ABresult
0X0
100
111
362 9 my $request = shift @{ $state->{queue} };
363 9 ▶ next if !$request || !$request->{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $request and $request->{"active"} 0 9 9
Condition33%1/3
$request and $request->{"active"}
!ll&&!rl&&r
009
MC/DC0%0/2
$request and $request->{"active"}
$request $request->{"active"}
Truth table33%1/3
A and B
A= $requestB= $request->{"active"}
ABresult
0X0
100
111
364 9 $request->{active} = 0;
365 9 $state->{pending}--;
366 9 $processed++;
367 9 my $options = delete $request->{options};
368 9 my $on_parent = delete $request->{on_parent};
369 9 my $on_child = delete $request->{on_child};
370 9 my $on_error = delete $request->{on_error};
371  
372 9 my $pid;
373 9 my $ok = eval { $pid = $self->fork(%$options); 1 };
374 9 ▶ if (!$ok || !defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and defined $pid) 2 7 9
Condition66%2/3
$ok and defined $pid
!ll&&!rl&&r
207
MC/DC50%1/2
$ok and defined $pid
$ok defined $pid
Truth table66%2/3
A and B
A= $okB= defined $pid
ABresult
0X0
100
111
375 2 ▶ my $error = !$ok ? $@ : "defer_fork(): fork failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
!$ok ? : 2 0 2
376 2 ▶ if ($on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_error) 2 0 2
377 2 $on_error->($error);
378 2 next;
379 }
380 0 die $error;
381 }
382  
383 7 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 7 7
384 # fork() has discarded the inherited queue in this child.
385 0 ▶ $on_child->() if $on_child;
Branch: 1 branch
BranchTrueFalseTotal
if $on_child 0 0 0
386 0 return $processed;
387 }
388 7 ▶ $on_parent->($pid) if $on_parent;
Branch: 1 branch
BranchTrueFalseTotal
if $on_parent 7 0 7
389 }
390  
391 8 ▶ $state->{queue} = [] if !$state->{pending};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"pending"} 8 0 8
392 8 return $processed;
393 }
394  
395 260 ▶sub stop ($self) {
Subroutine stop: called CC=2 SCAR=6.9
Pod documented
396 # A user stop must not be mistaken for defer_fork's internal wakeup.
397 260 my $driver = $self->_current_fork_driver;
398 260 ▶ $driver->{user_stop} = 1 if $driver;
Branch: 1 branch
BranchTrueFalseTotal
if $driver 257 3 260
399 260 $self->_stop_native();
400 260 return;
401 }
402  
403 238 ▶sub run ($self) {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
404 238 my $driver = {
405 loop => $self,
406 parent => $FORK_DRIVER_FRAME,
407 user_stop => 0,
408 };
409 238 local $FORK_DRIVER_FRAME = $driver;
410 238 $self->_drain_deferred_forks;
411 238 ▶ while (!$driver->{user_stop}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver->{"user_stop"}) 238 3 241
412 238 $self->_run_native();
413 231 ▶ last if $driver->{user_stop};
Branch: 1 branch
BranchTrueFalseTotal
if $driver->{"user_stop"} 228 3 231
414 3 ▶ last if !$self->_drain_deferred_forks;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_drain_deferred_forks 0 3 3
415 }
416 231 return;
417 }
418  
419 91 ▶sub run_for ($self, $seconds) {
Subroutine run_for: called CC=7 SCAR=19.5
Pod documented
420 91 require Time::HiRes;
421 91 my $started = Time::HiRes::clock_gettime(Time::HiRes::CLOCK_MONOTONIC());
422 91 my $driver = {
423 loop => $self,
424 parent => $FORK_DRIVER_FRAME,
425 user_stop => 0,
426 };
427 91 local $FORK_DRIVER_FRAME = $driver;
428 91 $self->_drain_deferred_forks;
429 91 my $first = 1;
430 91 ▶ while (!$driver->{user_stop}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($driver->{"user_stop"}) 91 1 92
431 91 my $elapsed = Time::HiRes::clock_gettime(
432 Time::HiRes::CLOCK_MONOTONIC()) - $started;
433 91 my $remaining = $seconds - $elapsed;
434 91 ▶ last if !$first && $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $first and $remaining <= 0 0 91 91
Condition33%1/3
not $first and $remaining <= 0
!ll&&!rl&&r
9100
MC/DC0%0/2
not $first and $remaining <= 0
not $first $remaining <= 0
Truth table33%1/3
not(A) and B
A= not $firstB= $remaining <= 0
ABresult
0X0
100
111
435 # The first native entry still validates all run_for arguments.
436 91 ▶ $self->_run_for_native(
Branch: 1 branch
BranchTrueFalseTotal
$remaining > 0 ? : 88 3 91
437 $remaining > 0 ? $remaining : $seconds);
438 80 $first = 0;
439 80 ▶ last if $driver->{user_stop};
Branch: 1 branch
BranchTrueFalseTotal
if $driver->{"user_stop"} 24 56 80
440 56 ▶ last if !$self->_drain_deferred_forks;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_drain_deferred_forks 55 1 56
441 }
442 80 return;
443 }
444  
445 129 ▶sub run_once ($self, @args) {
Subroutine run_once: called CC=3 SCAR=11.0
Pod documented
446 129 my $driver = {
447 loop => $self,
448 parent => $FORK_DRIVER_FRAME,
449 user_stop => 0,
450 };
451 129 local $FORK_DRIVER_FRAME = $driver;
452 129 $self->_drain_deferred_forks;
453 129 ▶ return 0 if $driver->{user_stop};
Branch: 1 branch
BranchTrueFalseTotal
if $driver->{"user_stop"} 0 129 129
454 129 my $n = $self->_run_once_native(@args);
455 122 ▶ $self->_drain_deferred_forks if !$driver->{user_stop};
Branch: 1 branch
BranchTrueFalseTotal
unless $driver->{"user_stop"} 121 1 122
456 122 return $n;
457 }
458  
459 12 ▶sub poll ($self) {
Subroutine poll: called CC=3 SCAR=11.2
Pod documented
460 12 my $driver = {
461 loop => $self,
462 parent => $FORK_DRIVER_FRAME,
463 user_stop => 0,
464 };
465 12 local $FORK_DRIVER_FRAME = $driver;
466 12 $self->_drain_deferred_forks;
467 12 ▶ return 0 if $driver->{user_stop};
Branch: 1 branch
BranchTrueFalseTotal
if $driver->{"user_stop"} 0 12 12
468 12 my $n = $self->_poll_native();
469 11 ▶ $self->_drain_deferred_forks if !$driver->{user_stop};
Branch: 1 branch
BranchTrueFalseTotal
unless $driver->{"user_stop"} 11 0 11
470 11 return $n;
471 }
472  
473 0 ▶sub CLONE_SKIP ($class) { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
474  
475 package Linux::Event::Loop::_DeferService;
476  
477 1064 ▶sub _signal ($self) {
Subroutine _signal: called CC=2 SCAR=6.9
478 1064 ▶ return if $self->{signaled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"signaled"} 1036 28 1064
479 28 Linux::Event::Kernel::Event::_signal_fd($self->{fd}, 1);
480 28 $self->{signaled} = 1;
481 28 return;
482 }
483  
484 1055 ▶sub _enqueue ($self, $deferred) {
Subroutine _enqueue: called CC=2 SCAR=9.2
485 1055 push @{ $self->{queue} }, $deferred;
486 1055 $self->{pending}++;
487 1055 my $ok = eval { $self->_signal; 1 };
488 1055 ▶ return if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1055 0 1055
489  
490 0 my $error = $@;
491 0 pop @{ $self->{queue} };
492 0 $self->{pending}--;
493 0 $deferred->{active} = 0;
494 0 delete $deferred->{callback};
495 0 die $error;
496 }
497  
498 1 ▶sub _cancel ($self, $deferred) {
Subroutine _cancel: called CC=4 SCAR=14.5
499 1 ▶ return if !$deferred->{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $deferred->{"active"} 0 1 1
500 1 $deferred->{active} = 0;
501 1 delete $deferred->{callback};
502 1 ▶ $self->{pending}-- if $self->{pending};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pending"} 1 0 1
503 1 ▶ $self->{queue} = [] if !$self->{pending};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pending"} 0 1 1
504 1 return;
505 }
506  
507 0 ▶sub _fork_child_drop ($self) {
Subroutine _fork_child_drop: not called CC=5 SCAR=34.0
508 0 ▶ for my $deferred (@{ $self->{queue} // [] }) {
Condition0%0/2
$self->{"queue"} // []
l!l
00
MC/DC0%0/1
$self->{"queue"} // []
$self->{"queue"}
Truth table0%0/2
A
A= $self->{"queue"}
Aresult
11
00
509 0 ▶ next if !$deferred;
Branch: 1 branch
BranchTrueFalseTotal
unless $deferred 0 0 0
510 0 $deferred->{active} = 0;
511 0 delete $deferred->{callback};
512 }
513 0 $self->{queue} = [];
514 0 $self->{pending} = 0;
515 0 $self->{signaled} = 0;
516 0 my $fd = delete $self->{fd};
517 0 ▶ eval { Linux::Event::Kernel::Event::_close_fd($fd); 1 } if defined $fd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fd 0 0 0
518 0 return;
519 }
520  
521 25 ▶sub _dispatch ($self) {
Subroutine _dispatch: called CC=14 SCAR=27.0
522 25 ▶ return if !defined $self->{fd};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"fd"} 0 25 25
523 25 Linux::Event::Kernel::Event::_drain_fd($self->{fd});
524 25 $self->{signaled} = 0;
525  
526 25 my $eligible = scalar @{ $self->{queue} };
527 25 ▶ $eligible = $DEFER_CALLBACK_BATCH
Branch: 1 branch
BranchTrueFalseTotal
if $eligible > $DEFER_CALLBACK_BATCH 1 24 25
528 if $eligible > $DEFER_CALLBACK_BATCH;
529  
530 25 my $error;
531 25 for (1 .. $eligible) {
532 1052 my $deferred = shift @{ $self->{queue} };
533 1052 ▶ next if !$deferred || !$deferred->{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $deferred and $deferred->{"active"} 1 1051 1052
Condition66%2/3
$deferred and $deferred->{"active"}
!ll&&!rl&&r
011051
MC/DC50%1/2
$deferred and $deferred->{"active"}
$deferred $deferred->{"active"}
Truth table66%2/3
A and B
A= $deferredB= $deferred->{"active"}
ABresult
0X0
100
111
534  
535 1051 $deferred->{active} = 0;
536 1051 ▶ $self->{pending}-- if $self->{pending};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pending"} 1051 0 1051
537 1051 my $callback = delete $deferred->{callback};
538  
539 1051 local $@;
540 1051 my $ok = eval { $callback->(); 1 };
541 1051 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1050 1051
542 1 ▶ $error = $@ || "deferred callback failed\n";
Condition50%1/2
$@ || "deferred callback failed\n"
l!l
10
MC/DC0%0/1
$@ || "deferred callback failed\n"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
543 1 last;
544 }
545 }
546  
547 25 ▶ if (!$self->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'pending'}) { } 16 9 25
548 16 $self->{queue} = [];
549 }
550 else {
551 9 local $@;
552 9 my $ok = eval { $self->_signal; 1 };
553 9 ▶ $error = $@ || "defer event source signal failed\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $ok or defined $error 0 9 9
Condition20%1/5
$ok or defined $error
l!l&&r!l&&!r
900
$@ || "defer event source signal failed\n"
l!l
00
MC/DC0%0/2
$ok or defined $error
$ok defined $error
MC/DC0%0/1
$@ || "defer event source signal failed\n"
$@
Truth table33%1/3
A or B
A= $okB= defined $error
ABresult
1X1
011
000
Truth table0%0/2
A
A= $@
Aresult
11
00
554 if !$ok && !defined $error;
555 }
556  
557 25 ▶ die $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 1 24 25
558 24 return;
559 }
560  
561 0 ▶sub CLONE_SKIP ($class) { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
562  
563 18 ▶sub DESTROY ($self) {
Subroutine DESTROY: called CC=5 SCAR=16.3
564 18 ▶ for my $deferred (@{ $self->{queue} // [] }) {
Condition50%1/2
$self->{"queue"} // []
l!l
180
MC/DC0%0/1
$self->{"queue"} // []
$self->{"queue"}
Truth table50%1/2
A
A= $self->{"queue"}
Aresult
11
00
565 3 ▶ next if !$deferred;
Branch: 1 branch
BranchTrueFalseTotal
unless $deferred 0 3 3
566 3 $deferred->{active} = 0;
567 3 delete $deferred->{callback};
568 }
569 18 $self->{queue} = [];
570 18 $self->{pending} = 0;
571 18 my $fd = delete $self->{fd};
572 18 ▶ eval { Linux::Event::Kernel::Event::_close_fd($fd); 1 } if defined $fd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fd 18 0 18
573 18 return;
574 }
575  
576 package Linux::Event::_Deferred;
577  
578 2 ▶sub cancel ($self) {
Subroutine cancel: called CC=3 SCAR=11.6
579 2 ▶ return $self if !$self->{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"} 1 1 2
580 1 my $state = $self->{state};
581 1 ▶ if ($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) { } 1 0 1
582 1 $state->_cancel($self);
583 }
584 else {
585 0 $self->{active} = 0;
586 0 delete $self->{callback};
587 }
588 1 return $self;
589 }
590  
591 10 ▶sub is_active ($self) { !!$self->{active} }
Subroutine is_active: called CC=1 SCAR=0.0
592 0 ▶sub CLONE_SKIP ($class) { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
593  
594 package Linux::Event::_DeferredFork;
595  
596 2 ▶sub cancel ($self) {
Subroutine cancel: called CC=5 SCAR=16.7
597 2 ▶ return $self if !$self->{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"} 1 1 2
598 1 $self->{active} = 0;
599 1 delete @$self{qw(options on_parent on_child on_error)};
600 1 my $state = $self->{state};
601 1 ▶ if ($state && $state->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state and $state->{"pending"}) 1 0 1
Condition33%1/3
$state and $state->{"pending"}
!ll&&!rl&&r
001
MC/DC0%0/2
$state and $state->{"pending"}
$state $state->{"pending"}
Truth table33%1/3
A and B
A= $stateB= $state->{"pending"}
ABresult
0X0
100
111
602 1 $state->{pending}--;
603 1 ▶ $state->{queue} = [] if !$state->{pending};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"pending"} 1 0 1
604 }
605 1 return $self;
606 }
607  
608 6 ▶sub is_active ($self) { !!$self->{active} }
Subroutine is_active: called CC=1 SCAR=0.0
609 0 ▶sub CLONE_SKIP ($class) { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
610  
611 package Linux::Event::_Registration;
612 0 ▶sub CLONE_SKIP ($class) { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
613  
614 package Linux::Event::Loop;
615  
616 181 1;
617  
618 __END__
« blib/lib/Linux/Event/_Socket/Connection.pm ↑ Summary bench/run-reactor-comparison.pl »