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