ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.pm
Revision: 1.4
Committed: Sun Jul 10 20:57:00 2005 UTC (18 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-0_2
Changes since 1.3: +18 -4 lines
Log Message:
*** empty log message ***

File Contents

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