… | |
… | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use IO::AIO; |
7 | use IO::AIO; |
8 | |
8 | |
9 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
9 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
10 | my ($fh) = @_; |
10 | my $fh = shift |
|
|
11 | or die "/etc/passwd: $!"; |
11 | ... |
12 | ... |
12 | }; |
13 | }; |
13 | |
14 | |
14 | aio_unlink "/tmp/file", sub { }; |
15 | aio_unlink "/tmp/file", sub { }; |
15 | |
16 | |
… | |
… | |
61 | etc.), but can also be used to easily do operations in parallel that are |
62 | 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 |
63 | 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 |
64 | on a RAID volume or over NFS when you do a number of stat operations |
64 | concurrently. |
65 | concurrently. |
65 | |
66 | |
66 | While this works on all types of file descriptors (for example sockets), |
67 | While most of this works on all types of file descriptors (for example |
67 | using these functions on file descriptors that support nonblocking |
68 | sockets), using these functions on file descriptors that support |
68 | operation (again, sockets, pipes etc.) is very inefficient. Use an event |
69 | nonblocking operation (again, sockets, pipes etc.) is very inefficient or |
|
|
70 | 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 |
71 | for that (such as the L<Event|Event> module): IO::AIO will naturally fit |
70 | fit into such an event loop itself. |
72 | into such an event loop itself. |
71 | |
73 | |
72 | In this version, a number of threads are started that execute your |
74 | In this version, a number of threads are started that execute your |
73 | requests and signal their completion. You don't need thread support |
75 | 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 |
76 | 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 |
77 | to perl. In the future, this module might make use of the native aio |
… | |
… | |
98 | poll => 'r', |
100 | poll => 'r', |
99 | cb => \&IO::AIO::poll_cb); |
101 | cb => \&IO::AIO::poll_cb); |
100 | |
102 | |
101 | # queue the request to open /etc/passwd |
103 | # queue the request to open /etc/passwd |
102 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
104 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
103 | my $fh = $_[0] |
105 | my $fh = shift |
104 | or die "error while opening: $!"; |
106 | or die "error while opening: $!"; |
105 | |
107 | |
106 | # stat'ing filehandles is generally non-blocking |
108 | # stat'ing filehandles is generally non-blocking |
107 | my $size = -s $fh; |
109 | my $size = -s $fh; |
108 | |
110 | |
… | |
… | |
176 | Request has reached the end of its lifetime and holds no resources anymore |
178 | 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 |
179 | (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 |
180 | aio request is severed and calling its methods will either do nothing or |
179 | result in a runtime error). |
181 | result in a runtime error). |
180 | |
182 | |
|
|
183 | =back |
|
|
184 | |
181 | =cut |
185 | =cut |
182 | |
186 | |
183 | package IO::AIO; |
187 | package IO::AIO; |
184 | |
188 | |
185 | no warnings; |
189 | no warnings; |
186 | use strict 'vars'; |
190 | use strict 'vars'; |
187 | |
191 | |
188 | use base 'Exporter'; |
192 | use base 'Exporter'; |
189 | |
193 | |
190 | BEGIN { |
194 | BEGIN { |
191 | our $VERSION = '2.1'; |
195 | our $VERSION = '2.2'; |
192 | |
196 | |
193 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
197 | 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 |
198 | 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 |
199 | aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link |
196 | aio_copy aio_group aio_nop aio_mknod); |
200 | aio_move aio_copy aio_group aio_nop aio_mknod); |
197 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
201 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
198 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
202 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
199 | min_parallel max_parallel max_idle |
203 | min_parallel max_parallel max_idle |
200 | nreqs nready npending nthreads |
204 | nreqs nready npending nthreads |
201 | max_poll_time max_poll_reqs); |
205 | max_poll_time max_poll_reqs); |
… | |
… | |
206 | XSLoader::load ("IO::AIO", $VERSION); |
210 | XSLoader::load ("IO::AIO", $VERSION); |
207 | } |
211 | } |
208 | |
212 | |
209 | =head1 FUNCTIONS |
213 | =head1 FUNCTIONS |
210 | |
214 | |
211 | =head2 AIO FUNCTIONS |
215 | =head2 AIO REQUEST FUNCTIONS |
212 | |
216 | |
213 | All the C<aio_*> calls are more or less thin wrappers around the syscall |
217 | 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, |
218 | 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 |
219 | 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 |
220 | which must be a code reference. This code reference will get called with |
… | |
… | |
219 | syscall has been executed asynchronously. |
223 | syscall has been executed asynchronously. |
220 | |
224 | |
221 | All functions expecting a filehandle keep a copy of the filehandle |
225 | All functions expecting a filehandle keep a copy of the filehandle |
222 | internally until the request has finished. |
226 | internally until the request has finished. |
223 | |
227 | |
224 | All requests return objects of type L<IO::AIO::REQ> that allow further |
228 | All functions return request objects of type L<IO::AIO::REQ> that allow |
225 | manipulation of those requests while they are in-flight. |
229 | further manipulation of those requests while they are in-flight. |
226 | |
230 | |
227 | The pathnames you pass to these routines I<must> be absolute and |
231 | 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 |
232 | 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 |
233 | request is being executed, the current working directory could have |
230 | changed. Alternatively, you can make sure that you never change the |
234 | changed. Alternatively, you can make sure that you never change the |
231 | current working directory. |
235 | current working directory anywhere in the program and then use relative |
|
|
236 | paths. |
232 | |
237 | |
233 | To encode pathnames to byte form, either make sure you either: a) |
238 | 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 |
239 | 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 |
240 | 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 |
241 | 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) |
242 | environment, d) use Glib::filename_from_unicode on unicode filenames or e) |
238 | use something else. |
243 | use something else to ensure your scalar has the correct contents. |
|
|
244 | |
|
|
245 | This works, btw. independent of the internal UTF-8 bit, which IO::AIO |
|
|
246 | handles correctly wether it is set or not. |
239 | |
247 | |
240 | =over 4 |
248 | =over 4 |
241 | |
249 | |
242 | =item $prev_pri = aioreq_pri [$pri] |
250 | =item $prev_pri = aioreq_pri [$pri] |
243 | |
251 | |
… | |
… | |
266 | }; |
274 | }; |
267 | |
275 | |
268 | =item aioreq_nice $pri_adjust |
276 | =item aioreq_nice $pri_adjust |
269 | |
277 | |
270 | Similar to C<aioreq_pri>, but subtracts the given value from the current |
278 | Similar to C<aioreq_pri>, but subtracts the given value from the current |
271 | priority, so effects are cumulative. |
279 | priority, so the effect is cumulative. |
272 | |
280 | |
273 | =item aio_open $pathname, $flags, $mode, $callback->($fh) |
281 | =item aio_open $pathname, $flags, $mode, $callback->($fh) |
274 | |
282 | |
275 | Asynchronously open or create a file and call the callback with a newly |
283 | Asynchronously open or create a file and call the callback with a newly |
276 | created filehandle for the file. |
284 | created filehandle for the file. |
… | |
… | |
410 | |
418 | |
411 | =item aio_symlink $srcpath, $dstpath, $callback->($status) |
419 | =item aio_symlink $srcpath, $dstpath, $callback->($status) |
412 | |
420 | |
413 | Asynchronously create a new symbolic link to the existing object at C<$srcpath> at |
421 | 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. |
422 | the path C<$dstpath> and call the callback with the result code. |
|
|
423 | |
|
|
424 | =item aio_readlink $path, $callback->($link) |
|
|
425 | |
|
|
426 | Asynchronously read the symlink specified by C<$path> and pass it to |
|
|
427 | the callback. If an error occurs, nothing or undef gets passed to the |
|
|
428 | callback. |
415 | |
429 | |
416 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
430 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
417 | |
431 | |
418 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
432 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
419 | rename(2) and call the callback with the result code. |
433 | 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 |
947 | 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 |
948 | 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 |
949 | C<IO::AIO::poll_cb> to process requests (more correctly the mininum amount |
936 | of time C<poll_cb> is allowed to use). |
950 | of time C<poll_cb> is allowed to use). |
937 | |
951 | |
|
|
952 | Setting C<max_poll_time> to a non-zero value creates an overhead of one |
|
|
953 | syscall per request processed, which is not normally a problem unless your |
|
|
954 | callbacks are really really fast or your OS is really really slow (I am |
|
|
955 | not mentioning Solaris here). Using C<max_poll_reqs> incurs no overhead. |
|
|
956 | |
938 | Setting these is useful if you want to ensure some level of |
957 | 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 |
958 | interactiveness when perl is not fast enough to process all requests in |
940 | time. |
959 | time. |
941 | |
960 | |
942 | For interactive programs, values such as C<0.01> to C<0.1> should be fine. |
961 | For interactive programs, values such as C<0.01> to C<0.1> should be fine. |
943 | |
962 | |
944 | Example: Install an Event watcher that automatically calls |
963 | Example: Install an Event watcher that automatically calls |
945 | IO::AIO::poll_some with low priority, to ensure that other parts of the |
964 | 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. |
965 | program get the CPU sometimes even under high AIO load. |
947 | |
966 | |
948 | # try not to spend much more than 0.1s in poll_cb |
967 | # try not to spend much more than 0.1s in poll_cb |
949 | IO::AIO::max_poll_time 0.1; |
968 | IO::AIO::max_poll_time 0.1; |
950 | |
969 | |
… | |
… | |
953 | poll => 'r', nice => 1, |
972 | poll => 'r', nice => 1, |
954 | cb => &IO::AIO::poll_cb); |
973 | cb => &IO::AIO::poll_cb); |
955 | |
974 | |
956 | =item IO::AIO::poll_wait |
975 | =item IO::AIO::poll_wait |
957 | |
976 | |
|
|
977 | If there are any outstanding requests and none of them in the result |
958 | Wait till the result filehandle becomes ready for reading (simply does a |
978 | phase, wait till the result filehandle becomes ready for reading (simply |
959 | C<select> on the filehandle. This is useful if you want to synchronously |
979 | does a C<select> on the filehandle. This is useful if you want to |
960 | wait for some requests to finish). |
980 | synchronously wait for some requests to finish). |
961 | |
981 | |
962 | See C<nreqs> for an example. |
982 | See C<nreqs> for an example. |
963 | |
983 | |
964 | =item IO::AIO::poll |
984 | =item IO::AIO::poll |
965 | |
985 | |
966 | Waits until some requests have been handled. |
986 | Waits until some requests have been handled. |
967 | |
987 | |
|
|
988 | Returns the number of requests processed, but is otherwise strictly |
968 | Strictly equivalent to: |
989 | equivalent to: |
969 | |
990 | |
970 | IO::AIO::poll_wait, IO::AIO::poll_cb |
991 | IO::AIO::poll_wait, IO::AIO::poll_cb |
971 | if IO::AIO::nreqs; |
|
|
972 | |
992 | |
973 | =item IO::AIO::flush |
993 | =item IO::AIO::flush |
974 | |
994 | |
975 | Wait till all outstanding AIO requests have been handled. |
995 | Wait till all outstanding AIO requests have been handled. |
976 | |
996 | |