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.14 by root, Wed May 5 13:54:37 2004 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
11=over 4 21=over 4
12 22
13=cut 23=cut
14 24
15package Linux::AIO; 25package Linux::AIO;
16 26
17use base 'Exporter'; 27use base 'Exporter';
18 28
19BEGIN { 29BEGIN {
20 $VERSION = 0.001; 30 $VERSION = '1.0';
21 31
22 @EXPORT = qw(aio_read aio_write); 32 @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat);
23 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); 33 @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs);
24 34
25 require XSLoader; 35 require XSLoader;
26 XSLoader::load Linux::AIO, $VERSION; 36 XSLoader::load Linux::AIO, $VERSION;
27} 37}
28 38
29=item Linux::AIO::min_parallel($nthreads) 39=item Linux::AIO::min_parallel $nthreads
30 40
31Set the minimum number of AIO threads to $nthreads. 41Set the minimum number of AIO threads to C<$nthreads>. You I<have> to call
42this function with a positive number at least once, otherwise no threads
43will be started and you aio-operations will seem to hang.
32 44
33=cut 45It is recommended to keep the number of threads low, as many linux
34 46kernel versions will scale negatively with the number of threads (higher
35=item aio_read($fh,$offset,$length, $data,$dataoffset,$callback) 47parallelity => MUCH higher latency).
36aio_write($fh,$offset,$length, $data,$dataoffset,$callback)
37
38Reads or writes C<length> bytes from the specified C<fh> and C<offset>
39into the scalar given by C<data> and offset C<dataoffset> and calls the
40callback without the actual number of bytes read (or undef on error).
41 48
42=item $fileno = Linux::AIO::poll_fileno 49=item $fileno = Linux::AIO::poll_fileno
43 50
44Return the request result pipe filehandle. This filehandle must be polled 51Return the I<request result pipe filehandle>. This filehandle must be
45for reading. If the pipe becomes readable you have to call C<poll_cb>. 52polled for reading by some mechanism outside this module (e.g. Event
53or select, see below). If the pipe becomes readable you have to call
54C<poll_cb> to check the results.
46 55
47=item Linux::AIO::poll_cb 56=item Linux::AIO::poll_cb
48 57
49Process all outstanding events on the result pipe. You have to call this 58Process all outstanding events on the result pipe. You have to call this
50regularly. Returns the number of events processed. 59regularly. Returns the number of events processed. Returns immediately
60when no events are outstanding.
61
62You can use Event to multiplex, e.g.:
63
64 Event->io (fd => Linux::AIO::poll_fileno,
65 poll => 'r', async => 1,
66 cb => \&Linux::AIO::poll_cb );
67
51 68
52=item Linux::AIO::nreqs 69=item Linux::AIO::nreqs
53 70
54Returns the number of requests currently outstanding. 71Returns the number of requests currently outstanding.
72
73=item aio_open $pathname, $flags, $mode, $callback
74
75Asynchronously open or create a file and call the callback with the
76filedescriptor (NOT a perl filehandle, sorry for that, but watch out, this
77might change in the future).
78
79=item aio_close $fh, $callback
80
81Asynchronously close a file and call the callback with the result code.
82
83=item aio_read $fh,$offset,$length, $data,$dataoffset,$callback
84
85=item aio_write $fh,$offset,$length, $data,$dataoffset,$callback
86
87Reads or writes C<length> bytes from the specified C<fh> and C<offset>
88into the scalar given by C<data> and offset C<dataoffset> and calls the
89callback without the actual number of bytes read (or C<undef> on error).
90
91=item aio_stat $fh_or_path, $callback
92
93=item aio_lstat $fh, $callback
94
95Works like perl's C<stat> or C<lstat> in void context. The callback will
96be called after the stat and the results will be available using C<stat _>
97or C<-s _> etc...
98
99Currently, the stats are always 64-bit-stats, i.e. instead of returning an
100error when stat'ing a large file, the results will be silently truncated
101unless perl itself is compiled with large file support.
55 102
56=cut 103=cut
57 104
58END { 105END {
59 max_parallel 0; 106 max_parallel 0;
63 110
64=back 111=back
65 112
66=head1 BUGS 113=head1 BUGS
67 114
68This module has not yet been extensively tested. Watch out! 115This module has been extensively tested in a large and very busy webserver
116for many years now.
69 117
70This module does not use the aio_* posix functions because a) linux does 118 - 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 119
74=head1 SEE ALSO 120=head1 SEE ALSO
75 121
76L<Coro>. 122L<Coro>.
77 123

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines