Perl/aio.pm

filter: Statementstmt 90.0% 99 / 110 Branchbran 59.0% 13 / 22 Conditioncond 80.0% 8 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 22 / 22 Podpod 100% 4 / 4 totaltotal 85.0% 148 / 174 CC 19 SCAR 20.9
CC
19
Cov
85%
new
19.6
getevents
5.1
new
2.0
CRAP 8.1
« Perl/memfd.pm ↑ Summary Perl/getrandom.pm »
1 package Linux::Perl::aio;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Linux:Perl::aio - asynchronous I/O
8  
9 =head1 SYNOPSIS
10  
11 #Platform-specific invocation uses e.g.:
12 # Linux::Perl::aio::x86_64->new(...)
13 # Linux::Perl::aio::Control::x86_64->new(...)
14  
15 my $aio = Linux::Perl::aio->new(16);
16  
17 my $ctrl = Linux::Perl::aio::Control->new(
18 $filehandle,
19 \$buffer,
20 lio_opcode => 'PREAD',
21 );
22  
23 #Multiple $ctrl objects can be submitted in a list.
24 $aio->submit($ctrl);
25  
26 my @events = $aio->getevents( $min, $max, $timeout );
27  
28 =head1 DESCRIPTION
29  
30 This module provides support for the kernel-level AIO interface.
31  
32 DESTROY handlers are provided for automatic reaping of unused
33 instances/contexts.
34  
35 This module is EXPERIMENTAL. For now only the C<x86_64> architecture
36 is supported; others may follow, though 32-bit architectures would
37 take a bit more work.
38  
39 =cut
40  
41 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 2 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use Linux::Perl::TimeSpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 ▶use parent qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
49 Linux::Perl::Base
50 Linux::Perl::Base::BitsTest
51 2 );
52  
53 2 my ($io_event_keys_ar, $io_event_pack, $io_event_size);
54  
55 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 my @_io_event_src = (
57 data => __PACKAGE__->_PACK_u64(),
58 obj => __PACKAGE__->_PACK_u64(),
59 res => __PACKAGE__->_PACK_i64(),
60 res2 => __PACKAGE__->_PACK_i64(),
61 );
62  
63 2 ($io_event_keys_ar, $io_event_pack) = Linux::Perl::EasyPack::split_pack_list(@_io_event_src);
64 2 $io_event_size = length pack $io_event_pack;
65 }
66  
67 =head1 METHODS
68  
69 =head2 I<CLASS>->new( NR_EVENTS )
70  
71 Calls C<io_setup> with the referred number of events to create
72 an AIO context. An object of CLASS is returned.
73  
74 =cut
75  
76 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
77 6 my ( $class, $nr_events ) = @_;
78  
79 6 ▶ die "Need number of events!" if !$nr_events;
Branch: 1 branch
BranchTrueFalseTotal
unless $nr_events 0 6 6
80  
81 6 $class = $class->_get_arch_module();
82  
83 6 my $context = "\0" x 8;
84  
85 6 Linux::Perl::call( $class->NR_io_setup(), 0 + $nr_events, $context );
86  
87 6 $context = unpack $class->_PACK_u64(), $context;
88  
89 6 return bless \$context, $class;
90 }
91  
92 =head2 I<CLASS>->create_control( FILEHANDLE, BUFFER_SR, %OPTS )
93  
94 Returns an instance of the relevant L<Linux::Perl::aio::Control>
95 subclass for your architecture.
96  
97 FILEHANDLE is a Perl filehandle object, and BUFFER_SR is a reference
98 to the buffer string. This buffer must be pre-initialized to at least
99 the needed/desired length.
100  
101 %OPTS is:
102  
103 =over
104  
105 =item * C<lio_opcode>: Required, one of: C<PREAD>, C<PWRITE>, C<FSYNC>,
106 C<FDSYNC>, C<NOOP>, C<PREADV>, C<PWRITEV>.
107  
108 =item * C<buffer_offset>: The byte offset in BUFFER_SR at which to start
109 the I/O operation. Defaults to 0.
110  
111 =item * C<nbytes>: The number of bytes on which to operate. This value
112 plus C<buffer_offset> must be less than the length of BUFFER_SR. Defaults
113 to length(BUFFER_SR) minus C<buffer_offset>.
114  
115 =item * C<rw_flags>: Optional, an array reference of any or all of: C<HIPRI>,
116 C<DSYNC>, C<SYNC>, C<NOWAIT>, C<APPEND>. Not supported in all kernel versions;
117 in fact, support seems more the exception than the rule!
118 See the kernel documentation (e.g., C<RWF_HIPRI>) for details on
119 what these flags mean and whether your system supports them.
120  
121 =item * C<reqprio>: Optional. See the kernel’s documentation.
122  
123 =item * C<eventfd>: Optional, an eventfd file descriptor
124 (i.e., unsigned integer) to receive updates when aio events are finished.
125 (See L<Linux::Perl::eventfd> for one way of making this work.)
126  
127 =back
128  
129 For more information, consult the definition and documentation
130 for struct C<iocb>. (cf. F<include/linux/aio_abi.h>)
131  
132 =cut
133  
134 12 ▶sub create_control {
Subroutine create_control: called CC=1 SCAR=0.0
Pod documented
135 12 my $self = shift;
136  
137 12 return Linux::Perl::aio::Control->new(@_);
138 }
139  
140 =head2 $num = I<OBJ>->submit( CTRL1, CTRL2, .. )
141  
142 Calls C<io_submit>. Each CTRL* is an instance of
143 L<Linux::Perl::aio::Control> and represets an I/O request.
144  
145 The return value is the number of control objects submitted.
146  
147 =cut
148  
149  
150 10 ▶sub submit {
Subroutine submit: called CC=1 SCAR=0.0
Pod documented
151 10 my ( $self, @control_objs ) = @_;
152  
153 10 my $ptrs = join( q<>, map { $_->pointer() } @control_objs );
154  
155 10 return Linux::Perl::call( $self->NR_io_submit(), 0 + $$self, 0 + @control_objs, $ptrs );
156 }
157  
158 =head2 @events = I<OBJ>->getevents( MIN, MAX, TIMEOUT )
159  
160 Calls C<io_getevents> with the relevant minimum, maximum, and timeout
161 values. (TIMEOUT can be a float.)
162  
163 If more than one event is requested (i.e., MAX > 1), then list
164 context is required.
165  
166 The return is a list of hash references; each hash reference has the following
167 values as in the kernel C<io_event> struct:
168  
169 =over
170  
171 =item * C<data>
172  
173 =item * C<obj> (corresponds to the Control instance C<id()>)
174  
175 =item * C<res>
176  
177 =item * C<res2>
178  
179 =back
180  
181 =cut
182  
183 10 ▶sub getevents {
Subroutine getevents: called CC=5 SCAR=16.2
Pod documented
184 10 my ( $self, $min_events, $max_events, $timeout ) = @_;
185  
186 #If they only asked for one, then allow scalar context.
187 10 ▶ if ($max_events > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_events > 1) 2 8 10
188 2 require Call::Context;
189 2 Call::Context::must_be_list();
190 }
191  
192 10 ▶ if (!$max_events) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($max_events) 0 10 10
193 0 die '$max_events must be >0!';
194 }
195  
196 10 my $buf = "\0" x ( $max_events * $io_event_size );
197  
198 10 my $evts = Linux::Perl::call(
199 $self->NR_io_getevents(),
200 $$self,
201 0 + $min_events,
202 0 + $max_events,
203 $buf,
204 Linux::Perl::TimeSpec::from_float($timeout),
205 );
206  
207 10 my @events;
208 10 for my $idx ( 0 .. ( $evts - 1 ) ) {
209 12 my @data = unpack $io_event_pack, substr( $buf, $idx * $io_event_size, $io_event_size );
210 12 my %event;
211 12 @event{ @$io_event_keys_ar } = @data;
212 12 push @events, \%event;
213 }
214  
215 10 ▶ return wantarray ? @events : $events[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 0 10
216 }
217  
218 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
219 6 my ($self) = @_;
220  
221 6 Linux::Perl::call( $self->NR_io_destroy(), 0 + $$self);
222  
223 6 return;
224 }
225  
226 #----------------------------------------------------------------------
227  
228 package Linux::Perl::aio::Control;
229  
230 2 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 2 ▶use Linux::Perl::Endian;
Subroutine BEGIN: called CC=1 SCAR=0.0
232  
233 =encoding utf-8
234  
235 =head1 NAME
236  
237 Linux::Perl::aio::Control
238  
239 =head1 SYNOPSIS
240  
241 my $ctrl = Linux::Perl::aio::Control->new(
242 $filehandle,
243 \$buffer,
244 lio_opcode => 'PREAD',
245 buffer_offset => 4,
246 nbytes => 2,
247 );
248  
249 =head1 DESCRIPTION
250  
251 This class encapsulates a kernel C<iocb> struct, i.e., an I/O request.
252  
253 You should not instantiate it directly; instead, use
254 L<Linux::Perl::aio>’s C<create_control()> method.
255  
256 =cut
257  
258 2 ▶use parent -norequire => 'Linux::Perl::Base::BitsTest';
Subroutine BEGIN: called CC=1 SCAR=0.0
259  
260 2 ▶use Linux::Perl::Pointer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
263 2 _RWF_HIPRI => 1,
264 _RWF_DSYNC => 2,
265 _RWF_SYNC => 4,
266 _RWF_NOWAIT => 8,
267 _RWF_APPEND => 16,
268  
269 _IOCB_CMD_PREAD => 0,
270 _IOCB_CMD_PWRITE => 1,
271 _IOCB_CMD_FSYNC => 2,
272 _IOCB_CMD_FDSYNC => 3,
273  
274 #experimental
275 #_IOCB_CMD_PREADX => 4,
276 #_IOCB_CMD_POLL => 5,
277  
278 _IOCB_CMD_NOOP => 6,
279 _IOCB_CMD_PREADV => 7,
280 _IOCB_CMD_PWRITEV => 8,
281  
282 _IOCB_FLAG_RESFD => 1,
283 2 };
284  
285 2 my ($iocb_keys_ar, $iocb_pack);
286  
287 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
288 2 my @_iocb_src = (
289 data => __PACKAGE__->_PACK_u64(), #aio_data
290  
291 (
292 Linux::Perl::Endian::SYSTEM_IS_BIG_ENDIAN()
293 ? (
294 rw_flags => 'L',
295 key => 'L',
296 )
297 : (
298 key => 'L',
299 rw_flags => 'L',
300 )
301 ),
302  
303 lio_opcode => 'S',
304 reqprio => 's',
305 fildes => 'L',
306  
307 #Would be a P, but we grab the P and do some byte arithmetic on it
308 #for the case of a buffer_offset.
309 buf => __PACKAGE__->_PACK_u64(),
310  
311 nbytes => __PACKAGE__->_PACK_u64(),
312  
313 offset => __PACKAGE__->_PACK_i64(),
314  
315 reserved2 => 'x8',
316  
317 flags => 'L',
318 resfd => 'L',
319 );
320  
321 2 ($iocb_keys_ar, $iocb_pack) = Linux::Perl::EasyPack::split_pack_list(@_iocb_src);
322 }
323  
324 =head1 METHODS
325  
326 =head2 I<CLASS>->new( FILEHANDLE, BUFFER_SR, %OPTS )
327  
328 =cut
329  
330 12 ▶sub new {
Subroutine new: called CC=14 SCAR=29.7
331 12 my ( $class, $fh, $buf_sr, %args ) = @_;
332  
333 12 ▶ my $opcode = $args{'lio_opcode'} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $opcode = $args{"lio_opcode"} or do { die "Need \342\200\234lio_opcode\342\200\235!" } 12 0 12
334 0 die "Need “lio_opcode”!";
335 };
336  
337 12 ▶ my $opcode_cr = $class->can("_IOCB_CMD_$opcode") or do {
Branch: 1 branch
BranchTrueFalseTotal
my $opcode_cr = $class->can("_IOCB_CMD_$opcode") or do { die "Unknown \342\200\234lio_opcode\342\200\235 ($opcode)" } 12 0 12
338 0 die "Unknown “lio_opcode” ($opcode)";
339 };
340  
341 12 my %opts;
342 12 @opts{'nbytes', 'buffer_offset'} = @args{'nbytes', 'buffer_offset'};
343  
344 12 $opts{'lio_opcode'} = 0 + $opcode_cr->();
345 12 $opts{'fildes'} = fileno $fh;
346 12 $opts{'reserved2'} = 0;
347 12 $opts{'reqprio'} = $args{'reqprio'};
348  
349 12 ▶ if ($args{'rw_flags'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"rw_flags"}) 0 12 12
350 0 my $flag = 0;
351 0 for my $flag_name ( @{ $args{'rw_flags'} } ) {
352 0 ▶ my $num = $class->can("_RWF_$flag_name") or do {
Branch: 1 branch
BranchTrueFalseTotal
my $num = $class->can("_RWF_$flag_name") or do { die "Unknown -rw_flags- value ($flag_name)" } 0 0 0
353 0 die "Unknown -rw_flags- value ($flag_name)";
354 };
355 0 $flag |= 0 + $num->();
356 }
357  
358 0 $opts{'rw_flags'} = $flag;
359 }
360  
361 12 ▶ if (defined $args{'eventfd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"eventfd"}) 4 8 12
362 4 $opts{'flags'} = _IOCB_FLAG_RESFD;
363 4 $opts{'resfd'} = $args{'eventfd'};
364 }
365  
366 12 my $buf_ptr = Linux::Perl::Pointer::get_address($$buf_sr);
367  
368 12 ▶ my $buffer_offset = $opts{'buffer_offset'} || 0;
Condition100%2/2
$opts{'buffer_offset'} || 0
l!l
48
MC/DC100%1/1
$opts{'buffer_offset'} || 0
$opts{'buffer_offset'}
Truth table100%2/2
A
A= $opts{'buffer_offset'}
Aresult
11
00
369  
370 12 ▶ if ( $opts{'buffer_offset'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'buffer_offset'}) { } 4 8 12
371 4 ▶ $opts{'nbytes'} ||= length($$buf_sr) - $opts{'buffer_offset'};
Condition66%2/3
$opts{"nbytes"} ||= length($$buf_sr) - $opts{"buffer_offset"}
l!l&&r!l&&!r
220
MC/DC0%0/2
$opts{"nbytes"} ||= length($$buf_sr) - $opts{"buffer_offset"}
$opts{"nbytes"} length($$buf_sr) - $opts{"buffer_offset"}
Truth table66%2/3
A ||= B
A= $opts{"nbytes"}B= length($$buf_sr) - $opts{"buffer_offset"}
ABresult
1X1
011
000
372  
373 4 $buf_ptr += $opts{'buffer_offset'};
374 }
375 else {
376 8 ▶ $opts{'nbytes'} ||= length $$buf_sr;
Condition66%2/3
$opts{"nbytes"} ||= length $$buf_sr
l!l&&r!l&&!r
260
MC/DC0%0/2
$opts{"nbytes"} ||= length $$buf_sr
$opts{"nbytes"} length $$buf_sr
Truth table66%2/3
A ||= B
A= $opts{"nbytes"}B= length $$buf_sr
ABresult
1X1
011
000
377 }
378  
379 12 ▶ if ( $opts{'nbytes'} + $buffer_offset > length $$buf_sr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"nbytes"} + $buffer_offset > length $$buf_sr) 0 12 12
380 0 die sprintf( "nbytes($opts{'nbytes'}) + buffer_offset($buffer_offset) > buffer_length(%d)", length $$buf_sr );
381 }
382  
383 12 $opts{'buf'} = $buf_ptr;
384  
385 12 ▶ $_ ||= 0 for @opts{ @$iocb_keys_ar };
Condition100%2/2
$_ ||= 0
l!l
4686
MC/DC100%1/1
$_ ||= 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
386  
387 12 my $packed = pack $iocb_pack, @opts{ @$iocb_keys_ar };
388 12 my $ptr = pack 'P', $packed;
389  
390 #We need $packed not to be garbage-collected.
391 12 return bless [ \$packed, $buf_sr, $ptr, unpack( Linux::Perl::Pointer::UNPACK_TMPL(), $ptr) ], $class;
392 }
393  
394 =head2 $sref = I<OBJ>->buffer_sr()
395  
396 Returns the string buffer reference given originally to C<new()>.
397  
398 =cut
399  
400 4 ▶sub buffer_sr { return $_[0][1] }
Subroutine buffer_sr: called CC=1 SCAR=0.0
401  
402 =head2 $sref = I<OBJ>->pointer()
403  
404 Returns the internal C<iocb>’s memory address as an octet string.
405  
406 =cut
407  
408 12 ▶sub pointer { return $_[0][2] }
Subroutine pointer: called CC=1 SCAR=0.0
409  
410 =head2 $sref = I<OBJ>->id()
411  
412 Returns the internal C<iocb>’s ID.
413  
414 =cut
415  
416 6 ▶sub id { return $_[0][3] }
Subroutine id: called CC=1 SCAR=0.0
417  
418 2 1;
« Perl/memfd.pm ↑ Summary Perl/getrandom.pm »