ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.pm
(Generate patch)

Comparing Linux-AIO/AIO.pm (file contents):
Revision 1.12 by root, Sat May 18 21:48:36 2002 UTC vs.
Revision 1.35 by root, Tue Jul 12 10:51:09 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines