… | |
… | |
50 | |
50 | |
51 | =head1 DESCRIPTION |
51 | =head1 DESCRIPTION |
52 | |
52 | |
53 | This module implements asynchronous I/O using whatever means your |
53 | This module implements asynchronous I/O using whatever means your |
54 | operating system supports. |
54 | operating system supports. |
|
|
55 | |
|
|
56 | Asynchronous means that operations that can normally block your program |
|
|
57 | (e.g. reading from disk) will be done asynchronously: the operation |
|
|
58 | will still block, but you can do something else in the meantime. This |
|
|
59 | is extremely useful for programs that need to stay interactive even |
|
|
60 | when doing heavy I/O (GUI programs, high performance network servers |
|
|
61 | etc.), but can also be used to easily do operations in parallel that are |
|
|
62 | normally done sequentially, e.g. stat'ing many files, which is much faster |
|
|
63 | on a RAID volume or over NFS when you do a number of stat operations |
|
|
64 | concurrently. |
|
|
65 | |
|
|
66 | While this works on all types of file descriptors (for example sockets), |
|
|
67 | using these functions on file descriptors that support nonblocking |
|
|
68 | operation (again, sockets, pipes etc.) is very inefficient. Use an event |
|
|
69 | loop for that (such as the L<Event|Event> module): IO::AIO will naturally |
|
|
70 | fit into such an event loop itself. |
55 | |
71 | |
56 | In this version, a number of threads are started that execute your |
72 | In this version, a number of threads are started that execute your |
57 | requests and signal their completion. You don't need thread support |
73 | requests and signal their completion. You don't need thread support |
58 | in perl, and the threads created by this module will not be visible |
74 | in perl, and the threads created by this module will not be visible |
59 | to perl. In the future, this module might make use of the native aio |
75 | to perl. In the future, this module might make use of the native aio |
60 | functions available on many operating systems. However, they are often |
76 | functions available on many operating systems. However, they are often |
61 | not well-supported or restricted (Linux doesn't allow them on normal |
77 | not well-supported or restricted (GNU/Linux doesn't allow them on normal |
62 | files currently, for example), and they would only support aio_read and |
78 | files currently, for example), and they would only support aio_read and |
63 | aio_write, so the remaining functionality would have to be implemented |
79 | aio_write, so the remaining functionality would have to be implemented |
64 | using threads anyway. |
80 | using threads anyway. |
65 | |
81 | |
66 | Although the module will work with in the presence of other (Perl-) |
82 | Although the module will work with in the presence of other (Perl-) |
… | |
… | |
131 | our $VERSION = '2.0'; |
147 | our $VERSION = '2.0'; |
132 | |
148 | |
133 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
149 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
134 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
150 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
135 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
151 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
136 | aio_group aio_nop); |
152 | aio_copy aio_group aio_nop aio_mknod); |
137 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
153 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
138 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
154 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
139 | min_parallel max_parallel nreqs nready npending); |
155 | min_parallel max_parallel nreqs nready npending); |
140 | |
156 | |
141 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
157 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
… | |
… | |
264 | |
280 | |
265 | aio_read $fh, 7, 15, $buffer, 0, sub { |
281 | aio_read $fh, 7, 15, $buffer, 0, sub { |
266 | $_[0] > 0 or die "read error: $!"; |
282 | $_[0] > 0 or die "read error: $!"; |
267 | print "read $_[0] bytes: <$buffer>\n"; |
283 | print "read $_[0] bytes: <$buffer>\n"; |
268 | }; |
284 | }; |
269 | |
|
|
270 | =item aio_move $srcpath, $dstpath, $callback->($status) |
|
|
271 | |
|
|
272 | Try to move the I<file> (directories not supported as either source or |
|
|
273 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
|
|
274 | the C<0> (error) or C<-1> ok. |
|
|
275 | |
|
|
276 | This is a composite request that tries to rename(2) the file first. If |
|
|
277 | rename files with C<EXDEV>, it creates the destination file with mode 0200 |
|
|
278 | and copies the contents of the source file into it using C<aio_sendfile>, |
|
|
279 | followed by restoring atime, mtime, access mode and uid/gid, in that |
|
|
280 | order, and unlinking the C<$srcpath>. |
|
|
281 | |
|
|
282 | If an error occurs, the partial destination file will be unlinked, if |
|
|
283 | possible, except when setting atime, mtime, access mode and uid/gid, where |
|
|
284 | errors are being ignored. |
|
|
285 | |
|
|
286 | =cut |
|
|
287 | |
|
|
288 | sub aio_move($$$) { |
|
|
289 | my ($src, $dst, $cb) = @_; |
|
|
290 | |
|
|
291 | my $pri = aioreq_pri; |
|
|
292 | my $grp = aio_group $cb; |
|
|
293 | |
|
|
294 | aioreq_pri $pri; |
|
|
295 | add $grp aio_rename $src, $dst, sub { |
|
|
296 | if ($_[0] && $! == EXDEV) { |
|
|
297 | aioreq_pri $pri; |
|
|
298 | add $grp aio_open $src, O_RDONLY, 0, sub { |
|
|
299 | if (my $src_fh = $_[0]) { |
|
|
300 | my @stat = stat $src_fh; |
|
|
301 | |
|
|
302 | aioreq_pri $pri; |
|
|
303 | add $grp aio_open $dst, O_WRONLY, 0200, sub { |
|
|
304 | if (my $dst_fh = $_[0]) { |
|
|
305 | aioreq_pri $pri; |
|
|
306 | add $grp aio_sendfile $dst_fh, $src_fh, 0, $stat[7], sub { |
|
|
307 | close $src_fh; |
|
|
308 | |
|
|
309 | if ($_[0] == $stat[7]) { |
|
|
310 | utime $stat[8], $stat[9], $dst; |
|
|
311 | chmod $stat[2] & 07777, $dst_fh; |
|
|
312 | chown $stat[4], $stat[5], $dst_fh; |
|
|
313 | close $dst_fh; |
|
|
314 | |
|
|
315 | aioreq_pri $pri; |
|
|
316 | add $grp aio_unlink $src, sub { |
|
|
317 | $grp->result ($_[0]); |
|
|
318 | }; |
|
|
319 | } else { |
|
|
320 | my $errno = $!; |
|
|
321 | aioreq_pri $pri; |
|
|
322 | add $grp aio_unlink $dst, sub { |
|
|
323 | $! = $errno; |
|
|
324 | $grp->result (-1); |
|
|
325 | }; |
|
|
326 | } |
|
|
327 | }; |
|
|
328 | } else { |
|
|
329 | $grp->result (-1); |
|
|
330 | } |
|
|
331 | }, |
|
|
332 | |
|
|
333 | } else { |
|
|
334 | $grp->result (-1); |
|
|
335 | } |
|
|
336 | }; |
|
|
337 | } else { |
|
|
338 | $grp->result ($_[0]); |
|
|
339 | } |
|
|
340 | }; |
|
|
341 | |
|
|
342 | $grp |
|
|
343 | } |
|
|
344 | |
285 | |
345 | =item aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
286 | =item aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
346 | |
287 | |
347 | Tries to copy C<$length> bytes from C<$in_fh> to C<$out_fh>. It starts |
288 | Tries to copy C<$length> bytes from C<$in_fh> to C<$out_fh>. It starts |
348 | reading at byte offset C<$in_offset>, and starts writing at the current |
289 | reading at byte offset C<$in_offset>, and starts writing at the current |
… | |
… | |
404 | =item aio_unlink $pathname, $callback->($status) |
345 | =item aio_unlink $pathname, $callback->($status) |
405 | |
346 | |
406 | Asynchronously unlink (delete) a file and call the callback with the |
347 | Asynchronously unlink (delete) a file and call the callback with the |
407 | result code. |
348 | result code. |
408 | |
349 | |
|
|
350 | =item aio_mknod $path, $mode, $dev, $callback->($status) |
|
|
351 | |
|
|
352 | Asynchronously create a device node (or fifo). See mknod(2). |
|
|
353 | |
|
|
354 | The only portable (POSIX) way of calling this function is: |
|
|
355 | |
|
|
356 | aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ... |
|
|
357 | |
409 | =item aio_link $srcpath, $dstpath, $callback->($status) |
358 | =item aio_link $srcpath, $dstpath, $callback->($status) |
410 | |
359 | |
411 | Asynchronously create a new link to the existing object at C<$srcpath> at |
360 | Asynchronously create a new link to the existing object at C<$srcpath> at |
412 | the path C<$dstpath> and call the callback with the result code. |
361 | the path C<$dstpath> and call the callback with the result code. |
413 | |
362 | |
… | |
… | |
432 | directory (i.e. opendir + readdir + closedir). The entries will not be |
381 | directory (i.e. opendir + readdir + closedir). The entries will not be |
433 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
382 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
434 | |
383 | |
435 | The callback a single argument which is either C<undef> or an array-ref |
384 | The callback a single argument which is either C<undef> or an array-ref |
436 | with the filenames. |
385 | with the filenames. |
|
|
386 | |
|
|
387 | =item aio_copy $srcpath, $dstpath, $callback->($status) |
|
|
388 | |
|
|
389 | Try to copy the I<file> (directories not supported as either source or |
|
|
390 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
|
|
391 | the C<0> (error) or C<-1> ok. |
|
|
392 | |
|
|
393 | This is a composite request that it creates the destination file with |
|
|
394 | mode 0200 and copies the contents of the source file into it using |
|
|
395 | C<aio_sendfile>, followed by restoring atime, mtime, access mode and |
|
|
396 | uid/gid, in that order. |
|
|
397 | |
|
|
398 | If an error occurs, the partial destination file will be unlinked, if |
|
|
399 | possible, except when setting atime, mtime, access mode and uid/gid, where |
|
|
400 | errors are being ignored. |
|
|
401 | |
|
|
402 | =cut |
|
|
403 | |
|
|
404 | sub aio_copy($$;$) { |
|
|
405 | my ($src, $dst, $cb) = @_; |
|
|
406 | |
|
|
407 | my $pri = aioreq_pri; |
|
|
408 | my $grp = aio_group $cb; |
|
|
409 | |
|
|
410 | aioreq_pri $pri; |
|
|
411 | add $grp aio_open $src, O_RDONLY, 0, sub { |
|
|
412 | if (my $src_fh = $_[0]) { |
|
|
413 | my @stat = stat $src_fh; |
|
|
414 | |
|
|
415 | aioreq_pri $pri; |
|
|
416 | add $grp aio_open $dst, O_CREAT | O_WRONLY | O_TRUNC, 0200, sub { |
|
|
417 | if (my $dst_fh = $_[0]) { |
|
|
418 | aioreq_pri $pri; |
|
|
419 | add $grp aio_sendfile $dst_fh, $src_fh, 0, $stat[7], sub { |
|
|
420 | if ($_[0] == $stat[7]) { |
|
|
421 | $grp->result (0); |
|
|
422 | close $src_fh; |
|
|
423 | |
|
|
424 | # those should not normally block. should. should. |
|
|
425 | utime $stat[8], $stat[9], $dst; |
|
|
426 | chmod $stat[2] & 07777, $dst_fh; |
|
|
427 | chown $stat[4], $stat[5], $dst_fh; |
|
|
428 | close $dst_fh; |
|
|
429 | } else { |
|
|
430 | $grp->result (-1); |
|
|
431 | close $src_fh; |
|
|
432 | close $dst_fh; |
|
|
433 | |
|
|
434 | aioreq $pri; |
|
|
435 | add $grp aio_unlink $dst; |
|
|
436 | } |
|
|
437 | }; |
|
|
438 | } else { |
|
|
439 | $grp->result (-1); |
|
|
440 | } |
|
|
441 | }, |
|
|
442 | |
|
|
443 | } else { |
|
|
444 | $grp->result (-1); |
|
|
445 | } |
|
|
446 | }; |
|
|
447 | |
|
|
448 | $grp |
|
|
449 | } |
|
|
450 | |
|
|
451 | =item aio_move $srcpath, $dstpath, $callback->($status) |
|
|
452 | |
|
|
453 | Try to move the I<file> (directories not supported as either source or |
|
|
454 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
|
|
455 | the C<0> (error) or C<-1> ok. |
|
|
456 | |
|
|
457 | This is a composite request that tries to rename(2) the file first. If |
|
|
458 | rename files with C<EXDEV>, it copies the file with C<aio_copy> and, if |
|
|
459 | that is successful, unlinking the C<$srcpath>. |
|
|
460 | |
|
|
461 | =cut |
|
|
462 | |
|
|
463 | sub aio_move($$;$) { |
|
|
464 | my ($src, $dst, $cb) = @_; |
|
|
465 | |
|
|
466 | my $pri = aioreq_pri; |
|
|
467 | my $grp = aio_group $cb; |
|
|
468 | |
|
|
469 | aioreq_pri $pri; |
|
|
470 | add $grp aio_rename $src, $dst, sub { |
|
|
471 | if ($_[0] && $! == EXDEV) { |
|
|
472 | aioreq_pri $pri; |
|
|
473 | add $grp aio_copy $src, $dst, sub { |
|
|
474 | $grp->result ($_[0]); |
|
|
475 | |
|
|
476 | if (!$_[0]) { |
|
|
477 | aioreq_pri $pri; |
|
|
478 | add $grp aio_unlink $src; |
|
|
479 | } |
|
|
480 | }; |
|
|
481 | } else { |
|
|
482 | $grp->result ($_[0]); |
|
|
483 | } |
|
|
484 | }; |
|
|
485 | |
|
|
486 | $grp |
|
|
487 | } |
437 | |
488 | |
438 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
489 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
439 | |
490 | |
440 | Scans a directory (similar to C<aio_readdir>) but additionally tries to |
491 | Scans a directory (similar to C<aio_readdir>) but additionally tries to |
441 | efficiently separate the entries of directory C<$path> into two sets of |
492 | efficiently separate the entries of directory C<$path> into two sets of |
… | |
… | |
443 | recurse into (everything else, including symlinks to directories). |
494 | recurse into (everything else, including symlinks to directories). |
444 | |
495 | |
445 | C<aio_scandir> is a composite request that creates of many sub requests_ |
496 | C<aio_scandir> is a composite request that creates of many sub requests_ |
446 | C<$maxreq> specifies the maximum number of outstanding aio requests that |
497 | C<$maxreq> specifies the maximum number of outstanding aio requests that |
447 | this function generates. If it is C<< <= 0 >>, then a suitable default |
498 | this function generates. If it is C<< <= 0 >>, then a suitable default |
448 | will be chosen (currently 6). |
499 | will be chosen (currently 4). |
449 | |
500 | |
450 | On error, the callback is called without arguments, otherwise it receives |
501 | On error, the callback is called without arguments, otherwise it receives |
451 | two array-refs with path-relative entry names. |
502 | two array-refs with path-relative entry names. |
452 | |
503 | |
453 | Example: |
504 | Example: |
… | |
… | |
494 | |
545 | |
495 | my $pri = aioreq_pri; |
546 | my $pri = aioreq_pri; |
496 | |
547 | |
497 | my $grp = aio_group $cb; |
548 | my $grp = aio_group $cb; |
498 | |
549 | |
499 | $maxreq = 6 if $maxreq <= 0; |
550 | $maxreq = 4 if $maxreq <= 0; |
500 | |
551 | |
501 | # stat once |
552 | # stat once |
502 | aioreq_pri $pri; |
553 | aioreq_pri $pri; |
503 | add $grp aio_stat $path, sub { |
554 | add $grp aio_stat $path, sub { |
504 | return $grp->result () if $_[0]; |
555 | return $grp->result () if $_[0]; |
… | |
… | |
956 | } |
1007 | } |
957 | |
1008 | |
958 | min_parallel 8; |
1009 | min_parallel 8; |
959 | |
1010 | |
960 | END { |
1011 | END { |
961 | max_parallel 0; |
1012 | min_parallel 1; |
962 | } |
1013 | flush; |
|
|
1014 | }; |
963 | |
1015 | |
964 | 1; |
1016 | 1; |
965 | |
1017 | |
966 | =head2 FORK BEHAVIOUR |
1018 | =head2 FORK BEHAVIOUR |
967 | |
1019 | |