--- IO-AIO/AIO.pm 2015/06/25 13:42:18 1.254 +++ IO-AIO/AIO.pm 2017/09/22 12:17:49 1.277 @@ -1,6 +1,6 @@ =head1 NAME -IO::AIO - Asynchronous Input/Output +IO::AIO - Asynchronous/Advanced Input/Output =head1 SYNOPSIS @@ -60,6 +60,10 @@ aio_write, so the remaining functionality would have to be implemented using threads anyway. +In addition to asynchronous I/O, this module also exports some rather +arcane interfaces, such as C or linux's C system call, +which is why the C in C can also mean I. + Although the module will work in the presence of other (Perl-) threads, it is currently not reentrant in any way, so use appropriate locking yourself, always call C from within the same thread, or never @@ -96,7 +100,7 @@ print $contents; # exit event loop and program - EV::unloop; + EV::break; }; }; @@ -104,7 +108,7 @@ # check for sockets etc. etc. # process events as long as there are some: - EV::loop; + EV::run; =head1 REQUEST ANATOMY AND LIFETIME @@ -169,14 +173,14 @@ use base 'Exporter'; BEGIN { - our $VERSION = 4.32; + our $VERSION = 4.35; our @AIO_REQ = qw(aio_sendfile aio_seek aio_read aio_write aio_open aio_close aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_readdirx - aio_scandir aio_symlink aio_readlink aio_realpath aio_sync - aio_fsync aio_syncfs aio_fdatasync aio_sync_file_range aio_allocate - aio_pathsync aio_readahead aio_fiemap - aio_rename aio_link aio_move aio_copy aio_group + aio_scandir aio_symlink aio_readlink aio_realpath aio_fcntl aio_ioctl + aio_sync aio_fsync aio_syncfs aio_fdatasync aio_sync_file_range + aio_pathsync aio_readahead aio_fiemap aio_allocate + aio_rename aio_rename2 aio_link aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir aio_chown aio_chmod aio_utime aio_truncate aio_msync aio_mtouch aio_mlock aio_mlockall @@ -231,6 +235,7 @@ aio_readlink $pathname, $callback->($link) aio_realpath $pathname, $callback->($path) aio_rename $srcpath, $dstpath, $callback->($status) + aio_rename2 $srcpath, $dstpath, $flags, $callback->($status) aio_mkdir $pathname, $mode, $callback->($status) aio_rmdir $pathname, $callback->($status) aio_readdir $pathname, $callback->($entries) @@ -242,13 +247,15 @@ aio_copy $srcpath, $dstpath, $callback->($status) aio_move $srcpath, $dstpath, $callback->($status) aio_rmtree $pathname, $callback->($status) + aio_fcntl $fh, $cmd, $arg, $callback->($status) + aio_ioctl $fh, $request, $buf, $callback->($status) aio_sync $callback->($status) aio_syncfs $fh, $callback->($status) aio_fsync $fh, $callback->($status) aio_fdatasync $fh, $callback->($status) aio_sync_file_range $fh, $offset, $nbytes, $flags, $callback->($status) aio_pathsync $pathname, $callback->($status) - aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) + aio_msync $scalar, $offset = 0, $length = undef, flags = MS_SYNC, $callback->($status) aio_mtouch $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) aio_mlock $scalar, $offset = 0, $length = undef, $callback->($status) aio_mlockall $flags, $callback->($status) @@ -272,6 +279,7 @@ IO::AIO::nreqs IO::AIO::nready IO::AIO::npending + IO::AIO::min_fdlimit $nfd; IO::AIO::sendfile $ofh, $ifh, $offset, $count IO::AIO::fadvise $fh, $offset, $len, $advice @@ -396,7 +404,7 @@ C, C, C, C, C, C, C, C, C, C, C, -C, C and C. +C, C, C, C, and C. =item aio_close $fh, $callback->($status) @@ -441,8 +449,8 @@ =item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) Reads or writes C<$length> bytes from or to the specified C<$fh> and -C<$offset> into the scalar given by C<$data> and offset C<$dataoffset> -and calls the callback without the actual number of bytes read (or -1 on +C<$offset> into the scalar given by C<$data> and offset C<$dataoffset> and +calls the callback with the actual number of bytes transferred (or -1 on error, just like the syscall). C will, like C, shrink or grow the C<$data> scalar to @@ -510,7 +518,7 @@ on many systems, this implementation tries to work around some known bugs in Linux and FreeBSD kernels (probably others, too), but that might fail, so you really really should check the return value of C - -fewre bytes than expected might have been transferred. +fewer bytes than expected might have been transferred. =item aio_readahead $fh,$offset,$length, $callback->($retval) @@ -524,8 +532,8 @@ (off-set+length). C does not read beyond the end of the file. The current file offset of the file is left unchanged. -If that syscall doesn't exist (likely if your OS isn't Linux) it will be -emulated by simply reading the data, which would have a similar effect. +If that syscall doesn't exist (likely if your kernel isn't Linux) it will +be emulated by simply reading the data, which would have a similar effect. =item aio_stat $fh_or_path, $callback->($status) @@ -628,8 +636,9 @@ 0x0000f15f ecryptfs 0x00414a53 efs 0x0000137d ext - 0x0000ef53 ext2/ext3 + 0x0000ef53 ext2/ext3/ext4 0x0000ef51 ext2 + 0xf2f52010 f2fs 0x00004006 fat 0x65735546 fuseblk 0x65735543 fusectl @@ -638,6 +647,7 @@ 0x47504653 gpfs 0x00004244 hfs 0xf995e849 hpfs + 0x00c0ffee hostfs 0x958458f6 hugetlbfs 0x2bad1dea inotifyfs 0x00009660 isofs @@ -662,6 +672,7 @@ 0x00009fa0 proc 0x6165676c pstorefs 0x0000002f qnx4 + 0x68191122 qnx6 0x858458f6 ramfs 0x52654973 reiserfs 0x00007275 romfs @@ -732,11 +743,13 @@ to deallocate a file range. IO::AIO also supports C, to remove a range -(without leaving a hole) and C, to zero a range (see -your L manpage). +(without leaving a hole), C, to zero a range, +C to insert a range and C +to unshare shared blocks (see your L manpage). The file system block size used by C is presumably the -C returned by C. +C returned by C, but different filesystems and filetypes +can dictate other limitations. If C isn't available or cannot be emulated (currently no emulation will be attempted), passes C<-1> and sets C<$!> to C. @@ -805,6 +818,22 @@ of failing, C is called on the absolute path of C<$wd>. +=item aio_rename2 $srcpath, $dstpath, $flags, $callback->($status) + +Basically a version of C with an additional C<$flags> +argument. Calling this with C<$flags=0> is the same as calling +C. + +Non-zero flags are currently only supported on GNU/Linux systems that +support renameat2. Other systems fail with C in this case. + +The following constants are available (missing ones are, as usual C<0>), +see renameat2(2) for details: + +C, C +and C. + + =item aio_mkdir $pathname, $mode, $callback->($status) Asynchronously mkdir (create) a directory and call the callback with @@ -932,6 +961,8 @@ destination) from C<$srcpath> to C<$dstpath> and call the callback with a status of C<0> (ok) or C<-1> (error, see C<$!>). +Existing destination files will be truncated. + This is a composite request that creates the destination file with mode 0200 and copies the contents of the source file into it using C, followed by restoring atime, mtime, access mode and @@ -1050,7 +1081,7 @@ names, directories you can recurse into (directories), and ones you cannot recurse into (everything else, including symlinks to directories). -C is a composite request that creates of many sub requests_ +C is a composite request that generates many sub requests. C<$maxreq> specifies the maximum number of outstanding aio requests that this function generates. If it is C<< <= 0 >>, then a suitable default will be chosen (currently 4). @@ -1226,6 +1257,50 @@ $grp } +=item aio_fcntl $fh, $cmd, $arg, $callback->($status) + +=item aio_ioctl $fh, $request, $buf, $callback->($status) + +These work just like the C and C built-in functions, except +they execute asynchronously and pass the return value to the callback. + +Both calls can be used for a lot of things, some of which make more sense +to run asynchronously in their own thread, while some others make less +sense. For example, calls that block waiting for external events, such +as locking, will also lock down an I/O thread while it is waiting, which +can deadlock the whole I/O system. At the same time, there might be no +alternative to using a thread to wait. + +So in general, you should only use these calls for things that do +(filesystem) I/O, not for things that wait for other events (network, +other processes), although if you are careful and know what you are doing, +you still can. + +The following constants are available (missing ones are, as usual C<0>): + +C, + +C, C, C, + +C, C, C, C, C, C. + +C, C, C, C, +C. + +C, C, C, +C, C, C. + +C, C, C, C, C, +C, C, C, C, +C, C, C, C, +C, C, C, C, C, +C. + +C, C, C, C, +C, C, C, C, +C, C, C, C, +C, C, C, C, + =item aio_sync $callback->($status) Asynchronously call sync and call the callback when finished. @@ -1303,7 +1378,7 @@ $grp } -=item aio_msync $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) +=item aio_msync $scalar, $offset = 0, $length = undef, flags = MS_SYNC, $callback->($status) This is a rather advanced IO::AIO call, which only works on mmap(2)ed scalars (see the C function, although it also works on data @@ -1315,8 +1390,8 @@ area starting at C<$offset> in the string and ending C<$length> bytes later. If C<$length> is negative, counts from the end, and if C<$length> is C, then it goes till the end of the string. The flags can be -a combination of C, C and -C. +either C or C, plus an optional +C. =item aio_mtouch $scalar, $offset = 0, $length = undef, flags = 0, $callback->($status) @@ -1535,8 +1610,8 @@ nowhere at all), while the directory fd, if available on the system, will still point to the original directory. Most functions accepting a pathname will use the directory fd on newer systems, and the string on -older systems. Some functions (such as realpath) will always rely on the -string form of the pathname. +older systems. Some functions (such as C) will always rely on +the string form of the pathname. So this functionality is mainly useful to get some protection against C, to easily get an absolute path out of a relative path for future @@ -1938,7 +2013,7 @@ use an C together with a feed callback. Its main use is in scripts without an event loop - when you want to stat -a lot of files, you can write somehting like this: +a lot of files, you can write something like this: IO::AIO::max_outstanding 32; @@ -1994,6 +2069,27 @@ =over 4 +=item $numfd = IO::AIO::get_fdlimit + +Tries to find the current file descriptor limit and returns it, or +C and sets C<$!> in case of an error. The limit is one larger than +the highest valid file descriptor number. + +=item IO::AIO::min_fdlimit [$numfd] + +Try to increase the current file descriptor limit(s) to at least C<$numfd> +by changing the soft or hard file descriptor resource limit. If C<$numfd> +is missing, it will try to set a very high limit, although this is not +recommended when you know the actual minimum that you require. + +If the limit cannot be raised enough, the function makes a best-effort +attempt to increase the limit as much as possible, using various +tricks, while still failing. You can query the resulting limit using +C. + +If an error occurs, returns C and sets C<$!>, otherwise returns +true. + =item IO::AIO::sendfile $ofh, $ifh, $offset, $count Calls the C function, which is like C, @@ -2019,7 +2115,12 @@ Simply calls the C function (see its manpage for details). The following advice constants are available: C, C, -C, C, C. +C, C, +C. + +If C<$offset> is negative, counts from the end. If C<$length> is negative, +the remaining length of the C<$scalar> is used. If possible, C<$length> +will be reduced to fit into the C<$scalar>. On systems that do not implement C, this function returns ENOSYS, otherwise the return value of C. @@ -2031,6 +2132,10 @@ constants are available: C, C, C, C. +If C<$offset> is negative, counts from the end. If C<$length> is negative, +the remaining length of the C<$scalar> is used. If possible, C<$length> +will be reduced to fit into the C<$scalar>. + On systems that do not implement C, this function returns ENOSYS, otherwise the return value of C. @@ -2040,15 +2145,19 @@ given C<$scalar>, which will act like a string scalar. Returns true on success, and false otherwise. -The only operations allowed on the scalar are C/C that don't -change the string length, and most read-only operations such as copying it -or searching it with regexes and so on. +The scalar must exist, but its contents do not matter - this means you +cannot use a nonexistant array or hash element. When in doubt, C +the scalar first. + +The only operations allowed on the mmapped scalar are C/C, +which don't change the string length, and most read-only operations such +as copying it or searching it with regexes and so on. Anything else is unsafe and will, at best, result in memory leaks. The memory map associated with the C<$scalar> is automatically removed -when the C<$scalar> is destroyed, or when the C or -C functions are called. +when the C<$scalar> is undef'd or destroyed, or when the C +or C functions are called on it. This calls the C(2) function internally. See your system's manual page for details on the C<$length>, C<$prot> and C<$flags> parameters. @@ -2059,13 +2168,20 @@ C<$prot> is a combination of C, C, C and/or C, -C<$flags> can be a combination of C or -C, or a number of system-specific flags (when -not available, the are defined as 0): C -(which is set to C if your system only provides this -constant), C, C, -C, C or -C +C<$flags> can be a combination of +C or +C, +or a number of system-specific flags (when not available, the are C<0>): +C (which is set to C if your system only provides this constant), +C, +C, +C, +C, +C, +C, +C, +C or +C. If C<$fh> is C, then a file descriptor of C<-1> is passed.