… | |
… | |
367 | aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
367 | aio_sendfile $out_fh, $in_fh, $in_offset, $length, $callback->($retval) |
368 | 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 |
369 | 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 |
370 | 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 |
371 | 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 |
372 | 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. |
373 | |
374 | |
374 | 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 |
375 | 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 |
376 | been read from "aio_sendfile" alone, as "aio_sendfile" only provides |
377 | have been read from "aio_sendfile" alone, as "aio_sendfile" only |
377 | 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 |
378 | 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. |
379 | |
381 | |
380 | 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 |
381 | "aio_sendfile" on non-blocking sockets, as long as one end |
383 | "aio_sendfile" on non-blocking sockets, as long as one end |
382 | (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 |
383 | asynchronous, while the socket I/O will be non-blocking. Note, |
385 | asynchronous, while the socket I/O will be non-blocking. Note, |
384 | 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 |
385 | 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 |
386 | 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 |
387 | lost, forcing "aio_sendfile" to again hit the disk. Explicit |
389 | lost, forcing "aio_sendfile" to again hit the disk. Explicit |
388 | "aio_read" + "aio_write" let's you control resource usage much |
390 | "aio_read" + "aio_write" let's you better control resource usage. |
389 | better. |
|
|
390 | |
391 | |
391 | 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 |
392 | 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 |
393 | 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. |
394 | |
395 | |
395 | 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", |
396 | "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or "ENOTSOCK", |
397 | "EINVAL", "ENOTSUP", "EOPNOTSUPP", "EAFNOSUPPORT", "EPROTOTYPE" or |
397 | 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 |
398 | 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. |
399 | |
409 | |
400 | aio_readahead $fh,$offset,$length, $callback->($retval) |
410 | aio_readahead $fh,$offset,$length, $callback->($retval) |
401 | "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 |
402 | 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 |
403 | $offset argument specifies the starting point from which data is to |
413 | $offset argument specifies the starting point from which data is to |
… | |
… | |
1170 | IO::AIO::idle_timeout $seconds |
1180 | IO::AIO::idle_timeout $seconds |
1171 | Sets the minimum idle timeout (default 10) after which worker |
1181 | Sets the minimum idle timeout (default 10) after which worker |
1172 | threads are allowed to exit. SEe "IO::AIO::max_idle". |
1182 | threads are allowed to exit. SEe "IO::AIO::max_idle". |
1173 | |
1183 | |
1174 | 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 | |
1175 | 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 |
1176 | 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 |
1177 | inexact: Better use an "aio_group" together with a feed callback. |
1196 | inexact: Better use an "aio_group" together with a feed callback. |
1178 | |
1197 | |
1179 | 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 |
1180 | 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: |
1181 | "poll_cb" (and "poll_some" and other functions calling "poll_cb") |
|
|
1182 | function will block until the limit is no longer exceeded. |
|
|
1183 | |
1200 | |
1184 | 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 |
1185 | the number of outstanding requests. |
1217 | no practical limit on the number of outstanding requests. |
1186 | |
|
|
1187 | You can still queue as many requests as you want. Therefore, |
|
|
1188 | "max_outstanding" is mainly useful in simple scripts (with low |
|
|
1189 | values) or as a stop gap to shield against fatal memory overflow |
|
|
1190 | (with large values). |
|
|
1191 | |
1218 | |
1192 | STATISTICAL INFORMATION |
1219 | STATISTICAL INFORMATION |
1193 | IO::AIO::nreqs |
1220 | IO::AIO::nreqs |
1194 | Returns the number of requests currently in the ready, execute or |
1221 | Returns the number of requests currently in the ready, execute or |
1195 | 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 |
… | |
… | |
1341 | # Danga::Socket integration |
1368 | # Danga::Socket integration |
1342 | Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => |
1369 | Danga::Socket->AddOtherFds (IO::AIO::poll_fileno => |
1343 | \&IO::AIO::poll_cb); |
1370 | \&IO::AIO::poll_cb); |
1344 | |
1371 | |
1345 | FORK BEHAVIOUR |
1372 | FORK BEHAVIOUR |
1346 | 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. |
1347 | |
1379 | |
1348 | 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 |
1349 | 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. |
1350 | fork the parent simply leaves the quiescent state and continues |
1382 | At the time of this writing (2011) only GNU/Linux supports these |
1351 | request/result processing, while the child frees the request/result |
1383 | extensions to POSIX. |
1352 | queue (so that the requests started before the fork will only be handled |
|
|
1353 | in the parent). Threads will be started on demand until the limit set in |
|
|
1354 | the parent process has been reached again. |
|
|
1355 | |
|
|
1356 | In short: the parent will, after a short pause, continue as if fork had |
|
|
1357 | not been called, while the child will act as if IO::AIO has not been |
|
|
1358 | used yet. |
|
|
1359 | |
1384 | |
1360 | MEMORY USAGE |
1385 | MEMORY USAGE |
1361 | Per-request usage: |
1386 | Per-request usage: |
1362 | |
1387 | |
1363 | Each aio request uses - depending on your architecture - around 100-200 |
1388 | Each aio request uses - depending on your architecture - around 100-200 |