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.20 by root, Wed Jan 12 20:37:11 2005 UTC vs.
Revision 1.36 by root, Wed Aug 17 16:57:53 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines