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 |
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 |
|
21 |
Although the module will work with threads, it is not reentrant, so use |
22 |
appropriate locking yourself. |
23 |
|
24 |
=over 4 |
25 |
|
26 |
=cut |
27 |
|
28 |
package Linux::AIO; |
29 |
|
30 |
use base 'Exporter'; |
31 |
|
32 |
BEGIN { |
33 |
$VERSION = 1.1; |
34 |
|
35 |
@EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat); |
36 |
@EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
37 |
|
38 |
require XSLoader; |
39 |
XSLoader::load Linux::AIO, $VERSION; |
40 |
} |
41 |
|
42 |
=item Linux::AIO::min_parallel $nthreads |
43 |
|
44 |
Set the minimum number of AIO threads to C<$nthreads>. The default is |
45 |
C<1>, which means a single asynchronous operation can be done at one time |
46 |
(the number of outstanding operations, however, is unlimited). |
47 |
|
48 |
It is recommended to keep the number of threads low, as many linux |
49 |
kernel versions will scale negatively with the number of threads (higher |
50 |
parallelity => MUCH higher latency). |
51 |
|
52 |
=item $fileno = Linux::AIO::poll_fileno |
53 |
|
54 |
Return the I<request result pipe filehandle>. This filehandle must be |
55 |
polled for reading by some mechanism outside this module (e.g. Event |
56 |
or select, see below). If the pipe becomes readable you have to call |
57 |
C<poll_cb> to check the results. |
58 |
|
59 |
=item Linux::AIO::poll_cb |
60 |
|
61 |
Process all outstanding events on the result pipe. You have to call this |
62 |
regularly. Returns the number of events processed. Returns immediately |
63 |
when no events are outstanding. |
64 |
|
65 |
You can use Event to multiplex, e.g.: |
66 |
|
67 |
Event->io (fd => Linux::AIO::poll_fileno, |
68 |
poll => 'r', async => 1, |
69 |
cb => \&Linux::AIO::poll_cb ); |
70 |
|
71 |
|
72 |
=item Linux::AIO::nreqs |
73 |
|
74 |
Returns the number of requests currently outstanding. |
75 |
|
76 |
=item aio_open $pathname, $flags, $mode, $callback |
77 |
|
78 |
Asynchronously open or create a file and call the callback with the |
79 |
filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
80 |
might change in the future). |
81 |
|
82 |
=item aio_close $fh, $callback |
83 |
|
84 |
Asynchronously close a file and call the callback with the result code. |
85 |
|
86 |
=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
87 |
|
88 |
=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
89 |
|
90 |
Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
91 |
into the scalar given by C<data> and offset C<dataoffset> and calls the |
92 |
callback without the actual number of bytes read (or C<undef> on error). |
93 |
|
94 |
=item aio_stat $fh_or_path, $callback |
95 |
|
96 |
=item aio_lstat $fh, $callback |
97 |
|
98 |
Works like perl's C<stat> or C<lstat> in void context. The callback will |
99 |
be called after the stat and the results will be available using C<stat _> |
100 |
or C<-s _> etc... |
101 |
|
102 |
Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
103 |
error when stat'ing a large file, the results will be silently truncated |
104 |
unless perl itself is compiled with large file support. |
105 |
|
106 |
=cut |
107 |
|
108 |
min_parallel 1; |
109 |
|
110 |
END { |
111 |
max_parallel 0; |
112 |
} |
113 |
|
114 |
1; |
115 |
|
116 |
=back |
117 |
|
118 |
=head1 BUGS |
119 |
|
120 |
This module has been extensively tested in a large and very busy webserver |
121 |
for many years now. |
122 |
|
123 |
- aio_open gives a fd, but all other functions expect a perl filehandle. |
124 |
|
125 |
=head1 SEE ALSO |
126 |
|
127 |
L<Coro>. |
128 |
|
129 |
=head1 AUTHOR |
130 |
|
131 |
Marc Lehmann <pcg@goof.com> |
132 |
http://www.goof.com/pcg/marc/ |
133 |
|
134 |
=cut |
135 |
|