… | |
… | |
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; |
… | |
… | |
187 | aio_fdatasync $fh, $callback->($status) |
187 | aio_fdatasync $fh, $callback->($status) |
188 | aio_sync_file_range $fh, $offset, $nbytes, $flags, $callback->($status) |
188 | aio_sync_file_range $fh, $offset, $nbytes, $flags, $callback->($status) |
189 | aio_pathsync $path, $callback->($status) |
189 | aio_pathsync $path, $callback->($status) |
190 | aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
190 | aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
191 | aio_mtouch $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
191 | aio_mtouch $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) |
|
|
192 | aio_mlock $scalar, $offset = 0, $length = undef, $callback->($status) |
|
|
193 | aio_mlockall $flags, $callback->($status) |
192 | aio_group $callback->(...) |
194 | aio_group $callback->(...) |
193 | aio_nop $callback->() |
195 | aio_nop $callback->() |
194 | |
196 | |
195 | $prev_pri = aioreq_pri [$pri] |
197 | $prev_pri = aioreq_pri [$pri] |
196 | aioreq_nice $pri_adjust |
198 | aioreq_nice $pri_adjust |
… | |
… | |
209 | IO::AIO::nready |
211 | IO::AIO::nready |
210 | IO::AIO::npending |
212 | IO::AIO::npending |
211 | |
213 | |
212 | IO::AIO::sendfile $ofh, $ifh, $offset, $count |
214 | IO::AIO::sendfile $ofh, $ifh, $offset, $count |
213 | IO::AIO::fadvise $fh, $offset, $len, $advice |
215 | IO::AIO::fadvise $fh, $offset, $len, $advice |
214 | IO::AIO::mlockall $flags |
216 | IO::AIO::madvise $scalar, $offset, $length, $advice |
|
|
217 | IO::AIO::mprotect $scalar, $offset, $length, $protect |
|
|
218 | IO::AIO::munlock $scalar, $offset = 0, $length = undef |
215 | IO::AIO::munlockall |
219 | IO::AIO::munlockall |
216 | |
220 | |
217 | AIO REQUEST FUNCTIONS |
221 | AIO REQUEST FUNCTIONS |
218 | All the "aio_*" calls are more or less thin wrappers around the syscall |
222 | 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 |
223 | 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 |
296 | will be modified by the umask in effect then the request is being |
293 | executed, so better never change the umask. |
297 | executed, so better never change the umask. |
294 | |
298 | |
295 | Example: |
299 | Example: |
296 | |
300 | |
297 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
301 | aio_open "/etc/passwd", IO::AIO::O_RDONLY, 0, sub { |
298 | if ($_[0]) { |
302 | if ($_[0]) { |
299 | print "open successful, fh is $_[0]\n"; |
303 | print "open successful, fh is $_[0]\n"; |
300 | ... |
304 | ... |
301 | } else { |
305 | } else { |
302 | die "open failed: $!\n"; |
306 | die "open failed: $!\n"; |
… | |
… | |
751 | "aio_msync", above, except for flags, which must be either 0 (which |
755 | "aio_msync", above, except for flags, which must be either 0 (which |
752 | reads all pages and ensures they are instantiated) or |
756 | reads all pages and ensures they are instantiated) or |
753 | "IO::AIO::MT_MODIFY", which modifies the memory page s(by reading |
757 | "IO::AIO::MT_MODIFY", which modifies the memory page s(by reading |
754 | and writing an octet from it, which dirties the page). |
758 | and writing an octet from it, which dirties the page). |
755 | |
759 | |
|
|
760 | aio_mlock $scalar, $offset = 0, $length = undef, $callback->($status) |
|
|
761 | This is a rather advanced IO::AIO call, which works best on |
|
|
762 | mmap(2)ed scalars. |
|
|
763 | |
|
|
764 | It reads in all the pages of the underlying storage into memory (if |
|
|
765 | any) and locks them, so they are not getting swapped/paged out or |
|
|
766 | removed. |
|
|
767 | |
|
|
768 | If $length is undefined, then the scalar will be locked till the |
|
|
769 | end. |
|
|
770 | |
|
|
771 | On systems that do not implement "mlock", this function returns -1 |
|
|
772 | and sets errno to "ENOSYS". |
|
|
773 | |
|
|
774 | Note that the corresponding "munlock" is synchronous and is |
|
|
775 | documented under "MISCELLANEOUS FUNCTIONS". |
|
|
776 | |
|
|
777 | Example: open a file, mmap and mlock it - both will be undone when |
|
|
778 | $data gets destroyed. |
|
|
779 | |
|
|
780 | open my $fh, "<", $path or die "$path: $!"; |
|
|
781 | my $data; |
|
|
782 | IO::AIO::mmap $data, -s $fh, IO::AIO::PROT_READ, IO::AIO::MAP_SHARED, $fh; |
|
|
783 | aio_mlock $data; # mlock in background |
|
|
784 | |
|
|
785 | aio_mlockall $flags, $callback->($status) |
|
|
786 | Calls the "mlockall" function with the given $flags (a combination |
|
|
787 | of "IO::AIO::MCL_CURRENT" and "IO::AIO::MCL_FUTURE"). |
|
|
788 | |
|
|
789 | On systems that do not implement "mlockall", this function returns |
|
|
790 | -1 and sets errno to "ENOSYS". |
|
|
791 | |
|
|
792 | Note that the corresponding "munlockall" is synchronous and is |
|
|
793 | documented under "MISCELLANEOUS FUNCTIONS". |
|
|
794 | |
|
|
795 | Example: asynchronously lock all current and future pages into |
|
|
796 | memory. |
|
|
797 | |
|
|
798 | aio_mlockall IO::AIO::MCL_FUTURE; |
|
|
799 | |
756 | aio_group $callback->(...) |
800 | aio_group $callback->(...) |
757 | This is a very special aio request: Instead of doing something, it |
801 | 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 |
802 | 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 |
803 | to bundle many requests into a single, composite, request with a |
760 | definite callback and the ability to cancel the whole request with |
804 | definite callback and the ability to cancel the whole request with |
… | |
… | |
1130 | set to non-blocking operations). |
1174 | set to non-blocking operations). |
1131 | |
1175 | |
1132 | Returns the number of bytes copied, or -1 on error. |
1176 | Returns the number of bytes copied, or -1 on error. |
1133 | |
1177 | |
1134 | IO::AIO::fadvise $fh, $offset, $len, $advice |
1178 | IO::AIO::fadvise $fh, $offset, $len, $advice |
1135 | Simply calls the "posix_fadvise" function (see it's manpage for |
1179 | Simply calls the "posix_fadvise" function (see its manpage for |
1136 | details). The following advice constants are avaiable: |
1180 | details). The following advice constants are avaiable: |
1137 | "IO::AIO::FADV_NORMAL", "IO::AIO::FADV_SEQUENTIAL", |
1181 | "IO::AIO::FADV_NORMAL", "IO::AIO::FADV_SEQUENTIAL", |
1138 | "IO::AIO::FADV_RANDOM", "IO::AIO::FADV_NOREUSE", |
1182 | "IO::AIO::FADV_RANDOM", "IO::AIO::FADV_NOREUSE", |
1139 | "IO::AIO::FADV_WILLNEED", "IO::AIO::FADV_DONTNEED". |
1183 | "IO::AIO::FADV_WILLNEED", "IO::AIO::FADV_DONTNEED". |
1140 | |
1184 | |
1141 | On systems that do not implement "posix_fadvise", this function |
1185 | On systems that do not implement "posix_fadvise", this function |
1142 | returns ENOSYS, otherwise the return value of "posix_fadvise". |
1186 | returns ENOSYS, otherwise the return value of "posix_fadvise". |
|
|
1187 | |
|
|
1188 | IO::AIO::madvise $scalar, $offset, $len, $advice |
|
|
1189 | Simply calls the "posix_madvise" function (see its manpage for |
|
|
1190 | details). The following advice constants are avaiable: |
|
|
1191 | "IO::AIO::MADV_NORMAL", "IO::AIO::MADV_SEQUENTIAL", |
|
|
1192 | "IO::AIO::MADV_RANDOM", "IO::AIO::MADV_WILLNEED", |
|
|
1193 | "IO::AIO::MADV_DONTNEED". |
|
|
1194 | |
|
|
1195 | On systems that do not implement "posix_madvise", this function |
|
|
1196 | returns ENOSYS, otherwise the return value of "posix_madvise". |
|
|
1197 | |
|
|
1198 | IO::AIO::mprotect $scalar, $offset, $len, $protect |
|
|
1199 | Simply calls the "mprotect" function on the preferably AIO::mmap'ed |
|
|
1200 | $scalar (see its manpage for details). The following protect |
|
|
1201 | constants are avaiable: "IO::AIO::PROT_NONE", "IO::AIO::PROT_READ", |
|
|
1202 | "IO::AIO::PROT_WRITE", "IO::AIO::PROT_EXEC". |
|
|
1203 | |
|
|
1204 | On systems that do not implement "mprotect", this function returns |
|
|
1205 | ENOSYS, otherwise the return value of "mprotect". |
1143 | |
1206 | |
1144 | IO::AIO::mmap $scalar, $length, $prot, $flags, $fh[, $offset] |
1207 | IO::AIO::mmap $scalar, $length, $prot, $flags, $fh[, $offset] |
1145 | Memory-maps a file (or anonymous memory range) and attaches it to |
1208 | Memory-maps a file (or anonymous memory range) and attaches it to |
1146 | the given $scalar, which will act like a string scalar. |
1209 | the given $scalar, which will act like a string scalar. |
1147 | |
1210 | |
… | |
… | |
1192 | my $fast_md5 = md5 $data; |
1255 | my $fast_md5 = md5 $data; |
1193 | |
1256 | |
1194 | IO::AIO::munmap $scalar |
1257 | IO::AIO::munmap $scalar |
1195 | Removes a previous mmap and undefines the $scalar. |
1258 | Removes a previous mmap and undefines the $scalar. |
1196 | |
1259 | |
1197 | IO::AIO::mlockall $flags |
1260 | IO::AIO::munlock $scalar, $offset = 0, $length = undef |
1198 | Calls the "mlockall" function with the given $flags (a combination |
1261 | Calls the "munlock" function, undoing the effects of a previous |
1199 | of "IO::AIO::MCL_CURRENT" and "IO::AIO::MCL__FUTURE"). |
1262 | "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 | |
1263 | |
1204 | IO::AIO::munlockall |
1264 | IO::AIO::munlockall |
1205 | Calls the "munlockall" function. |
1265 | Calls the "munlockall" function. |
1206 | |
1266 | |
1207 | On systems that do not implement "munlockall", this function returns |
1267 | On systems that do not implement "munlockall", this function returns |