… | |
… | |
2 | IO::AIO - Asynchronous Input/Output |
2 | IO::AIO - Asynchronous Input/Output |
3 | |
3 | |
4 | SYNOPSIS |
4 | SYNOPSIS |
5 | use IO::AIO; |
5 | use IO::AIO; |
6 | |
6 | |
7 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
7 | aio_open "/etc/passwd", IO::AIO::O_RDONLY, 0, sub { |
8 | my $fh = shift |
8 | my $fh = shift |
9 | or die "/etc/passwd: $!"; |
9 | or die "/etc/passwd: $!"; |
10 | ... |
10 | ... |
11 | }; |
11 | }; |
12 | |
12 | |
… | |
… | |
72 | |
72 | |
73 | # register the IO::AIO callback with EV |
73 | # register the IO::AIO callback with EV |
74 | my $aio_w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
74 | my $aio_w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
75 | |
75 | |
76 | # queue the request to open /etc/passwd |
76 | # queue the request to open /etc/passwd |
77 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
77 | aio_open "/etc/passwd", IO::AIO::O_RDONLY, 0, sub { |
78 | my $fh = shift |
78 | my $fh = shift |
79 | or die "error while opening: $!"; |
79 | or die "error while opening: $!"; |
80 | |
80 | |
81 | # stat'ing filehandles is generally non-blocking |
81 | # stat'ing filehandles is generally non-blocking |
82 | my $size = -s $fh; |
82 | my $size = -s $fh; |
… | |
… | |
168 | aio_unlink $pathname, $callback->($status) |
168 | aio_unlink $pathname, $callback->($status) |
169 | aio_mknod $path, $mode, $dev, $callback->($status) |
169 | aio_mknod $path, $mode, $dev, $callback->($status) |
170 | aio_link $srcpath, $dstpath, $callback->($status) |
170 | aio_link $srcpath, $dstpath, $callback->($status) |
171 | aio_symlink $srcpath, $dstpath, $callback->($status) |
171 | aio_symlink $srcpath, $dstpath, $callback->($status) |
172 | aio_readlink $path, $callback->($link) |
172 | aio_readlink $path, $callback->($link) |
|
|
173 | aio_realpath $path, $callback->($link) |
173 | aio_rename $srcpath, $dstpath, $callback->($status) |
174 | aio_rename $srcpath, $dstpath, $callback->($status) |
174 | aio_mkdir $pathname, $mode, $callback->($status) |
175 | aio_mkdir $pathname, $mode, $callback->($status) |
175 | aio_rmdir $pathname, $callback->($status) |
176 | aio_rmdir $pathname, $callback->($status) |
176 | aio_readdir $pathname, $callback->($entries) |
177 | aio_readdir $pathname, $callback->($entries) |
177 | aio_readdirx $pathname, $flags, $callback->($entries, $flags) |
178 | aio_readdirx $pathname, $flags, $callback->($entries, $flags) |
… | |
… | |
187 | aio_fdatasync $fh, $callback->($status) |
188 | aio_fdatasync $fh, $callback->($status) |
188 | aio_sync_file_range $fh, $offset, $nbytes, $flags, $callback->($status) |
189 | aio_sync_file_range $fh, $offset, $nbytes, $flags, $callback->($status) |
189 | aio_pathsync $path, $callback->($status) |
190 | aio_pathsync $path, $callback->($status) |
190 | aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
191 | aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
191 | aio_mtouch $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
192 | aio_mtouch $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
|
|
193 | aio_mlock $scalar, $offset = 0, $length = undef, $callback->($status) |
|
|
194 | aio_mlockall $flags, $callback->($status) |
192 | aio_group $callback->(...) |
195 | aio_group $callback->(...) |
193 | aio_nop $callback->() |
196 | aio_nop $callback->() |
194 | |
197 | |
195 | $prev_pri = aioreq_pri [$pri] |
198 | $prev_pri = aioreq_pri [$pri] |
196 | aioreq_nice $pri_adjust |
199 | aioreq_nice $pri_adjust |
… | |
… | |
202 | IO::AIO::max_poll_reqs $nreqs |
205 | IO::AIO::max_poll_reqs $nreqs |
203 | IO::AIO::max_poll_time $seconds |
206 | IO::AIO::max_poll_time $seconds |
204 | IO::AIO::min_parallel $nthreads |
207 | IO::AIO::min_parallel $nthreads |
205 | IO::AIO::max_parallel $nthreads |
208 | IO::AIO::max_parallel $nthreads |
206 | IO::AIO::max_idle $nthreads |
209 | IO::AIO::max_idle $nthreads |
|
|
210 | IO::AIO::idle_timeout $seconds |
207 | IO::AIO::max_outstanding $maxreqs |
211 | IO::AIO::max_outstanding $maxreqs |
208 | IO::AIO::nreqs |
212 | IO::AIO::nreqs |
209 | IO::AIO::nready |
213 | IO::AIO::nready |
210 | IO::AIO::npending |
214 | IO::AIO::npending |
211 | |
215 | |
212 | IO::AIO::sendfile $ofh, $ifh, $offset, $count |
216 | IO::AIO::sendfile $ofh, $ifh, $offset, $count |
213 | IO::AIO::fadvise $fh, $offset, $len, $advice |
217 | IO::AIO::fadvise $fh, $offset, $len, $advice |
214 | IO::AIO::mlockall $flags |
218 | IO::AIO::madvise $scalar, $offset, $length, $advice |
|
|
219 | IO::AIO::mprotect $scalar, $offset, $length, $protect |
|
|
220 | IO::AIO::munlock $scalar, $offset = 0, $length = undef |
215 | IO::AIO::munlockall |
221 | IO::AIO::munlockall |
216 | |
222 | |
217 | AIO REQUEST FUNCTIONS |
223 | AIO REQUEST FUNCTIONS |
218 | All the "aio_*" calls are more or less thin wrappers around the syscall |
224 | All the "aio_*" calls are more or less thin wrappers around the syscall |
219 | with the same name (sans "aio_"). The arguments are similar or |
225 | with the same name (sans "aio_"). The arguments are similar or |
… | |
… | |
292 | will be modified by the umask in effect then the request is being |
298 | will be modified by the umask in effect then the request is being |
293 | executed, so better never change the umask. |
299 | executed, so better never change the umask. |
294 | |
300 | |
295 | Example: |
301 | Example: |
296 | |
302 | |
297 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
303 | aio_open "/etc/passwd", IO::AIO::O_RDONLY, 0, sub { |
298 | if ($_[0]) { |
304 | if ($_[0]) { |
299 | print "open successful, fh is $_[0]\n"; |
305 | print "open successful, fh is $_[0]\n"; |
300 | ... |
306 | ... |
301 | } else { |
307 | } else { |
302 | die "open failed: $!\n"; |
308 | die "open failed: $!\n"; |
303 | } |
309 | } |
304 | }; |
310 | }; |
305 | |
311 | |
|
|
312 | In addition to all the common open modes/flags ("O_RDONLY", |
|
|
313 | "O_WRONLY", "O_RDWR", "O_CREAT", "O_TRUNC", "O_EXCL" and |
|
|
314 | "O_APPEND"), the following POSIX and non-POSIX constants are |
|
|
315 | available (missing ones on your system are, as usual, 0): |
|
|
316 | |
|
|
317 | "O_ASYNC", "O_DIRECT", "O_NOATIME", "O_CLOEXEC", "O_NOCTTY", |
|
|
318 | "O_NOFOLLOW", "O_NONBLOCK", "O_EXEC", "O_SEARCH", "O_DIRECTORY", |
|
|
319 | "O_DSYNC", "O_RSYNC", "O_SYNC" and "O_TTY_INIT". |
|
|
320 | |
306 | aio_close $fh, $callback->($status) |
321 | aio_close $fh, $callback->($status) |
307 | Asynchronously close a file and call the callback with the result |
322 | Asynchronously close a file and call the callback with the result |
308 | code. |
323 | code. |
309 | |
324 | |
310 | Unfortunately, you can't do this to perl. Perl *insists* very |
325 | Unfortunately, you can't do this to perl. Perl *insists* very |
… | |
… | |
353 | aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
368 | aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
354 | Tries to copy $length bytes from $in_fh to $out_fh. It starts |
369 | Tries to copy $length bytes from $in_fh to $out_fh. It starts |
355 | reading at byte offset $in_offset, and starts writing at the current |
370 | reading at byte offset $in_offset, and starts writing at the current |
356 | file offset of $out_fh. Because of that, it is not safe to issue |
371 | file offset of $out_fh. Because of that, it is not safe to issue |
357 | more than one "aio_sendfile" per $out_fh, as they will interfere |
372 | more than one "aio_sendfile" per $out_fh, as they will interfere |
358 | with each other. |
373 | with each other. The same $in_fh works fine though, as this function |
|
|
374 | does not move or use the file offset of $in_fh. |
359 | |
375 | |
|
|
376 | Please note that "aio_sendfile" can read more bytes from $in_fh than |
|
|
377 | are written, and there is no way to find out how many more bytes |
|
|
378 | have been read from "aio_sendfile" alone, as "aio_sendfile" only |
|
|
379 | provides the number of bytes written to $out_fh. Only if the result |
|
|
380 | value equals $length one can assume that $length bytes have been |
|
|
381 | read. |
|
|
382 | |
|
|
383 | Unlike with other "aio_" functions, it makes a lot of sense to use |
|
|
384 | "aio_sendfile" on non-blocking sockets, as long as one end |
|
|
385 | (typically the $in_fh) is a file - the file I/O will then be |
|
|
386 | asynchronous, while the socket I/O will be non-blocking. Note, |
|
|
387 | however, that you can run into a trap where "aio_sendfile" reads |
|
|
388 | some data with readahead, then fails to write all data, and when the |
|
|
389 | socket is ready the next time, the data in the cache is already |
|
|
390 | lost, forcing "aio_sendfile" to again hit the disk. Explicit |
|
|
391 | "aio_read" + "aio_write" let's you better control resource usage. |
|
|
392 | |
360 | This call tries to make use of a native "sendfile" syscall to |
393 | This call tries to make use of a native "sendfile"-like syscall to |
361 | provide zero-copy operation. For this to work, $out_fh should refer |
394 | provide zero-copy operation. For this to work, $out_fh should refer |
362 | to a socket, and $in_fh should refer to an mmap'able file. |
395 | to a socket, and $in_fh should refer to an mmap'able file. |
363 | |
396 | |
364 | If a native sendfile cannot be found or it fails with "ENOSYS", |
397 | If a native sendfile cannot be found or it fails with "ENOSYS", |
365 | "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK", |
398 | "EINVAL", "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or |
366 | it will be emulated, so you can call "aio_sendfile" on any type of |
399 | "ENOTSOCK", it will be emulated, so you can call "aio_sendfile" on |
367 | filehandle regardless of the limitations of the operating system. |
400 | any type of filehandle regardless of the limitations of the |
|
|
401 | operating system. |
368 | |
402 | |
369 | Please note, however, that "aio_sendfile" can read more bytes from |
403 | As native sendfile syscalls (as practically any non-POSIX interface |
370 | $in_fh than are written, and there is no way to find out how many |
404 | hacked together in a hurry to improve benchmark numbers) tend to be |
371 | bytes have been read from "aio_sendfile" alone, as "aio_sendfile" |
405 | rather buggy on many systems, this implementation tries to work |
372 | only provides the number of bytes written to $out_fh. Only if the |
406 | around some known bugs in Linux and FreeBSD kernels (probably |
373 | result value equals $length one can assume that $length bytes have |
407 | others, too), but that might fail, so you really really should check |
374 | been read. |
408 | the return value of "aio_sendfile" - fewre bytes than expected might |
|
|
409 | have been transferred. |
375 | |
410 | |
376 | aio_readahead $fh,$offset,$length, $callback->($retval) |
411 | aio_readahead $fh,$offset,$length, $callback->($retval) |
377 | "aio_readahead" populates the page cache with data from a file so |
412 | "aio_readahead" populates the page cache with data from a file so |
378 | that subsequent reads from that file will not block on disk I/O. The |
413 | that subsequent reads from that file will not block on disk I/O. The |
379 | $offset argument specifies the starting point from which data is to |
414 | $offset argument specifies the starting point from which data is to |
… | |
… | |
399 | |
434 | |
400 | Currently, the stats are always 64-bit-stats, i.e. instead of |
435 | Currently, the stats are always 64-bit-stats, i.e. instead of |
401 | returning an error when stat'ing a large file, the results will be |
436 | returning an error when stat'ing a large file, the results will be |
402 | silently truncated unless perl itself is compiled with large file |
437 | silently truncated unless perl itself is compiled with large file |
403 | support. |
438 | support. |
|
|
439 | |
|
|
440 | To help interpret the mode and dev/rdev stat values, IO::AIO offers |
|
|
441 | the following constants and functions (if not implemented, the |
|
|
442 | constants will be 0 and the functions will either "croak" or fall |
|
|
443 | back on traditional behaviour). |
|
|
444 | |
|
|
445 | "S_IFMT", "S_IFIFO", "S_IFCHR", "S_IFBLK", "S_IFLNK", "S_IFREG", |
|
|
446 | "S_IFDIR", "S_IFWHT", "S_IFSOCK", "IO::AIO::major $dev_t", |
|
|
447 | "IO::AIO::minor $dev_t", "IO::AIO::makedev $major, $minor". |
404 | |
448 | |
405 | Example: Print the length of /etc/passwd: |
449 | Example: Print the length of /etc/passwd: |
406 | |
450 | |
407 | aio_stat "/etc/passwd", sub { |
451 | aio_stat "/etc/passwd", sub { |
408 | $_[0] and die "stat failed: $!"; |
452 | $_[0] and die "stat failed: $!"; |
… | |
… | |
497 | |
541 | |
498 | The only (POSIX-) portable way of calling this function is: |
542 | The only (POSIX-) portable way of calling this function is: |
499 | |
543 | |
500 | aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ... |
544 | aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ... |
501 | |
545 | |
|
|
546 | See "aio_stat" for info about some potentially helpful extra |
|
|
547 | constants and functions. |
|
|
548 | |
502 | aio_link $srcpath, $dstpath, $callback->($status) |
549 | aio_link $srcpath, $dstpath, $callback->($status) |
503 | Asynchronously create a new link to the existing object at $srcpath |
550 | Asynchronously create a new link to the existing object at $srcpath |
504 | at the path $dstpath and call the callback with the result code. |
551 | at the path $dstpath and call the callback with the result code. |
505 | |
552 | |
506 | aio_symlink $srcpath, $dstpath, $callback->($status) |
553 | aio_symlink $srcpath, $dstpath, $callback->($status) |
… | |
… | |
511 | aio_readlink $path, $callback->($link) |
558 | aio_readlink $path, $callback->($link) |
512 | Asynchronously read the symlink specified by $path and pass it to |
559 | Asynchronously read the symlink specified by $path and pass it to |
513 | the callback. If an error occurs, nothing or undef gets passed to |
560 | the callback. If an error occurs, nothing or undef gets passed to |
514 | the callback. |
561 | the callback. |
515 | |
562 | |
|
|
563 | aio_realpath $path, $callback->($path) |
|
|
564 | Asynchronously make the path absolute and resolve any symlinks in |
|
|
565 | $path. The resulting path only consists of directories (Same as |
|
|
566 | Cwd::realpath). |
|
|
567 | |
|
|
568 | This request can be used to get the absolute path of the current |
|
|
569 | working directory by passing it a path of . (a single dot). |
|
|
570 | |
516 | aio_rename $srcpath, $dstpath, $callback->($status) |
571 | aio_rename $srcpath, $dstpath, $callback->($status) |
517 | Asynchronously rename the object at $srcpath to $dstpath, just as |
572 | Asynchronously rename the object at $srcpath to $dstpath, just as |
518 | rename(2) and call the callback with the result code. |
573 | rename(2) and call the callback with the result code. |
519 | |
574 | |
520 | aio_mkdir $pathname, $mode, $callback->($status) |
575 | aio_mkdir $pathname, $mode, $callback->($status) |
… | |
… | |
542 | The flags are a combination of the following constants, ORed |
597 | The flags are a combination of the following constants, ORed |
543 | together (the flags will also be passed to the callback, possibly |
598 | together (the flags will also be passed to the callback, possibly |
544 | modified): |
599 | modified): |
545 | |
600 | |
546 | IO::AIO::READDIR_DENTS |
601 | IO::AIO::READDIR_DENTS |
547 | When this flag is off, then the callback gets an arrayref with |
602 | When this flag is off, then the callback gets an arrayref |
548 | of names only (as with "aio_readdir"), otherwise it gets an |
603 | consisting of names only (as with "aio_readdir"), otherwise it |
549 | arrayref with "[$name, $type, $inode]" arrayrefs, each |
604 | gets an arrayref with "[$name, $type, $inode]" arrayrefs, each |
550 | describing a single directory entry in more detail. |
605 | describing a single directory entry in more detail. |
551 | |
606 | |
552 | $name is the name of the entry. |
607 | $name is the name of the entry. |
553 | |
608 | |
554 | $type is one of the "IO::AIO::DT_xxx" constants: |
609 | $type is one of the "IO::AIO::DT_xxx" constants: |
… | |
… | |
567 | unspecified content on systems that do not deliver the inode |
622 | unspecified content on systems that do not deliver the inode |
568 | information. |
623 | information. |
569 | |
624 | |
570 | IO::AIO::READDIR_DIRS_FIRST |
625 | IO::AIO::READDIR_DIRS_FIRST |
571 | When this flag is set, then the names will be returned in an |
626 | When this flag is set, then the names will be returned in an |
572 | order where likely directories come first. This is useful when |
627 | order where likely directories come first, in optimal stat |
573 | you need to quickly find directories, or you want to find all |
628 | order. This is useful when you need to quickly find directories, |
574 | directories while avoiding to stat() each entry. |
629 | or you want to find all directories while avoiding to stat() |
|
|
630 | each entry. |
575 | |
631 | |
576 | If the system returns type information in readdir, then this is |
632 | If the system returns type information in readdir, then this is |
577 | used to find directories directly. Otherwise, likely directories |
633 | used to find directories directly. Otherwise, likely directories |
578 | are files beginning with ".", or otherwise files with no dots, |
634 | are names beginning with ".", or otherwise names with no dots, |
579 | of which files with short names are tried first. |
635 | of which names with short names are tried first. |
580 | |
636 | |
581 | IO::AIO::READDIR_STAT_ORDER |
637 | IO::AIO::READDIR_STAT_ORDER |
582 | When this flag is set, then the names will be returned in an |
638 | When this flag is set, then the names will be returned in an |
583 | order suitable for stat()'ing each one. That is, when you plan |
639 | order suitable for stat()'ing each one. That is, when you plan |
584 | to stat() all files in the given directory, then the returned |
640 | to stat() all files in the given directory, then the returned |
… | |
… | |
751 | "aio_msync", above, except for flags, which must be either 0 (which |
807 | "aio_msync", above, except for flags, which must be either 0 (which |
752 | reads all pages and ensures they are instantiated) or |
808 | reads all pages and ensures they are instantiated) or |
753 | "IO::AIO::MT_MODIFY", which modifies the memory page s(by reading |
809 | "IO::AIO::MT_MODIFY", which modifies the memory page s(by reading |
754 | and writing an octet from it, which dirties the page). |
810 | and writing an octet from it, which dirties the page). |
755 | |
811 | |
|
|
812 | aio_mlock $scalar, $offset = 0, $length = undef, $callback->($status) |
|
|
813 | This is a rather advanced IO::AIO call, which works best on |
|
|
814 | mmap(2)ed scalars. |
|
|
815 | |
|
|
816 | It reads in all the pages of the underlying storage into memory (if |
|
|
817 | any) and locks them, so they are not getting swapped/paged out or |
|
|
818 | removed. |
|
|
819 | |
|
|
820 | If $length is undefined, then the scalar will be locked till the |
|
|
821 | end. |
|
|
822 | |
|
|
823 | On systems that do not implement "mlock", this function returns -1 |
|
|
824 | and sets errno to "ENOSYS". |
|
|
825 | |
|
|
826 | Note that the corresponding "munlock" is synchronous and is |
|
|
827 | documented under "MISCELLANEOUS FUNCTIONS". |
|
|
828 | |
|
|
829 | Example: open a file, mmap and mlock it - both will be undone when |
|
|
830 | $data gets destroyed. |
|
|
831 | |
|
|
832 | open my $fh, "<", $path or die "$path: $!"; |
|
|
833 | my $data; |
|
|
834 | IO::AIO::mmap $data, -s $fh, IO::AIO::PROT_READ, IO::AIO::MAP_SHARED, $fh; |
|
|
835 | aio_mlock $data; # mlock in background |
|
|
836 | |
|
|
837 | aio_mlockall $flags, $callback->($status) |
|
|
838 | Calls the "mlockall" function with the given $flags (a combination |
|
|
839 | of "IO::AIO::MCL_CURRENT" and "IO::AIO::MCL_FUTURE"). |
|
|
840 | |
|
|
841 | On systems that do not implement "mlockall", this function returns |
|
|
842 | -1 and sets errno to "ENOSYS". |
|
|
843 | |
|
|
844 | Note that the corresponding "munlockall" is synchronous and is |
|
|
845 | documented under "MISCELLANEOUS FUNCTIONS". |
|
|
846 | |
|
|
847 | Example: asynchronously lock all current and future pages into |
|
|
848 | memory. |
|
|
849 | |
|
|
850 | aio_mlockall IO::AIO::MCL_FUTURE; |
|
|
851 | |
756 | aio_group $callback->(...) |
852 | aio_group $callback->(...) |
757 | This is a very special aio request: Instead of doing something, it |
853 | This is a very special aio request: Instead of doing something, it |
758 | is a container for other aio requests, which is useful if you want |
854 | is a container for other aio requests, which is useful if you want |
759 | to bundle many requests into a single, composite, request with a |
855 | to bundle many requests into a single, composite, request with a |
760 | definite callback and the ability to cancel the whole request with |
856 | definite callback and the ability to cancel the whole request with |
… | |
… | |
955 | |
1051 | |
956 | See "poll_cb" for an example. |
1052 | See "poll_cb" for an example. |
957 | |
1053 | |
958 | IO::AIO::poll_cb |
1054 | IO::AIO::poll_cb |
959 | Process some outstanding events on the result pipe. You have to call |
1055 | Process some outstanding events on the result pipe. You have to call |
960 | this regularly. Returns 0 if all events could be processed, or -1 if |
1056 | this regularly. Returns 0 if all events could be processed (or there |
961 | it returned earlier for whatever reason. Returns immediately when no |
1057 | were no events to process), or -1 if it returned earlier for |
962 | events are outstanding. The amount of events processed depends on |
1058 | whatever reason. Returns immediately when no events are outstanding. |
963 | the settings of "IO::AIO::max_poll_req" and |
1059 | The amount of events processed depends on the settings of |
964 | "IO::AIO::max_poll_time". |
1060 | "IO::AIO::max_poll_req" and "IO::AIO::max_poll_time". |
965 | |
1061 | |
966 | If not all requests were processed for whatever reason, the |
1062 | If not all requests were processed for whatever reason, the |
967 | filehandle will still be ready when "poll_cb" returns, so normally |
1063 | filehandle will still be ready when "poll_cb" returns, so normally |
968 | you don't have to do anything special to have it called later. |
1064 | you don't have to do anything special to have it called later. |
|
|
1065 | |
|
|
1066 | Apart from calling "IO::AIO::poll_cb" when the event filehandle |
|
|
1067 | becomes ready, it can be beneficial to call this function from loops |
|
|
1068 | which submit a lot of requests, to make sure the results get |
|
|
1069 | processed when they become available and not just when the loop is |
|
|
1070 | finished and the event loop takes over again. This function returns |
|
|
1071 | very fast when there are no outstanding requests. |
969 | |
1072 | |
970 | Example: Install an Event watcher that automatically calls |
1073 | Example: Install an Event watcher that automatically calls |
971 | IO::AIO::poll_cb with high priority (more examples can be found in |
1074 | IO::AIO::poll_cb with high priority (more examples can be found in |
972 | the SYNOPSIS section, at the top of this document): |
1075 | the SYNOPSIS section, at the top of this document): |
973 | |
1076 | |
… | |
… | |
1067 | |
1170 | |
1068 | Under normal circumstances you don't need to call this function. |
1171 | Under normal circumstances you don't need to call this function. |
1069 | |
1172 | |
1070 | IO::AIO::max_idle $nthreads |
1173 | IO::AIO::max_idle $nthreads |
1071 | Limit the number of threads (default: 4) that are allowed to idle |
1174 | Limit the number of threads (default: 4) that are allowed to idle |
1072 | (i.e., threads that did not get a request to process within 10 |
1175 | (i.e., threads that did not get a request to process within the idle |
1073 | seconds). That means if a thread becomes idle while $nthreads other |
1176 | timeout (default: 10 seconds). That means if a thread becomes idle |
1074 | threads are also idle, it will free its resources and exit. |
1177 | while $nthreads other threads are also idle, it will free its |
|
|
1178 | resources and exit. |
1075 | |
1179 | |
1076 | This is useful when you allow a large number of threads (e.g. 100 or |
1180 | This is useful when you allow a large number of threads (e.g. 100 or |
1077 | 1000) to allow for extremely high load situations, but want to free |
1181 | 1000) to allow for extremely high load situations, but want to free |
1078 | resources under normal circumstances (1000 threads can easily |
1182 | resources under normal circumstances (1000 threads can easily |
1079 | consume 30MB of RAM). |
1183 | consume 30MB of RAM). |
1080 | |
1184 | |
1081 | The default is probably ok in most situations, especially if thread |
1185 | The default is probably ok in most situations, especially if thread |
1082 | creation is fast. If thread creation is very slow on your system you |
1186 | creation is fast. If thread creation is very slow on your system you |
1083 | might want to use larger values. |
1187 | might want to use larger values. |
1084 | |
1188 | |
|
|
1189 | IO::AIO::idle_timeout $seconds |
|
|
1190 | Sets the minimum idle timeout (default 10) after which worker |
|
|
1191 | threads are allowed to exit. SEe "IO::AIO::max_idle". |
|
|
1192 | |
1085 | IO::AIO::max_outstanding $maxreqs |
1193 | IO::AIO::max_outstanding $maxreqs |
|
|
1194 | Sets the maximum number of outstanding requests to $nreqs. If you do |
|
|
1195 | queue up more than this number of requests, the next call to |
|
|
1196 | "IO::AIO::poll_cb" (and other functions calling "poll_cb", such as |
|
|
1197 | "IO::AIO::flush" or "IO::AIO::poll") will block until the limit is |
|
|
1198 | no longer exceeded. |
|
|
1199 | |
|
|
1200 | In other words, this setting does not enforce a queue limit, but can |
|
|
1201 | be used to make poll functions block if the limit is exceeded. |
|
|
1202 | |
1086 | This is a very bad function to use in interactive programs because |
1203 | This is a very bad function to use in interactive programs because |
1087 | it blocks, and a bad way to reduce concurrency because it is |
1204 | it blocks, and a bad way to reduce concurrency because it is |
1088 | inexact: Better use an "aio_group" together with a feed callback. |
1205 | inexact: Better use an "aio_group" together with a feed callback. |
1089 | |
1206 | |
1090 | Sets the maximum number of outstanding requests to $nreqs. If you do |
1207 | It's main use is in scripts without an event loop - when you want to |
1091 | queue up more than this number of requests, the next call to the |
1208 | stat a lot of files, you can write somehting like this: |
1092 | "poll_cb" (and "poll_some" and other functions calling "poll_cb") |
|
|
1093 | function will block until the limit is no longer exceeded. |
|
|
1094 | |
1209 | |
1095 | The default value is very large, so there is no practical limit on |
1210 | IO::AIO::max_outstanding 32; |
|
|
1211 | |
|
|
1212 | for my $path (...) { |
|
|
1213 | aio_stat $path , ...; |
|
|
1214 | IO::AIO::poll_cb; |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | IO::AIO::flush; |
|
|
1218 | |
|
|
1219 | The call to "poll_cb" inside the loop will normally return |
|
|
1220 | instantly, but as soon as more thna 32 reqeusts are in-flight, it |
|
|
1221 | will block until some requests have been handled. This keeps the |
|
|
1222 | loop from pushing a large number of "aio_stat" requests onto the |
|
|
1223 | queue. |
|
|
1224 | |
|
|
1225 | The default value for "max_outstanding" is very large, so there is |
1096 | the number of outstanding requests. |
1226 | no practical limit on the number of outstanding requests. |
1097 | |
|
|
1098 | You can still queue as many requests as you want. Therefore, |
|
|
1099 | "max_outstanding" is mainly useful in simple scripts (with low |
|
|
1100 | values) or as a stop gap to shield against fatal memory overflow |
|
|
1101 | (with large values). |
|
|
1102 | |
1227 | |
1103 | STATISTICAL INFORMATION |
1228 | STATISTICAL INFORMATION |
1104 | IO::AIO::nreqs |
1229 | IO::AIO::nreqs |
1105 | Returns the number of requests currently in the ready, execute or |
1230 | Returns the number of requests currently in the ready, execute or |
1106 | pending states (i.e. for which their callback has not been invoked |
1231 | pending states (i.e. for which their callback has not been invoked |
… | |
… | |
1130 | set to non-blocking operations). |
1255 | set to non-blocking operations). |
1131 | |
1256 | |
1132 | Returns the number of bytes copied, or -1 on error. |
1257 | Returns the number of bytes copied, or -1 on error. |
1133 | |
1258 | |
1134 | IO::AIO::fadvise $fh, $offset, $len, $advice |
1259 | IO::AIO::fadvise $fh, $offset, $len, $advice |
1135 | Simply calls the "posix_fadvise" function (see it's manpage for |
1260 | Simply calls the "posix_fadvise" function (see its manpage for |
1136 | details). The following advice constants are avaiable: |
1261 | details). The following advice constants are avaiable: |
1137 | "IO::AIO::FADV_NORMAL", "IO::AIO::FADV_SEQUENTIAL", |
1262 | "IO::AIO::FADV_NORMAL", "IO::AIO::FADV_SEQUENTIAL", |
1138 | "IO::AIO::FADV_RANDOM", "IO::AIO::FADV_NOREUSE", |
1263 | "IO::AIO::FADV_RANDOM", "IO::AIO::FADV_NOREUSE", |
1139 | "IO::AIO::FADV_WILLNEED", "IO::AIO::FADV_DONTNEED". |
1264 | "IO::AIO::FADV_WILLNEED", "IO::AIO::FADV_DONTNEED". |
1140 | |
1265 | |
1141 | On systems that do not implement "posix_fadvise", this function |
1266 | On systems that do not implement "posix_fadvise", this function |
1142 | returns ENOSYS, otherwise the return value of "posix_fadvise". |
1267 | returns ENOSYS, otherwise the return value of "posix_fadvise". |
|
|
1268 | |
|
|
1269 | IO::AIO::madvise $scalar, $offset, $len, $advice |
|
|
1270 | Simply calls the "posix_madvise" function (see its manpage for |
|
|
1271 | details). The following advice constants are avaiable: |
|
|
1272 | "IO::AIO::MADV_NORMAL", "IO::AIO::MADV_SEQUENTIAL", |
|
|
1273 | "IO::AIO::MADV_RANDOM", "IO::AIO::MADV_WILLNEED", |
|
|
1274 | "IO::AIO::MADV_DONTNEED". |
|
|
1275 | |
|
|
1276 | On systems that do not implement "posix_madvise", this function |
|
|
1277 | returns ENOSYS, otherwise the return value of "posix_madvise". |
|
|
1278 | |
|
|
1279 | IO::AIO::mprotect $scalar, $offset, $len, $protect |
|
|
1280 | Simply calls the "mprotect" function on the preferably AIO::mmap'ed |
|
|
1281 | $scalar (see its manpage for details). The following protect |
|
|
1282 | constants are avaiable: "IO::AIO::PROT_NONE", "IO::AIO::PROT_READ", |
|
|
1283 | "IO::AIO::PROT_WRITE", "IO::AIO::PROT_EXEC". |
|
|
1284 | |
|
|
1285 | On systems that do not implement "mprotect", this function returns |
|
|
1286 | ENOSYS, otherwise the return value of "mprotect". |
1143 | |
1287 | |
1144 | IO::AIO::mmap $scalar, $length, $prot, $flags, $fh[, $offset] |
1288 | IO::AIO::mmap $scalar, $length, $prot, $flags, $fh[, $offset] |
1145 | Memory-maps a file (or anonymous memory range) and attaches it to |
1289 | Memory-maps a file (or anonymous memory range) and attaches it to |
1146 | the given $scalar, which will act like a string scalar. |
1290 | the given $scalar, which will act like a string scalar. |
1147 | |
1291 | |
… | |
… | |
1192 | my $fast_md5 = md5 $data; |
1336 | my $fast_md5 = md5 $data; |
1193 | |
1337 | |
1194 | IO::AIO::munmap $scalar |
1338 | IO::AIO::munmap $scalar |
1195 | Removes a previous mmap and undefines the $scalar. |
1339 | Removes a previous mmap and undefines the $scalar. |
1196 | |
1340 | |
1197 | IO::AIO::mlockall $flags |
1341 | IO::AIO::munlock $scalar, $offset = 0, $length = undef |
1198 | Calls the "mlockall" function with the given $flags (a combination |
1342 | Calls the "munlock" function, undoing the effects of a previous |
1199 | of "IO::AIO::MCL_CURRENT" and "IO::AIO::MCL__FUTURE"). |
1343 | "aio_mlock" call (see its description for details). |
1200 | |
|
|
1201 | On systems that do not implement "mlockall", this function returns |
|
|
1202 | ENOSYS, otherwise the return value of "mlockall". |
|
|
1203 | |
1344 | |
1204 | IO::AIO::munlockall |
1345 | IO::AIO::munlockall |
1205 | Calls the "munlockall" function. |
1346 | Calls the "munlockall" function. |
1206 | |
1347 | |
1207 | On systems that do not implement "munlockall", this function returns |
1348 | On systems that do not implement "munlockall", this function returns |
… | |
… | |
1236 | # Danga::Socket integration |
1377 | # Danga::Socket integration |
1237 | Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => |
1378 | Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => |
1238 | \&IO::AIO::poll_cb); |
1379 | \&IO::AIO::poll_cb); |
1239 | |
1380 | |
1240 | FORK BEHAVIOUR |
1381 | FORK BEHAVIOUR |
1241 | This module should do "the right thing" when the process using it forks: |
1382 | Usage of pthreads in a program changes the semantics of fork |
|
|
1383 | considerably. Specifically, only async-safe functions can be called |
|
|
1384 | after fork. Perl doesn't know about this, so in general, you cannot call |
|
|
1385 | fork with defined behaviour in perl if pthreads are involved. IO::AIO |
|
|
1386 | uses pthreads, so this applies, but many other extensions and (for |
|
|
1387 | inexplicable reasons) perl itself often is linked against pthreads, so |
|
|
1388 | this limitation applies to quite a lot of perls. |
1242 | |
1389 | |
1243 | Before the fork, IO::AIO enters a quiescent state where no requests can |
1390 | This module no longer tries to fight your OS, or POSIX. That means |
1244 | be added in other threads and no results will be processed. After the |
1391 | IO::AIO only works in the process that loaded it. Forking is fully |
1245 | fork the parent simply leaves the quiescent state and continues |
1392 | supported, but using IO::AIO in the child is not. |
1246 | request/result processing, while the child frees the request/result |
|
|
1247 | queue (so that the requests started before the fork will only be handled |
|
|
1248 | in the parent). Threads will be started on demand until the limit set in |
|
|
1249 | the parent process has been reached again. |
|
|
1250 | |
1393 | |
1251 | In short: the parent will, after a short pause, continue as if fork had |
1394 | You might get around by not *using* IO::AIO before (or after) forking. |
1252 | not been called, while the child will act as if IO::AIO has not been |
1395 | You could also try to call the IO::AIO::reinit function in the child: |
1253 | used yet. |
1396 | |
|
|
1397 | IO::AIO::reinit |
|
|
1398 | Abondons all current requests and I/O threads and simply |
|
|
1399 | reinitialises all data structures. This is not an operation |
|
|
1400 | suppported by any standards, but happens to work on GNU/Linux and |
|
|
1401 | some newer BSD systems. |
|
|
1402 | |
|
|
1403 | The only reasonable use for this function is to call it after |
|
|
1404 | forking, if "IO::AIO" was used in the parent. Calling it while |
|
|
1405 | IO::AIO is active in the process will result in undefined behaviour. |
|
|
1406 | Calling it at any time will also result in any undefined (by POSIX) |
|
|
1407 | behaviour. |
1254 | |
1408 | |
1255 | MEMORY USAGE |
1409 | MEMORY USAGE |
1256 | Per-request usage: |
1410 | Per-request usage: |
1257 | |
1411 | |
1258 | Each aio request uses - depending on your architecture - around 100-200 |
1412 | Each aio request uses - depending on your architecture - around 100-200 |