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.27 |
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 (even as of 2.6.12, only O_DIRECT |
14 |
|
|
files are supported) and even if, it would only allow aio_read and write, |
15 |
|
|
not open, stat and so on. |
16 |
root |
1.14 |
|
17 |
|
|
Instead, in this module a number of (non-posix) threads are started that |
18 |
|
|
execute your read/writes and signal their completion. You don't need |
19 |
|
|
thread support in your libc or perl, and the threads created by this |
20 |
|
|
module will not be visible to the pthreads library. |
21 |
root |
1.3 |
|
22 |
root |
1.17 |
NOTICE: the threads created by this module will automatically be killed |
23 |
|
|
when the thread calling min_parallel exits. Make sure you only ever call |
24 |
|
|
min_parallel from the same thread that loaded this module. |
25 |
|
|
|
26 |
root |
1.27 |
Although the module will work with in the presence of other threads, it is |
27 |
|
|
not reentrant, so use appropriate locking yourself. |
28 |
root |
1.16 |
|
29 |
root |
1.30 |
=head2 API NOTES |
30 |
|
|
|
31 |
|
|
All the C<aio_*> calls are more or less thin wrappers around the syscall |
32 |
|
|
with the same name (sans C<aio_>). The arguments are similar or identical, |
33 |
|
|
and they all accept an additional C<$callback> argument which must be |
34 |
|
|
a code reference. This code reference will get called with the syscall |
35 |
|
|
return code (e.g. most syscalls return C<-1> on error, unlike perl, which |
36 |
|
|
usually delivers "false") as it's sole argument when the given syscall has |
37 |
|
|
been executed asynchronously. |
38 |
|
|
|
39 |
|
|
All functions that expect a filehandle will also accept a file descriptor. |
40 |
|
|
|
41 |
root |
1.32 |
The filenames you pass to these routines I<must> be absolute. The reason |
42 |
|
|
is that at the time the request is being executed, the current working |
43 |
|
|
directory could have changed. Alternatively, you can make sure that you |
44 |
root |
1.33 |
never change the current working directory. |
45 |
root |
1.32 |
|
46 |
root |
1.1 |
=over 4 |
47 |
|
|
|
48 |
|
|
=cut |
49 |
|
|
|
50 |
|
|
package Linux::AIO; |
51 |
|
|
|
52 |
root |
1.2 |
use base 'Exporter'; |
53 |
|
|
|
54 |
root |
1.1 |
BEGIN { |
55 |
root |
1.31 |
$VERSION = 1.71; |
56 |
root |
1.1 |
|
57 |
root |
1.29 |
@EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink |
58 |
|
|
aio_fsync aio_fdatasync aio_readahead); |
59 |
root |
1.2 |
@EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
60 |
|
|
|
61 |
root |
1.1 |
require XSLoader; |
62 |
|
|
XSLoader::load Linux::AIO, $VERSION; |
63 |
|
|
} |
64 |
|
|
|
65 |
root |
1.14 |
=item Linux::AIO::min_parallel $nthreads |
66 |
root |
1.1 |
|
67 |
root |
1.16 |
Set the minimum number of AIO threads to C<$nthreads>. The default is |
68 |
|
|
C<1>, which means a single asynchronous operation can be done at one time |
69 |
|
|
(the number of outstanding operations, however, is unlimited). |
70 |
root |
1.1 |
|
71 |
root |
1.24 |
It is recommended to keep the number of threads low, as some linux |
72 |
root |
1.14 |
kernel versions will scale negatively with the number of threads (higher |
73 |
|
|
parallelity => MUCH higher latency). |
74 |
|
|
|
75 |
root |
1.30 |
Under normal circumstances you don't need to call this function, as this |
76 |
|
|
module automatically starts a single async thread. |
77 |
|
|
|
78 |
root |
1.24 |
=item Linux::AIO::max_parallel $nthreads |
79 |
|
|
|
80 |
|
|
Sets the maximum number of AIO threads to C<$nthreads>. If more than |
81 |
|
|
the specified number of threads are currently running, kill them. This |
82 |
|
|
function blocks until the limit is reached. |
83 |
|
|
|
84 |
|
|
This module automatically runs C<max_parallel 0> at program end, to ensure |
85 |
|
|
that all threads are killed and that there are no outstanding requests. |
86 |
|
|
|
87 |
root |
1.30 |
Under normal circumstances you don't need to call this function. |
88 |
|
|
|
89 |
root |
1.2 |
=item $fileno = Linux::AIO::poll_fileno |
90 |
|
|
|
91 |
root |
1.14 |
Return the I<request result pipe filehandle>. This filehandle must be |
92 |
|
|
polled for reading by some mechanism outside this module (e.g. Event |
93 |
|
|
or select, see below). If the pipe becomes readable you have to call |
94 |
|
|
C<poll_cb> to check the results. |
95 |
root |
1.2 |
|
96 |
root |
1.30 |
See C<poll_cb> for an example. |
97 |
|
|
|
98 |
root |
1.2 |
=item Linux::AIO::poll_cb |
99 |
|
|
|
100 |
|
|
Process all outstanding events on the result pipe. You have to call this |
101 |
root |
1.14 |
regularly. Returns the number of events processed. Returns immediately |
102 |
|
|
when no events are outstanding. |
103 |
root |
1.2 |
|
104 |
root |
1.3 |
You can use Event to multiplex, e.g.: |
105 |
|
|
|
106 |
root |
1.14 |
Event->io (fd => Linux::AIO::poll_fileno, |
107 |
|
|
poll => 'r', async => 1, |
108 |
root |
1.30 |
cb => \&Linux::AIO::poll_cb); |
109 |
root |
1.3 |
|
110 |
root |
1.24 |
=item Linux::AIO::poll_wait |
111 |
|
|
|
112 |
|
|
Wait till the result filehandle becomes ready for reading (simply does a |
113 |
|
|
select on the filehandle. This is useful if you want to synchronously wait |
114 |
|
|
for some requests to finish). |
115 |
|
|
|
116 |
root |
1.30 |
See C<nreqs> for an example. |
117 |
|
|
|
118 |
root |
1.2 |
=item Linux::AIO::nreqs |
119 |
|
|
|
120 |
|
|
Returns the number of requests currently outstanding. |
121 |
root |
1.4 |
|
122 |
root |
1.30 |
Example: wait till there are no outstanding requests anymore: |
123 |
|
|
|
124 |
root |
1.32 |
Linux::AIO::poll_wait, Linux::AIO::poll_cb |
125 |
|
|
while Linux::AIO::nreqs; |
126 |
root |
1.30 |
|
127 |
|
|
=item aio_open $pathname, $flags, $mode, $callback |
128 |
root |
1.10 |
|
129 |
|
|
Asynchronously open or create a file and call the callback with the |
130 |
root |
1.14 |
filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this |
131 |
|
|
might change in the future). |
132 |
root |
1.10 |
|
133 |
root |
1.34 |
The pathname passed to C<aio_open> must be absolute. See API NOTES, above, |
134 |
|
|
for an explanation. |
135 |
|
|
|
136 |
root |
1.30 |
The C<$mode> argument is a bitmask. See the C<Fcntl> module for a |
137 |
|
|
list. They are the same as used in C<sysopen>. |
138 |
|
|
|
139 |
|
|
Example: |
140 |
|
|
|
141 |
|
|
aio_open "/etc/passwd", O_RDONLY, 0, sub { |
142 |
|
|
if ($_[0] >= 0) { |
143 |
|
|
open my $fh, "<&$_[0]"; # create a copy for perl |
144 |
|
|
aio_close $_[0], sub { }; # close the aio handle |
145 |
|
|
print "open successful, fh is $fh\n"; |
146 |
|
|
... |
147 |
|
|
} else { |
148 |
|
|
die "open failed: $!\n"; |
149 |
|
|
} |
150 |
|
|
}; |
151 |
|
|
|
152 |
root |
1.14 |
=item aio_close $fh, $callback |
153 |
root |
1.10 |
|
154 |
|
|
Asynchronously close a file and call the callback with the result code. |
155 |
|
|
|
156 |
root |
1.14 |
=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback |
157 |
root |
1.9 |
|
158 |
root |
1.14 |
=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback |
159 |
root |
1.4 |
|
160 |
|
|
Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
161 |
|
|
into the scalar given by C<data> and offset C<dataoffset> and calls the |
162 |
root |
1.30 |
callback without the actual number of bytes read (or -1 on error, just |
163 |
|
|
like the syscall). |
164 |
|
|
|
165 |
|
|
Example: Read 15 bytes at offset 7 into scalar C<$buffer>, strating at |
166 |
|
|
offset C<0> within the scalar: |
167 |
|
|
|
168 |
|
|
aio_read $fh, 7, 15, $buffer, 0, sub { |
169 |
|
|
$_[0] >= 0 or die "read error: $!"; |
170 |
|
|
print "read <$buffer>\n"; |
171 |
|
|
}; |
172 |
|
|
|
173 |
|
|
=item aio_readahead $fh,$offset,$length, $callback |
174 |
|
|
|
175 |
|
|
Asynchronously reads the specified byte range into the page cache, using |
176 |
|
|
the C<readahead> syscall. |
177 |
|
|
|
178 |
|
|
readahead() populates the page cache with data from a file so that |
179 |
|
|
subsequent reads from that file will not block on disk I/O. The C<$offset> |
180 |
|
|
argument specifies the starting point from which data is to be read and |
181 |
|
|
C<$length> specifies the number of bytes to be read. I/O is performed in |
182 |
|
|
whole pages, so that offset is effectively rounded down to a page boundary |
183 |
|
|
and bytes are read up to the next page boundary greater than or equal to |
184 |
|
|
(off-set+length). aio_readahead() does not read beyond the end of the |
185 |
|
|
file. The current file offset of the file is left unchanged. |
186 |
root |
1.4 |
|
187 |
root |
1.14 |
=item aio_stat $fh_or_path, $callback |
188 |
root |
1.4 |
|
189 |
root |
1.14 |
=item aio_lstat $fh, $callback |
190 |
root |
1.5 |
|
191 |
root |
1.14 |
Works like perl's C<stat> or C<lstat> in void context. The callback will |
192 |
|
|
be called after the stat and the results will be available using C<stat _> |
193 |
|
|
or C<-s _> etc... |
194 |
root |
1.5 |
|
195 |
root |
1.32 |
The pathname passed to C<aio_stat> must be absolute. See API NOTES, above, |
196 |
|
|
for an explanation. |
197 |
|
|
|
198 |
root |
1.10 |
Currently, the stats are always 64-bit-stats, i.e. instead of returning an |
199 |
|
|
error when stat'ing a large file, the results will be silently truncated |
200 |
|
|
unless perl itself is compiled with large file support. |
201 |
root |
1.17 |
|
202 |
root |
1.30 |
Example: Print the length of F</etc/passwd>: |
203 |
|
|
|
204 |
|
|
aio_stat "/etc/passwd", sub { |
205 |
|
|
$_[0] and die "stat failed: $!"; |
206 |
|
|
print "size is ", -s _, "\n"; |
207 |
|
|
}; |
208 |
|
|
|
209 |
|
|
=item aio_unlink $pathname, $callback |
210 |
|
|
|
211 |
|
|
Asynchronously unlink (delete) a file and call the callback with the |
212 |
|
|
result code. |
213 |
|
|
|
214 |
|
|
=item aio_fsync $fh, $callback |
215 |
|
|
|
216 |
|
|
Asynchronously call fsync on the given filehandle and call the callback |
217 |
|
|
with the fsync result code. |
218 |
|
|
|
219 |
|
|
=item aio_fdatasync $fh, $callback |
220 |
root |
1.17 |
|
221 |
root |
1.30 |
Asynchronously call fdatasync on the given filehandle and call the |
222 |
|
|
callback with the fdatasync result code. |
223 |
root |
1.2 |
|
224 |
root |
1.1 |
=cut |
225 |
root |
1.16 |
|
226 |
|
|
min_parallel 1; |
227 |
root |
1.2 |
|
228 |
|
|
END { |
229 |
|
|
max_parallel 0; |
230 |
|
|
} |
231 |
root |
1.1 |
|
232 |
|
|
1; |
233 |
|
|
|
234 |
|
|
=back |
235 |
|
|
|
236 |
|
|
=head1 BUGS |
237 |
|
|
|
238 |
root |
1.14 |
This module has been extensively tested in a large and very busy webserver |
239 |
|
|
for many years now. |
240 |
root |
1.1 |
|
241 |
root |
1.14 |
- aio_open gives a fd, but all other functions expect a perl filehandle. |
242 |
root |
1.6 |
|
243 |
root |
1.1 |
=head1 SEE ALSO |
244 |
|
|
|
245 |
root |
1.34 |
L<Coro>, L<IO::AIO>. |
246 |
root |
1.1 |
|
247 |
|
|
=head1 AUTHOR |
248 |
|
|
|
249 |
root |
1.22 |
Marc Lehmann <schmorp@schmorp.de> |
250 |
root |
1.20 |
http://home.schmorp.de/ |
251 |
root |
1.1 |
|
252 |
|
|
=cut |
253 |
|
|
|