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

Comparing IO-AIO/AIO.pm (file contents):
Revision 1.107 by root, Fri Jun 1 05:52:59 2007 UTC vs.
Revision 1.117 by root, Sat Oct 6 14:05:19 2007 UTC

62etc.), but can also be used to easily do operations in parallel that are 62etc.), but can also be used to easily do operations in parallel that are
63normally done sequentially, e.g. stat'ing many files, which is much faster 63normally done sequentially, e.g. stat'ing many files, which is much faster
64on a RAID volume or over NFS when you do a number of stat operations 64on a RAID volume or over NFS when you do a number of stat operations
65concurrently. 65concurrently.
66 66
67While most of this works on all types of file descriptors (for example 67While most of this works on all types of file descriptors (for
68sockets), using these functions on file descriptors that support 68example sockets), using these functions on file descriptors that
69nonblocking operation (again, sockets, pipes etc.) is very inefficient or 69support nonblocking operation (again, sockets, pipes etc.) is very
70might not work (aio_read fails on sockets/pipes/fifos). Use an event loop 70inefficient. Use an event loop for that (such as the L<Event|Event>
71for that (such as the L<Event|Event> module): IO::AIO will naturally fit 71module): IO::AIO will naturally fit into such an event loop itself.
72into such an event loop itself.
73 72
74In this version, a number of threads are started that execute your 73In this version, a number of threads are started that execute your
75requests and signal their completion. You don't need thread support 74requests and signal their completion. You don't need thread support
76in perl, and the threads created by this module will not be visible 75in perl, and the threads created by this module will not be visible
77to perl. In the future, this module might make use of the native aio 76to perl. In the future, this module might make use of the native aio
79not well-supported or restricted (GNU/Linux doesn't allow them on normal 78not well-supported or restricted (GNU/Linux doesn't allow them on normal
80files currently, for example), and they would only support aio_read and 79files currently, for example), and they would only support aio_read and
81aio_write, so the remaining functionality would have to be implemented 80aio_write, so the remaining functionality would have to be implemented
82using threads anyway. 81using threads anyway.
83 82
84Although the module will work with in the presence of other (Perl-) 83Although the module will work in the presence of other (Perl-) threads,
85threads, it is currently not reentrant in any way, so use appropriate 84it is currently not reentrant in any way, so use appropriate locking
86locking yourself, always call C<poll_cb> from within the same thread, or 85yourself, always call C<poll_cb> from within the same thread, or never
87never call C<poll_cb> (or other C<aio_> functions) recursively. 86call C<poll_cb> (or other C<aio_> functions) recursively.
88 87
89=head2 EXAMPLE 88=head2 EXAMPLE
90 89
91This is a simple example that uses the Event module and loads 90This is a simple example that uses the Event module and loads
92F</etc/passwd> asynchronously: 91F</etc/passwd> asynchronously:
184 183
185=cut 184=cut
186 185
187package IO::AIO; 186package IO::AIO;
188 187
188use Carp ();
189
189no warnings; 190no warnings;
190use strict 'vars'; 191use strict 'vars';
191 192
192use base 'Exporter'; 193use base 'Exporter';
193 194
194BEGIN { 195BEGIN {
195 our $VERSION = '2.4'; 196 our $VERSION = '2.51';
196 197
197 our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat 198 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 199 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 200 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 201 aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir
201 aio_chown aio_chmod aio_utime); 202 aio_chown aio_chmod aio_utime aio_truncate);
202 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); 203 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block));
203 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush 204 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush
204 min_parallel max_parallel max_idle 205 min_parallel max_parallel max_idle
205 nreqs nready npending nthreads 206 nreqs nready npending nthreads
206 max_poll_time max_poll_reqs); 207 max_poll_time max_poll_reqs);
312 313
313 314
314=item aio_close $fh, $callback->($status) 315=item aio_close $fh, $callback->($status)
315 316
316Asynchronously close a file and call the callback with the result 317Asynchronously close a file and call the callback with the result
317code. I<WARNING:> although accepted, you should not pass in a perl 318code.
318filehandle here, as perl will likely close the file descriptor another
319time when the filehandle is destroyed. Normally, you can safely call perls
320C<close> or just let filehandles go out of scope.
321 319
322This is supposed to be a bug in the API, so that might change. It's 320Unfortunately, you can't do this to perl. Perl I<insists> very strongly on
323therefore best to avoid this function. 321closing the file descriptor associated with the filehandle itself. Here is
322what aio_close will try:
323
324 1. dup()licate the fd
325 2. asynchronously close() the duplicated fd
326 3. dup()licate the fd once more
327 4. let perl close() the filehandle
328 5. asynchronously close the duplicated fd
329
330The idea is that the first close() flushes stuff to disk that closing an
331fd will flush, so when perl closes the fd, nothing much will need to be
332flushed. The second async. close() will then flush stuff to disk that
333closing the last fd to the file will flush.
334
335Just FYI, SuSv3 has this to say on close:
336
337 All outstanding record locks owned by the process on the file
338 associated with the file descriptor shall be removed.
339
340 If fildes refers to a socket, close() shall cause the socket to be
341 destroyed. ... close() shall block for up to the current linger
342 interval until all data is transmitted.
343 [this actually sounds like a specification bug, but who knows]
344
345And at least Linux additionally actually flushes stuff on every close,
346even when the file itself is still open.
347
348Sounds enourmously inefficient and complicated? Yes... please show me how
349to nuke perl's fd out of existence...
350
351=cut
352
353sub aio_close($;$) {
354 aio_block {
355 my ($fh, $cb) = @_;
356
357 my $pri = aioreq_pri;
358 my $grp = aio_group $cb;
359
360 my $fd = fileno $fh;
361
362 defined $fd or Carp::croak "aio_close called with fd-less filehandle";
363
364 # if the dups fail we will simply get EBADF
365 my $fd2 = _dup $fd;
366 aioreq_pri $pri;
367 add $grp _aio_close $fd2, sub {
368 my $fd2 = _dup $fd;
369 close $fh;
370 aioreq_pri $pri;
371 add $grp _aio_close $fd2, sub {
372 $grp->result ($_[0]);
373 };
374 };
375
376 $grp
377 }
378}
324 379
325 380
326=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 381=item aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
327 382
328=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) 383=item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
329 384
330Reads or writes C<length> bytes from the specified C<fh> and C<offset> 385Reads or writes C<$length> bytes from the specified C<$fh> and C<$offset>
331into the scalar given by C<data> and offset C<dataoffset> and calls the 386into the scalar given by C<$data> and offset C<$dataoffset> and calls the
332callback without the actual number of bytes read (or -1 on error, just 387callback without the actual number of bytes read (or -1 on error, just
333like the syscall). 388like the syscall).
334 389
390If C<$offset> is undefined, then the current file descriptor offset will
391be used (and updated), otherwise the file descriptor offset will not be
392changed by these calls.
393
394If C<$length> is undefined in C<aio_write>, use the remaining length of C<$data>.
395
396If C<$dataoffset> is less than zero, it will be counted from the end of
397C<$data>.
398
335The C<$data> scalar I<MUST NOT> be modified in any way while the request 399The C<$data> scalar I<MUST NOT> be modified in any way while the request
336is outstanding. Modifying it can result in segfaults or WW3 (if the 400is outstanding. Modifying it can result in segfaults or World War III (if
337necessary/optional hardware is installed). 401the necessary/optional hardware is installed).
338 402
339Example: Read 15 bytes at offset 7 into scalar C<$buffer>, starting at 403Example: Read 15 bytes at offset 7 into scalar C<$buffer>, starting at
340offset C<0> within the scalar: 404offset C<0> within the scalar:
341 405
342 aio_read $fh, 7, 15, $buffer, 0, sub { 406 aio_read $fh, 7, 15, $buffer, 0, sub {
434 498
435 # same as "chown root path" in the shell: 499 # same as "chown root path" in the shell:
436 aio_chown "path", 0, -1; 500 aio_chown "path", 0, -1;
437 # same as above: 501 # same as above:
438 aio_chown "path", 0, undef; 502 aio_chown "path", 0, undef;
503
504
505=item aio_truncate $fh_or_path, $offset, $callback->($status)
506
507Works like truncate(2) or ftruncate(2).
439 508
440 509
441=item aio_chmod $fh_or_path, $mode, $callback->($status) 510=item aio_chmod $fh_or_path, $mode, $callback->($status)
442 511
443Works like perl's C<chmod> function. 512Works like perl's C<chmod> function.
1194This is a very bad function to use in interactive programs because it 1263This is a very bad function to use in interactive programs because it
1195blocks, and a bad way to reduce concurrency because it is inexact: Better 1264blocks, and a bad way to reduce concurrency because it is inexact: Better
1196use an C<aio_group> together with a feed callback. 1265use an C<aio_group> together with a feed callback.
1197 1266
1198Sets the maximum number of outstanding requests to C<$nreqs>. If you 1267Sets the maximum number of outstanding requests to C<$nreqs>. If you
1199to queue up more than this number of requests, the next call to the 1268do queue up more than this number of requests, the next call to the
1200C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>) 1269C<poll_cb> (and C<poll_some> and other functions calling C<poll_cb>)
1201function will block until the limit is no longer exceeded. 1270function will block until the limit is no longer exceeded.
1202 1271
1203The default value is very large, so there is no practical limit on the 1272The default value is very large, so there is no practical limit on the
1204number of outstanding requests. 1273number of outstanding requests.
1234but not yet processed by poll_cb). 1303but not yet processed by poll_cb).
1235 1304
1236=back 1305=back
1237 1306
1238=cut 1307=cut
1239
1240# support function to convert a fd into a perl filehandle
1241sub _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 1308
1256min_parallel 8; 1309min_parallel 8;
1257 1310
1258END { flush } 1311END { flush }
1259 1312
1283bytes of memory. In addition, stat requests need a stat buffer (possibly 1336bytes of memory. In addition, stat requests need a stat buffer (possibly
1284a few hundred bytes), readdir requires a result buffer and so on. Perl 1337a few hundred bytes), readdir requires a result buffer and so on. Perl
1285scalars and other data passed into aio requests will also be locked and 1338scalars and other data passed into aio requests will also be locked and
1286will consume memory till the request has entered the done state. 1339will consume memory till the request has entered the done state.
1287 1340
1288This is now awfully much, so queuing lots of requests is not usually a 1341This is not awfully much, so queuing lots of requests is not usually a
1289problem. 1342problem.
1290 1343
1291Per-thread usage: 1344Per-thread usage:
1292 1345
1293In the execution phase, some aio requests require more memory for 1346In the execution phase, some aio requests require more memory for

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines