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