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

Comparing IO-AIO/README (file contents):
Revision 1.45 by root, Thu Dec 30 07:19:31 2010 UTC vs.
Revision 1.48 by root, Wed Jun 29 11:25:17 2011 UTC

204 IO::AIO::max_poll_reqs $nreqs 204 IO::AIO::max_poll_reqs $nreqs
205 IO::AIO::max_poll_time $seconds 205 IO::AIO::max_poll_time $seconds
206 IO::AIO::min_parallel $nthreads 206 IO::AIO::min_parallel $nthreads
207 IO::AIO::max_parallel $nthreads 207 IO::AIO::max_parallel $nthreads
208 IO::AIO::max_idle $nthreads 208 IO::AIO::max_idle $nthreads
209 IO::AIO::idle_timeout $seconds
209 IO::AIO::max_outstanding $maxreqs 210 IO::AIO::max_outstanding $maxreqs
210 IO::AIO::nreqs 211 IO::AIO::nreqs
211 IO::AIO::nready 212 IO::AIO::nready
212 IO::AIO::npending 213 IO::AIO::npending
213 214
305 } else { 306 } else {
306 die "open failed: $!\n"; 307 die "open failed: $!\n";
307 } 308 }
308 }; 309 };
309 310
311 In addition to all the common open modes/flags ("O_RDONLY",
312 "O_WRONLY", "O_RDWR", "O_CREAT", "O_TRUNC", "O_EXCL" and
313 "O_APPEND"), the following POSIX and non-POSIX constants are
314 available (missing ones on your system are, as usual, 0):
315
316 "O_ASYNC", "O_DIRECT", "O_NOATIME", "O_CLOEXEC", "O_NOCTTY",
317 "O_NOFOLLOW", "O_NONBLOCK", "O_EXEC", "O_SEARCH", "O_DIRECTORY",
318 "O_DSYNC", "O_RSYNC", "O_SYNC" and "O_TTY_INIT".
319
310 aio_close $fh, $callback->($status) 320 aio_close $fh, $callback->($status)
311 Asynchronously close a file and call the callback with the result 321 Asynchronously close a file and call the callback with the result
312 code. 322 code.
313 323
314 Unfortunately, you can't do this to perl. Perl *insists* very 324 Unfortunately, you can't do this to perl. Perl *insists* very
357 aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) 367 aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval)
358 Tries to copy $length bytes from $in_fh to $out_fh. It starts 368 Tries to copy $length bytes from $in_fh to $out_fh. It starts
359 reading at byte offset $in_offset, and starts writing at the current 369 reading at byte offset $in_offset, and starts writing at the current
360 file offset of $out_fh. Because of that, it is not safe to issue 370 file offset of $out_fh. Because of that, it is not safe to issue
361 more than one "aio_sendfile" per $out_fh, as they will interfere 371 more than one "aio_sendfile" per $out_fh, as they will interfere
362 with each other. 372 with each other. The same $in_fh works fine though, as this function
373 does not move or use the file offset of $in_fh.
363 374
364 Please note that "aio_sendfile" can read more bytes from $in_fh than 375 Please note that "aio_sendfile" can read more bytes from $in_fh than
365 are written, and there is no way to find out how many bytes have 376 are written, and there is no way to find out how many more bytes
366 been read from "aio_sendfile" alone, as "aio_sendfile" only provides 377 have been read from "aio_sendfile" alone, as "aio_sendfile" only
367 the number of bytes written to $out_fh. Only if the result value 378 provides the number of bytes written to $out_fh. Only if the result
368 equals $length one can assume that $length bytes have been read. 379 value equals $length one can assume that $length bytes have been
380 read.
369 381
370 Unlike with other "aio_" functions, it makes a lot of sense to use 382 Unlike with other "aio_" functions, it makes a lot of sense to use
371 "aio_sendfile" on non-blocking sockets, as long as one end 383 "aio_sendfile" on non-blocking sockets, as long as one end
372 (typically the $in_fh) is a file - the file I/O will then be 384 (typically the $in_fh) is a file - the file I/O will then be
373 asynchronous, while the socket I/O will be non-blocking. Note, 385 asynchronous, while the socket I/O will be non-blocking. Note,
374 however, that you can run into a trap where "aio_sendfile" reads 386 however, that you can run into a trap where "aio_sendfile" reads
375 some data with readahead, then fails to write all data, and when the 387 some data with readahead, then fails to write all data, and when the
376 socket is ready the next time, the data in the cache is already 388 socket is ready the next time, the data in the cache is already
377 lost, forcing "aio_sendfile" to again hit the disk. Explicit 389 lost, forcing "aio_sendfile" to again hit the disk. Explicit
378 "aio_read" + "aio_write" let's you control resource usage much 390 "aio_read" + "aio_write" let's you better control resource usage.
379 better.
380 391
381 This call tries to make use of a native "sendfile" syscall to 392 This call tries to make use of a native "sendfile"-like syscall to
382 provide zero-copy operation. For this to work, $out_fh should refer 393 provide zero-copy operation. For this to work, $out_fh should refer
383 to a socket, and $in_fh should refer to an mmap'able file. 394 to a socket, and $in_fh should refer to an mmap'able file.
384 395
385 If a native sendfile cannot be found or it fails with "ENOSYS", 396 If a native sendfile cannot be found or it fails with "ENOSYS",
386 "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK", 397 "EINVAL", "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or
387 it will be emulated, so you can call "aio_sendfile" on any type of 398 "ENOTSOCK", it will be emulated, so you can call "aio_sendfile" on
388 filehandle regardless of the limitations of the operating system. 399 any type of filehandle regardless of the limitations of the
400 operating system.
401
402 As native sendfile syscalls (as practically any non-POSIX interface
403 hacked together in a hurry to improve benchmark numbers) tend to be
404 rather buggy on many systems, this implementation tries to work
405 around some known bugs in Linux and FreeBSD kernels (probably
406 others, too), but that might fail, so you really really should check
407 the return value of "aio_sendfile" - fewre bytes than expected might
408 have been transferred.
389 409
390 aio_readahead $fh,$offset,$length, $callback->($retval) 410 aio_readahead $fh,$offset,$length, $callback->($retval)
391 "aio_readahead" populates the page cache with data from a file so 411 "aio_readahead" populates the page cache with data from a file so
392 that subsequent reads from that file will not block on disk I/O. The 412 that subsequent reads from that file will not block on disk I/O. The
393 $offset argument specifies the starting point from which data is to 413 $offset argument specifies the starting point from which data is to
413 433
414 Currently, the stats are always 64-bit-stats, i.e. instead of 434 Currently, the stats are always 64-bit-stats, i.e. instead of
415 returning an error when stat'ing a large file, the results will be 435 returning an error when stat'ing a large file, the results will be
416 silently truncated unless perl itself is compiled with large file 436 silently truncated unless perl itself is compiled with large file
417 support. 437 support.
438
439 To help interpret the mode and dev/rdev stat values, IO::AIO offers
440 the following constants and functions (if not implemented, the
441 constants will be 0 and the functions will either "croak" or fall
442 back on traditional behaviour).
443
444 "S_IFMT", "S_IFIFO", "S_IFCHR", "S_IFBLK", "S_IFLNK", "S_IFREG",
445 "S_IFDIR", "S_IFWHT", "S_IFSOCK", "IO::AIO::major $dev_t",
446 "IO::AIO::minor $dev_t", "IO::AIO::makedev $major, $minor".
418 447
419 Example: Print the length of /etc/passwd: 448 Example: Print the length of /etc/passwd:
420 449
421 aio_stat "/etc/passwd", sub { 450 aio_stat "/etc/passwd", sub {
422 $_[0] and die "stat failed: $!"; 451 $_[0] and die "stat failed: $!";
511 540
512 The only (POSIX-) portable way of calling this function is: 541 The only (POSIX-) portable way of calling this function is:
513 542
514 aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ... 543 aio_mknod $path, IO::AIO::S_IFIFO | $mode, 0, sub { ...
515 544
545 See "aio_stat" for info about some potentially helpful extra
546 constants and functions.
547
516 aio_link $srcpath, $dstpath, $callback->($status) 548 aio_link $srcpath, $dstpath, $callback->($status)
517 Asynchronously create a new link to the existing object at $srcpath 549 Asynchronously create a new link to the existing object at $srcpath
518 at the path $dstpath and call the callback with the result code. 550 at the path $dstpath and call the callback with the result code.
519 551
520 aio_symlink $srcpath, $dstpath, $callback->($status) 552 aio_symlink $srcpath, $dstpath, $callback->($status)
556 The flags are a combination of the following constants, ORed 588 The flags are a combination of the following constants, ORed
557 together (the flags will also be passed to the callback, possibly 589 together (the flags will also be passed to the callback, possibly
558 modified): 590 modified):
559 591
560 IO::AIO::READDIR_DENTS 592 IO::AIO::READDIR_DENTS
561 When this flag is off, then the callback gets an arrayref with 593 When this flag is off, then the callback gets an arrayref
562 of names only (as with "aio_readdir"), otherwise it gets an 594 consisting of names only (as with "aio_readdir"), otherwise it
563 arrayref with "[$name, $type, $inode]" arrayrefs, each 595 gets an arrayref with "[$name, $type, $inode]" arrayrefs, each
564 describing a single directory entry in more detail. 596 describing a single directory entry in more detail.
565 597
566 $name is the name of the entry. 598 $name is the name of the entry.
567 599
568 $type is one of the "IO::AIO::DT_xxx" constants: 600 $type is one of the "IO::AIO::DT_xxx" constants:
581 unspecified content on systems that do not deliver the inode 613 unspecified content on systems that do not deliver the inode
582 information. 614 information.
583 615
584 IO::AIO::READDIR_DIRS_FIRST 616 IO::AIO::READDIR_DIRS_FIRST
585 When this flag is set, then the names will be returned in an 617 When this flag is set, then the names will be returned in an
586 order where likely directories come first. This is useful when 618 order where likely directories come first, in optimal stat
587 you need to quickly find directories, or you want to find all 619 order. This is useful when you need to quickly find directories,
588 directories while avoiding to stat() each entry. 620 or you want to find all directories while avoiding to stat()
621 each entry.
589 622
590 If the system returns type information in readdir, then this is 623 If the system returns type information in readdir, then this is
591 used to find directories directly. Otherwise, likely directories 624 used to find directories directly. Otherwise, likely directories
592 are files beginning with ".", or otherwise files with no dots, 625 are names beginning with ".", or otherwise names with no dots,
593 of which files with short names are tried first. 626 of which names with short names are tried first.
594 627
595 IO::AIO::READDIR_STAT_ORDER 628 IO::AIO::READDIR_STAT_ORDER
596 When this flag is set, then the names will be returned in an 629 When this flag is set, then the names will be returned in an
597 order suitable for stat()'ing each one. That is, when you plan 630 order suitable for stat()'ing each one. That is, when you plan
598 to stat() all files in the given directory, then the returned 631 to stat() all files in the given directory, then the returned
1009 1042
1010 See "poll_cb" for an example. 1043 See "poll_cb" for an example.
1011 1044
1012 IO::AIO::poll_cb 1045 IO::AIO::poll_cb
1013 Process some outstanding events on the result pipe. You have to call 1046 Process some outstanding events on the result pipe. You have to call
1014 this regularly. Returns 0 if all events could be processed, or -1 if 1047 this regularly. Returns 0 if all events could be processed (or there
1015 it returned earlier for whatever reason. Returns immediately when no 1048 were no events to process), or -1 if it returned earlier for
1016 events are outstanding. The amount of events processed depends on 1049 whatever reason. Returns immediately when no events are outstanding.
1017 the settings of "IO::AIO::max_poll_req" and 1050 The amount of events processed depends on the settings of
1018 "IO::AIO::max_poll_time". 1051 "IO::AIO::max_poll_req" and "IO::AIO::max_poll_time".
1019 1052
1020 If not all requests were processed for whatever reason, the 1053 If not all requests were processed for whatever reason, the
1021 filehandle will still be ready when "poll_cb" returns, so normally 1054 filehandle will still be ready when "poll_cb" returns, so normally
1022 you don't have to do anything special to have it called later. 1055 you don't have to do anything special to have it called later.
1056
1057 Apart from calling "IO::AIO::poll_cb" when the event filehandle
1058 becomes ready, it can be beneficial to call this function from loops
1059 which submit a lot of requests, to make sure the results get
1060 processed when they become available and not just when the loop is
1061 finished and the event loop takes over again. This function returns
1062 very fast when there are no outstanding requests.
1023 1063
1024 Example: Install an Event watcher that automatically calls 1064 Example: Install an Event watcher that automatically calls
1025 IO::AIO::poll_cb with high priority (more examples can be found in 1065 IO::AIO::poll_cb with high priority (more examples can be found in
1026 the SYNOPSIS section, at the top of this document): 1066 the SYNOPSIS section, at the top of this document):
1027 1067
1121 1161
1122 Under normal circumstances you don't need to call this function. 1162 Under normal circumstances you don't need to call this function.
1123 1163
1124 IO::AIO::max_idle $nthreads 1164 IO::AIO::max_idle $nthreads
1125 Limit the number of threads (default: 4) that are allowed to idle 1165 Limit the number of threads (default: 4) that are allowed to idle
1126 (i.e., threads that did not get a request to process within 10 1166 (i.e., threads that did not get a request to process within the idle
1127 seconds). That means if a thread becomes idle while $nthreads other 1167 timeout (default: 10 seconds). That means if a thread becomes idle
1128 threads are also idle, it will free its resources and exit. 1168 while $nthreads other threads are also idle, it will free its
1169 resources and exit.
1129 1170
1130 This is useful when you allow a large number of threads (e.g. 100 or 1171 This is useful when you allow a large number of threads (e.g. 100 or
1131 1000) to allow for extremely high load situations, but want to free 1172 1000) to allow for extremely high load situations, but want to free
1132 resources under normal circumstances (1000 threads can easily 1173 resources under normal circumstances (1000 threads can easily
1133 consume 30MB of RAM). 1174 consume 30MB of RAM).
1134 1175
1135 The default is probably ok in most situations, especially if thread 1176 The default is probably ok in most situations, especially if thread
1136 creation is fast. If thread creation is very slow on your system you 1177 creation is fast. If thread creation is very slow on your system you
1137 might want to use larger values. 1178 might want to use larger values.
1138 1179
1180 IO::AIO::idle_timeout $seconds
1181 Sets the minimum idle timeout (default 10) after which worker
1182 threads are allowed to exit. SEe "IO::AIO::max_idle".
1183
1139 IO::AIO::max_outstanding $maxreqs 1184 IO::AIO::max_outstanding $maxreqs
1185 Sets the maximum number of outstanding requests to $nreqs. If you do
1186 queue up more than this number of requests, the next call to
1187 "IO::AIO::poll_cb" (and other functions calling "poll_cb", such as
1188 "IO::AIO::flush" or "IO::AIO::poll") will block until the limit is
1189 no longer exceeded.
1190
1191 In other words, this setting does not enforce a queue limit, but can
1192 be used to make poll functions block if the limit is exceeded.
1193
1140 This is a very bad function to use in interactive programs because 1194 This is a very bad function to use in interactive programs because
1141 it blocks, and a bad way to reduce concurrency because it is 1195 it blocks, and a bad way to reduce concurrency because it is
1142 inexact: Better use an "aio_group" together with a feed callback. 1196 inexact: Better use an "aio_group" together with a feed callback.
1143 1197
1144 Sets the maximum number of outstanding requests to $nreqs. If you do 1198 It's main use is in scripts without an event loop - when you want to
1145 queue up more than this number of requests, the next call to the 1199 stat a lot of files, you can write somehting like this:
1146 "poll_cb" (and "poll_some" and other functions calling "poll_cb")
1147 function will block until the limit is no longer exceeded.
1148 1200
1149 The default value is very large, so there is no practical limit on 1201 IO::AIO::max_outstanding 32;
1202
1203 for my $path (...) {
1204 aio_stat $path , ...;
1205 IO::AIO::poll_cb;
1206 }
1207
1208 IO::AIO::flush;
1209
1210 The call to "poll_cb" inside the loop will normally return
1211 instantly, but as soon as more thna 32 reqeusts are in-flight, it
1212 will block until some requests have been handled. This keeps the
1213 loop from pushing a large number of "aio_stat" requests onto the
1214 queue.
1215
1216 The default value for "max_outstanding" is very large, so there is
1150 the number of outstanding requests. 1217 no practical limit on the number of outstanding requests.
1151
1152 You can still queue as many requests as you want. Therefore,
1153 "max_outstanding" is mainly useful in simple scripts (with low
1154 values) or as a stop gap to shield against fatal memory overflow
1155 (with large values).
1156 1218
1157 STATISTICAL INFORMATION 1219 STATISTICAL INFORMATION
1158 IO::AIO::nreqs 1220 IO::AIO::nreqs
1159 Returns the number of requests currently in the ready, execute or 1221 Returns the number of requests currently in the ready, execute or
1160 pending states (i.e. for which their callback has not been invoked 1222 pending states (i.e. for which their callback has not been invoked
1306 # Danga::Socket integration 1368 # Danga::Socket integration
1307 Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => 1369 Danga::Socket->AddOtherFds (IO::AIO::poll_fileno =>
1308 \&IO::AIO::poll_cb); 1370 \&IO::AIO::poll_cb);
1309 1371
1310 FORK BEHAVIOUR 1372 FORK BEHAVIOUR
1311 This module should do "the right thing" when the process using it forks: 1373 Usage of pthreads in a program changes the semantics of fork
1374 considerably. Specifically, only async-safe functions can be called
1375 after fork. Perl doesn't know about this, so in general, you cannot call
1376 fork with defined behaviour in perl. IO::AIO uses pthreads, so this
1377 applies, but many other extensions and (for inexplicable reasons) perl
1378 itself often is linked against pthreads, so this limitation applies.
1312 1379
1313 Before the fork, IO::AIO enters a quiescent state where no requests can 1380 Some operating systems have extensions that allow safe use of fork, and
1314 be added in other threads and no results will be processed. After the 1381 this module should do "the right thing" on those, and tries on others.
1315 fork the parent simply leaves the quiescent state and continues 1382 At the time of this writing (2011) only GNU/Linux supports these
1316 request/result processing, while the child frees the request/result 1383 extensions to POSIX.
1317 queue (so that the requests started before the fork will only be handled
1318 in the parent). Threads will be started on demand until the limit set in
1319 the parent process has been reached again.
1320
1321 In short: the parent will, after a short pause, continue as if fork had
1322 not been called, while the child will act as if IO::AIO has not been
1323 used yet.
1324 1384
1325 MEMORY USAGE 1385 MEMORY USAGE
1326 Per-request usage: 1386 Per-request usage:
1327 1387
1328 Each aio request uses - depending on your architecture - around 100-200 1388 Each aio request uses - depending on your architecture - around 100-200

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines