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.2 by root, Tue Aug 14 18:06:37 2001 UTC vs.
Revision 1.26 by root, Thu Jul 7 23:17:23 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 asynchronous i/o using the means available to linux
12- 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
14aio_read and write, not open and stat).
15
16Instead, in this module a number of (non-posix) threads are started that
17execute your read/writes and signal their completion. You don't need
18thread support in your libc or perl, and the threads created by this
19module will not be visible to the pthreads library.
20
21NOTICE: the threads created by this module will automatically be killed
22when the thread calling min_parallel exits. Make sure you only ever call
23min_parallel from the same thread that loaded this module.
24
25Although the module will work with threads, it is not reentrant, so use
26appropriate locking yourself.
27
11=over 4 28=over 4
12 29
13=cut 30=cut
14 31
15package Linux::AIO; 32package Linux::AIO;
16 33
17use base 'Exporter'; 34use base 'Exporter';
18 35
19BEGIN { 36BEGIN {
20 $VERSION = 0.001; 37 $VERSION = 1.6;
21 38
22 @EXPORT = qw(aio_read aio_write); 39 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink);
23 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); 40 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs);
24 41
25 require XSLoader; 42 require XSLoader;
26 XSLoader::load Linux::AIO, $VERSION; 43 XSLoader::load Linux::AIO, $VERSION;
27} 44}
28 45
29=item Linux::AIO::min_parallel($nthreads) 46=item Linux::AIO::min_parallel $nthreads
30 47
31Set the minimum number of AIO threads to $nthreads. 48Set 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
50(the number of outstanding operations, however, is unlimited).
32 51
33=cut 52It is recommended to keep the number of threads low, as some linux
53kernel versions will scale negatively with the number of threads (higher
54parallelity => MUCH higher latency).
34 55
35=item aio_read($fh,$offset,$length, $data,$dataoffset,$callback) 56=item Linux::AIO::max_parallel $nthreads
36aio_write($fh,$offset,$length, $data,$dataoffset,$callback)
37 57
38Reads or writes C<length> bytes from the specified C<fh> and C<offset> 58Sets the maximum number of AIO threads to C<$nthreads>. If more than
39into the scalar given by C<data> and offset C<dataoffset> and calls the 59the specified number of threads are currently running, kill them. This
40callback without the actual number of bytes read (or undef on error). 60function blocks until the limit is reached.
61
62This module automatically runs C<max_parallel 0> at program end, to ensure
63that all threads are killed and that there are no outstanding requests.
41 64
42=item $fileno = Linux::AIO::poll_fileno 65=item $fileno = Linux::AIO::poll_fileno
43 66
44Return the request result pipe filehandle. This filehandle must be polled 67Return the I<request result pipe filehandle>. This filehandle must be
45for reading. If the pipe becomes readable you have to call C<poll_cb>. 68polled for reading by some mechanism outside this module (e.g. Event
69or select, see below). If the pipe becomes readable you have to call
70C<poll_cb> to check the results.
46 71
47=item Linux::AIO::poll_cb 72=item Linux::AIO::poll_cb
48 73
49Process all outstanding events on the result pipe. You have to call this 74Process all outstanding events on the result pipe. You have to call this
50regularly. Returns the number of events processed. 75regularly. Returns the number of events processed. Returns immediately
76when no events are outstanding.
77
78You can use Event to multiplex, e.g.:
79
80 Event->io (fd => Linux::AIO::poll_fileno,
81 poll => 'r', async => 1,
82 cb => \&Linux::AIO::poll_cb );
83
84=item Linux::AIO::poll_wait
85
86Wait till the result filehandle becomes ready for reading (simply does a
87select on the filehandle. This is useful if you want to synchronously wait
88for some requests to finish).
51 89
52=item Linux::AIO::nreqs 90=item Linux::AIO::nreqs
53 91
54Returns the number of requests currently outstanding. 92Returns the number of requests currently outstanding.
55 93
94=item aio_open $pathname, $flags, $mode, $callback
95
96Asynchronously open or create a file and call the callback with the
97filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this
98might change in the future).
99
100=item aio_close $fh, $callback
101
102Asynchronously close a file and call the callback with the result code.
103
104=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback
105
106=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback
107
108Reads or writes C<length> bytes from the specified C<fh> and C<offset>
109into the scalar given by C<data> and offset C<dataoffset> and calls the
110callback without the actual number of bytes read (or C<undef> on error).
111
112=item aio_stat $fh_or_path, $callback
113
114=item aio_lstat $fh, $callback
115
116Works like perl's C<stat> or C<lstat> in void context. The callback will
117be called after the stat and the results will be available using C<stat _>
118or C<-s _> etc...
119
120Currently, the stats are always 64-bit-stats, i.e. instead of returning an
121error when stat'ing a large file, the results will be silently truncated
122unless perl itself is compiled with large file support.
123
124=item aio_unlink $pathname, $callback
125
126Asynchronously unlink a file.
127
56=cut 128=cut
129
130min_parallel 1;
57 131
58END { 132END {
59 max_parallel 0; 133 max_parallel 0;
60} 134}
61 135
63 137
64=back 138=back
65 139
66=head1 BUGS 140=head1 BUGS
67 141
68This module has not yet been extensively tested. Watch out! 142This module has been extensively tested in a large and very busy webserver
143for many years now.
69 144
70This module does not use the aio_* posix functions because a) linux does 145 - aio_open gives a fd, but all other functions expect a perl filehandle.
71not have aio, b) the existing aio_* functions use pthreads, which are too
72buggy for my usage.
73 146
74=head1 SEE ALSO 147=head1 SEE ALSO
75 148
76L<Coro>. 149L<Coro>.
77 150
78=head1 AUTHOR 151=head1 AUTHOR
79 152
80 Marc Lehmann <pcg@goof.com> 153 Marc Lehmann <schmorp@schmorp.de>
81 http://www.goof.com/pcg/marc/ 154 http://home.schmorp.de/
82 155
83=cut 156=cut
84 157

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines