Perl/epoll.pm

filter: Statementstmt 97.8% 93 / 95 Branchbran 75.0% 15 / 20 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 86.8% 139 / 160 CC 18 SCAR 12.4
CC
18
Cov
89%
_opts_to_event
8.0
wait
5.0
new
4.0
CRAP 3.5
« Perl/timerfd.pm ↑ Summary Perl/SigSet.pm »
1 package Linux::Perl::epoll;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Linux::Perl::epoll
11  
12 =head1 SYNOPSIS
13  
14 my $epl = Linux::Perl::epoll->new();
15  
16 $epl->add( $fh, events => ['IN', 'ET'] );
17  
18 my @events = $epl->wait(
19 maxevents => 3,
20 timeout => 2, #seconds
21 sigmask => ['INT', 'TERM'], #optional
22 );
23  
24 $epl->delete($fh);
25  
26 =head1 DESCRIPTION
27  
28 An interface to Linux’s “epoll” feature.
29  
30 Note that older kernel versions may not support all of the functionality
31 documented here. Check your system’s epoll documentation (i.e.,
32 C<man 7 epoll> and the various system calls’ pages) for full details.
33  
34 =cut
35  
36 3 ▶use parent 'Linux::Perl::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Linux::Perl::Constants::Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Linux::Perl::ParseFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Linux::Perl::SigSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 3 *_flag_CLOEXEC = \*Linux::Perl::Constants::Fcntl::flag_CLOEXEC;
45  
46 =head1 METHODS
47  
48 =head2 I<CLASS>->new( %OPTS )
49  
50 Creates a new epoll instance. %OPTS are:
51  
52 =over
53  
54 =item * C<flags> - Currently only C<CLOEXEC> is recognized.
55  
56 =item * C<size> - Optional, and only useful on pre-2.6.8 kernels.
57 See C<main 2 epoll_create> for more details.
58  
59 =back
60  
61 =cut
62  
63 sub new {
64 16 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
65  
66 16 local ($!, $^E);
67  
68 16 my $arch_module = $class->_get_arch_module();
69  
70 16 my $flags = Linux::Perl::ParseFlags::parse( $class, $opts{'flags'} );
71  
72 16 my $call_name = 'NR_epoll_create';
73  
74 16 my $fd;
75  
76 16 ▶ if ($flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags) { } 4 12 16
77 4 $call_name .= '1';
78  
79 4 $fd = Linux::Perl::call( $arch_module->$call_name(), 0 + $flags );
80 }
81 else {
82 12 ▶ $opts{'size'} ||= 1;
Condition50%1/2
$opts{"size"} ||= 1
l!l
012
MC/DC0%0/1
$opts{"size"} ||= 1
$opts{"size"}
Truth table50%1/2
A
A= $opts{"size"}
Aresult
11
00
83  
84 12 $fd = Linux::Perl::call( $arch_module->$call_name(), 0 + $opts{'size'} );
85 }
86  
87 # Force the CLOEXEC behavior that Perl imposes on its file handles
88 # unless the CLOEXEC flag was given explicitly.
89 16 my $fh;
90  
91 16 ▶ if ( !($flags & _flag_CLOEXEC()) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags & &_flag_CLOEXEC()) 12 4 16
92 12 open $fh, '+<&=' . $fd;
93 }
94  
95 # NB: tests access the filehandle directly.
96 16 return bless [$fd, $fh], $arch_module;
97 }
98  
99 sub DESTROY {
100 16 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=2 SCAR=6.9
101  
102 # Create a Perl filehandle for the file descriptor so
103 # that we get a close() when the filehandle object goes away.
104 16 ▶ $self->[1] || do {
Branch: 1 branch
BranchTrueFalseTotal
$self->[1] or do { local $^F = 1 + $self->[0]; open my $temp_fh, '+<&=' . $self->[0] } 4 12 16
105 4 local $^F = 1 + $self->[0];
106 4 open my $temp_fh, '+<&=' . $self->[0];
107 };
108  
109 16 return;
110 }
111  
112 3 my ($epoll_event_keys_ar, $epoll_event_pack);
113  
114 BEGIN {
115 3 ▶ my $arch_is_64bit = (8 == length pack 'L!');
Subroutine BEGIN: called CC=2 SCAR=7.1
116  
117 3 ▶ my @_epoll_event_src = (
Branch: 1 branch
BranchTrueFalseTotal
$arch_is_64bit ? : 3 0 3
118 events => 'L', #uint32_t
119 (
120 $arch_is_64bit
121 ? ( data => 'Q' )
122 : (
123 q<> => 'xxxx',
124 data => 'L!', #uint64_t
125 ),
126 ),
127 );
128  
129 3 ($epoll_event_keys_ar, $epoll_event_pack) = Linux::Perl::EasyPack::split_pack_list(@_epoll_event_src);
130 }
131  
132 #----------------------------------------------------------------------
133  
134 =head2 I<CLASS>->EVENT_NUMBER()
135  
136 Returns a (constant) hash reference that cross-references event names
137 and their numbers. This is useful, e.g., for parsing events from the return
138 of C<wait()>.
139  
140 The recognized event names are C<IN>, C<OUT>, C<RDHUP>, C<PRI>, C<ERR>,
141 and C<HUP>.
142  
143 =cut
144  
145 use constant {
146 3 EVENT_NUMBER => {
147 IN => 1,
148 OUT => 4,
149 RDHUP => 0x2000,
150 PRI => 2,
151 ERR => 8,
152 HUP => 16,
153 },
154  
155 _EPOLL_CTL_ADD => 1,
156 _EPOLL_CTL_DEL => 2,
157 _EPOLL_CTL_MOD => 3,
158  
159 _EVENT_FLAGS => {
160 ET => (1 << 31),
161 ONESHOT => (1 << 30),
162 WAKEUP => (1 << 29),
163 EXCLUSIVE => (1 << 28),
164 },
165 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 #----------------------------------------------------------------------
168  
169 =head2 I<OBJ>->add( $FD_OR_FH, %OPTS )
170  
171 Adds a listener to the epoll instance. $FD_OR_FH is either a
172 Perl filehandle or a file descriptor number. %OPTS are:
173  
174 =over
175  
176 =item * C<events> - An array reference of events/switches. Each member
177 is either a key from C<EVENT_NUMBER()> or one of the following
178 switches: C<ET>, C<ONESHOT>, C<WAKEUP>, C<EXCLUSIVE>. Your kernel
179 may not support all of those; check C<man 2 epoll_ctl> for details.
180  
181 =item * C<data> - Optional, an arbitrary number to store with the file
182 descriptor. This defaults to the file descriptor because this is the obvious
183 way to correlate an event with its filehandle; however, you can set your own
184 numeric value here if you’d rather.
185  
186 =back
187  
188 =cut
189  
190 sub add {
191 8 ▶ my ($self, $fd_or_fh, @opts_kv) = @_;
Subroutine add: called CC=1 SCAR=0.0
Pod documented
192  
193 8 return $self->_add_or_modify( _EPOLL_CTL_ADD(), $fd_or_fh, @opts_kv );
194 }
195  
196 =head2 I<OBJ>->modify( $FD_OR_FH, %OPTS )
197  
198 Same arguments as C<add()>; use this to update an existing epoll listener.
199  
200 =cut
201  
202 sub modify {
203 2 ▶ my ($self, $fd_or_fh, @opts_kv) = @_;
Subroutine modify: called CC=1 SCAR=0.0
Pod documented
204  
205 2 return $self->_add_or_modify( _EPOLL_CTL_MOD(), $fd_or_fh, @opts_kv );
206 }
207  
208 sub _opts_to_event {
209 10 ▶ my ($opts_hr) = @_;
Subroutine _opts_to_event: called CC=6 SCAR=20.8
210  
211 10 ▶ if (!$opts_hr->{'events'} || !@{ $opts_hr->{'events'} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts_hr->{"events"} and @{$opts_hr->{"events"};}) 0 10 10
Condition33%1/3
$opts_hr->{"events"} and @{$opts_hr->{"events"};}
!ll&&!rl&&r
0010
MC/DC0%0/2
$opts_hr->{"events"} and @{$opts_hr->{"events"};}
$opts_hr->{"events"} @{$opts_hr->{"events"};}
Truth table33%1/3
A and B
A= $opts_hr->{"events"}B= @{$opts_hr->{"events"};}
ABresult
0X0
100
111
212 0 die 'Need events!';
213 }
214  
215 10 my $events = 0;
216 10 for my $evtname ( @{ $opts_hr->{'events'} } ) {
217 12 ▶ $events |= EVENT_NUMBER()->{$evtname} || _EVENT_FLAGS()->{$evtname} || do {
Condition50%3/6
{ERR => 8, HUP => 16, IN => 1, OUT => 4, PRI => 2, RDHUP => 8192}->{$evtname} || {ET => 2147483648, EXCLUSIVE => 268435456, ONESHOT => 1073741824, WAKEUP => 536870912}->{$evtname} || do { die "Unknown event '${evtname}'" }
l!l&&r!l&&!r
1200
{"ERR" => 8,"HUP" => 16,"IN" => 1,"OUT" => 4,"PRI" => 2,"RDHUP" => 8192}->{$evtname} or {"ET" => 2147483648,"EXCLUSIVE" => 268435456,"ONESHOT" => 1073741824,"WAKEUP" => 536870912}->{$evtname}
l!l&&r!l&&!r
1020
MC/DC0%0/3
{ERR => 8, HUP => 16, IN => 1, OUT => 4, PRI => 2, RDHUP => 8192}->{$evtname} || {ET => 2147483648, EXCLUSIVE => 268435456, ONESHOT => 1073741824, WAKEUP => 536870912}->{$evtname} || do { die "Unknown event '${evtname}'" }
{"ERR" => 8,"HUP" => 16,"IN" => 1,"OUT" => 4,"PRI" => 2,"RDHUP" => 8192}->{$evtname} {"ET" => 2147483648,"EXCLUSIVE" => 268435456,"ONESHOT" => 1073741824,"WAKEUP" => 536870912}->{$evtname} do { die "Unknown event '${evtname}'" }
Truth table50%2/4
A or B || C
A= {"ERR" => 8,"HUP" => 16,"IN" => 1,"OUT" => 4,"PRI" => 2,"RDHUP" => 8192}->{$evtname}B= {"ET" => 2147483648,"EXCLUSIVE" => 268435456,"ONESHOT" => 1073741824,"WAKEUP" => 536870912}->{$evtname}C= do { die "Unknown event '${evtname}'" }
ABCresult
1XX1
01X1
0011
0000
218 0 die "Unknown event '$evtname'";
219 };
220 }
221  
222 10 return pack $epoll_event_pack, $events, $opts_hr->{'data'};
223 }
224  
225 sub _add_or_modify {
226 10 ▶ my ($self, $op, $fd_or_fh, %opts) = @_;
Subroutine _add_or_modify: called CC=3 SCAR=11.2
227  
228 10 ▶ my $fd = ref($fd_or_fh) ? fileno($fd_or_fh) : $fd_or_fh;
Branch: 1 branch
BranchTrueFalseTotal
ref $fd_or_fh ? : 10 0 10
229  
230 10 ▶ if (!defined $opts{'data'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"data"}) 10 0 10
231 10 $opts{'data'} = $fd;
232 }
233  
234 10 my $event_packed = _opts_to_event(\%opts);
235  
236 10 Linux::Perl::call(
237 $self->NR_epoll_ctl(),
238 0 + $self->[0],
239 0 + $op,
240 0 + $fd,
241 $event_packed,
242 );
243  
244 10 return $self;
245 }
246  
247 #----------------------------------------------------------------------
248  
249 =head2 I<OBJ>->delete( $FD_OR_FH )
250  
251 Removes an epoll listener.
252  
253 =cut
254  
255 sub delete {
256 2 ▶ my ($self, $fd_or_fh) = @_;
Subroutine delete: called CC=2 SCAR=7.0
Pod documented
257  
258 2 ▶ my $fd = ref($fd_or_fh) ? fileno($fd_or_fh) : $fd_or_fh;
Branch: 1 branch
BranchTrueFalseTotal
ref $fd_or_fh ? : 2 0 2
259  
260 2 Linux::Perl::call(
261 $self->NR_epoll_ctl(),
262 0 + $self->[0],
263 0 + _EPOLL_CTL_DEL(),
264 0 + $fd,
265 (pack $epoll_event_pack), #accommodate pre-2.6.9 kernels
266 );
267  
268 2 return $self;
269 }
270  
271 #----------------------------------------------------------------------
272  
273 =head2 @events = I<OBJ>->wait( %OPTS )
274  
275 Waits for one or more events on the epoll. %OPTS are:
276  
277 =over
278  
279 =item * C<maxevents> - The number of events to listen for.
280  
281 =item * C<timeout> - in seconds
282  
283 =item * C<sigmask> - Optional, an array of signals to block as part of
284 this function call. Give signals either as names (e.g., C<INT>) or as numbers. See C<man 2 epoll_pwait> for why you might want to do this. Also see
285 L<Linux::Perl::sigprocmask> for an easy, light way to block signals.
286  
287 =back
288  
289 The return is a list of hash references, one for each received event.
290 Each hash reference is:
291  
292 =over
293  
294 =item * C<data> - The same number given in C<add()>—or, if you didn’t
295 set a custom C<data> value, the file descriptor associated with the event.
296  
297 =item * C<events> - Corresponds to the same-named array given in C<add()>,
298 but to optimize performance this is returned as a single number. Check
299 for specific events by iterating through the C<EVENT_NUMBER()> hash
300 reference.
301  
302 =back
303  
304 =cut
305  
306 sub wait {
307 16 ▶ my ($self, %opts) = @_;
Subroutine wait: called CC=5 SCAR=16.1
Pod documented
308  
309 16 my $sigmask;
310  
311 16 my $call_name = 'NR_epoll_';
312 16 ▶ if ($opts{'sigmask'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'sigmask'}) { } 2 14 16
313 2 $call_name .= 'pwait';
314 2 $sigmask = Linux::Perl::SigSet::from_list( @{$opts{'sigmask'}} );
315 }
316 else {
317 14 $call_name .= 'wait';
318 }
319  
320 16 my $blank_event = pack $epoll_event_pack;
321 16 my $buf = $blank_event x $opts{'maxevents'};
322  
323 16 my $timeout = int(1000 * $opts{'timeout'});
324  
325 my $count = Linux::Perl::call(
326 $self->$call_name(),
327 0 + $self->[0],
328 $buf,
329 16 ▶ 0 + $opts{'maxevents'},
Branch: 1 branch
BranchTrueFalseTotal
defined $sigmask && length $sigmask ? : 2 14 16
Condition66%2/3
defined $sigmask and length $sigmask
!ll&&!rl&&r
1402
MC/DC50%1/2
defined $sigmask and length $sigmask
defined $sigmask length $sigmask
Truth table66%2/3
A and B
A= defined $sigmaskB= length $sigmask
ABresult
0X0
100
111
330 0 + $timeout,
331 ( (defined($sigmask) && length($sigmask))
332 ? ( $sigmask, length $sigmask )
333 : (),
334 ),
335 );
336  
337 16 my @events;
338 16 for (1 .. $count) {
339 8 my ($events_num, $data) = unpack( $epoll_event_pack, substr( $buf, 0, length($blank_event), q<> ) );
340  
341 8 push @events, {
342 events => $events_num,
343 data => $data,
344 };
345 }
346  
347 16 return @events;
348 }
349  
350 3 1;
« Perl/timerfd.pm ↑ Summary Perl/SigSet.pm »