… | |
… | |
61 | etc.), but can also be used to easily do operations in parallel that are |
61 | etc.), but can also be used to easily do operations in parallel that are |
62 | normally done sequentially, e.g. stat'ing many files, which is much faster |
62 | normally done sequentially, e.g. stat'ing many files, which is much faster |
63 | on a RAID volume or over NFS when you do a number of stat operations |
63 | on a RAID volume or over NFS when you do a number of stat operations |
64 | concurrently. |
64 | concurrently. |
65 | |
65 | |
66 | While this works on all types of file descriptors (for example sockets), |
66 | While most of this works on all types of file descriptors (for example |
67 | using these functions on file descriptors that support nonblocking |
67 | sockets), using these functions on file descriptors that support |
68 | operation (again, sockets, pipes etc.) is very inefficient. Use an event |
68 | nonblocking operation (again, sockets, pipes etc.) is very inefficient or |
|
|
69 | might not work (aio_read fails on sockets/pipes/fifos). Use an event loop |
69 | loop for that (such as the L<Event|Event> module): IO::AIO will naturally |
70 | for that (such as the L<Event|Event> module): IO::AIO will naturally fit |
70 | fit into such an event loop itself. |
71 | into such an event loop itself. |
71 | |
72 | |
72 | In this version, a number of threads are started that execute your |
73 | In this version, a number of threads are started that execute your |
73 | requests and signal their completion. You don't need thread support |
74 | requests and signal their completion. You don't need thread support |
74 | in perl, and the threads created by this module will not be visible |
75 | in perl, and the threads created by this module will not be visible |
75 | to perl. In the future, this module might make use of the native aio |
76 | to perl. In the future, this module might make use of the native aio |
… | |
… | |
176 | Request has reached the end of its lifetime and holds no resources anymore |
177 | Request has reached the end of its lifetime and holds no resources anymore |
177 | (except possibly for the Perl object, but its connection to the actual |
178 | (except possibly for the Perl object, but its connection to the actual |
178 | aio request is severed and calling its methods will either do nothing or |
179 | aio request is severed and calling its methods will either do nothing or |
179 | result in a runtime error). |
180 | result in a runtime error). |
180 | |
181 | |
|
|
182 | =back |
|
|
183 | |
181 | =cut |
184 | =cut |
182 | |
185 | |
183 | package IO::AIO; |
186 | package IO::AIO; |
184 | |
187 | |
185 | no warnings; |
188 | no warnings; |
186 | use strict 'vars'; |
189 | use strict 'vars'; |
187 | |
190 | |
188 | use base 'Exporter'; |
191 | use base 'Exporter'; |
189 | |
192 | |
190 | BEGIN { |
193 | BEGIN { |
191 | our $VERSION = '2.1'; |
194 | our $VERSION = '2.2'; |
192 | |
195 | |
193 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
196 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
194 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
197 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
195 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
198 | aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link |
196 | aio_copy aio_group aio_nop aio_mknod); |
199 | aio_move aio_copy aio_group aio_nop aio_mknod); |
197 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
200 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
198 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
201 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
199 | min_parallel max_parallel max_idle |
202 | min_parallel max_parallel max_idle |
200 | nreqs nready npending nthreads |
203 | nreqs nready npending nthreads |
201 | max_poll_time max_poll_reqs); |
204 | max_poll_time max_poll_reqs); |
… | |
… | |
206 | XSLoader::load ("IO::AIO", $VERSION); |
209 | XSLoader::load ("IO::AIO", $VERSION); |
207 | } |
210 | } |
208 | |
211 | |
209 | =head1 FUNCTIONS |
212 | =head1 FUNCTIONS |
210 | |
213 | |
211 | =head2 AIO FUNCTIONS |
214 | =head2 AIO REQUEST FUNCTIONS |
212 | |
215 | |
213 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
216 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
214 | with the same name (sans C<aio_>). The arguments are similar or identical, |
217 | with the same name (sans C<aio_>). The arguments are similar or identical, |
215 | and they all accept an additional (and optional) C<$callback> argument |
218 | and they all accept an additional (and optional) C<$callback> argument |
216 | which must be a code reference. This code reference will get called with |
219 | which must be a code reference. This code reference will get called with |
… | |
… | |
219 | syscall has been executed asynchronously. |
222 | syscall has been executed asynchronously. |
220 | |
223 | |
221 | All functions expecting a filehandle keep a copy of the filehandle |
224 | All functions expecting a filehandle keep a copy of the filehandle |
222 | internally until the request has finished. |
225 | internally until the request has finished. |
223 | |
226 | |
224 | All requests return objects of type L<IO::AIO::REQ> that allow further |
227 | All functions return request objects of type L<IO::AIO::REQ> that allow |
225 | manipulation of those requests while they are in-flight. |
228 | further manipulation of those requests while they are in-flight. |
226 | |
229 | |
227 | The pathnames you pass to these routines I<must> be absolute and |
230 | The pathnames you pass to these routines I<must> be absolute and |
228 | encoded in byte form. The reason for the former is that at the time the |
231 | encoded as octets. The reason for the former is that at the time the |
229 | request is being executed, the current working directory could have |
232 | request is being executed, the current working directory could have |
230 | changed. Alternatively, you can make sure that you never change the |
233 | changed. Alternatively, you can make sure that you never change the |
231 | current working directory. |
234 | current working directory anywhere in the program and then use relative |
|
|
235 | paths. |
232 | |
236 | |
233 | To encode pathnames to byte form, either make sure you either: a) |
237 | To encode pathnames as octets, either make sure you either: a) always pass |
234 | always pass in filenames you got from outside (command line, readdir |
238 | in filenames you got from outside (command line, readdir etc.) without |
235 | etc.), b) are ASCII or ISO 8859-1, c) use the Encode module and encode |
239 | tinkering, b) are ASCII or ISO 8859-1, c) use the Encode module and encode |
236 | your pathnames to the locale (or other) encoding in effect in the user |
240 | your pathnames to the locale (or other) encoding in effect in the user |
237 | environment, d) use Glib::filename_from_unicode on unicode filenames or e) |
241 | environment, d) use Glib::filename_from_unicode on unicode filenames or e) |
238 | use something else. |
242 | use something else to ensure your scalar has the correct contents. |
|
|
243 | |
|
|
244 | This works, btw. independent of the internal UTF-8 bit, which IO::AIO |
|
|
245 | handles correctly wether it is set or not. |
239 | |
246 | |
240 | =over 4 |
247 | =over 4 |
241 | |
248 | |
242 | =item $prev_pri = aioreq_pri [$pri] |
249 | =item $prev_pri = aioreq_pri [$pri] |
243 | |
250 | |
… | |
… | |
266 | }; |
273 | }; |
267 | |
274 | |
268 | =item aioreq_nice $pri_adjust |
275 | =item aioreq_nice $pri_adjust |
269 | |
276 | |
270 | Similar to C<aioreq_pri>, but subtracts the given value from the current |
277 | Similar to C<aioreq_pri>, but subtracts the given value from the current |
271 | priority, so effects are cumulative. |
278 | priority, so the effect is cumulative. |
272 | |
279 | |
273 | =item aio_open $pathname, $flags, $mode, $callback->($fh) |
280 | =item aio_open $pathname, $flags, $mode, $callback->($fh) |
274 | |
281 | |
275 | Asynchronously open or create a file and call the callback with a newly |
282 | Asynchronously open or create a file and call the callback with a newly |
276 | created filehandle for the file. |
283 | created filehandle for the file. |
… | |
… | |
410 | |
417 | |
411 | =item aio_symlink $srcpath, $dstpath, $callback->($status) |
418 | =item aio_symlink $srcpath, $dstpath, $callback->($status) |
412 | |
419 | |
413 | Asynchronously create a new symbolic link to the existing object at C<$srcpath> at |
420 | Asynchronously create a new symbolic link to the existing object at C<$srcpath> at |
414 | the path C<$dstpath> and call the callback with the result code. |
421 | the path C<$dstpath> and call the callback with the result code. |
|
|
422 | |
|
|
423 | =item aio_readlink $path, $callback->($link) |
|
|
424 | |
|
|
425 | Asynchronously read the symlink specified by C<$path> and pass it to |
|
|
426 | the callback. If an error occurs, nothing or undef gets passed to the |
|
|
427 | callback. |
415 | |
428 | |
416 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
429 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
417 | |
430 | |
418 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
431 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
419 | rename(2) and call the callback with the result code. |
432 | rename(2) and call the callback with the result code. |
… | |
… | |
933 | that are being processed by C<IO::AIO::poll_cb> in one call, respectively |
946 | that are being processed by C<IO::AIO::poll_cb> in one call, respectively |
934 | the maximum amount of time (default C<0>, meaning infinity) spent in |
947 | the maximum amount of time (default C<0>, meaning infinity) spent in |
935 | C<IO::AIO::poll_cb> to process requests (more correctly the mininum amount |
948 | C<IO::AIO::poll_cb> to process requests (more correctly the mininum amount |
936 | of time C<poll_cb> is allowed to use). |
949 | of time C<poll_cb> is allowed to use). |
937 | |
950 | |
|
|
951 | Setting C<max_poll_time> to a non-zero value creates an overhead of one |
|
|
952 | syscall per request processed, which is not normally a problem unless your |
|
|
953 | callbacks are really really fast or your OS is really really slow (I am |
|
|
954 | not mentioning Solaris here). Using C<max_poll_reqs> incurs no overhead. |
|
|
955 | |
938 | Setting these is useful if you want to ensure some level of |
956 | Setting these is useful if you want to ensure some level of |
939 | interactiveness when perl is not fast enough to process all requests in |
957 | interactiveness when perl is not fast enough to process all requests in |
940 | time. |
958 | time. |
941 | |
959 | |
942 | For interactive programs, values such as C<0.01> to C<0.1> should be fine. |
960 | For interactive programs, values such as C<0.01> to C<0.1> should be fine. |
943 | |
961 | |
944 | Example: Install an Event watcher that automatically calls |
962 | Example: Install an Event watcher that automatically calls |
945 | IO::AIO::poll_some with low priority, to ensure that other parts of the |
963 | IO::AIO::poll_cb with low priority, to ensure that other parts of the |
946 | program get the CPU sometimes even under high AIO load. |
964 | program get the CPU sometimes even under high AIO load. |
947 | |
965 | |
948 | # try not to spend much more than 0.1s in poll_cb |
966 | # try not to spend much more than 0.1s in poll_cb |
949 | IO::AIO::max_poll_time 0.1; |
967 | IO::AIO::max_poll_time 0.1; |
950 | |
968 | |