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

Comparing IO-AIO/README (file contents):
Revision 1.43 by root, Sun Jan 10 23:44:02 2010 UTC vs.
Revision 1.45 by root, Thu Dec 30 07:19:31 2010 UTC

2 IO::AIO - Asynchronous Input/Output 2 IO::AIO - Asynchronous Input/Output
3 3
4SYNOPSIS 4SYNOPSIS
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";
355 reading at byte offset $in_offset, and starts writing at the current 359 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 360 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 361 more than one "aio_sendfile" per $out_fh, as they will interfere
358 with each other. 362 with each other.
359 363
364 Please note that "aio_sendfile" can read more bytes from $in_fh than
365 are written, and there is no way to find out how many bytes have
366 been read from "aio_sendfile" alone, as "aio_sendfile" only provides
367 the number of bytes written to $out_fh. Only if the result value
368 equals $length one can assume that $length bytes have been read.
369
370 Unlike with other "aio_" functions, it makes a lot of sense to use
371 "aio_sendfile" on non-blocking sockets, as long as one end
372 (typically the $in_fh) is a file - the file I/O will then be
373 asynchronous, while the socket I/O will be non-blocking. Note,
374 however, that you can run into a trap where "aio_sendfile" reads
375 some data with readahead, then fails to write all data, and when the
376 socket is ready the next time, the data in the cache is already
377 lost, forcing "aio_sendfile" to again hit the disk. Explicit
378 "aio_read" + "aio_write" let's you control resource usage much
379 better.
380
360 This call tries to make use of a native "sendfile" syscall to 381 This call tries to make use of a native "sendfile" syscall to
361 provide zero-copy operation. For this to work, $out_fh should refer 382 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. 383 to a socket, and $in_fh should refer to an mmap'able file.
363 384
364 If a native sendfile cannot be found or it fails with "ENOSYS", 385 If a native sendfile cannot be found or it fails with "ENOSYS",
365 "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK", 386 "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK",
366 it will be emulated, so you can call "aio_sendfile" on any type of 387 it will be emulated, so you can call "aio_sendfile" on any type of
367 filehandle regardless of the limitations of the operating system. 388 filehandle regardless of the limitations of the operating system.
368
369 Please note, however, that "aio_sendfile" can read more bytes from
370 $in_fh than are written, and there is no way to find out how many
371 bytes have been read from "aio_sendfile" alone, as "aio_sendfile"
372 only provides the number of bytes written to $out_fh. Only if the
373 result value equals $length one can assume that $length bytes have
374 been read.
375 389
376 aio_readahead $fh,$offset,$length, $callback->($retval) 390 aio_readahead $fh,$offset,$length, $callback->($retval)
377 "aio_readahead" populates the page cache with data from a file so 391 "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 392 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 393 $offset argument specifies the starting point from which data is to
751 "aio_msync", above, except for flags, which must be either 0 (which 765 "aio_msync", above, except for flags, which must be either 0 (which
752 reads all pages and ensures they are instantiated) or 766 reads all pages and ensures they are instantiated) or
753 "IO::AIO::MT_MODIFY", which modifies the memory page s(by reading 767 "IO::AIO::MT_MODIFY", which modifies the memory page s(by reading
754 and writing an octet from it, which dirties the page). 768 and writing an octet from it, which dirties the page).
755 769
770 aio_mlock $scalar, $offset = 0, $length = undef, $callback->($status)
771 This is a rather advanced IO::AIO call, which works best on
772 mmap(2)ed scalars.
773
774 It reads in all the pages of the underlying storage into memory (if
775 any) and locks them, so they are not getting swapped/paged out or
776 removed.
777
778 If $length is undefined, then the scalar will be locked till the
779 end.
780
781 On systems that do not implement "mlock", this function returns -1
782 and sets errno to "ENOSYS".
783
784 Note that the corresponding "munlock" is synchronous and is
785 documented under "MISCELLANEOUS FUNCTIONS".
786
787 Example: open a file, mmap and mlock it - both will be undone when
788 $data gets destroyed.
789
790 open my $fh, "<", $path or die "$path: $!";
791 my $data;
792 IO::AIO::mmap $data, -s $fh, IO::AIO::PROT_READ, IO::AIO::MAP_SHARED, $fh;
793 aio_mlock $data; # mlock in background
794
795 aio_mlockall $flags, $callback->($status)
796 Calls the "mlockall" function with the given $flags (a combination
797 of "IO::AIO::MCL_CURRENT" and "IO::AIO::MCL_FUTURE").
798
799 On systems that do not implement "mlockall", this function returns
800 -1 and sets errno to "ENOSYS".
801
802 Note that the corresponding "munlockall" is synchronous and is
803 documented under "MISCELLANEOUS FUNCTIONS".
804
805 Example: asynchronously lock all current and future pages into
806 memory.
807
808 aio_mlockall IO::AIO::MCL_FUTURE;
809
756 aio_group $callback->(...) 810 aio_group $callback->(...)
757 This is a very special aio request: Instead of doing something, it 811 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 812 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 813 to bundle many requests into a single, composite, request with a
760 definite callback and the ability to cancel the whole request with 814 definite callback and the ability to cancel the whole request with
1130 set to non-blocking operations). 1184 set to non-blocking operations).
1131 1185
1132 Returns the number of bytes copied, or -1 on error. 1186 Returns the number of bytes copied, or -1 on error.
1133 1187
1134 IO::AIO::fadvise $fh, $offset, $len, $advice 1188 IO::AIO::fadvise $fh, $offset, $len, $advice
1135 Simply calls the "posix_fadvise" function (see it's manpage for 1189 Simply calls the "posix_fadvise" function (see its manpage for
1136 details). The following advice constants are avaiable: 1190 details). The following advice constants are avaiable:
1137 "IO::AIO::FADV_NORMAL", "IO::AIO::FADV_SEQUENTIAL", 1191 "IO::AIO::FADV_NORMAL", "IO::AIO::FADV_SEQUENTIAL",
1138 "IO::AIO::FADV_RANDOM", "IO::AIO::FADV_NOREUSE", 1192 "IO::AIO::FADV_RANDOM", "IO::AIO::FADV_NOREUSE",
1139 "IO::AIO::FADV_WILLNEED", "IO::AIO::FADV_DONTNEED". 1193 "IO::AIO::FADV_WILLNEED", "IO::AIO::FADV_DONTNEED".
1140 1194
1141 On systems that do not implement "posix_fadvise", this function 1195 On systems that do not implement "posix_fadvise", this function
1142 returns ENOSYS, otherwise the return value of "posix_fadvise". 1196 returns ENOSYS, otherwise the return value of "posix_fadvise".
1197
1198 IO::AIO::madvise $scalar, $offset, $len, $advice
1199 Simply calls the "posix_madvise" function (see its manpage for
1200 details). The following advice constants are avaiable:
1201 "IO::AIO::MADV_NORMAL", "IO::AIO::MADV_SEQUENTIAL",
1202 "IO::AIO::MADV_RANDOM", "IO::AIO::MADV_WILLNEED",
1203 "IO::AIO::MADV_DONTNEED".
1204
1205 On systems that do not implement "posix_madvise", this function
1206 returns ENOSYS, otherwise the return value of "posix_madvise".
1207
1208 IO::AIO::mprotect $scalar, $offset, $len, $protect
1209 Simply calls the "mprotect" function on the preferably AIO::mmap'ed
1210 $scalar (see its manpage for details). The following protect
1211 constants are avaiable: "IO::AIO::PROT_NONE", "IO::AIO::PROT_READ",
1212 "IO::AIO::PROT_WRITE", "IO::AIO::PROT_EXEC".
1213
1214 On systems that do not implement "mprotect", this function returns
1215 ENOSYS, otherwise the return value of "mprotect".
1143 1216
1144 IO::AIO::mmap $scalar, $length, $prot, $flags, $fh[, $offset] 1217 IO::AIO::mmap $scalar, $length, $prot, $flags, $fh[, $offset]
1145 Memory-maps a file (or anonymous memory range) and attaches it to 1218 Memory-maps a file (or anonymous memory range) and attaches it to
1146 the given $scalar, which will act like a string scalar. 1219 the given $scalar, which will act like a string scalar.
1147 1220
1192 my $fast_md5 = md5 $data; 1265 my $fast_md5 = md5 $data;
1193 1266
1194 IO::AIO::munmap $scalar 1267 IO::AIO::munmap $scalar
1195 Removes a previous mmap and undefines the $scalar. 1268 Removes a previous mmap and undefines the $scalar.
1196 1269
1197 IO::AIO::mlockall $flags 1270 IO::AIO::munlock $scalar, $offset = 0, $length = undef
1198 Calls the "mlockall" function with the given $flags (a combination 1271 Calls the "munlock" function, undoing the effects of a previous
1199 of "IO::AIO::MCL_CURRENT" and "IO::AIO::MCL__FUTURE"). 1272 "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 1273
1204 IO::AIO::munlockall 1274 IO::AIO::munlockall
1205 Calls the "munlockall" function. 1275 Calls the "munlockall" function.
1206 1276
1207 On systems that do not implement "munlockall", this function returns 1277 On systems that do not implement "munlockall", this function returns

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines