ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.pm
(Generate patch)

Comparing IO-AIO/AIO.pm (file contents):
Revision 1.72 by root, Tue Oct 24 14:25:53 2006 UTC vs.
Revision 1.77 by root, Wed Oct 25 17:57:30 2006 UTC

134 aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink 134 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 135 aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move
136 aio_group aio_nop); 136 aio_group aio_nop);
137 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); 137 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice));
138 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush 138 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush
139 min_parallel max_parallel max_outstanding nreqs); 139 min_parallel max_parallel nreqs);
140 140
141 @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; 141 @IO::AIO::GRP::ISA = 'IO::AIO::REQ';
142 142
143 require XSLoader; 143 require XSLoader;
144 XSLoader::load ("IO::AIO", $VERSION); 144 XSLoader::load ("IO::AIO", $VERSION);
426with the filenames. 426with the filenames.
427 427
428=item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) 428=item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs)
429 429
430Scans a directory (similar to C<aio_readdir>) but additionally tries to 430Scans a directory (similar to C<aio_readdir>) but additionally tries to
431separate the entries of directory C<$path> into two sets of names, ones 431efficiently separate the entries of directory C<$path> into two sets of
432you can recurse into (directories or links to them), and ones you cannot 432names, directories you can recurse into (directories), and ones you cannot
433recurse into (everything else). 433recurse into (everything else, including symlinks to directories).
434 434
435C<aio_scandir> is a composite request that creates of many sub requests_ 435C<aio_scandir> is a composite request that creates of many sub requests_
436C<$maxreq> specifies the maximum number of outstanding aio requests that 436C<$maxreq> specifies the maximum number of outstanding aio requests that
437this function generates. If it is C<< <= 0 >>, then a suitable default 437this function generates. If it is C<< <= 0 >>, then a suitable default
438will be chosen (currently 6). 438will be chosen (currently 6).
520 map [$_, sprintf "%s%04d", (/.\./ ? "1" : "0"), length], 520 map [$_, sprintf "%s%04d", (/.\./ ? "1" : "0"), length],
521 @$entries]; 521 @$entries];
522 522
523 my (@dirs, @nondirs); 523 my (@dirs, @nondirs);
524 524
525 my ($statcb, $schedcb);
526 my $nreq = 0;
527
528 my $statgrp = add $grp aio_group; 525 my $statgrp = add $grp aio_group sub {
526 $grp->result (\@dirs, \@nondirs);
527 };
529 528
530 $schedcb = sub { 529 limit $statgrp $maxreq;
531 if (@$entries) { 530 feed $statgrp sub {
532 if ($nreq < $maxreq) { 531 return unless @$entries;
533 my $ent = pop @$entries; 532 my $entry = pop @$entries;
533
534 add $statgrp aio_stat "$path/$entry/.", sub {
535 if ($_[0] < 0) {
536 push @nondirs, $entry;
537 } else {
538 # need to check for real directory
539 add $statgrp aio_lstat "$path/$entry", sub {
540 if (-d _) {
541 push @dirs, $entry;
542
543 unless (--$ndirs) {
544 push @nondirs, @$entries;
545 feed $statgrp;
546 }
547 } else {
548 push @nondirs, $entry;
549 }
534 $nreq++; 550 }
535 add $statgrp aio_stat "$path/$ent/.", sub { $statcb->($_[0], $ent) };
536 } 551 }
537 } elsif (!$nreq) {
538 # finished
539 $statgrp->cancel;
540 undef $statcb;
541 undef $schedcb;
542 $grp->result (\@dirs, \@nondirs);
543 } 552 };
544 }; 553 };
545 $statcb = sub {
546 my ($status, $entry) = @_;
547
548 if ($status < 0) {
549 $nreq--;
550 push @nondirs, $entry;
551 &$schedcb;
552 } else {
553 # need to check for real directory
554 add $grp aio_lstat "$path/$entry", sub {
555 $nreq--;
556
557 if (-d _) {
558 push @dirs, $entry;
559
560 if (!--$ndirs) {
561 push @nondirs, @$entries;
562 $entries = [];
563 }
564 } else {
565 push @nondirs, $entry;
566 }
567
568 &$schedcb;
569 }
570 }
571 };
572
573 &$schedcb while @$entries && $nreq < $maxreq;
574 }; 554 };
575 }; 555 };
576 }; 556 };
577 557
578 $grp 558 $grp
590 570
591If this call isn't available because your OS lacks it or it couldn't be 571If this call isn't available because your OS lacks it or it couldn't be
592detected, it will be emulated by calling C<fsync> instead. 572detected, it will be emulated by calling C<fsync> instead.
593 573
594=item aio_group $callback->(...) 574=item aio_group $callback->(...)
595
596[EXPERIMENTAL]
597 575
598This is a very special aio request: Instead of doing something, it is a 576This is a very special aio request: Instead of doing something, it is a
599container for other aio requests, which is useful if you want to bundle 577container for other aio requests, which is useful if you want to bundle
600many requests into a single, composite, request with a definite callback 578many requests into a single, composite, request with a definite callback
601and the ability to cancel the whole request with its subrequests. 579and the ability to cancel the whole request with its subrequests.
704=item * They can also can also be added to other IO::AIO::GRP objects. 682=item * They can also can also be added to other IO::AIO::GRP objects.
705 683
706=item * You must not add requests to a group from within the group callback (or 684=item * You must not add requests to a group from within the group callback (or
707any later time). 685any later time).
708 686
709=item * This does not harmonise well with C<max_outstanding>, so best do
710not combine C<aio_group> with it. Groups and feeders are recommended for
711this kind of concurrency-limiting.
712
713=back 687=back
714 688
715Their lifetime, simplified, looks like this: when they are empty, they 689Their lifetime, simplified, looks like this: when they are empty, they
716will finish very quickly. If they contain only requests that are in the 690will finish very quickly. If they contain only requests that are in the
717C<done> state, they will also finish. Otherwise they will continue to 691C<done> state, they will also finish. Otherwise they will continue to
732be added, including other groups, as long as you do not create circular 706be added, including other groups, as long as you do not create circular
733dependencies. 707dependencies.
734 708
735Returns all its arguments. 709Returns all its arguments.
736 710
711=item $grp->cancel_subs
712
713Cancel all subrequests and clears any feeder, but not the group request
714itself. Useful when you queued a lot of events but got a result early.
715
737=item $grp->result (...) 716=item $grp->result (...)
738 717
739Set the result value(s) that will be passed to the group callback when all 718Set the result value(s) that will be passed to the group callback when all
740subrequests have finished. By default, no argument will be passed. 719subrequests have finished. By default, no argument will be passed.
741 720
742=item feed $grp $callback->($grp) 721=item feed $grp $callback->($grp)
743
744[VERY EXPERIMENTAL]
745 722
746Sets a feeder/generator on this group: every group can have an attached 723Sets a feeder/generator on this group: every group can have an attached
747generator that generates requests if idle. The idea behind this is that, 724generator that generates requests if idle. The idea behind this is that,
748although you could just queue as many requests as you want in a group, 725although you could just queue as many requests as you want in a group,
749this might starve other requests for a potentially long time. For 726this might starve other requests for a potentially long time. For
880 857
881Under normal circumstances you don't need to call this function. 858Under normal circumstances you don't need to call this function.
882 859
883=item $oldnreqs = IO::AIO::max_outstanding $nreqs 860=item $oldnreqs = IO::AIO::max_outstanding $nreqs
884 861
885[DEPRECATED] 862[REMOVED]
886 863
864Pre-2.x versions used max_outstanding for a crude request queue length limit.
865
866In 2.x+ you are advised to use a group and a feeder to limit
867concurrency. The max_outstanding feature ran very unstable (endless
868recursions causing segfaults, bad interaction with groups etc.) and was
869removed.
870
871I am deeply sorry, but I am still on the hunt for a good limiting interface.
872
873Original description was as follows:
874
887Sets the maximum number of outstanding requests to C<$nreqs>. If you 875Sets the maximum number of outstanding requests to C<$nreqs>. If you try
888try to queue up more than this number of requests, the caller will block until 876to queue up more than this number of requests, the caller will block until
889some requests have been handled. 877some requests have been handled.
890
891The default is very large, so normally there is no practical limit. If you
892queue up many requests in a loop it often improves speed if you set
893this to a relatively low number, such as C<100>.
894
895This function does not work well together with C<aio_group>'s, and their
896feeder interface is better suited to limiting concurrency, so do not use
897this function.
898
899Under normal circumstances you don't need to call this function.
900 878
901=back 879=back
902 880
903=cut 881=cut
904 882
936request/result processing, while the child frees the request/result queue 914request/result processing, while the child frees the request/result queue
937(so that the requests started before the fork will only be handled in the 915(so that the requests started before the fork will only be handled in the
938parent). Threads will be started on demand until the limit set in the 916parent). Threads will be started on demand until the limit set in the
939parent process has been reached again. 917parent process has been reached again.
940 918
941Temporary memory that was allocated for request processing is not
942reclaimed in the child, however. While this is possible in some cases, it
943is almost impossible in others (threads are evil you know), so you will
944have to live with it. This is around 64k buffer (for sendfile, readahead
945emulation) + the size of the directory being scanned (readdir).
946
947In short: the parent will, after a short pause, continue as if fork had 919In short: the parent will, after a short pause, continue as if fork had
948not been called, while the child will act as if IO::AIO has not been used 920not been called, while the child will act as if IO::AIO has not been used
949yet. 921yet.
950 922
951=head2 MEMORY USAGE 923=head2 MEMORY USAGE
967temporary buffers, and each thread requires a stack and other data 939temporary buffers, and each thread requires a stack and other data
968structures (usually around 16k-128k, depending on the OS). 940structures (usually around 16k-128k, depending on the OS).
969 941
970=head1 KNOWN BUGS 942=head1 KNOWN BUGS
971 943
972See FORK BEHAVIOUR, above. 944Known bugs will be fixed in the next release.
973 945
974=head1 SEE ALSO 946=head1 SEE ALSO
975 947
976L<Coro::AIO>. 948L<Coro::AIO>.
977 949

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines