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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines