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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines