ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.pm
(Generate patch)

Comparing IO-AIO/AIO.pm (file contents):
Revision 1.275 by root, Fri Sep 22 05:20:39 2017 UTC vs.
Revision 1.283 by root, Tue Feb 20 06:05:19 2018 UTC

171use common::sense; 171use common::sense;
172 172
173use base 'Exporter'; 173use base 'Exporter';
174 174
175BEGIN { 175BEGIN {
176 our $VERSION = 4.35; 176 our $VERSION = 4.4;
177 177
178 our @AIO_REQ = qw(aio_sendfile aio_seek aio_read aio_write aio_open aio_close 178 our @AIO_REQ = qw(aio_sendfile aio_seek aio_read aio_write aio_open aio_close
179 aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_readdirx 179 aio_stat aio_lstat aio_unlink aio_rmdir aio_readdir aio_readdirx
180 aio_scandir aio_symlink aio_readlink aio_realpath aio_fcntl aio_ioctl 180 aio_scandir aio_symlink aio_readlink aio_realpath aio_fcntl aio_ioctl
181 aio_sync aio_fsync aio_syncfs aio_fdatasync aio_sync_file_range 181 aio_sync aio_fsync aio_syncfs aio_fdatasync aio_sync_file_range
183 aio_rename aio_rename2 aio_link aio_move aio_copy aio_group 183 aio_rename aio_rename2 aio_link aio_move aio_copy aio_group
184 aio_nop aio_mknod aio_load aio_rmtree aio_mkdir aio_chown 184 aio_nop aio_mknod aio_load aio_rmtree aio_mkdir aio_chown
185 aio_chmod aio_utime aio_truncate 185 aio_chmod aio_utime aio_truncate
186 aio_msync aio_mtouch aio_mlock aio_mlockall 186 aio_msync aio_mtouch aio_mlock aio_mlockall
187 aio_statvfs 187 aio_statvfs
188 aio_slurp
188 aio_wd); 189 aio_wd);
189 190
190 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); 191 our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice));
191 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush 192 our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush
192 min_parallel max_parallel max_idle idle_timeout 193 min_parallel max_parallel max_idle idle_timeout
277 IO::AIO::idle_timeout $seconds 278 IO::AIO::idle_timeout $seconds
278 IO::AIO::max_outstanding $maxreqs 279 IO::AIO::max_outstanding $maxreqs
279 IO::AIO::nreqs 280 IO::AIO::nreqs
280 IO::AIO::nready 281 IO::AIO::nready
281 IO::AIO::npending 282 IO::AIO::npending
283 $nfd = IO::AIO::get_fdlimit [EXPERIMENTAL]
282 IO::AIO::min_fdlimit $nfd; 284 IO::AIO::min_fdlimit $nfd [EXPERIMENTAL]
283 285
284 IO::AIO::sendfile $ofh, $ifh, $offset, $count 286 IO::AIO::sendfile $ofh, $ifh, $offset, $count
285 IO::AIO::fadvise $fh, $offset, $len, $advice 287 IO::AIO::fadvise $fh, $offset, $len, $advice
286 IO::AIO::mmap $scalar, $length, $prot, $flags[, $fh[, $offset]] 288 IO::AIO::mmap $scalar, $length, $prot, $flags[, $fh[, $offset]]
287 IO::AIO::munmap $scalar 289 IO::AIO::munmap $scalar
610 namemax => 255, 612 namemax => 255,
611 frsize => 1024, 613 frsize => 1024,
612 fsid => 1810 614 fsid => 1810
613 } 615 }
614 616
615Here is a (likely partial - send me updates!) list of fsid values used by
616Linux - it is safe to hardcode these when C<$^O> is C<linux>:
617
618 0x0000adf5 adfs
619 0x0000adff affs
620 0x5346414f afs
621 0x09041934 anon-inode filesystem
622 0x00000187 autofs
623 0x42465331 befs
624 0x1badface bfs
625 0x42494e4d binfmt_misc
626 0x9123683e btrfs
627 0x0027e0eb cgroupfs
628 0xff534d42 cifs
629 0x73757245 coda
630 0x012ff7b7 coh
631 0x28cd3d45 cramfs
632 0x453dcd28 cramfs-wend (wrong endianness)
633 0x64626720 debugfs
634 0x00001373 devfs
635 0x00001cd1 devpts
636 0x0000f15f ecryptfs
637 0x00414a53 efs
638 0x0000137d ext
639 0x0000ef53 ext2/ext3/ext4
640 0x0000ef51 ext2
641 0xf2f52010 f2fs
642 0x00004006 fat
643 0x65735546 fuseblk
644 0x65735543 fusectl
645 0x0bad1dea futexfs
646 0x01161970 gfs2
647 0x47504653 gpfs
648 0x00004244 hfs
649 0xf995e849 hpfs
650 0x00c0ffee hostfs
651 0x958458f6 hugetlbfs
652 0x2bad1dea inotifyfs
653 0x00009660 isofs
654 0x000072b6 jffs2
655 0x3153464a jfs
656 0x6b414653 k-afs
657 0x0bd00bd0 lustre
658 0x0000137f minix
659 0x0000138f minix 30 char names
660 0x00002468 minix v2
661 0x00002478 minix v2 30 char names
662 0x00004d5a minix v3
663 0x19800202 mqueue
664 0x00004d44 msdos
665 0x0000564c novell
666 0x00006969 nfs
667 0x6e667364 nfsd
668 0x00003434 nilfs
669 0x5346544e ntfs
670 0x00009fa1 openprom
671 0x7461636F ocfs2
672 0x00009fa0 proc
673 0x6165676c pstorefs
674 0x0000002f qnx4
675 0x68191122 qnx6
676 0x858458f6 ramfs
677 0x52654973 reiserfs
678 0x00007275 romfs
679 0x67596969 rpc_pipefs
680 0x73636673 securityfs
681 0xf97cff8c selinux
682 0x0000517b smb
683 0x534f434b sockfs
684 0x73717368 squashfs
685 0x62656572 sysfs
686 0x012ff7b6 sysv2
687 0x012ff7b5 sysv4
688 0x01021994 tmpfs
689 0x15013346 udf
690 0x00011954 ufs
691 0x54190100 ufs byteswapped
692 0x00009fa2 usbdevfs
693 0x01021997 v9fs
694 0xa501fcf5 vxfs
695 0xabba1974 xenfs
696 0x012ff7b4 xenix
697 0x58465342 xfs
698 0x012fd16d xia
699
700=item aio_utime $fh_or_path, $atime, $mtime, $callback->($status) 617=item aio_utime $fh_or_path, $atime, $mtime, $callback->($status)
701 618
702Works like perl's C<utime> function (including the special case of $atime 619Works like perl's C<utime> function (including the special case of $atime
703and $mtime being undef). Fractional times are supported if the underlying 620and $mtime being undef). Fractional times are supported if the underlying
704syscalls support them. 621syscalls support them.
925C<$type>'s are known, which can be used to speed up some algorithms. 842C<$type>'s are known, which can be used to speed up some algorithms.
926 843
927=back 844=back
928 845
929 846
847=item aio_slurp $pathname, $offset, $length, $data, $callback->($status)
848
849Opens, reads and closes the given file. The data is put into C<$data>,
850which is resized as required.
851
852If C<$offset> is negative, then it is counted from the end of the file.
853
854If C<$length> is zero, then the remaining length of the file is
855used. Also, in this case, the same limitations to modifying C<$data> apply
856as when IO::AIO::mmap is used, i.e. it must only be modified in-place
857with C<substr>. If the size of the file is known, specifying a non-zero
858C<$length> results in a performance advantage.
859
860This request is similar to the older C<aio_load> request, but since it is
861a single request, it might be more efficient to use.
862
863Example: load F</etc/passwd> into C<$passwd>.
864
865 my $passwd;
866 aio_slurp "/etc/passwd", 0, 0, $passwd, sub {
867 $_[0] >= 0
868 or die "/etc/passwd: $!\n";
869
870 printf "/etc/passwd is %d bytes long, and contains:\n", length $passwd;
871 print $passwd;
872 };
873 IO::AIO::flush;
874
875
930=item aio_load $pathname, $data, $callback->($status) 876=item aio_load $pathname, $data, $callback->($status)
931 877
932This is a composite request that tries to fully load the given file into 878This is a composite request that tries to fully load the given file into
933memory. Status is the same as with aio_read. 879memory. Status is the same as with aio_read.
880
881Using C<aio_slurp> might be more efficient, as it is a single request.
934 882
935=cut 883=cut
936 884
937sub aio_load($$;$) { 885sub aio_load($$;$) {
938 my ($path, undef, $cb) = @_; 886 my ($path, undef, $cb) = @_;
1079Scans a directory (similar to C<aio_readdir>) but additionally tries to 1027Scans a directory (similar to C<aio_readdir>) but additionally tries to
1080efficiently separate the entries of directory C<$path> into two sets of 1028efficiently separate the entries of directory C<$path> into two sets of
1081names, directories you can recurse into (directories), and ones you cannot 1029names, directories you can recurse into (directories), and ones you cannot
1082recurse into (everything else, including symlinks to directories). 1030recurse into (everything else, including symlinks to directories).
1083 1031
1084C<aio_scandir> is a composite request that creates of many sub requests_ 1032C<aio_scandir> is a composite request that generates many sub requests.
1085C<$maxreq> specifies the maximum number of outstanding aio requests that 1033C<$maxreq> specifies the maximum number of outstanding aio requests that
1086this function generates. If it is C<< <= 0 >>, then a suitable default 1034this function generates. If it is C<< <= 0 >>, then a suitable default
1087will be chosen (currently 4). 1035will be chosen (currently 4).
1088 1036
1089On error, the callback is called without arguments, otherwise it receives 1037On error, the callback is called without arguments, otherwise it receives
1482C<IO::AIO::FIEMAP_EXTENT_DATA_ENCRYPTED>, C<IO::AIO::FIEMAP_EXTENT_NOT_ALIGNED>, 1430C<IO::AIO::FIEMAP_EXTENT_DATA_ENCRYPTED>, C<IO::AIO::FIEMAP_EXTENT_NOT_ALIGNED>,
1483C<IO::AIO::FIEMAP_EXTENT_DATA_INLINE>, C<IO::AIO::FIEMAP_EXTENT_DATA_TAIL>, 1431C<IO::AIO::FIEMAP_EXTENT_DATA_INLINE>, C<IO::AIO::FIEMAP_EXTENT_DATA_TAIL>,
1484C<IO::AIO::FIEMAP_EXTENT_UNWRITTEN>, C<IO::AIO::FIEMAP_EXTENT_MERGED> or 1432C<IO::AIO::FIEMAP_EXTENT_UNWRITTEN>, C<IO::AIO::FIEMAP_EXTENT_MERGED> or
1485C<IO::AIO::FIEMAP_EXTENT_SHARED>. 1433C<IO::AIO::FIEMAP_EXTENT_SHARED>.
1486 1434
1487At the time of this writing (Linux 3.2), this requets is unreliable unless 1435At the time of this writing (Linux 3.2), this request is unreliable unless
1488C<$count> is C<undef>, as the kernel has all sorts of bugs preventing 1436C<$count> is C<undef>, as the kernel has all sorts of bugs preventing
1489it to return all extents of a range for files with large number of 1437it to return all extents of a range for files with a large number of
1490extents. The code works around all these issues if C<$count> is undef. 1438extents. The code (only) works around all these issues if C<$count> is
1439C<undef>.
1491 1440
1492=item aio_group $callback->(...) 1441=item aio_group $callback->(...)
1493 1442
1494This is a very special aio request: Instead of doing something, it is a 1443This is a very special aio request: Instead of doing something, it is a
1495container for other aio requests, which is useful if you want to bundle 1444container for other aio requests, which is useful if you want to bundle
1608There are some caveats: when directories get renamed (or deleted), the 1557There are some caveats: when directories get renamed (or deleted), the
1609pathname string doesn't change, so will point to the new directory (or 1558pathname string doesn't change, so will point to the new directory (or
1610nowhere at all), while the directory fd, if available on the system, 1559nowhere at all), while the directory fd, if available on the system,
1611will still point to the original directory. Most functions accepting a 1560will still point to the original directory. Most functions accepting a
1612pathname will use the directory fd on newer systems, and the string on 1561pathname will use the directory fd on newer systems, and the string on
1613older systems. Some functions (such as realpath) will always rely on the 1562older systems. Some functions (such as C<aio_realpath>) will always rely on
1614string form of the pathname. 1563the string form of the pathname.
1615 1564
1616So this functionality is mainly useful to get some protection against 1565So this functionality is mainly useful to get some protection against
1617C<chdir>, to easily get an absolute path out of a relative path for future 1566C<chdir>, to easily get an absolute path out of a relative path for future
1618reference, and to speed up doing many operations in the same directory 1567reference, and to speed up doing many operations in the same directory
1619(e.g. when stat'ing all files in a directory). 1568(e.g. when stat'ing all files in a directory).
2069 2018
2070=over 4 2019=over 4
2071 2020
2072=item $numfd = IO::AIO::get_fdlimit 2021=item $numfd = IO::AIO::get_fdlimit
2073 2022
2023This function is I<EXPERIMENTAL> and subject to change.
2024
2074Tries to find the current file descriptor limit and returns it, or 2025Tries to find the current file descriptor limit and returns it, or
2075C<undef> and sets C<$!> in case of an error. The limit is one larger than 2026C<undef> and sets C<$!> in case of an error. The limit is one larger than
2076the highest valid file descriptor number. 2027the highest valid file descriptor number.
2077 2028
2078=item IO::AIO::min_fdlimit [$numfd] 2029=item IO::AIO::min_fdlimit [$numfd]
2030
2031This function is I<EXPERIMENTAL> and subject to change.
2079 2032
2080Try to increase the current file descriptor limit(s) to at least C<$numfd> 2033Try to increase the current file descriptor limit(s) to at least C<$numfd>
2081by changing the soft or hard file descriptor resource limit. If C<$numfd> 2034by changing the soft or hard file descriptor resource limit. If C<$numfd>
2082is missing, it will try to set a very high limit, although this is not 2035is missing, it will try to set a very high limit, although this is not
2083recommended when you know the actual minimum that you require. 2036recommended when you know the actual minimum that you require.
2085If the limit cannot be raised enough, the function makes a best-effort 2038If the limit cannot be raised enough, the function makes a best-effort
2086attempt to increase the limit as much as possible, using various 2039attempt to increase the limit as much as possible, using various
2087tricks, while still failing. You can query the resulting limit using 2040tricks, while still failing. You can query the resulting limit using
2088C<IO::AIO::get_fdlimit>. 2041C<IO::AIO::get_fdlimit>.
2089 2042
2090If an error occurs, returns C<undef> and sets C<$!>. 2043If an error occurs, returns C<undef> and sets C<$!>, otherwise returns
2044true.
2091 2045
2092=item IO::AIO::sendfile $ofh, $ifh, $offset, $count 2046=item IO::AIO::sendfile $ofh, $ifh, $offset, $count
2093 2047
2094Calls the C<eio_sendfile_sync> function, which is like C<aio_sendfile>, 2048Calls the C<eio_sendfile_sync> function, which is like C<aio_sendfile>,
2095but is blocking (this makes most sense if you know the input data is 2049but is blocking (this makes most sense if you know the input data is
2261 2215
2262Please refer to L<pipe2(2)> for more info on the C<$flags>, but at the 2216Please refer to L<pipe2(2)> for more info on the C<$flags>, but at the
2263time of this writing, C<IO::AIO::O_CLOEXEC>, C<IO::AIO::O_NONBLOCK> and 2217time of this writing, C<IO::AIO::O_CLOEXEC>, C<IO::AIO::O_NONBLOCK> and
2264C<IO::AIO::O_DIRECT> (Linux 3.4, for packet-based pipes) were supported. 2218C<IO::AIO::O_DIRECT> (Linux 3.4, for packet-based pipes) were supported.
2265 2219
2220Example: create a pipe race-free w.r.t. threads and fork:
2221
2222 my ($rfh, $wfh) = IO::AIO::pipe2 IO::AIO::O_CLOEXEC
2223 or die "pipe2: $!\n";
2224
2225=item $fh = IO::AIO::eventfd [$initval, [$flags]]
2226
2227This is a direct interface to the Linux L<eventfd(2)> system call. The
2228(unhelpful) defaults for C<$initval> and C<$flags> are C<0> for both.
2229
2230On success, the new eventfd filehandle is returned, otherwise returns
2231C<undef>. If the eventfd syscall is missing, fails with C<ENOSYS>.
2232
2233Please refer to L<eventfd(2)> for more info on this call.
2234
2235The following symbol flag values are available: C<IO::AIO::EFD_CLOEXEC>,
2236C<IO::AIO::EFD_NONBLOCK> and C<IO::AIO::EFD_SEMAPHORE> (Linux 2.6.30).
2237
2238Example: create a new eventfd filehandle:
2239
2240 $fh = IO::AIO::eventfd 0, IO::AIO::O_CLOEXEC
2241 or die "eventfd: $!\n";
2242
2243=item $fh = IO::AIO::timerfd_create $clockid[, $flags]
2244
2245This is a direct interface to the Linux L<timerfd_create(2)> system call. The
2246(unhelpful) default for C<$flags> is C<0>.
2247
2248On success, the new timerfd filehandle is returned, otherwise returns
2249C<undef>. If the eventfd syscall is missing, fails with C<ENOSYS>.
2250
2251Please refer to L<timerfd_create(2)> for more info on this call.
2252
2253The following C<$clockid> values are
2254available: C<IO::AIO::CLOCK_REALTIME>, C<IO::AIO::CLOCK_MONOTONIC>
2255C<IO::AIO::CLOCK_CLOCK_BOOTTIME> (Linux 3.15)
2256C<IO::AIO::CLOCK_CLOCK_REALTIME_ALARM> (Linux 3.11) and
2257C<IO::AIO::CLOCK_CLOCK_BOOTTIME_ALARM> (Linux 3.11).
2258
2259The following C<$flags> values are available (Linux
22602.6.27): C<IO::AIO::TFD_NONBLOCK> and C<IO::AIO::TFD_CLOEXEC>.
2261
2262Example: create a new timerfd and set it to one-second repeated alarms,
2263then wait for two alarms:
2264
2265 my $fh = IO::AIO::timerfd_create IO::AIO::CLOCK_BOOTTIME, IO::AIO::TFD_CLOEXEC
2266 or die "timerfd_create: $!\n";
2267
2268 defined IO::AIO::timerfd_settime $fh, 0, 1, 1
2269 or die "timerfd_settime: $!\n";
2270
2271 for (1..2) {
2272 8 == sysread $fh, my $buf, 8
2273 or die "timerfd read failure\n";
2274
2275 printf "number of expirations (likely 1): %d\n",
2276 unpack "Q", $buf;
2277 }
2278
2279=item ($cur_interval, $cur_value) = IO::AIO::timerfd_settime $fh, $flags, $new_interval, $nbw_value
2280
2281This is a direct interface to the Linux L<timerfd_settime(2)> system
2282call. Please refer to its manpage for more info on this call.
2283
2284The new itimerspec is specified using two (possibly fractional) second
2285values, C<$new_interval> and C<$new_value>).
2286
2287On success, the current interval and value are returned (as per
2288C<timerfd_gettime>). On failure, the empty list is returned.
2289
2290The following C<$flags> values are
2291available: C<IO::AIO::TFD_TIMER_ABSTIME> and
2292C<IO::AIO::TFD_TIMER_CANCEL_ON_SET>.
2293
2294See C<IO::AIO::timerfd_create> for a full example.
2295
2296=item ($cur_interval, $cur_value) = IO::AIO::timerfd_gettime $fh
2297
2298This is a direct interface to the Linux L<timerfd_gettime(2)> system
2299call. Please refer to its manpage for more info on this call.
2300
2301On success, returns the current values of interval and value for the given
2302timerfd (as potentially fractional second values). On failure, the empty
2303list is returned.
2304
2266=back 2305=back
2267 2306
2268=cut 2307=cut
2269 2308
2270min_parallel 8; 2309min_parallel 8;
2335the process will result in undefined behaviour. Calling it at any time 2374the process will result in undefined behaviour. Calling it at any time
2336will also result in any undefined (by POSIX) behaviour. 2375will also result in any undefined (by POSIX) behaviour.
2337 2376
2338=back 2377=back
2339 2378
2379=head2 LINUX-SPECIFIC CALLS
2380
2381When a call is documented as "linux-specific" then this means it
2382originated on GNU/Linux. C<IO::AIO> will usually try to autodetect the
2383availability and compatibility of such calls regardless of the platform
2384it is compiled on, so platforms such as FreeBSD which often implement
2385these calls will work. When in doubt, call them and see if they fail wth
2386C<ENOSYS>.
2387
2340=head2 MEMORY USAGE 2388=head2 MEMORY USAGE
2341 2389
2342Per-request usage: 2390Per-request usage:
2343 2391
2344Each aio request uses - depending on your architecture - around 100-200 2392Each aio request uses - depending on your architecture - around 100-200
2356temporary buffers, and each thread requires a stack and other data 2404temporary buffers, and each thread requires a stack and other data
2357structures (usually around 16k-128k, depending on the OS). 2405structures (usually around 16k-128k, depending on the OS).
2358 2406
2359=head1 KNOWN BUGS 2407=head1 KNOWN BUGS
2360 2408
2361Known bugs will be fixed in the next release. 2409Known bugs will be fixed in the next release :)
2410
2411=head1 KNOWN ISSUES
2412
2413Calls that try to "import" foreign memory areas (such as C<IO::AIO::mmap>
2414or C<IO::AIO::aio_slurp>) do not work with generic lvalues, such as
2415non-created hash slots or other scalars I didn't think of. It's best to
2416avoid such and either use scalar variables or making sure that the scalar
2417exists (e.g. by storing C<undef>) and isn't "funny" (e.g. tied).
2418
2419I am not sure anything can be done about this, so this is considered a
2420known issue, rather than a bug.
2362 2421
2363=head1 SEE ALSO 2422=head1 SEE ALSO
2364 2423
2365L<AnyEvent::AIO> for easy integration into event loops, L<Coro::AIO> for a 2424L<AnyEvent::AIO> for easy integration into event loops, L<Coro::AIO> for a
2366more natural syntax. 2425more natural syntax.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines