… | |
… | |
21 | remaining functionality would have to be implemented using threads anyway. |
21 | remaining functionality would have to be implemented using threads anyway. |
22 | |
22 | |
23 | Although the module will work with in the presence of other threads, it is |
23 | Although the module will work with in the presence of other threads, it is |
24 | currently not reentrant, so use appropriate locking yourself. |
24 | currently not reentrant, so use appropriate locking yourself. |
25 | |
25 | |
26 | =head2 API NOTES |
26 | =cut |
|
|
27 | |
|
|
28 | package IO::AIO; |
|
|
29 | |
|
|
30 | use base 'Exporter'; |
|
|
31 | |
|
|
32 | use Fcntl (); |
|
|
33 | |
|
|
34 | BEGIN { |
|
|
35 | $VERSION = 0.2; |
|
|
36 | |
|
|
37 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink |
|
|
38 | aio_fsync aio_fdatasync aio_readahead); |
|
|
39 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs); |
|
|
40 | |
|
|
41 | require XSLoader; |
|
|
42 | XSLoader::load IO::AIO, $VERSION; |
|
|
43 | } |
|
|
44 | |
|
|
45 | =head1 FUNCTIONS |
|
|
46 | |
|
|
47 | =head2 AIO FUNCTIONS |
27 | |
48 | |
28 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
49 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
29 | with the same name (sans C<aio_>). The arguments are similar or identical, |
50 | with the same name (sans C<aio_>). The arguments are similar or identical, |
30 | and they all accept an additional C<$callback> argument which must be |
51 | and they all accept an additional C<$callback> argument which must be |
31 | a code reference. This code reference will get called with the syscall |
52 | a code reference. This code reference will get called with the syscall |
… | |
… | |
39 | is that at the time the request is being executed, the current working |
60 | is that at the time the request is being executed, the current working |
40 | directory could have changed. Alternatively, you can make sure that you |
61 | directory could have changed. Alternatively, you can make sure that you |
41 | never change the current working directory. |
62 | never change the current working directory. |
42 | |
63 | |
43 | =over 4 |
64 | =over 4 |
44 | |
|
|
45 | =cut |
|
|
46 | |
|
|
47 | package IO::AIO; |
|
|
48 | |
|
|
49 | use base 'Exporter'; |
|
|
50 | |
|
|
51 | use Fcntl (); |
|
|
52 | |
|
|
53 | BEGIN { |
|
|
54 | $VERSION = 0.2; |
|
|
55 | |
|
|
56 | @EXPORT = qw(aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink |
|
|
57 | aio_fsync aio_fdatasync aio_readahead); |
|
|
58 | @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel nreqs); |
|
|
59 | |
|
|
60 | require XSLoader; |
|
|
61 | XSLoader::load IO::AIO, $VERSION; |
|
|
62 | } |
|
|
63 | |
|
|
64 | =item IO::AIO::min_parallel $nthreads |
|
|
65 | |
|
|
66 | Set the minimum number of AIO threads to C<$nthreads>. The default is |
|
|
67 | C<1>, which means a single asynchronous operation can be done at one time |
|
|
68 | (the number of outstanding operations, however, is unlimited). |
|
|
69 | |
|
|
70 | It is recommended to keep the number of threads low, as some linux |
|
|
71 | kernel versions will scale negatively with the number of threads (higher |
|
|
72 | parallelity => MUCH higher latency). |
|
|
73 | |
|
|
74 | Under normal circumstances you don't need to call this function, as this |
|
|
75 | module automatically starts a single async thread. |
|
|
76 | |
|
|
77 | =item IO::AIO::max_parallel $nthreads |
|
|
78 | |
|
|
79 | Sets the maximum number of AIO threads to C<$nthreads>. If more than |
|
|
80 | the specified number of threads are currently running, kill them. This |
|
|
81 | function blocks until the limit is reached. |
|
|
82 | |
|
|
83 | This module automatically runs C<max_parallel 0> at program end, to ensure |
|
|
84 | that all threads are killed and that there are no outstanding requests. |
|
|
85 | |
|
|
86 | Under normal circumstances you don't need to call this function. |
|
|
87 | |
|
|
88 | =item $fileno = IO::AIO::poll_fileno |
|
|
89 | |
|
|
90 | Return the I<request result pipe filehandle>. This filehandle must be |
|
|
91 | polled for reading by some mechanism outside this module (e.g. Event |
|
|
92 | or select, see below). If the pipe becomes readable you have to call |
|
|
93 | C<poll_cb> to check the results. |
|
|
94 | |
|
|
95 | See C<poll_cb> for an example. |
|
|
96 | |
|
|
97 | =item IO::AIO::poll_cb |
|
|
98 | |
|
|
99 | Process all outstanding events on the result pipe. You have to call this |
|
|
100 | regularly. Returns the number of events processed. Returns immediately |
|
|
101 | when no events are outstanding. |
|
|
102 | |
|
|
103 | You can use Event to multiplex, e.g.: |
|
|
104 | |
|
|
105 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
106 | poll => 'r', async => 1, |
|
|
107 | cb => \&IO::AIO::poll_cb); |
|
|
108 | |
|
|
109 | =item IO::AIO::poll_wait |
|
|
110 | |
|
|
111 | Wait till the result filehandle becomes ready for reading (simply does a |
|
|
112 | select on the filehandle. This is useful if you want to synchronously wait |
|
|
113 | for some requests to finish). |
|
|
114 | |
|
|
115 | See C<nreqs> for an example. |
|
|
116 | |
|
|
117 | =item IO::AIO::nreqs |
|
|
118 | |
|
|
119 | Returns the number of requests currently outstanding. |
|
|
120 | |
|
|
121 | Example: wait till there are no outstanding requests anymore: |
|
|
122 | |
|
|
123 | IO::AIO::poll_wait, IO::AIO::poll_cb |
|
|
124 | while IO::AIO::nreqs; |
|
|
125 | |
65 | |
126 | =item aio_open $pathname, $flags, $mode, $callback |
66 | =item aio_open $pathname, $flags, $mode, $callback |
127 | |
67 | |
128 | Asynchronously open or create a file and call the callback with a newly |
68 | Asynchronously open or create a file and call the callback with a newly |
129 | created filehandle for the file. |
69 | created filehandle for the file. |
… | |
… | |
220 | =item aio_fdatasync $fh, $callback |
160 | =item aio_fdatasync $fh, $callback |
221 | |
161 | |
222 | Asynchronously call fdatasync on the given filehandle and call the |
162 | Asynchronously call fdatasync on the given filehandle and call the |
223 | callback with the fdatasync result code. |
163 | callback with the fdatasync result code. |
224 | |
164 | |
|
|
165 | =back |
|
|
166 | |
|
|
167 | =head2 SUPPORT FUNCTIONS |
|
|
168 | |
|
|
169 | =over 4 |
|
|
170 | |
|
|
171 | =item $fileno = IO::AIO::poll_fileno |
|
|
172 | |
|
|
173 | Return the I<request result pipe filehandle>. This filehandle must be |
|
|
174 | polled for reading by some mechanism outside this module (e.g. Event |
|
|
175 | or select, see below). If the pipe becomes readable you have to call |
|
|
176 | C<poll_cb> to check the results. |
|
|
177 | |
|
|
178 | See C<poll_cb> for an example. |
|
|
179 | |
|
|
180 | =item IO::AIO::poll_cb |
|
|
181 | |
|
|
182 | Process all outstanding events on the result pipe. You have to call this |
|
|
183 | regularly. Returns the number of events processed. Returns immediately |
|
|
184 | when no events are outstanding. |
|
|
185 | |
|
|
186 | You can use Event to multiplex, e.g.: |
|
|
187 | |
|
|
188 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
189 | poll => 'r', async => 1, |
|
|
190 | cb => \&IO::AIO::poll_cb); |
|
|
191 | |
|
|
192 | =item IO::AIO::poll_wait |
|
|
193 | |
|
|
194 | Wait till the result filehandle becomes ready for reading (simply does a |
|
|
195 | select on the filehandle. This is useful if you want to synchronously wait |
|
|
196 | for some requests to finish). |
|
|
197 | |
|
|
198 | See C<nreqs> for an example. |
|
|
199 | |
|
|
200 | =item IO::AIO::nreqs |
|
|
201 | |
|
|
202 | Returns the number of requests currently outstanding. |
|
|
203 | |
|
|
204 | Example: wait till there are no outstanding requests anymore: |
|
|
205 | |
|
|
206 | IO::AIO::poll_wait, IO::AIO::poll_cb |
|
|
207 | while IO::AIO::nreqs; |
|
|
208 | |
|
|
209 | =item IO::AIO::min_parallel $nthreads |
|
|
210 | |
|
|
211 | Set the minimum number of AIO threads to C<$nthreads>. The default is |
|
|
212 | C<1>, which means a single asynchronous operation can be done at one time |
|
|
213 | (the number of outstanding operations, however, is unlimited). |
|
|
214 | |
|
|
215 | It is recommended to keep the number of threads low, as some Linux |
|
|
216 | kernel versions will scale negatively with the number of threads (higher |
|
|
217 | parallelity => MUCH higher latency). With current Linux 2.6 versions, 4-32 |
|
|
218 | threads should be fine. |
|
|
219 | |
|
|
220 | Under normal circumstances you don't need to call this function, as this |
|
|
221 | module automatically starts some threads (the exact number might change, |
|
|
222 | and is currently 4). |
|
|
223 | |
|
|
224 | =item IO::AIO::max_parallel $nthreads |
|
|
225 | |
|
|
226 | Sets the maximum number of AIO threads to C<$nthreads>. If more than |
|
|
227 | the specified number of threads are currently running, kill them. This |
|
|
228 | function blocks until the limit is reached. |
|
|
229 | |
|
|
230 | This module automatically runs C<max_parallel 0> at program end, to ensure |
|
|
231 | that all threads are killed and that there are no outstanding requests. |
|
|
232 | |
|
|
233 | Under normal circumstances you don't need to call this function. |
|
|
234 | |
|
|
235 | =item $oldnreqs = IO::AIO::max_outstanding $nreqs |
|
|
236 | |
|
|
237 | Sets the maximum number of outstanding requests to C<$nreqs>. If you |
|
|
238 | try to queue up more than this number of requests, the caller will block until |
|
|
239 | some requests have been handled. |
|
|
240 | |
|
|
241 | The default is very large, so normally there is no practical limit. If you |
|
|
242 | queue up many requests in a loop it it often improves speed if you set |
|
|
243 | this to a relatively low number, such as C<100>. |
|
|
244 | |
|
|
245 | Under normal circumstances you don't need to call this function. |
|
|
246 | |
|
|
247 | =back |
|
|
248 | |
225 | =cut |
249 | =cut |
226 | |
250 | |
227 | # support function to convert a fd into a perl filehandle |
251 | # support function to convert a fd into a perl filehandle |
228 | sub _fd2fh { |
252 | sub _fd2fh { |
229 | return undef if $_[0] < 0; |
253 | return undef if $_[0] < 0; |
… | |
… | |
242 | max_parallel 0; |
266 | max_parallel 0; |
243 | } |
267 | } |
244 | |
268 | |
245 | 1; |
269 | 1; |
246 | |
270 | |
247 | =back |
|
|
248 | |
|
|
249 | =head1 BUGS |
|
|
250 | |
|
|
251 | - could be optimized to use more semaphores instead of filehandles. |
|
|
252 | |
|
|
253 | =head1 SEE ALSO |
271 | =head1 SEE ALSO |
254 | |
272 | |
255 | L<Coro>, L<Linux::AIO>. |
273 | L<Coro>, L<Linux::AIO>. |
256 | |
274 | |
257 | =head1 AUTHOR |
275 | =head1 AUTHOR |