… | |
… | |
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); |
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'; |
142 | |
158 | |
143 | require XSLoader; |
159 | require XSLoader; |
144 | XSLoader::load ("IO::AIO", $VERSION); |
160 | XSLoader::load ("IO::AIO", $VERSION); |
… | |
… | |
175 | environment, d) use Glib::filename_from_unicode on unicode filenames or e) |
191 | environment, d) use Glib::filename_from_unicode on unicode filenames or e) |
176 | use something else. |
192 | use something else. |
177 | |
193 | |
178 | =over 4 |
194 | =over 4 |
179 | |
195 | |
180 | =item aioreq_pri $pri |
196 | =item $prev_pri = aioreq_pri [$pri] |
181 | |
197 | |
182 | Sets the priority for the next aio request. The default priority |
198 | Returns the priority value that would be used for the next request and, if |
|
|
199 | C<$pri> is given, sets the priority for the next aio request. |
|
|
200 | |
183 | is C<0>, the minimum and maximum priorities are C<-4> and C<4>, |
201 | The default priority is C<0>, the minimum and maximum priorities are C<-4> |
184 | respectively. Requests with higher priority will be serviced first. |
202 | and C<4>, respectively. Requests with higher priority will be serviced |
|
|
203 | first. |
185 | |
204 | |
186 | The priority will be reset to C<0> after each call to one of the C<aio_> |
205 | The priority will be reset to C<0> after each call to one of the C<aio_*> |
187 | functions. |
206 | functions. |
188 | |
207 | |
189 | Example: open a file with low priority, then read something from it with |
208 | Example: open a file with low priority, then read something from it with |
190 | higher priority so the read request is serviced before other low priority |
209 | higher priority so the read request is serviced before other low priority |
191 | open requests (potentially spamming the cache): |
210 | open requests (potentially spamming the cache): |
… | |
… | |
261 | |
280 | |
262 | aio_read $fh, 7, 15, $buffer, 0, sub { |
281 | aio_read $fh, 7, 15, $buffer, 0, sub { |
263 | $_[0] > 0 or die "read error: $!"; |
282 | $_[0] > 0 or die "read error: $!"; |
264 | print "read $_[0] bytes: <$buffer>\n"; |
283 | print "read $_[0] bytes: <$buffer>\n"; |
265 | }; |
284 | }; |
266 | |
|
|
267 | =item aio_move $srcpath, $dstpath, $callback->($status) |
|
|
268 | |
|
|
269 | Try to move the I<file> (directories not supported as either source or |
|
|
270 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
|
|
271 | the C<0> (error) or C<-1> ok. |
|
|
272 | |
|
|
273 | This is a composite request that tries to rename(2) the file first. If |
|
|
274 | rename files with C<EXDEV>, it creates the destination file with mode 0200 |
|
|
275 | and copies the contents of the source file into it using C<aio_sendfile>, |
|
|
276 | followed by restoring atime, mtime, access mode and uid/gid, in that |
|
|
277 | order, and unlinking the C<$srcpath>. |
|
|
278 | |
|
|
279 | If an error occurs, the partial destination file will be unlinked, if |
|
|
280 | possible, except when setting atime, mtime, access mode and uid/gid, where |
|
|
281 | errors are being ignored. |
|
|
282 | |
|
|
283 | =cut |
|
|
284 | |
|
|
285 | sub aio_move($$$) { |
|
|
286 | my ($src, $dst, $cb) = @_; |
|
|
287 | |
|
|
288 | my $grp = aio_group $cb; |
|
|
289 | |
|
|
290 | add $grp aio_rename $src, $dst, sub { |
|
|
291 | if ($_[0] && $! == EXDEV) { |
|
|
292 | add $grp aio_open $src, O_RDONLY, 0, sub { |
|
|
293 | if (my $src_fh = $_[0]) { |
|
|
294 | my @stat = stat $src_fh; |
|
|
295 | |
|
|
296 | add $grp aio_open $dst, O_WRONLY, 0200, sub { |
|
|
297 | if (my $dst_fh = $_[0]) { |
|
|
298 | add $grp aio_sendfile $dst_fh, $src_fh, 0, $stat[7], sub { |
|
|
299 | close $src_fh; |
|
|
300 | |
|
|
301 | if ($_[0] == $stat[7]) { |
|
|
302 | utime $stat[8], $stat[9], $dst; |
|
|
303 | chmod $stat[2] & 07777, $dst_fh; |
|
|
304 | chown $stat[4], $stat[5], $dst_fh; |
|
|
305 | close $dst_fh; |
|
|
306 | |
|
|
307 | add $grp aio_unlink $src, sub { |
|
|
308 | $grp->result ($_[0]); |
|
|
309 | }; |
|
|
310 | } else { |
|
|
311 | my $errno = $!; |
|
|
312 | add $grp aio_unlink $dst, sub { |
|
|
313 | $! = $errno; |
|
|
314 | $grp->result (-1); |
|
|
315 | }; |
|
|
316 | } |
|
|
317 | }; |
|
|
318 | } else { |
|
|
319 | $grp->result (-1); |
|
|
320 | } |
|
|
321 | }, |
|
|
322 | |
|
|
323 | } else { |
|
|
324 | $grp->result (-1); |
|
|
325 | } |
|
|
326 | }; |
|
|
327 | } else { |
|
|
328 | $grp->result ($_[0]); |
|
|
329 | } |
|
|
330 | }; |
|
|
331 | |
|
|
332 | $grp |
|
|
333 | } |
|
|
334 | |
285 | |
335 | =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) |
336 | |
287 | |
337 | 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 |
338 | 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 |
… | |
… | |
394 | =item aio_unlink $pathname, $callback->($status) |
345 | =item aio_unlink $pathname, $callback->($status) |
395 | |
346 | |
396 | Asynchronously unlink (delete) a file and call the callback with the |
347 | Asynchronously unlink (delete) a file and call the callback with the |
397 | result code. |
348 | result code. |
398 | |
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 | |
399 | =item aio_link $srcpath, $dstpath, $callback->($status) |
358 | =item aio_link $srcpath, $dstpath, $callback->($status) |
400 | |
359 | |
401 | 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 |
402 | 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. |
403 | |
362 | |
… | |
… | |
422 | directory (i.e. opendir + readdir + closedir). The entries will not be |
381 | directory (i.e. opendir + readdir + closedir). The entries will not be |
423 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
382 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
424 | |
383 | |
425 | 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 |
426 | 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 | } |
427 | |
488 | |
428 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
489 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
429 | |
490 | |
430 | 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 |
431 | 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 |
… | |
… | |
433 | recurse into (everything else, including symlinks to directories). |
494 | recurse into (everything else, including symlinks to directories). |
434 | |
495 | |
435 | 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_ |
436 | C<$maxreq> specifies the maximum number of outstanding aio requests that |
497 | C<$maxreq> specifies the maximum number of outstanding aio requests that |
437 | 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 |
438 | will be chosen (currently 6). |
499 | will be chosen (currently 4). |
439 | |
500 | |
440 | On error, the callback is called without arguments, otherwise it receives |
501 | On error, the callback is called without arguments, otherwise it receives |
441 | two array-refs with path-relative entry names. |
502 | two array-refs with path-relative entry names. |
442 | |
503 | |
443 | Example: |
504 | Example: |
… | |
… | |
480 | =cut |
541 | =cut |
481 | |
542 | |
482 | sub aio_scandir($$$) { |
543 | sub aio_scandir($$$) { |
483 | my ($path, $maxreq, $cb) = @_; |
544 | my ($path, $maxreq, $cb) = @_; |
484 | |
545 | |
|
|
546 | my $pri = aioreq_pri; |
|
|
547 | |
485 | my $grp = aio_group $cb; |
548 | my $grp = aio_group $cb; |
486 | |
549 | |
487 | $maxreq = 6 if $maxreq <= 0; |
550 | $maxreq = 4 if $maxreq <= 0; |
488 | |
551 | |
489 | # stat once |
552 | # stat once |
|
|
553 | aioreq_pri $pri; |
490 | add $grp aio_stat $path, sub { |
554 | add $grp aio_stat $path, sub { |
491 | return $grp->result () if $_[0]; |
555 | return $grp->result () if $_[0]; |
492 | my $now = time; |
556 | my $now = time; |
493 | my $hash1 = join ":", (stat _)[0,1,3,7,9]; |
557 | my $hash1 = join ":", (stat _)[0,1,3,7,9]; |
494 | |
558 | |
495 | # read the directory entries |
559 | # read the directory entries |
|
|
560 | aioreq_pri $pri; |
496 | add $grp aio_readdir $path, sub { |
561 | add $grp aio_readdir $path, sub { |
497 | my $entries = shift |
562 | my $entries = shift |
498 | or return $grp->result (); |
563 | or return $grp->result (); |
499 | |
564 | |
500 | # stat the dir another time |
565 | # stat the dir another time |
|
|
566 | aioreq_pri $pri; |
501 | add $grp aio_stat $path, sub { |
567 | add $grp aio_stat $path, sub { |
502 | my $hash2 = join ":", (stat _)[0,1,3,7,9]; |
568 | my $hash2 = join ":", (stat _)[0,1,3,7,9]; |
503 | |
569 | |
504 | my $ndirs; |
570 | my $ndirs; |
505 | |
571 | |
… | |
… | |
529 | limit $statgrp $maxreq; |
595 | limit $statgrp $maxreq; |
530 | feed $statgrp sub { |
596 | feed $statgrp sub { |
531 | return unless @$entries; |
597 | return unless @$entries; |
532 | my $entry = pop @$entries; |
598 | my $entry = pop @$entries; |
533 | |
599 | |
|
|
600 | aioreq_pri $pri; |
534 | add $statgrp aio_stat "$path/$entry/.", sub { |
601 | add $statgrp aio_stat "$path/$entry/.", sub { |
535 | if ($_[0] < 0) { |
602 | if ($_[0] < 0) { |
536 | push @nondirs, $entry; |
603 | push @nondirs, $entry; |
537 | } else { |
604 | } else { |
538 | # need to check for real directory |
605 | # need to check for real directory |
|
|
606 | aioreq_pri $pri; |
539 | add $statgrp aio_lstat "$path/$entry", sub { |
607 | add $statgrp aio_lstat "$path/$entry", sub { |
540 | if (-d _) { |
608 | if (-d _) { |
541 | push @dirs, $entry; |
609 | push @dirs, $entry; |
542 | |
610 | |
543 | unless (--$ndirs) { |
611 | unless (--$ndirs) { |
… | |
… | |
714 | itself. Useful when you queued a lot of events but got a result early. |
782 | itself. Useful when you queued a lot of events but got a result early. |
715 | |
783 | |
716 | =item $grp->result (...) |
784 | =item $grp->result (...) |
717 | |
785 | |
718 | Set the result value(s) that will be passed to the group callback when all |
786 | Set the result value(s) that will be passed to the group callback when all |
719 | subrequests have finished. By default, no argument will be passed. |
787 | subrequests have finished and set thre groups errno to the current value |
|
|
788 | of errno (just like calling C<errno> without an error number). By default, |
|
|
789 | no argument will be passed and errno is zero. |
|
|
790 | |
|
|
791 | =item $grp->errno ([$errno]) |
|
|
792 | |
|
|
793 | Sets the group errno value to C<$errno>, or the current value of errno |
|
|
794 | when the argument is missing. |
|
|
795 | |
|
|
796 | Every aio request has an associated errno value that is restored when |
|
|
797 | the callback is invoked. This method lets you change this value from its |
|
|
798 | default (0). |
|
|
799 | |
|
|
800 | Calling C<result> will also set errno, so make sure you either set C<$!> |
|
|
801 | before the call to C<result>, or call c<errno> after it. |
720 | |
802 | |
721 | =item feed $grp $callback->($grp) |
803 | =item feed $grp $callback->($grp) |
722 | |
804 | |
723 | Sets a feeder/generator on this group: every group can have an attached |
805 | Sets a feeder/generator on this group: every group can have an attached |
724 | generator that generates requests if idle. The idea behind this is that, |
806 | generator that generates requests if idle. The idea behind this is that, |
… | |
… | |
816 | |
898 | |
817 | See C<nreqs> for an example. |
899 | See C<nreqs> for an example. |
818 | |
900 | |
819 | =item IO::AIO::nreqs |
901 | =item IO::AIO::nreqs |
820 | |
902 | |
821 | Returns the number of requests currently outstanding (i.e. for which their |
903 | Returns the number of requests currently in the ready, execute or pending |
822 | callback has not been invoked yet). |
904 | states (i.e. for which their callback has not been invoked yet). |
823 | |
905 | |
824 | Example: wait till there are no outstanding requests anymore: |
906 | Example: wait till there are no outstanding requests anymore: |
825 | |
907 | |
826 | IO::AIO::poll_wait, IO::AIO::poll_cb |
908 | IO::AIO::poll_wait, IO::AIO::poll_cb |
827 | while IO::AIO::nreqs; |
909 | while IO::AIO::nreqs; |
|
|
910 | |
|
|
911 | =item IO::AIO::nready |
|
|
912 | |
|
|
913 | Returns the number of requests currently in the ready state (not yet |
|
|
914 | executed). |
|
|
915 | |
|
|
916 | =item IO::AIO::npending |
|
|
917 | |
|
|
918 | Returns the number of requests currently in the pending state (executed, |
|
|
919 | but not yet processed by poll_cb). |
828 | |
920 | |
829 | =item IO::AIO::flush |
921 | =item IO::AIO::flush |
830 | |
922 | |
831 | Wait till all outstanding AIO requests have been handled. |
923 | Wait till all outstanding AIO requests have been handled. |
832 | |
924 | |
… | |
… | |
915 | } |
1007 | } |
916 | |
1008 | |
917 | min_parallel 8; |
1009 | min_parallel 8; |
918 | |
1010 | |
919 | END { |
1011 | END { |
920 | max_parallel 0; |
1012 | min_parallel 1; |
921 | } |
1013 | flush; |
|
|
1014 | }; |
922 | |
1015 | |
923 | 1; |
1016 | 1; |
924 | |
1017 | |
925 | =head2 FORK BEHAVIOUR |
1018 | =head2 FORK BEHAVIOUR |
926 | |
1019 | |