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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines