Perl/epoll.pm

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