… | |
… | |
62 | etc.), but can also be used to easily do operations in parallel that are |
62 | etc.), but can also be used to easily do operations in parallel that are |
63 | normally done sequentially, e.g. stat'ing many files, which is much faster |
63 | normally done sequentially, e.g. stat'ing many files, which is much faster |
64 | on a RAID volume or over NFS when you do a number of stat operations |
64 | on a RAID volume or over NFS when you do a number of stat operations |
65 | concurrently. |
65 | concurrently. |
66 | |
66 | |
67 | While most of this works on all types of file descriptors (for example |
67 | While most of this works on all types of file descriptors (for |
68 | sockets), using these functions on file descriptors that support |
68 | example sockets), using these functions on file descriptors that |
69 | nonblocking operation (again, sockets, pipes etc.) is very inefficient or |
69 | support nonblocking operation (again, sockets, pipes etc.) is very |
70 | might not work (aio_read fails on sockets/pipes/fifos). Use an event loop |
70 | inefficient. Use an event loop for that (such as the L<Event|Event> |
71 | for that (such as the L<Event|Event> module): IO::AIO will naturally fit |
71 | module): IO::AIO will naturally fit into such an event loop itself. |
72 | into such an event loop itself. |
|
|
73 | |
72 | |
74 | In this version, a number of threads are started that execute your |
73 | In this version, a number of threads are started that execute your |
75 | requests and signal their completion. You don't need thread support |
74 | requests and signal their completion. You don't need thread support |
76 | in perl, and the threads created by this module will not be visible |
75 | in perl, and the threads created by this module will not be visible |
77 | to perl. In the future, this module might make use of the native aio |
76 | to perl. In the future, this module might make use of the native aio |
… | |
… | |
79 | not well-supported or restricted (GNU/Linux doesn't allow them on normal |
78 | not well-supported or restricted (GNU/Linux doesn't allow them on normal |
80 | files currently, for example), and they would only support aio_read and |
79 | files currently, for example), and they would only support aio_read and |
81 | aio_write, so the remaining functionality would have to be implemented |
80 | aio_write, so the remaining functionality would have to be implemented |
82 | using threads anyway. |
81 | using threads anyway. |
83 | |
82 | |
84 | Although the module will work with in the presence of other (Perl-) |
83 | Although the module will work in the presence of other (Perl-) threads, |
85 | threads, it is currently not reentrant in any way, so use appropriate |
84 | it is currently not reentrant in any way, so use appropriate locking |
86 | locking yourself, always call C<poll_cb> from within the same thread, or |
85 | yourself, always call C<poll_cb> from within the same thread, or never |
87 | never call C<poll_cb> (or other C<aio_> functions) recursively. |
86 | call C<poll_cb> (or other C<aio_> functions) recursively. |
88 | |
87 | |
89 | =head2 EXAMPLE |
88 | =head2 EXAMPLE |
90 | |
89 | |
91 | This is a simple example that uses the Event module and loads |
90 | This is a simple example that uses the Event module and loads |
92 | F</etc/passwd> asynchronously: |
91 | F</etc/passwd> asynchronously: |
… | |
… | |
190 | use strict 'vars'; |
189 | use strict 'vars'; |
191 | |
190 | |
192 | use base 'Exporter'; |
191 | use base 'Exporter'; |
193 | |
192 | |
194 | BEGIN { |
193 | BEGIN { |
195 | our $VERSION = '2.4'; |
194 | our $VERSION = '2.5'; |
196 | |
195 | |
197 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
196 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
198 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
197 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
199 | aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link |
198 | aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link |
200 | aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir |
199 | aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir |
201 | aio_chown aio_chmod aio_utime); |
200 | aio_chown aio_chmod aio_utime aio_truncate); |
202 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); |
201 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); |
203 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
202 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
204 | min_parallel max_parallel max_idle |
203 | min_parallel max_parallel max_idle |
205 | nreqs nready npending nthreads |
204 | nreqs nready npending nthreads |
206 | max_poll_time max_poll_reqs); |
205 | max_poll_time max_poll_reqs); |
… | |
… | |
312 | |
311 | |
313 | |
312 | |
314 | =item aio_close $fh, $callback->($status) |
313 | =item aio_close $fh, $callback->($status) |
315 | |
314 | |
316 | Asynchronously close a file and call the callback with the result |
315 | Asynchronously close a file and call the callback with the result |
317 | code. I<WARNING:> although accepted, you should not pass in a perl |
316 | code. |
318 | filehandle here, as perl will likely close the file descriptor another |
|
|
319 | time when the filehandle is destroyed. Normally, you can safely call perls |
|
|
320 | C<close> or just let filehandles go out of scope. |
|
|
321 | |
317 | |
322 | This is supposed to be a bug in the API, so that might change. It's |
318 | Unlike the other functions operating on files, this function uses the |
323 | therefore best to avoid this function. |
319 | PerlIO layer to close the filehandle. The reason is that the PerlIO API |
|
|
320 | insists on closing the underlying fd itself, no matter what, and doesn't |
|
|
321 | allow modifications to the fd. Unfortunately, it is not clear that you can |
|
|
322 | call PerlIO from different threads (actually, its quite clear that this |
|
|
323 | won't work in some cases), so while it likely works perfectly with simple |
|
|
324 | file handles (such as the ones created by C<aio_open>) it might fail in |
|
|
325 | interesting ways for others. |
|
|
326 | |
|
|
327 | Having said that, aio_close tries to clean up the filehandle as much as |
|
|
328 | possible before handing it to an io thread, and generally does work. |
324 | |
329 | |
325 | |
330 | |
326 | =item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
331 | =item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
327 | |
332 | |
328 | =item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
333 | =item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
329 | |
334 | |
330 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
335 | Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset> |
331 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
336 | into the scalar given by C<$data> and offset C<$dataoffset> and calls the |
332 | callback without the actual number of bytes read (or -1 on error, just |
337 | callback without the actual number of bytes read (or -1 on error, just |
333 | like the syscall). |
338 | like the syscall). |
334 | |
339 | |
|
|
340 | If C<$offset> is undefined, then the current file descriptor offset will |
|
|
341 | be used (and updated), otherwise the file descriptor offset will not be |
|
|
342 | changed by these calls. |
|
|
343 | |
|
|
344 | If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>. |
|
|
345 | |
|
|
346 | If C<$dataoffset> is less than zero, it will be counted from the end of |
|
|
347 | C<$data>. |
|
|
348 | |
335 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
349 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
336 | is outstanding. Modifying it can result in segfaults or WW3 (if the |
350 | is outstanding. Modifying it can result in segfaults or World War III (if |
337 | necessary/optional hardware is installed). |
351 | the necessary/optional hardware is installed). |
338 | |
352 | |
339 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, starting at |
353 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, starting at |
340 | offset C<0> within the scalar: |
354 | offset C<0> within the scalar: |
341 | |
355 | |
342 | aio_read $fh, 7, 15, $buffer, 0, sub { |
356 | aio_read $fh, 7, 15, $buffer, 0, sub { |
… | |
… | |
417 | utime(2). If called on a file descriptor, uses futimes(2) if available, |
431 | utime(2). If called on a file descriptor, uses futimes(2) if available, |
418 | otherwise returns ENOSYS, so this is not portable. |
432 | otherwise returns ENOSYS, so this is not portable. |
419 | |
433 | |
420 | Examples: |
434 | Examples: |
421 | |
435 | |
422 | # set atime and mtime to current time: |
436 | # set atime and mtime to current time (basically touch(1)): |
423 | aio_utime "path", undef, undef; |
437 | aio_utime "path", undef, undef; |
424 | # set atime to current time and mtime to beginning of the epoch: |
438 | # set atime to current time and mtime to beginning of the epoch: |
425 | aio_utime "path", time, undef; # undef==0 |
439 | aio_utime "path", time, undef; # undef==0 |
426 | |
440 | |
427 | |
441 | |
… | |
… | |
434 | |
448 | |
435 | # same as "chown root path" in the shell: |
449 | # same as "chown root path" in the shell: |
436 | aio_chown "path", 0, -1; |
450 | aio_chown "path", 0, -1; |
437 | # same as above: |
451 | # same as above: |
438 | aio_chown "path", 0, undef; |
452 | aio_chown "path", 0, undef; |
|
|
453 | |
|
|
454 | |
|
|
455 | =item aio_truncate $fh_or_path, $offset, $callback->($status) |
|
|
456 | |
|
|
457 | Works like truncate(2) or ftruncate(2). |
439 | |
458 | |
440 | |
459 | |
441 | =item aio_chmod $fh_or_path, $mode, $callback->($status) |
460 | =item aio_chmod $fh_or_path, $mode, $callback->($status) |
442 | |
461 | |
443 | Works like perl's C<chmod> function. |
462 | Works like perl's C<chmod> function. |
… | |
… | |
1194 | This is a very bad function to use in interactive programs because it |
1213 | This is a very bad function to use in interactive programs because it |
1195 | blocks, and a bad way to reduce concurrency because it is inexact: Better |
1214 | blocks, and a bad way to reduce concurrency because it is inexact: Better |
1196 | use an C<aio_group> together with a feed callback. |
1215 | use an C<aio_group> together with a feed callback. |
1197 | |
1216 | |
1198 | Sets the maximum number of outstanding requests to C<$nreqs>. If you |
1217 | Sets the maximum number of outstanding requests to C<$nreqs>. If you |
1199 | to queue up more than this number of requests, the next call to the |
1218 | do queue up more than this number of requests, the next call to the |
1200 | C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
1219 | C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) |
1201 | function will block until the limit is no longer exceeded. |
1220 | function will block until the limit is no longer exceeded. |
1202 | |
1221 | |
1203 | The default value is very large, so there is no practical limit on the |
1222 | The default value is very large, so there is no practical limit on the |
1204 | number of outstanding requests. |
1223 | number of outstanding requests. |
… | |
… | |
1234 | but not yet processed by poll_cb). |
1253 | but not yet processed by poll_cb). |
1235 | |
1254 | |
1236 | =back |
1255 | =back |
1237 | |
1256 | |
1238 | =cut |
1257 | =cut |
1239 | |
|
|
1240 | # support function to convert a fd into a perl filehandle |
|
|
1241 | sub _fd2fh { |
|
|
1242 | return undef if $_[0] < 0; |
|
|
1243 | |
|
|
1244 | # try to generate nice filehandles |
|
|
1245 | my $sym = "IO::AIO::fd#$_[0]"; |
|
|
1246 | local *$sym; |
|
|
1247 | |
|
|
1248 | open *$sym, "+<&=$_[0]" # usually works under any unix |
|
|
1249 | or open *$sym, "<&=$_[0]" # cygwin needs this |
|
|
1250 | or open *$sym, ">&=$_[0]" # or this |
|
|
1251 | or return undef; |
|
|
1252 | |
|
|
1253 | *$sym |
|
|
1254 | } |
|
|
1255 | |
1258 | |
1256 | min_parallel 8; |
1259 | min_parallel 8; |
1257 | |
1260 | |
1258 | END { flush } |
1261 | END { flush } |
1259 | |
1262 | |
… | |
… | |
1283 | bytes of memory. In addition, stat requests need a stat buffer (possibly |
1286 | bytes of memory. In addition, stat requests need a stat buffer (possibly |
1284 | a few hundred bytes), readdir requires a result buffer and so on. Perl |
1287 | a few hundred bytes), readdir requires a result buffer and so on. Perl |
1285 | scalars and other data passed into aio requests will also be locked and |
1288 | scalars and other data passed into aio requests will also be locked and |
1286 | will consume memory till the request has entered the done state. |
1289 | will consume memory till the request has entered the done state. |
1287 | |
1290 | |
1288 | This is now awfully much, so queuing lots of requests is not usually a |
1291 | This is not awfully much, so queuing lots of requests is not usually a |
1289 | problem. |
1292 | problem. |
1290 | |
1293 | |
1291 | Per-thread usage: |
1294 | Per-thread usage: |
1292 | |
1295 | |
1293 | In the execution phase, some aio requests require more memory for |
1296 | In the execution phase, some aio requests require more memory for |