ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/README
Revision: 1.41
Committed: Sat Jan 2 14:24:32 2010 UTC (14 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-3_4
Changes since 1.40: +33 -3 lines
Log Message:
3.4

File Contents

# User Rev Content
1 root 1.1 NAME
2     IO::AIO - Asynchronous Input/Output
3    
4     SYNOPSIS
5     use IO::AIO;
6    
7 root 1.5 aio_open "/etc/passwd", O_RDONLY, 0, sub {
8 root 1.21 my $fh = shift
9     or die "/etc/passwd: $!";
10 root 1.5 ...
11     };
12    
13     aio_unlink "/tmp/file", sub { };
14    
15     aio_read $fh, 30000, 1024, $buffer, 0, sub {
16     $_[0] > 0 or die "read error: $!";
17     };
18    
19 root 1.18 # version 2+ has request and group objects
20     use IO::AIO 2;
21    
22     aioreq_pri 4; # give next request a very high priority
23     my $req = aio_unlink "/tmp/file", sub { };
24     $req->cancel; # cancel request if still in queue
25    
26     my $grp = aio_group sub { print "all stats done\n" };
27     add $grp aio_stat "..." for ...;
28    
29 root 1.30 # AnyEvent integration (EV, Event, Glib, Tk, POE, urxvt, pureperl...)
30     use AnyEvent::AIO;
31 root 1.14
32 root 1.28 # EV integration
33 root 1.38 my $aio_w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
34 root 1.28
35 root 1.18 # Event integration
36 root 1.5 Event->io (fd => IO::AIO::poll_fileno,
37     poll => 'r',
38     cb => \&IO::AIO::poll_cb);
39    
40 root 1.18 # Glib/Gtk2 integration
41 root 1.5 add_watch Glib::IO IO::AIO::poll_fileno,
42 root 1.7 in => sub { IO::AIO::poll_cb; 1 };
43 root 1.5
44 root 1.18 # Tk integration
45 root 1.5 Tk::Event::IO->fileevent (IO::AIO::poll_fileno, "",
46     readable => \&IO::AIO::poll_cb);
47    
48 root 1.18 # Danga::Socket integration
49 root 1.6 Danga::Socket->AddOtherFds (IO::AIO::poll_fileno =>
50     \&IO::AIO::poll_cb);
51    
52 root 1.1 DESCRIPTION
53     This module implements asynchronous I/O using whatever means your
54 root 1.38 operating system supports. It is implemented as an interface to "libeio"
55     (<http://software.schmorp.de/pkg/libeio.html>).
56 root 1.1
57 root 1.19 Asynchronous means that operations that can normally block your program
58     (e.g. reading from disk) will be done asynchronously: the operation will
59     still block, but you can do something else in the meantime. This is
60     extremely useful for programs that need to stay interactive even when
61     doing heavy I/O (GUI programs, high performance network servers etc.),
62     but can also be used to easily do operations in parallel that are
63     normally done sequentially, e.g. stat'ing many files, which is much
64     faster on a RAID volume or over NFS when you do a number of stat
65     operations concurrently.
66    
67 root 1.20 While most of this works on all types of file descriptors (for example
68     sockets), using these functions on file descriptors that support
69 root 1.24 nonblocking operation (again, sockets, pipes etc.) is very inefficient.
70 root 1.38 Use an event loop for that (such as the EV module): IO::AIO will
71 root 1.24 naturally fit into such an event loop itself.
72 root 1.19
73 root 1.18 In this version, a number of threads are started that execute your
74     requests and signal their completion. You don't need thread support in
75     perl, and the threads created by this module will not be visible to
76     perl. In the future, this module might make use of the native aio
77     functions available on many operating systems. However, they are often
78 root 1.19 not well-supported or restricted (GNU/Linux doesn't allow them on normal
79 root 1.18 files currently, for example), and they would only support aio_read and
80 root 1.2 aio_write, so the remaining functionality would have to be implemented
81     using threads anyway.
82 root 1.1
83 root 1.24 Although the module will work in the presence of other (Perl-) threads,
84     it is currently not reentrant in any way, so use appropriate locking
85     yourself, always call "poll_cb" from within the same thread, or never
86     call "poll_cb" (or other "aio_" functions) recursively.
87 root 1.18
88 root 1.19 EXAMPLE
89 root 1.38 This is a simple example that uses the EV module and loads /etc/passwd
90     asynchronously:
91 root 1.19
92     use Fcntl;
93 root 1.38 use EV;
94 root 1.19 use IO::AIO;
95    
96 root 1.38 # register the IO::AIO callback with EV
97     my $aio_w = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
98 root 1.19
99     # queue the request to open /etc/passwd
100     aio_open "/etc/passwd", O_RDONLY, 0, sub {
101 root 1.21 my $fh = shift
102 root 1.19 or die "error while opening: $!";
103    
104     # stat'ing filehandles is generally non-blocking
105     my $size = -s $fh;
106    
107     # queue a request to read the file
108     my $contents;
109     aio_read $fh, 0, $size, $contents, 0, sub {
110     $_[0] == $size
111     or die "short read: $!";
112    
113     close $fh;
114    
115     # file contents now in $contents
116     print $contents;
117    
118     # exit event loop and program
119 root 1.38 EV::unloop;
120 root 1.19 };
121     };
122    
123     # possibly queue up other requests, or open GUI windows,
124     # check for sockets etc. etc.
125    
126     # process events as long as there are some:
127 root 1.38 EV::loop;
128 root 1.19
129 root 1.18 REQUEST ANATOMY AND LIFETIME
130     Every "aio_*" function creates a request. which is a C data structure
131     not directly visible to Perl.
132    
133     If called in non-void context, every request function returns a Perl
134     object representing the request. In void context, nothing is returned,
135     which saves a bit of memory.
136    
137     The perl object is a fairly standard ref-to-hash object. The hash
138     contents are not used by IO::AIO so you are free to store anything you
139     like in it.
140    
141     During their existance, aio requests travel through the following
142     states, in order:
143    
144     ready
145     Immediately after a request is created it is put into the ready
146     state, waiting for a thread to execute it.
147    
148     execute
149     A thread has accepted the request for processing and is currently
150     executing it (e.g. blocking in read).
151    
152     pending
153     The request has been executed and is waiting for result processing.
154    
155     While request submission and execution is fully asynchronous, result
156     processing is not and relies on the perl interpreter calling
157     "poll_cb" (or another function with the same effect).
158    
159     result
160     The request results are processed synchronously by "poll_cb".
161    
162     The "poll_cb" function will process all outstanding aio requests by
163     calling their callbacks, freeing memory associated with them and
164     managing any groups they are contained in.
165    
166     done
167     Request has reached the end of its lifetime and holds no resources
168     anymore (except possibly for the Perl object, but its connection to
169     the actual aio request is severed and calling its methods will
170     either do nothing or result in a runtime error).
171 root 1.1
172 root 1.4 FUNCTIONS
173 root 1.19 AIO REQUEST FUNCTIONS
174 root 1.20 All the "aio_*" calls are more or less thin wrappers around the syscall
175     with the same name (sans "aio_"). The arguments are similar or
176     identical, and they all accept an additional (and optional) $callback
177     argument which must be a code reference. This code reference will get
178     called with the syscall return code (e.g. most syscalls return -1 on
179 root 1.32 error, unlike perl, which usually delivers "false") as its sole argument
180     after the given syscall has been executed asynchronously.
181 root 1.20
182     All functions expecting a filehandle keep a copy of the filehandle
183     internally until the request has finished.
184    
185     All functions return request objects of type IO::AIO::REQ that allow
186     further manipulation of those requests while they are in-flight.
187    
188     The pathnames you pass to these routines *must* be absolute and encoded
189     as octets. The reason for the former is that at the time the request is
190     being executed, the current working directory could have changed.
191     Alternatively, you can make sure that you never change the current
192     working directory anywhere in the program and then use relative paths.
193    
194     To encode pathnames as octets, either make sure you either: a) always
195     pass in filenames you got from outside (command line, readdir etc.)
196     without tinkering, b) are ASCII or ISO 8859-1, c) use the Encode module
197     and encode your pathnames to the locale (or other) encoding in effect in
198     the user environment, d) use Glib::filename_from_unicode on unicode
199     filenames or e) use something else to ensure your scalar has the correct
200     contents.
201    
202     This works, btw. independent of the internal UTF-8 bit, which IO::AIO
203 root 1.32 handles correctly whether it is set or not.
204 root 1.20
205     $prev_pri = aioreq_pri [$pri]
206     Returns the priority value that would be used for the next request
207     and, if $pri is given, sets the priority for the next aio request.
208    
209     The default priority is 0, the minimum and maximum priorities are -4
210     and 4, respectively. Requests with higher priority will be serviced
211     first.
212    
213     The priority will be reset to 0 after each call to one of the
214     "aio_*" functions.
215    
216     Example: open a file with low priority, then read something from it
217     with higher priority so the read request is serviced before other
218     low priority open requests (potentially spamming the cache):
219    
220     aioreq_pri -3;
221     aio_open ..., sub {
222     return unless $_[0];
223    
224     aioreq_pri -2;
225     aio_read $_[0], ..., sub {
226     ...
227     };
228     };
229    
230     aioreq_nice $pri_adjust
231     Similar to "aioreq_pri", but subtracts the given value from the
232     current priority, so the effect is cumulative.
233    
234     aio_open $pathname, $flags, $mode, $callback->($fh)
235     Asynchronously open or create a file and call the callback with a
236     newly created filehandle for the file.
237    
238     The pathname passed to "aio_open" must be absolute. See API NOTES,
239     above, for an explanation.
240    
241     The $flags argument is a bitmask. See the "Fcntl" module for a list.
242     They are the same as used by "sysopen".
243    
244     Likewise, $mode specifies the mode of the newly created file, if it
245     didn't exist and "O_CREAT" has been given, just like perl's
246     "sysopen", except that it is mandatory (i.e. use 0 if you don't
247 root 1.23 create new files, and 0666 or 0777 if you do). Note that the $mode
248     will be modified by the umask in effect then the request is being
249     executed, so better never change the umask.
250 root 1.20
251     Example:
252    
253     aio_open "/etc/passwd", O_RDONLY, 0, sub {
254     if ($_[0]) {
255     print "open successful, fh is $_[0]\n";
256     ...
257     } else {
258     die "open failed: $!\n";
259     }
260     };
261    
262     aio_close $fh, $callback->($status)
263     Asynchronously close a file and call the callback with the result
264 root 1.26 code.
265 root 1.20
266 root 1.27 Unfortunately, you can't do this to perl. Perl *insists* very
267     strongly on closing the file descriptor associated with the
268 root 1.29 filehandle itself.
269 root 1.27
270 root 1.29 Therefore, "aio_close" will not close the filehandle - instead it
271     will use dup2 to overwrite the file descriptor with the write-end of
272     a pipe (the pipe fd will be created on demand and will be cached).
273 root 1.27
274 root 1.29 Or in other words: the file descriptor will be closed, but it will
275     not be free for reuse until the perl filehandle is closed.
276 root 1.20
277     aio_read $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
278     aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval)
279 root 1.35 Reads or writes $length bytes from or to the specified $fh and
280     $offset into the scalar given by $data and offset $dataoffset and
281     calls the callback without the actual number of bytes read (or -1 on
282     error, just like the syscall).
283    
284     "aio_read" will, like "sysread", shrink or grow the $data scalar to
285     offset plus the actual number of bytes read.
286 root 1.24
287 root 1.25 If $offset is undefined, then the current file descriptor offset
288     will be used (and updated), otherwise the file descriptor offset
289     will not be changed by these calls.
290 root 1.24
291     If $length is undefined in "aio_write", use the remaining length of
292     $data.
293    
294     If $dataoffset is less than zero, it will be counted from the end of
295     $data.
296 root 1.20
297     The $data scalar *MUST NOT* be modified in any way while the request
298 root 1.24 is outstanding. Modifying it can result in segfaults or World War
299     III (if the necessary/optional hardware is installed).
300 root 1.20
301     Example: Read 15 bytes at offset 7 into scalar $buffer, starting at
302     offset 0 within the scalar:
303    
304     aio_read $fh, 7, 15, $buffer, 0, sub {
305     $_[0] > 0 or die "read error: $!";
306     print "read $_[0] bytes: <$buffer>\n";
307     };
308    
309     aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval)
310     Tries to copy $length bytes from $in_fh to $out_fh. It starts
311     reading at byte offset $in_offset, and starts writing at the current
312     file offset of $out_fh. Because of that, it is not safe to issue
313     more than one "aio_sendfile" per $out_fh, as they will interfere
314     with each other.
315    
316     This call tries to make use of a native "sendfile" syscall to
317     provide zero-copy operation. For this to work, $out_fh should refer
318     to a socket, and $in_fh should refer to mmap'able file.
319    
320 root 1.41 If a native sendfile cannot be found or it fails with "ENOSYS",
321     "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK",
322     it will be emulated, so you can call "aio_sendfile" on any type of
323     filehandle regardless of the limitations of the operating system.
324 root 1.20
325     Please note, however, that "aio_sendfile" can read more bytes from
326     $in_fh than are written, and there is no way to find out how many
327     bytes have been read from "aio_sendfile" alone, as "aio_sendfile"
328     only provides the number of bytes written to $out_fh. Only if the
329     result value equals $length one can assume that $length bytes have
330     been read.
331    
332     aio_readahead $fh,$offset,$length, $callback->($retval)
333     "aio_readahead" populates the page cache with data from a file so
334     that subsequent reads from that file will not block on disk I/O. The
335     $offset argument specifies the starting point from which data is to
336     be read and $length specifies the number of bytes to be read. I/O is
337     performed in whole pages, so that offset is effectively rounded down
338     to a page boundary and bytes are read up to the next page boundary
339     greater than or equal to (off-set+length). "aio_readahead" does not
340     read beyond the end of the file. The current file offset of the file
341     is left unchanged.
342    
343     If that syscall doesn't exist (likely if your OS isn't Linux) it
344     will be emulated by simply reading the data, which would have a
345     similar effect.
346    
347     aio_stat $fh_or_path, $callback->($status)
348     aio_lstat $fh, $callback->($status)
349     Works like perl's "stat" or "lstat" in void context. The callback
350     will be called after the stat and the results will be available
351     using "stat _" or "-s _" etc...
352    
353     The pathname passed to "aio_stat" must be absolute. See API NOTES,
354     above, for an explanation.
355    
356     Currently, the stats are always 64-bit-stats, i.e. instead of
357     returning an error when stat'ing a large file, the results will be
358     silently truncated unless perl itself is compiled with large file
359     support.
360    
361     Example: Print the length of /etc/passwd:
362    
363     aio_stat "/etc/passwd", sub {
364     $_[0] and die "stat failed: $!";
365     print "size is ", -s _, "\n";
366     };
367    
368 root 1.24 aio_utime $fh_or_path, $atime, $mtime, $callback->($status)
369     Works like perl's "utime" function (including the special case of
370     $atime and $mtime being undef). Fractional times are supported if
371     the underlying syscalls support them.
372    
373     When called with a pathname, uses utimes(2) if available, otherwise
374     utime(2). If called on a file descriptor, uses futimes(2) if
375     available, otherwise returns ENOSYS, so this is not portable.
376    
377     Examples:
378    
379     # set atime and mtime to current time (basically touch(1)):
380     aio_utime "path", undef, undef;
381     # set atime to current time and mtime to beginning of the epoch:
382     aio_utime "path", time, undef; # undef==0
383    
384     aio_chown $fh_or_path, $uid, $gid, $callback->($status)
385     Works like perl's "chown" function, except that "undef" for either
386     $uid or $gid is being interpreted as "do not change" (but -1 can
387     also be used).
388    
389     Examples:
390    
391     # same as "chown root path" in the shell:
392     aio_chown "path", 0, -1;
393     # same as above:
394     aio_chown "path", 0, undef;
395    
396     aio_truncate $fh_or_path, $offset, $callback->($status)
397     Works like truncate(2) or ftruncate(2).
398    
399     aio_chmod $fh_or_path, $mode, $callback->($status)
400     Works like perl's "chmod" function.
401    
402 root 1.20 aio_unlink $pathname, $callback->($status)
403     Asynchronously unlink (delete) a file and call the callback with the
404     result code.
405    
406     aio_mknod $path, $mode, $dev, $callback->($status)
407     [EXPERIMENTAL]
408    
409     Asynchronously create a device node (or fifo). See mknod(2).
410    
411     The only (POSIX-) portable way of calling this function is:
412    
413     aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ...
414    
415     aio_link $srcpath, $dstpath, $callback->($status)
416     Asynchronously create a new link to the existing object at $srcpath
417     at the path $dstpath and call the callback with the result code.
418    
419     aio_symlink $srcpath, $dstpath, $callback->($status)
420     Asynchronously create a new symbolic link to the existing object at
421     $srcpath at the path $dstpath and call the callback with the result
422     code.
423    
424     aio_readlink $path, $callback->($link)
425     Asynchronously read the symlink specified by $path and pass it to
426     the callback. If an error occurs, nothing or undef gets passed to
427     the callback.
428    
429     aio_rename $srcpath, $dstpath, $callback->($status)
430     Asynchronously rename the object at $srcpath to $dstpath, just as
431     rename(2) and call the callback with the result code.
432    
433 root 1.23 aio_mkdir $pathname, $mode, $callback->($status)
434     Asynchronously mkdir (create) a directory and call the callback with
435     the result code. $mode will be modified by the umask at the time the
436     request is executed, so do not change your umask.
437    
438 root 1.20 aio_rmdir $pathname, $callback->($status)
439     Asynchronously rmdir (delete) a directory and call the callback with
440     the result code.
441    
442     aio_readdir $pathname, $callback->($entries)
443     Unlike the POSIX call of the same name, "aio_readdir" reads an
444     entire directory (i.e. opendir + readdir + closedir). The entries
445     will not be sorted, and will NOT include the "." and ".." entries.
446    
447 root 1.36 The callback is passed a single argument which is either "undef" or
448     an array-ref with the filenames.
449    
450     aio_readdirx $pathname, $flags, $callback->($entries, $flags)
451     Quite similar to "aio_readdir", but the $flags argument allows to
452     tune behaviour and output format. In case of an error, $entries will
453     be "undef".
454    
455     The flags are a combination of the following constants, ORed
456     together (the flags will also be passed to the callback, possibly
457     modified):
458    
459     IO::AIO::READDIR_DENTS
460     When this flag is off, then the callback gets an arrayref with
461     of names only (as with "aio_readdir"), otherwise it gets an
462     arrayref with "[$name, $type, $inode]" arrayrefs, each
463     describing a single directory entry in more detail.
464    
465     $name is the name of the entry.
466    
467     $type is one of the "IO::AIO::DT_xxx" constants:
468    
469     "IO::AIO::DT_UNKNOWN", "IO::AIO::DT_FIFO", "IO::AIO::DT_CHR",
470     "IO::AIO::DT_DIR", "IO::AIO::DT_BLK", "IO::AIO::DT_REG",
471     "IO::AIO::DT_LNK", "IO::AIO::DT_SOCK", "IO::AIO::DT_WHT".
472    
473     "IO::AIO::DT_UNKNOWN" means just that: readdir does not know. If
474     you need to know, you have to run stat yourself. Also, for speed
475     reasons, the $type scalars are read-only: you can not modify
476     them.
477    
478     $inode is the inode number (which might not be exact on systems
479 root 1.38 with 64 bit inode numbers and 32 bit perls). This field has
480     unspecified content on systems that do not deliver the inode
481     information.
482 root 1.36
483     IO::AIO::READDIR_DIRS_FIRST
484     When this flag is set, then the names will be returned in an
485     order where likely directories come first. This is useful when
486     you need to quickly find directories, or you want to find all
487     directories while avoiding to stat() each entry.
488    
489     If the system returns type information in readdir, then this is
490     used to find directories directly. Otherwise, likely directories
491     are files beginning with ".", or otherwise files with no dots,
492     of which files with short names are tried first.
493    
494     IO::AIO::READDIR_STAT_ORDER
495     When this flag is set, then the names will be returned in an
496     order suitable for stat()'ing each one. That is, when you plan
497     to stat() all files in the given directory, then the returned
498     order will likely be fastest.
499    
500     If both this flag and "IO::AIO::READDIR_DIRS_FIRST" are
501     specified, then the likely dirs come first, resulting in a less
502     optimal stat order.
503    
504     IO::AIO::READDIR_FOUND_UNKNOWN
505     This flag should not be set when calling "aio_readdirx".
506     Instead, it is being set by "aio_readdirx", when any of the
507     $type's found were "IO::AIO::DT_UNKNOWN". The absense of this
508     flag therefore indicates that all $type's are known, which can
509     be used to speed up some algorithms.
510 root 1.20
511 root 1.22 aio_load $path, $data, $callback->($status)
512     This is a composite request that tries to fully load the given file
513     into memory. Status is the same as with aio_read.
514    
515 root 1.20 aio_copy $srcpath, $dstpath, $callback->($status)
516     Try to copy the *file* (directories not supported as either source
517     or destination) from $srcpath to $dstpath and call the callback with
518 root 1.40 a status of 0 (ok) or -1 (error, see $!).
519 root 1.20
520 root 1.32 This is a composite request that creates the destination file with
521     mode 0200 and copies the contents of the source file into it using
522     "aio_sendfile", followed by restoring atime, mtime, access mode and
523     uid/gid, in that order.
524 root 1.20
525     If an error occurs, the partial destination file will be unlinked,
526     if possible, except when setting atime, mtime, access mode and
527     uid/gid, where errors are being ignored.
528    
529     aio_move $srcpath, $dstpath, $callback->($status)
530     Try to move the *file* (directories not supported as either source
531     or destination) from $srcpath to $dstpath and call the callback with
532 root 1.40 a status of 0 (ok) or -1 (error, see $!).
533 root 1.20
534 root 1.33 This is a composite request that tries to rename(2) the file first;
535     if rename fails with "EXDEV", it copies the file with "aio_copy"
536     and, if that is successful, unlinks the $srcpath.
537 root 1.20
538     aio_scandir $path, $maxreq, $callback->($dirs, $nondirs)
539     Scans a directory (similar to "aio_readdir") but additionally tries
540     to efficiently separate the entries of directory $path into two sets
541     of names, directories you can recurse into (directories), and ones
542     you cannot recurse into (everything else, including symlinks to
543     directories).
544    
545     "aio_scandir" is a composite request that creates of many sub
546     requests_ $maxreq specifies the maximum number of outstanding aio
547     requests that this function generates. If it is "<= 0", then a
548     suitable default will be chosen (currently 4).
549    
550     On error, the callback is called without arguments, otherwise it
551     receives two array-refs with path-relative entry names.
552    
553     Example:
554    
555     aio_scandir $dir, 0, sub {
556     my ($dirs, $nondirs) = @_;
557     print "real directories: @$dirs\n";
558     print "everything else: @$nondirs\n";
559     };
560    
561     Implementation notes.
562    
563     The "aio_readdir" cannot be avoided, but "stat()"'ing every entry
564     can.
565    
566 root 1.36 If readdir returns file type information, then this is used directly
567     to find directories.
568    
569     Otherwise, after reading the directory, the modification time, size
570     etc. of the directory before and after the readdir is checked, and
571     if they match (and isn't the current time), the link count will be
572     used to decide how many entries are directories (if >= 2).
573     Otherwise, no knowledge of the number of subdirectories will be
574     assumed.
575    
576     Then entries will be sorted into likely directories a non-initial
577     dot currently) and likely non-directories (see "aio_readdirx"). Then
578     every entry plus an appended "/." will be "stat"'ed, likely
579     directories first, in order of their inode numbers. If that
580     succeeds, it assumes that the entry is a directory or a symlink to
581     directory (which will be checked seperately). This is often faster
582     than stat'ing the entry itself because filesystems might detect the
583     type of the entry without reading the inode data (e.g. ext2fs
584     filetype feature), even on systems that cannot return the filetype
585     information on readdir.
586 root 1.20
587     If the known number of directories (link count - 2) has been
588     reached, the rest of the entries is assumed to be non-directories.
589    
590     This only works with certainty on POSIX (= UNIX) filesystems, which
591     fortunately are the vast majority of filesystems around.
592    
593     It will also likely work on non-POSIX filesystems with reduced
594     efficiency as those tend to return 0 or 1 as link counts, which
595     disables the directory counting heuristic.
596    
597 root 1.23 aio_rmtree $path, $callback->($status)
598     Delete a directory tree starting (and including) $path, return the
599     status of the final "rmdir" only. This is a composite request that
600     uses "aio_scandir" to recurse into and rmdir directories, and unlink
601     everything else.
602    
603 root 1.28 aio_sync $callback->($status)
604     Asynchronously call sync and call the callback when finished.
605    
606 root 1.20 aio_fsync $fh, $callback->($status)
607     Asynchronously call fsync on the given filehandle and call the
608     callback with the fsync result code.
609    
610     aio_fdatasync $fh, $callback->($status)
611     Asynchronously call fdatasync on the given filehandle and call the
612     callback with the fdatasync result code.
613    
614     If this call isn't available because your OS lacks it or it couldn't
615     be detected, it will be emulated by calling "fsync" instead.
616    
617 root 1.34 aio_sync_file_range $fh, $offset, $nbytes, $flags, $callback->($status)
618     Sync the data portion of the file specified by $offset and $length
619     to disk (but NOT the metadata), by calling the Linux-specific
620     sync_file_range call. If sync_file_range is not available or it
621     returns ENOSYS, then fdatasync or fsync is being substituted.
622    
623     $flags can be a combination of
624     "IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE",
625     "IO::AIO::SYNC_FILE_RANGE_WRITE" and
626     "IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER": refer to the sync_file_range
627     manpage for details.
628    
629 root 1.28 aio_pathsync $path, $callback->($status)
630     This request tries to open, fsync and close the given path. This is
631 root 1.32 a composite request intended to sync directories after directory
632 root 1.28 operations (E.g. rename). This might not work on all operating
633     systems or have any specific effect, but usually it makes sure that
634     directory changes get written to disc. It works for anything that
635     can be opened for read-only, not just directories.
636    
637 root 1.39 Future versions of this function might fall back to other methods
638     when "fsync" on the directory fails (such as calling "sync").
639    
640 root 1.28 Passes 0 when everything went ok, and -1 on error.
641    
642 root 1.41 aio_msync $scalar, $offset = 0, $length = undef, flags = 0,
643     $callback->($status)
644     This is a rather advanced IO::AIO call, which only works on
645     mmap(2)ed scalars (see the Sys::Mmap or Mmap modules for details on
646     this, note that the scalar must only be modified in-place while an
647     aio operation is pending on it).
648    
649     It calls the "msync" function of your OS, if available, with the
650     memory area starting at $offset in the string and ending $length
651     bytes later. If $length is negative, counts from the end, and if
652     $length is "undef", then it goes till the end of the string. The
653     flags can be a combination of "IO::AIO::MS_ASYNC",
654     "IO::AIO::MS_INVALIDATE" and "IO::AIO::MS_SYNC".
655    
656     aio_mtouch $scalar, $offset = 0, $length = undef, flags = 0,
657     $callback->($status)
658     This is a rather advanced IO::AIO call, which works best on
659     mmap(2)ed scalars.
660    
661     It touches (reads or writes) all memory pages in the specified range
662     inside the scalar. All caveats and parameters are the same as for
663     "aio_msync", above, except for flags, which must be either 0 (which
664     reads all pages and ensures they are instantiated) or
665     "IO::AIO::MT_MODIFY", which modifies the memory page s(by reading
666     and writing an octet from it, which dirties the page).
667    
668 root 1.20 aio_group $callback->(...)
669     This is a very special aio request: Instead of doing something, it
670     is a container for other aio requests, which is useful if you want
671     to bundle many requests into a single, composite, request with a
672     definite callback and the ability to cancel the whole request with
673     its subrequests.
674    
675     Returns an object of class IO::AIO::GRP. See its documentation below
676     for more info.
677    
678     Example:
679    
680     my $grp = aio_group sub {
681     print "all stats done\n";
682     };
683    
684     add $grp
685     (aio_stat ...),
686     (aio_stat ...),
687     ...;
688    
689     aio_nop $callback->()
690     This is a special request - it does nothing in itself and is only
691     used for side effects, such as when you want to add a dummy request
692     to a group so that finishing the requests in the group depends on
693     executing the given code.
694    
695     While this request does nothing, it still goes through the execution
696     phase and still requires a worker thread. Thus, the callback will
697     not be executed immediately but only after other requests in the
698     queue have entered their execution phase. This can be used to
699     measure request latency.
700    
701     IO::AIO::aio_busy $fractional_seconds, $callback->() *NOT EXPORTED*
702     Mainly used for debugging and benchmarking, this aio request puts
703     one of the request workers to sleep for the given time.
704    
705     While it is theoretically handy to have simple I/O scheduling
706     requests like sleep and file handle readable/writable, the overhead
707     this creates is immense (it blocks a thread for a long time) so do
708     not use this function except to put your application under
709     artificial I/O pressure.
710 root 1.18
711     IO::AIO::REQ CLASS
712 root 1.20 All non-aggregate "aio_*" functions return an object of this class when
713     called in non-void context.
714 root 1.18
715 root 1.20 cancel $req
716     Cancels the request, if possible. Has the effect of skipping
717     execution when entering the execute state and skipping calling the
718     callback when entering the the result state, but will leave the
719 root 1.37 request otherwise untouched (with the exception of readdir). That
720     means that requests that currently execute will not be stopped and
721     resources held by the request will not be freed prematurely.
722 root 1.18
723 root 1.20 cb $req $callback->(...)
724     Replace (or simply set) the callback registered to the request.
725 root 1.18
726     IO::AIO::GRP CLASS
727 root 1.20 This class is a subclass of IO::AIO::REQ, so all its methods apply to
728     objects of this class, too.
729 root 1.18
730 root 1.20 A IO::AIO::GRP object is a special request that can contain multiple
731     other aio requests.
732 root 1.18
733 root 1.20 You create one by calling the "aio_group" constructing function with a
734     callback that will be called when all contained requests have entered
735     the "done" state:
736 root 1.18
737 root 1.20 my $grp = aio_group sub {
738     print "all requests are done\n";
739     };
740    
741     You add requests by calling the "add" method with one or more
742     "IO::AIO::REQ" objects:
743    
744     $grp->add (aio_unlink "...");
745    
746     add $grp aio_stat "...", sub {
747     $_[0] or return $grp->result ("error");
748 root 1.1
749 root 1.20 # add another request dynamically, if first succeeded
750     add $grp aio_open "...", sub {
751     $grp->result ("ok");
752     };
753     };
754 root 1.18
755 root 1.20 This makes it very easy to create composite requests (see the source of
756     "aio_move" for an application) that work and feel like simple requests.
757 root 1.18
758 root 1.28 * The IO::AIO::GRP objects will be cleaned up during calls to
759     "IO::AIO::poll_cb", just like any other request.
760    
761     * They can be canceled like any other request. Canceling will cancel
762     not only the request itself, but also all requests it contains.
763    
764     * They can also can also be added to other IO::AIO::GRP objects.
765    
766     * You must not add requests to a group from within the group callback
767     (or any later time).
768 root 1.20
769     Their lifetime, simplified, looks like this: when they are empty, they
770     will finish very quickly. If they contain only requests that are in the
771     "done" state, they will also finish. Otherwise they will continue to
772     exist.
773    
774 root 1.32 That means after creating a group you have some time to add requests
775     (precisely before the callback has been invoked, which is only done
776     within the "poll_cb"). And in the callbacks of those requests, you can
777     add further requests to the group. And only when all those requests have
778     finished will the the group itself finish.
779 root 1.20
780     add $grp ...
781     $grp->add (...)
782     Add one or more requests to the group. Any type of IO::AIO::REQ can
783     be added, including other groups, as long as you do not create
784     circular dependencies.
785    
786     Returns all its arguments.
787    
788     $grp->cancel_subs
789     Cancel all subrequests and clears any feeder, but not the group
790     request itself. Useful when you queued a lot of events but got a
791     result early.
792    
793 root 1.41 The group request will finish normally (you cannot add requests to
794     the group).
795    
796 root 1.20 $grp->result (...)
797     Set the result value(s) that will be passed to the group callback
798 root 1.28 when all subrequests have finished and set the groups errno to the
799 root 1.20 current value of errno (just like calling "errno" without an error
800     number). By default, no argument will be passed and errno is zero.
801    
802     $grp->errno ([$errno])
803     Sets the group errno value to $errno, or the current value of errno
804     when the argument is missing.
805    
806     Every aio request has an associated errno value that is restored
807     when the callback is invoked. This method lets you change this value
808     from its default (0).
809    
810     Calling "result" will also set errno, so make sure you either set $!
811     before the call to "result", or call c<errno> after it.
812    
813     feed $grp $callback->($grp)
814     Sets a feeder/generator on this group: every group can have an
815     attached generator that generates requests if idle. The idea behind
816     this is that, although you could just queue as many requests as you
817     want in a group, this might starve other requests for a potentially
818     long time. For example, "aio_scandir" might generate hundreds of
819     thousands "aio_stat" requests, delaying any later requests for a
820     long time.
821    
822     To avoid this, and allow incremental generation of requests, you can
823     instead a group and set a feeder on it that generates those
824     requests. The feed callback will be called whenever there are few
825     enough (see "limit", below) requests active in the group itself and
826     is expected to queue more requests.
827    
828     The feed callback can queue as many requests as it likes (i.e. "add"
829     does not impose any limits).
830    
831     If the feed does not queue more requests when called, it will be
832     automatically removed from the group.
833    
834 root 1.33 If the feed limit is 0 when this method is called, it will be set to
835     2 automatically.
836 root 1.20
837     Example:
838    
839     # stat all files in @files, but only ever use four aio requests concurrently:
840    
841     my $grp = aio_group sub { print "finished\n" };
842     limit $grp 4;
843     feed $grp sub {
844     my $file = pop @files
845     or return;
846 root 1.18
847 root 1.20 add $grp aio_stat $file, sub { ... };
848 root 1.1 };
849    
850 root 1.20 limit $grp $num
851     Sets the feeder limit for the group: The feeder will be called
852     whenever the group contains less than this many requests.
853 root 1.18
854 root 1.20 Setting the limit to 0 will pause the feeding process.
855 root 1.17
856 root 1.33 The default value for the limit is 0, but note that setting a feeder
857     automatically bumps it up to 2.
858    
859 root 1.18 SUPPORT FUNCTIONS
860 root 1.19 EVENT PROCESSING AND EVENT LOOP INTEGRATION
861 root 1.20 $fileno = IO::AIO::poll_fileno
862     Return the *request result pipe file descriptor*. This filehandle
863     must be polled for reading by some mechanism outside this module
864 root 1.38 (e.g. EV, Glib, select and so on, see below or the SYNOPSIS). If the
865     pipe becomes readable you have to call "poll_cb" to check the
866     results.
867 root 1.20
868     See "poll_cb" for an example.
869    
870     IO::AIO::poll_cb
871     Process some outstanding events on the result pipe. You have to call
872 root 1.31 this regularly. Returns 0 if all events could be processed, or -1 if
873     it returned earlier for whatever reason. Returns immediately when no
874     events are outstanding. The amount of events processed depends on
875     the settings of "IO::AIO::max_poll_req" and
876 root 1.20 "IO::AIO::max_poll_time".
877    
878     If not all requests were processed for whatever reason, the
879 root 1.31 filehandle will still be ready when "poll_cb" returns, so normally
880     you don't have to do anything special to have it called later.
881 root 1.20
882     Example: Install an Event watcher that automatically calls
883 root 1.38 IO::AIO::poll_cb with high priority (more examples can be found in
884     the SYNOPSIS section, at the top of this document):
885 root 1.20
886     Event->io (fd => IO::AIO::poll_fileno,
887     poll => 'r', async => 1,
888     cb => \&IO::AIO::poll_cb);
889    
890     IO::AIO::max_poll_reqs $nreqs
891     IO::AIO::max_poll_time $seconds
892     These set the maximum number of requests (default 0, meaning
893     infinity) that are being processed by "IO::AIO::poll_cb" in one
894     call, respectively the maximum amount of time (default 0, meaning
895     infinity) spent in "IO::AIO::poll_cb" to process requests (more
896     correctly the mininum amount of time "poll_cb" is allowed to use).
897    
898     Setting "max_poll_time" to a non-zero value creates an overhead of
899     one syscall per request processed, which is not normally a problem
900     unless your callbacks are really really fast or your OS is really
901     really slow (I am not mentioning Solaris here). Using
902     "max_poll_reqs" incurs no overhead.
903    
904     Setting these is useful if you want to ensure some level of
905     interactiveness when perl is not fast enough to process all requests
906     in time.
907    
908     For interactive programs, values such as 0.01 to 0.1 should be fine.
909 root 1.4
910 root 1.20 Example: Install an Event watcher that automatically calls
911     IO::AIO::poll_cb with low priority, to ensure that other parts of
912     the program get the CPU sometimes even under high AIO load.
913 root 1.4
914 root 1.20 # try not to spend much more than 0.1s in poll_cb
915     IO::AIO::max_poll_time 0.1;
916 root 1.4
917 root 1.20 # use a low priority so other tasks have priority
918     Event->io (fd => IO::AIO::poll_fileno,
919     poll => 'r', nice => 1,
920     cb => &IO::AIO::poll_cb);
921    
922     IO::AIO::poll_wait
923 root 1.21 If there are any outstanding requests and none of them in the result
924     phase, wait till the result filehandle becomes ready for reading
925     (simply does a "select" on the filehandle. This is useful if you
926     want to synchronously wait for some requests to finish).
927 root 1.20
928     See "nreqs" for an example.
929    
930     IO::AIO::poll
931     Waits until some requests have been handled.
932    
933 root 1.21 Returns the number of requests processed, but is otherwise strictly
934     equivalent to:
935 root 1.20
936     IO::AIO::poll_wait, IO::AIO::poll_cb
937    
938     IO::AIO::flush
939     Wait till all outstanding AIO requests have been handled.
940    
941     Strictly equivalent to:
942    
943     IO::AIO::poll_wait, IO::AIO::poll_cb
944     while IO::AIO::nreqs;
945 root 1.6
946 root 1.19 CONTROLLING THE NUMBER OF THREADS
947 root 1.20 IO::AIO::min_parallel $nthreads
948     Set the minimum number of AIO threads to $nthreads. The current
949     default is 8, which means eight asynchronous operations can execute
950     concurrently at any one time (the number of outstanding requests,
951     however, is unlimited).
952    
953     IO::AIO starts threads only on demand, when an AIO request is queued
954     and no free thread exists. Please note that queueing up a hundred
955     requests can create demand for a hundred threads, even if it turns
956     out that everything is in the cache and could have been processed
957     faster by a single thread.
958    
959     It is recommended to keep the number of threads relatively low, as
960     some Linux kernel versions will scale negatively with the number of
961     threads (higher parallelity => MUCH higher latency). With current
962     Linux 2.6 versions, 4-32 threads should be fine.
963    
964     Under most circumstances you don't need to call this function, as
965     the module selects a default that is suitable for low to moderate
966     load.
967    
968     IO::AIO::max_parallel $nthreads
969     Sets the maximum number of AIO threads to $nthreads. If more than
970     the specified number of threads are currently running, this function
971     kills them. This function blocks until the limit is reached.
972    
973     While $nthreads are zero, aio requests get queued but not executed
974     until the number of threads has been increased again.
975    
976     This module automatically runs "max_parallel 0" at program end, to
977     ensure that all threads are killed and that there are no outstanding
978     requests.
979    
980     Under normal circumstances you don't need to call this function.
981    
982     IO::AIO::max_idle $nthreads
983     Limit the number of threads (default: 4) that are allowed to idle
984     (i.e., threads that did not get a request to process within 10
985     seconds). That means if a thread becomes idle while $nthreads other
986     threads are also idle, it will free its resources and exit.
987    
988     This is useful when you allow a large number of threads (e.g. 100 or
989     1000) to allow for extremely high load situations, but want to free
990     resources under normal circumstances (1000 threads can easily
991     consume 30MB of RAM).
992    
993     The default is probably ok in most situations, especially if thread
994     creation is fast. If thread creation is very slow on your system you
995     might want to use larger values.
996    
997 root 1.30 IO::AIO::max_outstanding $maxreqs
998 root 1.20 This is a very bad function to use in interactive programs because
999     it blocks, and a bad way to reduce concurrency because it is
1000     inexact: Better use an "aio_group" together with a feed callback.
1001    
1002 root 1.25 Sets the maximum number of outstanding requests to $nreqs. If you do
1003 root 1.20 queue up more than this number of requests, the next call to the
1004     "poll_cb" (and "poll_some" and other functions calling "poll_cb")
1005     function will block until the limit is no longer exceeded.
1006    
1007     The default value is very large, so there is no practical limit on
1008     the number of outstanding requests.
1009    
1010     You can still queue as many requests as you want. Therefore,
1011 root 1.30 "max_outstanding" is mainly useful in simple scripts (with low
1012 root 1.20 values) or as a stop gap to shield against fatal memory overflow
1013     (with large values).
1014 root 1.1
1015 root 1.19 STATISTICAL INFORMATION
1016 root 1.20 IO::AIO::nreqs
1017     Returns the number of requests currently in the ready, execute or
1018     pending states (i.e. for which their callback has not been invoked
1019     yet).
1020    
1021     Example: wait till there are no outstanding requests anymore:
1022    
1023     IO::AIO::poll_wait, IO::AIO::poll_cb
1024     while IO::AIO::nreqs;
1025    
1026     IO::AIO::nready
1027     Returns the number of requests currently in the ready state (not yet
1028     executed).
1029    
1030     IO::AIO::npending
1031     Returns the number of requests currently in the pending state
1032     (executed, but not yet processed by poll_cb).
1033 root 1.19
1034 root 1.38 MISCELLANEOUS FUNCTIONS
1035     IO::AIO implements some functions that might be useful, but are not
1036     asynchronous.
1037    
1038     IO::AIO::sendfile $ofh, $ifh, $offset, $count
1039     Calls the "eio_sendfile_sync" function, which is like
1040     "aio_sendfile", but is blocking (this makes most sense if you know
1041     the input data is likely cached already and the output filehandle is
1042     set to non-blocking operations).
1043    
1044     Returns the number of bytes copied, or -1 on error.
1045    
1046     IO::AIO::fadvise $fh, $offset, $len, $advice
1047     Simply calls the "posix_fadvise" function (see it's manpage for
1048     details). The following advice constants are avaiable:
1049     "IO::AIO::FADV_NORMAL", "IO::AIO::FADV_SEQUENTIAL",
1050     "IO::AIO::FADV_RANDOM", "IO::AIO::FADV_NOREUSE",
1051     "IO::AIO::FADV_WILLNEED", "IO::AIO::FADV_DONTNEED".
1052    
1053     On systems that do not implement "posix_fadvise", this function
1054     returns ENOSYS, otherwise the return value of "posix_fadvise".
1055    
1056 root 1.9 FORK BEHAVIOUR
1057 root 1.20 This module should do "the right thing" when the process using it forks:
1058 root 1.18
1059 root 1.20 Before the fork, IO::AIO enters a quiescent state where no requests can
1060     be added in other threads and no results will be processed. After the
1061     fork the parent simply leaves the quiescent state and continues
1062     request/result processing, while the child frees the request/result
1063     queue (so that the requests started before the fork will only be handled
1064     in the parent). Threads will be started on demand until the limit set in
1065     the parent process has been reached again.
1066    
1067     In short: the parent will, after a short pause, continue as if fork had
1068     not been called, while the child will act as if IO::AIO has not been
1069     used yet.
1070 root 1.18
1071     MEMORY USAGE
1072 root 1.20 Per-request usage:
1073 root 1.18
1074 root 1.20 Each aio request uses - depending on your architecture - around 100-200
1075     bytes of memory. In addition, stat requests need a stat buffer (possibly
1076     a few hundred bytes), readdir requires a result buffer and so on. Perl
1077     scalars and other data passed into aio requests will also be locked and
1078     will consume memory till the request has entered the done state.
1079    
1080 root 1.25 This is not awfully much, so queuing lots of requests is not usually a
1081 root 1.20 problem.
1082    
1083     Per-thread usage:
1084    
1085     In the execution phase, some aio requests require more memory for
1086     temporary buffers, and each thread requires a stack and other data
1087     structures (usually around 16k-128k, depending on the OS).
1088 root 1.18
1089     KNOWN BUGS
1090 root 1.20 Known bugs will be fixed in the next release.
1091 root 1.9
1092 root 1.1 SEE ALSO
1093 root 1.30 AnyEvent::AIO for easy integration into event loops, Coro::AIO for a
1094     more natural syntax.
1095 root 1.1
1096     AUTHOR
1097 root 1.20 Marc Lehmann <schmorp@schmorp.de>
1098     http://home.schmorp.de/
1099 root 1.1