… | |
… | |
204 | IO::AIO::max_poll_reqs $nreqs |
204 | IO::AIO::max_poll_reqs $nreqs |
205 | IO::AIO::max_poll_time $seconds |
205 | IO::AIO::max_poll_time $seconds |
206 | IO::AIO::min_parallel $nthreads |
206 | IO::AIO::min_parallel $nthreads |
207 | IO::AIO::max_parallel $nthreads |
207 | IO::AIO::max_parallel $nthreads |
208 | IO::AIO::max_idle $nthreads |
208 | IO::AIO::max_idle $nthreads |
|
|
209 | IO::AIO::idle_timeout $seconds |
209 | IO::AIO::max_outstanding $maxreqs |
210 | IO::AIO::max_outstanding $maxreqs |
210 | IO::AIO::nreqs |
211 | IO::AIO::nreqs |
211 | IO::AIO::nready |
212 | IO::AIO::nready |
212 | IO::AIO::npending |
213 | IO::AIO::npending |
213 | |
214 | |
… | |
… | |
305 | } else { |
306 | } else { |
306 | die "open failed: $!\n"; |
307 | die "open failed: $!\n"; |
307 | } |
308 | } |
308 | }; |
309 | }; |
309 | |
310 | |
|
|
311 | In addition to all the common open modes/flags ("O_RDONLY", |
|
|
312 | "O_WRONLY", "O_RDWR", "O_CREAT", "O_TRUNC", "O_EXCL" and |
|
|
313 | "O_APPEND"), the following POSIX and non-POSIX constants are |
|
|
314 | available (missing ones on your system are, as usual, 0): |
|
|
315 | |
|
|
316 | "O_ASYNC", "O_DIRECT", "O_NOATIME", "O_CLOEXEC", "O_NOCTTY", |
|
|
317 | "O_NOFOLLOW", "O_NONBLOCK", "O_EXEC", "O_SEARCH", "O_DIRECTORY", |
|
|
318 | "O_DSYNC", "O_RSYNC", "O_SYNC" and "O_TTY_INIT". |
|
|
319 | |
310 | aio_close $fh, $callback->($status) |
320 | aio_close $fh, $callback->($status) |
311 | Asynchronously close a file and call the callback with the result |
321 | Asynchronously close a file and call the callback with the result |
312 | code. |
322 | code. |
313 | |
323 | |
314 | Unfortunately, you can't do this to perl. Perl *insists* very |
324 | Unfortunately, you can't do this to perl. Perl *insists* very |
… | |
… | |
359 | reading at byte offset $in_offset, and starts writing at the current |
369 | reading at byte offset $in_offset, and starts writing at the current |
360 | file offset of $out_fh. Because of that, it is not safe to issue |
370 | file offset of $out_fh. Because of that, it is not safe to issue |
361 | more than one "aio_sendfile" per $out_fh, as they will interfere |
371 | more than one "aio_sendfile" per $out_fh, as they will interfere |
362 | with each other. |
372 | with each other. |
363 | |
373 | |
|
|
374 | Please note that "aio_sendfile" can read more bytes from $in_fh than |
|
|
375 | are written, and there is no way to find out how many bytes have |
|
|
376 | been read from "aio_sendfile" alone, as "aio_sendfile" only provides |
|
|
377 | the number of bytes written to $out_fh. Only if the result value |
|
|
378 | equals $length one can assume that $length bytes have been read. |
|
|
379 | |
|
|
380 | Unlike with other "aio_" functions, it makes a lot of sense to use |
|
|
381 | "aio_sendfile" on non-blocking sockets, as long as one end |
|
|
382 | (typically the $in_fh) is a file - the file I/O will then be |
|
|
383 | asynchronous, while the socket I/O will be non-blocking. Note, |
|
|
384 | however, that you can run into a trap where "aio_sendfile" reads |
|
|
385 | some data with readahead, then fails to write all data, and when the |
|
|
386 | socket is ready the next time, the data in the cache is already |
|
|
387 | lost, forcing "aio_sendfile" to again hit the disk. Explicit |
|
|
388 | "aio_read" + "aio_write" let's you control resource usage much |
|
|
389 | better. |
|
|
390 | |
364 | This call tries to make use of a native "sendfile" syscall to |
391 | This call tries to make use of a native "sendfile" syscall to |
365 | provide zero-copy operation. For this to work, $out_fh should refer |
392 | provide zero-copy operation. For this to work, $out_fh should refer |
366 | to a socket, and $in_fh should refer to an mmap'able file. |
393 | to a socket, and $in_fh should refer to an mmap'able file. |
367 | |
394 | |
368 | If a native sendfile cannot be found or it fails with "ENOSYS", |
395 | If a native sendfile cannot be found or it fails with "ENOSYS", |
369 | "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK", |
396 | "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK", |
370 | it will be emulated, so you can call "aio_sendfile" on any type of |
397 | it will be emulated, so you can call "aio_sendfile" on any type of |
371 | filehandle regardless of the limitations of the operating system. |
398 | filehandle regardless of the limitations of the operating system. |
372 | |
|
|
373 | Please note, however, that "aio_sendfile" can read more bytes from |
|
|
374 | $in_fh than are written, and there is no way to find out how many |
|
|
375 | bytes have been read from "aio_sendfile" alone, as "aio_sendfile" |
|
|
376 | only provides the number of bytes written to $out_fh. Only if the |
|
|
377 | result value equals $length one can assume that $length bytes have |
|
|
378 | been read. |
|
|
379 | |
399 | |
380 | aio_readahead $fh,$offset,$length, $callback->($retval) |
400 | aio_readahead $fh,$offset,$length, $callback->($retval) |
381 | "aio_readahead" populates the page cache with data from a file so |
401 | "aio_readahead" populates the page cache with data from a file so |
382 | that subsequent reads from that file will not block on disk I/O. The |
402 | that subsequent reads from that file will not block on disk I/O. The |
383 | $offset argument specifies the starting point from which data is to |
403 | $offset argument specifies the starting point from which data is to |
… | |
… | |
403 | |
423 | |
404 | Currently, the stats are always 64-bit-stats, i.e. instead of |
424 | Currently, the stats are always 64-bit-stats, i.e. instead of |
405 | returning an error when stat'ing a large file, the results will be |
425 | returning an error when stat'ing a large file, the results will be |
406 | silently truncated unless perl itself is compiled with large file |
426 | silently truncated unless perl itself is compiled with large file |
407 | support. |
427 | support. |
|
|
428 | |
|
|
429 | To help interpret the mode and dev/rdev stat values, IO::AIO offers |
|
|
430 | the following constants and functions (if not implemented, the |
|
|
431 | constants will be 0 and the functions will either "croak" or fall |
|
|
432 | back on traditional behaviour). |
|
|
433 | |
|
|
434 | "S_IFMT", "S_IFIFO", "S_IFCHR", "S_IFBLK", "S_IFLNK", "S_IFREG", |
|
|
435 | "S_IFDIR", "S_IFWHT", "S_IFSOCK", "IO::AIO::major $dev_t", |
|
|
436 | "IO::AIO::minor $dev_t", "IO::AIO::makedev $major, $minor". |
408 | |
437 | |
409 | Example: Print the length of /etc/passwd: |
438 | Example: Print the length of /etc/passwd: |
410 | |
439 | |
411 | aio_stat "/etc/passwd", sub { |
440 | aio_stat "/etc/passwd", sub { |
412 | $_[0] and die "stat failed: $!"; |
441 | $_[0] and die "stat failed: $!"; |
… | |
… | |
501 | |
530 | |
502 | The only (POSIX-) portable way of calling this function is: |
531 | The only (POSIX-) portable way of calling this function is: |
503 | |
532 | |
504 | aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ... |
533 | aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ... |
505 | |
534 | |
|
|
535 | See "aio_stat" for info about some potentially helpful extra |
|
|
536 | constants and functions. |
|
|
537 | |
506 | aio_link $srcpath, $dstpath, $callback->($status) |
538 | aio_link $srcpath, $dstpath, $callback->($status) |
507 | Asynchronously create a new link to the existing object at $srcpath |
539 | Asynchronously create a new link to the existing object at $srcpath |
508 | at the path $dstpath and call the callback with the result code. |
540 | at the path $dstpath and call the callback with the result code. |
509 | |
541 | |
510 | aio_symlink $srcpath, $dstpath, $callback->($status) |
542 | aio_symlink $srcpath, $dstpath, $callback->($status) |
… | |
… | |
546 | The flags are a combination of the following constants, ORed |
578 | The flags are a combination of the following constants, ORed |
547 | together (the flags will also be passed to the callback, possibly |
579 | together (the flags will also be passed to the callback, possibly |
548 | modified): |
580 | modified): |
549 | |
581 | |
550 | IO::AIO::READDIR_DENTS |
582 | IO::AIO::READDIR_DENTS |
551 | When this flag is off, then the callback gets an arrayref with |
583 | When this flag is off, then the callback gets an arrayref |
552 | of names only (as with "aio_readdir"), otherwise it gets an |
584 | consisting of names only (as with "aio_readdir"), otherwise it |
553 | arrayref with "[$name, $type, $inode]" arrayrefs, each |
585 | gets an arrayref with "[$name, $type, $inode]" arrayrefs, each |
554 | describing a single directory entry in more detail. |
586 | describing a single directory entry in more detail. |
555 | |
587 | |
556 | $name is the name of the entry. |
588 | $name is the name of the entry. |
557 | |
589 | |
558 | $type is one of the "IO::AIO::DT_xxx" constants: |
590 | $type is one of the "IO::AIO::DT_xxx" constants: |
… | |
… | |
571 | unspecified content on systems that do not deliver the inode |
603 | unspecified content on systems that do not deliver the inode |
572 | information. |
604 | information. |
573 | |
605 | |
574 | IO::AIO::READDIR_DIRS_FIRST |
606 | IO::AIO::READDIR_DIRS_FIRST |
575 | When this flag is set, then the names will be returned in an |
607 | When this flag is set, then the names will be returned in an |
576 | order where likely directories come first. This is useful when |
608 | order where likely directories come first, in optimal stat |
577 | you need to quickly find directories, or you want to find all |
609 | order. This is useful when you need to quickly find directories, |
578 | directories while avoiding to stat() each entry. |
610 | or you want to find all directories while avoiding to stat() |
|
|
611 | each entry. |
579 | |
612 | |
580 | If the system returns type information in readdir, then this is |
613 | If the system returns type information in readdir, then this is |
581 | used to find directories directly. Otherwise, likely directories |
614 | used to find directories directly. Otherwise, likely directories |
582 | are files beginning with ".", or otherwise files with no dots, |
615 | are names beginning with ".", or otherwise names with no dots, |
583 | of which files with short names are tried first. |
616 | of which names with short names are tried first. |
584 | |
617 | |
585 | IO::AIO::READDIR_STAT_ORDER |
618 | IO::AIO::READDIR_STAT_ORDER |
586 | When this flag is set, then the names will be returned in an |
619 | When this flag is set, then the names will be returned in an |
587 | order suitable for stat()'ing each one. That is, when you plan |
620 | order suitable for stat()'ing each one. That is, when you plan |
588 | to stat() all files in the given directory, then the returned |
621 | to stat() all files in the given directory, then the returned |
… | |
… | |
999 | |
1032 | |
1000 | See "poll_cb" for an example. |
1033 | See "poll_cb" for an example. |
1001 | |
1034 | |
1002 | IO::AIO::poll_cb |
1035 | IO::AIO::poll_cb |
1003 | Process some outstanding events on the result pipe. You have to call |
1036 | Process some outstanding events on the result pipe. You have to call |
1004 | this regularly. Returns 0 if all events could be processed, or -1 if |
1037 | this regularly. Returns 0 if all events could be processed (or there |
1005 | it returned earlier for whatever reason. Returns immediately when no |
1038 | were no events to process), or -1 if it returned earlier for |
1006 | events are outstanding. The amount of events processed depends on |
1039 | whatever reason. Returns immediately when no events are outstanding. |
1007 | the settings of "IO::AIO::max_poll_req" and |
1040 | The amount of events processed depends on the settings of |
1008 | "IO::AIO::max_poll_time". |
1041 | "IO::AIO::max_poll_req" and "IO::AIO::max_poll_time". |
1009 | |
1042 | |
1010 | If not all requests were processed for whatever reason, the |
1043 | If not all requests were processed for whatever reason, the |
1011 | filehandle will still be ready when "poll_cb" returns, so normally |
1044 | filehandle will still be ready when "poll_cb" returns, so normally |
1012 | you don't have to do anything special to have it called later. |
1045 | you don't have to do anything special to have it called later. |
|
|
1046 | |
|
|
1047 | Apart from calling "IO::AIO::poll_cb" when the event filehandle |
|
|
1048 | becomes ready, it can be beneficial to call this function from loops |
|
|
1049 | which submit a lot of requests, to make sure the results get |
|
|
1050 | processed when they become available and not just when the loop is |
|
|
1051 | finished and the event loop takes over again. This function returns |
|
|
1052 | very fast when there are no outstanding requests. |
1013 | |
1053 | |
1014 | Example: Install an Event watcher that automatically calls |
1054 | Example: Install an Event watcher that automatically calls |
1015 | IO::AIO::poll_cb with high priority (more examples can be found in |
1055 | IO::AIO::poll_cb with high priority (more examples can be found in |
1016 | the SYNOPSIS section, at the top of this document): |
1056 | the SYNOPSIS section, at the top of this document): |
1017 | |
1057 | |
… | |
… | |
1111 | |
1151 | |
1112 | Under normal circumstances you don't need to call this function. |
1152 | Under normal circumstances you don't need to call this function. |
1113 | |
1153 | |
1114 | IO::AIO::max_idle $nthreads |
1154 | IO::AIO::max_idle $nthreads |
1115 | Limit the number of threads (default: 4) that are allowed to idle |
1155 | Limit the number of threads (default: 4) that are allowed to idle |
1116 | (i.e., threads that did not get a request to process within 10 |
1156 | (i.e., threads that did not get a request to process within the idle |
1117 | seconds). That means if a thread becomes idle while $nthreads other |
1157 | timeout (default: 10 seconds). That means if a thread becomes idle |
1118 | threads are also idle, it will free its resources and exit. |
1158 | while $nthreads other threads are also idle, it will free its |
|
|
1159 | resources and exit. |
1119 | |
1160 | |
1120 | This is useful when you allow a large number of threads (e.g. 100 or |
1161 | This is useful when you allow a large number of threads (e.g. 100 or |
1121 | 1000) to allow for extremely high load situations, but want to free |
1162 | 1000) to allow for extremely high load situations, but want to free |
1122 | resources under normal circumstances (1000 threads can easily |
1163 | resources under normal circumstances (1000 threads can easily |
1123 | consume 30MB of RAM). |
1164 | consume 30MB of RAM). |
1124 | |
1165 | |
1125 | The default is probably ok in most situations, especially if thread |
1166 | The default is probably ok in most situations, especially if thread |
1126 | creation is fast. If thread creation is very slow on your system you |
1167 | creation is fast. If thread creation is very slow on your system you |
1127 | might want to use larger values. |
1168 | might want to use larger values. |
|
|
1169 | |
|
|
1170 | IO::AIO::idle_timeout $seconds |
|
|
1171 | Sets the minimum idle timeout (default 10) after which worker |
|
|
1172 | threads are allowed to exit. SEe "IO::AIO::max_idle". |
1128 | |
1173 | |
1129 | IO::AIO::max_outstanding $maxreqs |
1174 | IO::AIO::max_outstanding $maxreqs |
1130 | This is a very bad function to use in interactive programs because |
1175 | This is a very bad function to use in interactive programs because |
1131 | it blocks, and a bad way to reduce concurrency because it is |
1176 | it blocks, and a bad way to reduce concurrency because it is |
1132 | inexact: Better use an "aio_group" together with a feed callback. |
1177 | inexact: Better use an "aio_group" together with a feed callback. |