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.1 by root, Sun Jul 10 17:07:44 2005 UTC vs.
Revision 1.5 by root, Sun Jul 10 21:04:24 2005 UTC

7 use IO::AIO; 7 use IO::AIO;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module implements asynchronous I/O using whatever means your 11This module implements asynchronous I/O using whatever means your
12operating system supports. Currently, it falls back to Linux::AIO if that 12operating system supports.
13module is available, or uses pthreads to emulato aio functionality.
14 13
15Currently, in this module a number of threads are started that execute 14Currently, a number of threads are started that execute your read/writes
16your read/writes and signal their completion. You don't need thread 15and signal their completion. You don't need thread support in your libc or
17support in your libc or perl, and the threads created by this module will 16perl, and the threads created by this module will not be visible to the
18not be visible to the pthreads library. 17pthreads library. In the future, this module might make use of the native
18aio functions available on many operating systems. However, they are often
19not well-supported (Linux doesn't allow them on normal files currently,
20for example), and they would only support aio_read and aio_write, so the
21remaining functionality would have to be implemented using threads anyway.
19 22
20Although the module will work with in the presence of other threads, it is 23Although the module will work with in the presence of other threads, it is
21not reentrant, so use appropriate locking yourself. 24currently not reentrant, so use appropriate locking yourself.
22 25
23=head2 API NOTES 26=cut
27
28package IO::AIO;
29
30use base 'Exporter';
31
32use Fcntl ();
33
34BEGIN {
35 $VERSION = 0.2;
36
37 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink
38 aio_fsync aio_fdatasync aio_readahead);
39 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs);
40
41 require XSLoader;
42 XSLoader::load IO::AIO, $VERSION;
43}
44
45=head1 FUNCTIONS
46
47=head2 AIO FUNCTIONS
24 48
25All the C<aio_*> calls are more or less thin wrappers around the syscall 49All the C<aio_*> calls are more or less thin wrappers around the syscall
26with the same name (sans C<aio_>). The arguments are similar or identical, 50with the same name (sans C<aio_>). The arguments are similar or identical,
27and they all accept an additional C<$callback> argument which must be 51and they all accept an additional C<$callback> argument which must be
28a code reference. This code reference will get called with the syscall 52a code reference. This code reference will get called with the syscall
37directory could have changed. Alternatively, you can make sure that you 61directory could have changed. Alternatively, you can make sure that you
38never change the current working directory. 62never change the current working directory.
39 63
40=over 4 64=over 4
41 65
42=cut
43
44package IO::AIO;
45
46use base 'Exporter';
47
48BEGIN {
49 $VERSION = 0.1;
50
51 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink
52 aio_fsync aio_fdatasync aio_readahead);
53 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs);
54
55 require XSLoader;
56 XSLoader::load IO::AIO, $VERSION;
57}
58
59=item IO::AIO::min_parallel $nthreads
60
61Set the minimum number of AIO threads to C<$nthreads>. The default is
62C<1>, which means a single asynchronous operation can be done at one time
63(the number of outstanding operations, however, is unlimited).
64
65It is recommended to keep the number of threads low, as some linux
66kernel versions will scale negatively with the number of threads (higher
67parallelity => MUCH higher latency).
68
69Under normal circumstances you don't need to call this function, as this
70module automatically starts a single async thread.
71
72=item IO::AIO::max_parallel $nthreads
73
74Sets the maximum number of AIO threads to C<$nthreads>. If more than
75the specified number of threads are currently running, kill them. This
76function blocks until the limit is reached.
77
78This module automatically runs C<max_parallel 0> at program end, to ensure
79that all threads are killed and that there are no outstanding requests.
80
81Under normal circumstances you don't need to call this function.
82
83=item $fileno = IO::AIO::poll_fileno
84
85Return the I<request result pipe filehandle>. This filehandle must be
86polled for reading by some mechanism outside this module (e.g. Event
87or select, see below). If the pipe becomes readable you have to call
88C<poll_cb> to check the results.
89
90See C<poll_cb> for an example.
91
92=item IO::AIO::poll_cb
93
94Process all outstanding events on the result pipe. You have to call this
95regularly. Returns the number of events processed. Returns immediately
96when no events are outstanding.
97
98You can use Event to multiplex, e.g.:
99
100 Event->io (fd => IO::AIO::poll_fileno,
101 poll => 'r', async => 1,
102 cb => \&IO::AIO::poll_cb);
103
104=item IO::AIO::poll_wait
105
106Wait till the result filehandle becomes ready for reading (simply does a
107select on the filehandle. This is useful if you want to synchronously wait
108for some requests to finish).
109
110See C<nreqs> for an example.
111
112=item IO::AIO::nreqs
113
114Returns the number of requests currently outstanding.
115
116Example: wait till there are no outstanding requests anymore:
117
118 IO::AIO::poll_wait, IO::AIO::poll_cb
119 while IO::AIO::nreqs;
120
121=item aio_open $pathname, $flags, $mode, $callback 66=item aio_open $pathname, $flags, $mode, $callback
122 67
123Asynchronously open or create a file and call the callback with the 68Asynchronously open or create a file and call the callback with a newly
124filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this 69created filehandle for the file.
125might change in the future).
126 70
127The pathname passed to C<aio_open> must be absolute. See API NOTES, above, 71The pathname passed to C<aio_open> must be absolute. See API NOTES, above,
128for an explanation. 72for an explanation.
129 73
130The C<$mode> argument is a bitmask. See the C<Fcntl> module for a 74The C<$mode> argument is a bitmask. See the C<Fcntl> module for a
131list. They are the same as used in C<sysopen>. 75list. They are the same as used in C<sysopen>.
132 76
133Example: 77Example:
134 78
135 aio_open "/etc/passwd", O_RDONLY, 0, sub { 79 aio_open "/etc/passwd", O_RDONLY, 0, sub {
136 if ($_[0] >= 0) { 80 if ($_[0]) {
137 open my $fh, "<&$_[0]"; # create a copy for perl
138 aio_close $_[0], sub { }; # close the aio handle
139 print "open successful, fh is $fh\n"; 81 print "open successful, fh is $_[0]\n";
140 ... 82 ...
141 } else { 83 } else {
142 die "open failed: $!\n"; 84 die "open failed: $!\n";
143 } 85 }
144 }; 86 };
145 87
146=item aio_close $fh, $callback 88=item aio_close $fh, $callback
147 89
148Asynchronously close a file and call the callback with the result code. 90Asynchronously close a file and call the callback with the result
91code. I<WARNING:> although accepted, you should not pass in a perl
92filehandle here, as perl will likely close the file descriptor itself when
93the filehandle is destroyed. Normally, you can safely call perls C<close>
94or just let filehandles go out of scope.
149 95
150=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback 96=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback
151 97
152=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback 98=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback
153 99
214=item aio_fdatasync $fh, $callback 160=item aio_fdatasync $fh, $callback
215 161
216Asynchronously call fdatasync on the given filehandle and call the 162Asynchronously call fdatasync on the given filehandle and call the
217callback with the fdatasync result code. 163callback with the fdatasync result code.
218 164
165=back
166
167=head2 SUPPORT FUNCTIONS
168
169=over 4
170
171=item $fileno = IO::AIO::poll_fileno
172
173Return the I<request result pipe filehandle>. This filehandle must be
174polled for reading by some mechanism outside this module (e.g. Event
175or select, see below). If the pipe becomes readable you have to call
176C<poll_cb> to check the results.
177
178See C<poll_cb> for an example.
179
180=item IO::AIO::poll_cb
181
182Process all outstanding events on the result pipe. You have to call this
183regularly. Returns the number of events processed. Returns immediately
184when no events are outstanding.
185
186You can use Event to multiplex, e.g.:
187
188 Event->io (fd => IO::AIO::poll_fileno,
189 poll => 'r', async => 1,
190 cb => \&IO::AIO::poll_cb);
191
192=item IO::AIO::poll_wait
193
194Wait till the result filehandle becomes ready for reading (simply does a
195select on the filehandle. This is useful if you want to synchronously wait
196for some requests to finish).
197
198See C<nreqs> for an example.
199
200=item IO::AIO::nreqs
201
202Returns the number of requests currently outstanding.
203
204Example: wait till there are no outstanding requests anymore:
205
206 IO::AIO::poll_wait, IO::AIO::poll_cb
207 while IO::AIO::nreqs;
208
209=item IO::AIO::min_parallel $nthreads
210
211Set the minimum number of AIO threads to C<$nthreads>. The default is
212C<1>, which means a single asynchronous operation can be done at one time
213(the number of outstanding operations, however, is unlimited).
214
215It is recommended to keep the number of threads low, as some Linux
216kernel versions will scale negatively with the number of threads (higher
217parallelity => MUCH higher latency). With current Linux 2.6 versions, 4-32
218threads should be fine.
219
220Under normal circumstances you don't need to call this function, as this
221module automatically starts some threads (the exact number might change,
222and is currently 4).
223
224=item IO::AIO::max_parallel $nthreads
225
226Sets the maximum number of AIO threads to C<$nthreads>. If more than
227the specified number of threads are currently running, kill them. This
228function blocks until the limit is reached.
229
230This module automatically runs C<max_parallel 0> at program end, to ensure
231that all threads are killed and that there are no outstanding requests.
232
233Under normal circumstances you don't need to call this function.
234
235=item $oldnreqs = IO::AIO::max_outstanding $nreqs
236
237Sets the maximum number of outstanding requests to C<$nreqs>. If you
238try to queue up more than this number of requests, the caller will block until
239some requests have been handled.
240
241The default is very large, so normally there is no practical limit. If you
242queue up many requests in a loop it it often improves speed if you set
243this to a relatively low number, such as C<100>.
244
245Under normal circumstances you don't need to call this function.
246
247=back
248
219=cut 249=cut
250
251# support function to convert a fd into a perl filehandle
252sub _fd2fh {
253 return undef if $_[0] < 0;
254
255 # try to be perl5.6-compatible
256 local *AIO_FH;
257 open AIO_FH, "+<&=$_[0]"
258 or return undef;
259
260 *AIO_FH
261}
220 262
221min_parallel 4; 263min_parallel 4;
222 264
223END { 265END {
224 max_parallel 0; 266 max_parallel 0;
225} 267}
226 268
2271; 2691;
228 270
229=back
230
231=head1 BUGS
232
233 - aio_open gives a fd, but all other functions expect a perl filehandle.
234
235=head1 SEE ALSO 271=head1 SEE ALSO
236 272
237L<Coro>, L<Linux::AIO>. 273L<Coro>, L<Linux::AIO>.
238 274
239=head1 AUTHOR 275=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines