| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
Linux::AIO - linux-specific aio implemented using clone |
| 4 |
|
|
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
|
|
use Linux::AIO; |
| 8 |
|
|
|
| 9 |
|
|
=head1 DESCRIPTION |
| 10 |
|
|
|
| 11 |
root |
1.14 |
This module implements asynchronous i/o using the means available to linux |
| 12 |
|
|
- clone. It does not hook into the POSIX aio_* functions because linux |
| 13 |
|
|
does not yet support these in the kernel (and even if, it would only allow |
| 14 |
|
|
aio_read and write, not open and stat). |
| 15 |
|
|
|
| 16 |
|
|
Instead, in this module a number of (non-posix) threads are started that |
| 17 |
|
|
execute your read/writes and signal their completion. You don't need |
| 18 |
|
|
thread support in your libc or perl, and the threads created by this |
| 19 |
|
|
module will not be visible to the pthreads library. |
| 20 |
root |
1.3 |
|
| 21 |
root |
1.17 |
NOTICE: the threads created by this module will automatically be killed |
| 22 |
|
|
when the thread calling min_parallel exits. Make sure you only ever call |
| 23 |
|
|
min_parallel from the same thread that loaded this module. |
| 24 |
|
|
|
| 25 |
root |
1.16 |
Although the module will work with threads, it is not reentrant, so use |
| 26 |
|
|
appropriate locking yourself. |
| 27 |
|
|
|
| 28 |
root |
1.1 |
=over 4 |
| 29 |
|
|
|
| 30 |
|
|
=cut |
| 31 |
|
|
|
| 32 |
|
|
package Linux::AIO; |
| 33 |
|
|
|
| 34 |
root |
1.2 |
use base 'Exporter'; |
| 35 |
|
|
|
| 36 |
root |
1.1 |
BEGIN { |
| 37 |
root |
1.22 |
$VERSION = 1.41; |
| 38 |
root |
1.1 |
|
| 39 |
root |
1.17 |
@EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink); |
| 40 |
root |
1.2 |
@EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
| 41 |
|
|
|
| 42 |
root |
1.1 |
require XSLoader; |
| 43 |
|
|
XSLoader::load Linux::AIO, $VERSION; |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
root |
1.14 |
=item Linux::AIO::min_parallel $nthreads |
| 47 |
root |
1.1 |
|
| 48 |
root |
1.16 |
Set the minimum number of AIO threads to C<$nthreads>. The default is |
| 49 |
|
|
C<1>, which means a single asynchronous operation can be done at one time |
| 50 |
|
|
(the number of outstanding operations, however, is unlimited). |
| 51 |
root |
1.1 |
|
| 52 |
root |
1.14 |
It is recommended to keep the number of threads low, as many linux |
| 53 |
|
|
kernel versions will scale negatively with the number of threads (higher |
| 54 |
|
|
parallelity => MUCH higher latency). |
| 55 |
|
|
|
| 56 |
root |
1.2 |
=item $fileno = Linux::AIO::poll_fileno |
| 57 |
|
|
|
| 58 |
root |
1.14 |
Return the I<request result pipe filehandle>. This filehandle must be |
| 59 |
|
|
polled for reading by some mechanism outside this module (e.g. Event |
| 60 |
|
|
or select, see below). If the pipe becomes readable you have to call |
| 61 |
|
|
C<poll_cb> to check the results. |
| 62 |
root |
1.2 |
|
| 63 |
|
|
=item Linux::AIO::poll_cb |
| 64 |
|
|
|
| 65 |
|
|
Process all outstanding events on the result pipe. You have to call this |
| 66 |
root |
1.14 |
regularly. Returns the number of events processed. Returns immediately |
| 67 |
|
|
when no events are outstanding. |
| 68 |
root |
1.2 |
|
| 69 |
root |
1.3 |
You can use Event to multiplex, e.g.: |
| 70 |
|
|
|
| 71 |
root |
1.14 |
Event->io (fd => Linux::AIO::poll_fileno, |
| 72 |
|
|
poll => 'r', async => 1, |
| 73 |
|
|
cb => \&Linux::AIO::poll_cb ); |
| 74 |
root |
1.3 |
|
| 75 |
|
|
|
| 76 |
root |
1.2 |
=item Linux::AIO::nreqs |
| 77 |
|
|
|
| 78 |
|
|
Returns the number of requests currently outstanding. |
| 79 |
root |
1.4 |
|
| 80 |
root |
1.14 |
=item aio_open $pathname, $flags, $mode, $callback |
| 81 |
root |
1.10 |
|
| 82 |
|
|
Asynchronously open or create a file and call the callback with the |
| 83 |
root |
1.14 |
filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
| 84 |
|
|
might change in the future). |
| 85 |
root |
1.10 |
|
| 86 |
root |
1.14 |
=item aio_close $fh, $callback |
| 87 |
root |
1.10 |
|
| 88 |
|
|
Asynchronously close a file and call the callback with the result code. |
| 89 |
|
|
|
| 90 |
root |
1.14 |
=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
| 91 |
root |
1.9 |
|
| 92 |
root |
1.14 |
=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
| 93 |
root |
1.4 |
|
| 94 |
|
|
Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
| 95 |
|
|
into the scalar given by C<data> and offset C<dataoffset> and calls the |
| 96 |
root |
1.14 |
callback without the actual number of bytes read (or C<undef> on error). |
| 97 |
root |
1.4 |
|
| 98 |
root |
1.14 |
=item aio_stat $fh_or_path, $callback |
| 99 |
root |
1.4 |
|
| 100 |
root |
1.14 |
=item aio_lstat $fh, $callback |
| 101 |
root |
1.5 |
|
| 102 |
root |
1.14 |
Works like perl's C<stat> or C<lstat> in void context. The callback will |
| 103 |
|
|
be called after the stat and the results will be available using C<stat _> |
| 104 |
|
|
or C<-s _> etc... |
| 105 |
root |
1.5 |
|
| 106 |
root |
1.10 |
Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
| 107 |
|
|
error when stat'ing a large file, the results will be silently truncated |
| 108 |
|
|
unless perl itself is compiled with large file support. |
| 109 |
root |
1.17 |
|
| 110 |
|
|
=item aio_unlink $pathname, $callback |
| 111 |
|
|
|
| 112 |
root |
1.18 |
Asynchronously unlink a file. |
| 113 |
root |
1.2 |
|
| 114 |
root |
1.1 |
=cut |
| 115 |
root |
1.16 |
|
| 116 |
|
|
min_parallel 1; |
| 117 |
root |
1.2 |
|
| 118 |
|
|
END { |
| 119 |
|
|
max_parallel 0; |
| 120 |
|
|
} |
| 121 |
root |
1.1 |
|
| 122 |
|
|
1; |
| 123 |
|
|
|
| 124 |
|
|
=back |
| 125 |
|
|
|
| 126 |
|
|
=head1 BUGS |
| 127 |
|
|
|
| 128 |
root |
1.14 |
This module has been extensively tested in a large and very busy webserver |
| 129 |
|
|
for many years now. |
| 130 |
root |
1.1 |
|
| 131 |
root |
1.14 |
- aio_open gives a fd, but all other functions expect a perl filehandle. |
| 132 |
root |
1.6 |
|
| 133 |
root |
1.1 |
=head1 SEE ALSO |
| 134 |
|
|
|
| 135 |
|
|
L<Coro>. |
| 136 |
|
|
|
| 137 |
|
|
=head1 AUTHOR |
| 138 |
|
|
|
| 139 |
root |
1.22 |
Marc Lehmann <schmorp@schmorp.de> |
| 140 |
root |
1.20 |
http://home.schmorp.de/ |
| 141 |
root |
1.1 |
|
| 142 |
|
|
=cut |
| 143 |
|
|
|