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