… | |
… | |
384 | =item C<EVBACKEND_EPOLL> (value 4, Linux) |
384 | =item C<EVBACKEND_EPOLL> (value 4, Linux) |
385 | |
385 | |
386 | For few fds, this backend is a bit little slower than poll and select, |
386 | For few fds, this backend is a bit little slower than poll and select, |
387 | but it scales phenomenally better. While poll and select usually scale |
387 | but it scales phenomenally better. While poll and select usually scale |
388 | like O(total_fds) where n is the total number of fds (or the highest fd), |
388 | like O(total_fds) where n is the total number of fds (or the highest fd), |
389 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
389 | epoll scales either O(1) or O(active_fds). |
390 | of shortcomings, such as silently dropping events in some hard-to-detect |
390 | |
391 | cases and requiring a system call per fd change, no fork support and bad |
391 | The epoll syscalls are the most misdesigned of the more advanced event |
392 | support for dup. |
392 | mechanisms: problems include silently dropping fds, requiring a system |
|
|
393 | call per change per fd (and unnecessary guessing of parameters), problems |
|
|
394 | with dup and so on. The biggest issue is fork races, however - if a |
|
|
395 | program forks then I<both> parent and child process have to recreate the |
|
|
396 | epoll set, which can take considerable time (one syscall per fd) and is of |
|
|
397 | course hard to detect. |
|
|
398 | |
|
|
399 | Epoll is also notoriously buggy - embedding epoll fds should work, but |
|
|
400 | of course doesn't, and epoll just loves to report events for totally |
|
|
401 | I<different> file descriptors (even already closed ones, so one cannot |
|
|
402 | even remove them from the set) than registered in the set (especially |
|
|
403 | on SMP systems). Libev tries to counter these spurious notifications by |
|
|
404 | employing an additional generation counter and comparing that against the |
|
|
405 | events to filter out spurious ones. |
393 | |
406 | |
394 | While stopping, setting and starting an I/O watcher in the same iteration |
407 | While stopping, setting and starting an I/O watcher in the same iteration |
395 | will result in some caching, there is still a system call per such incident |
408 | will result in some caching, there is still a system call per such incident |
396 | (because the fd could point to a different file description now), so its |
409 | (because the fd could point to a different file description now), so its |
397 | best to avoid that. Also, C<dup ()>'ed file descriptors might not work |
410 | best to avoid that. Also, C<dup ()>'ed file descriptors might not work |
398 | very well if you register events for both fds. |
411 | very well if you register events for both fds. |
399 | |
412 | |
400 | Please note that epoll sometimes generates spurious notifications, so you |
|
|
401 | need to use non-blocking I/O or other means to avoid blocking when no data |
|
|
402 | (or space) is available. |
|
|
403 | |
|
|
404 | Best performance from this backend is achieved by not unregistering all |
413 | Best performance from this backend is achieved by not unregistering all |
405 | watchers for a file descriptor until it has been closed, if possible, |
414 | watchers for a file descriptor until it has been closed, if possible, |
406 | i.e. keep at least one watcher active per fd at all times. Stopping and |
415 | i.e. keep at least one watcher active per fd at all times. Stopping and |
407 | starting a watcher (without re-setting it) also usually doesn't cause |
416 | starting a watcher (without re-setting it) also usually doesn't cause |
408 | extra overhead. |
417 | extra overhead. A fork can both result in spurious notifications as well |
|
|
418 | as in libev having to destroy and recreate the epoll object, which can |
|
|
419 | take considerable time and thus should be avoided. |
409 | |
420 | |
410 | While nominally embeddable in other event loops, this feature is broken in |
421 | While nominally embeddable in other event loops, this feature is broken in |
411 | all kernel versions tested so far. |
422 | all kernel versions tested so far. |
412 | |
423 | |
413 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
424 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
… | |
… | |
428 | |
439 | |
429 | It scales in the same way as the epoll backend, but the interface to the |
440 | It scales in the same way as the epoll backend, but the interface to the |
430 | kernel is more efficient (which says nothing about its actual speed, of |
441 | kernel is more efficient (which says nothing about its actual speed, of |
431 | course). While stopping, setting and starting an I/O watcher does never |
442 | course). While stopping, setting and starting an I/O watcher does never |
432 | cause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to |
443 | cause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to |
433 | two event changes per incident. Support for C<fork ()> is very bad and it |
444 | two event changes per incident. Support for C<fork ()> is very bad (but |
434 | drops fds silently in similarly hard-to-detect cases. |
445 | sane, unlike epoll) and it drops fds silently in similarly hard-to-detect |
|
|
446 | cases |
435 | |
447 | |
436 | This backend usually performs well under most conditions. |
448 | This backend usually performs well under most conditions. |
437 | |
449 | |
438 | While nominally embeddable in other event loops, this doesn't work |
450 | While nominally embeddable in other event loops, this doesn't work |
439 | everywhere, so you might need to test for this. And since it is broken |
451 | everywhere, so you might need to test for this. And since it is broken |
… | |
… | |
468 | might perform better. |
480 | might perform better. |
469 | |
481 | |
470 | On the positive side, with the exception of the spurious readiness |
482 | On the positive side, with the exception of the spurious readiness |
471 | notifications, this backend actually performed fully to specification |
483 | notifications, this backend actually performed fully to specification |
472 | in all tests and is fully embeddable, which is a rare feat among the |
484 | in all tests and is fully embeddable, which is a rare feat among the |
473 | OS-specific backends. |
485 | OS-specific backends (I vastly prefer correctness over speed hacks). |
474 | |
486 | |
475 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
487 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
476 | C<EVBACKEND_POLL>. |
488 | C<EVBACKEND_POLL>. |
477 | |
489 | |
478 | =item C<EVBACKEND_ALL> |
490 | =item C<EVBACKEND_ALL> |
… | |
… | |
1910 | |
1922 | |
1911 | |
1923 | |
1912 | =head2 C<ev_stat> - did the file attributes just change? |
1924 | =head2 C<ev_stat> - did the file attributes just change? |
1913 | |
1925 | |
1914 | This watches a file system path for attribute changes. That is, it calls |
1926 | This watches a file system path for attribute changes. That is, it calls |
1915 | C<stat> regularly (or when the OS says it changed) and sees if it changed |
1927 | C<stat> on that path in regular intervals (or when the OS says it changed) |
1916 | compared to the last time, invoking the callback if it did. |
1928 | and sees if it changed compared to the last time, invoking the callback if |
|
|
1929 | it did. |
1917 | |
1930 | |
1918 | The path does not need to exist: changing from "path exists" to "path does |
1931 | The path does not need to exist: changing from "path exists" to "path does |
1919 | not exist" is a status change like any other. The condition "path does |
1932 | not exist" is a status change like any other. The condition "path does |
1920 | not exist" is signified by the C<st_nlink> field being zero (which is |
1933 | not exist" is signified by the C<st_nlink> field being zero (which is |
1921 | otherwise always forced to be at least one) and all the other fields of |
1934 | otherwise always forced to be at least one) and all the other fields of |
1922 | the stat buffer having unspecified contents. |
1935 | the stat buffer having unspecified contents. |
1923 | |
1936 | |
1924 | The path I<should> be absolute and I<must not> end in a slash. If it is |
1937 | The path I<must not> end in a slash or contain special components such as |
|
|
1938 | C<.> or C<..>. The path I<should> be absolute: If it is relative and |
1925 | relative and your working directory changes, the behaviour is undefined. |
1939 | your working directory changes, then the behaviour is undefined. |
1926 | |
1940 | |
1927 | Since there is no standard kernel interface to do this, the portable |
1941 | Since there is no portable change notification interface available, the |
1928 | implementation simply calls C<stat (2)> regularly on the path to see if |
1942 | portable implementation simply calls C<stat(2)> regularly on the path |
1929 | it changed somehow. You can specify a recommended polling interval for |
1943 | to see if it changed somehow. You can specify a recommended polling |
1930 | this case. If you specify a polling interval of C<0> (highly recommended!) |
1944 | interval for this case. If you specify a polling interval of C<0> (highly |
1931 | then a I<suitable, unspecified default> value will be used (which |
1945 | recommended!) then a I<suitable, unspecified default> value will be used |
1932 | you can expect to be around five seconds, although this might change |
1946 | (which you can expect to be around five seconds, although this might |
1933 | dynamically). Libev will also impose a minimum interval which is currently |
1947 | change dynamically). Libev will also impose a minimum interval which is |
1934 | around C<0.1>, but thats usually overkill. |
1948 | currently around C<0.1>, but thats usually overkill. |
1935 | |
1949 | |
1936 | This watcher type is not meant for massive numbers of stat watchers, |
1950 | This watcher type is not meant for massive numbers of stat watchers, |
1937 | as even with OS-supported change notifications, this can be |
1951 | as even with OS-supported change notifications, this can be |
1938 | resource-intensive. |
1952 | resource-intensive. |
1939 | |
1953 | |
… | |
… | |
1949 | support disabled by default, you get the 32 bit version of the stat |
1963 | support disabled by default, you get the 32 bit version of the stat |
1950 | structure. When using the library from programs that change the ABI to |
1964 | structure. When using the library from programs that change the ABI to |
1951 | use 64 bit file offsets the programs will fail. In that case you have to |
1965 | use 64 bit file offsets the programs will fail. In that case you have to |
1952 | compile libev with the same flags to get binary compatibility. This is |
1966 | compile libev with the same flags to get binary compatibility. This is |
1953 | obviously the case with any flags that change the ABI, but the problem is |
1967 | obviously the case with any flags that change the ABI, but the problem is |
1954 | most noticeably disabled with ev_stat and large file support. |
1968 | most noticeably displayed with ev_stat and large file support. |
1955 | |
1969 | |
1956 | The solution for this is to lobby your distribution maker to make large |
1970 | The solution for this is to lobby your distribution maker to make large |
1957 | file interfaces available by default (as e.g. FreeBSD does) and not |
1971 | file interfaces available by default (as e.g. FreeBSD does) and not |
1958 | optional. Libev cannot simply switch on large file support because it has |
1972 | optional. Libev cannot simply switch on large file support because it has |
1959 | to exchange stat structures with application programs compiled using the |
1973 | to exchange stat structures with application programs compiled using the |
… | |
… | |
1978 | descriptor open on the object at all times, and detecting renames, unlinks |
1992 | descriptor open on the object at all times, and detecting renames, unlinks |
1979 | etc. is difficult. |
1993 | etc. is difficult. |
1980 | |
1994 | |
1981 | =head3 The special problem of stat time resolution |
1995 | =head3 The special problem of stat time resolution |
1982 | |
1996 | |
1983 | The C<stat ()> system call only supports full-second resolution portably, and |
1997 | The C<stat ()> system call only supports full-second resolution portably, |
1984 | even on systems where the resolution is higher, most file systems still |
1998 | and even on systems where the resolution is higher, most file systems |
1985 | only support whole seconds. |
1999 | still only support whole seconds. |
1986 | |
2000 | |
1987 | That means that, if the time is the only thing that changes, you can |
2001 | That means that, if the time is the only thing that changes, you can |
1988 | easily miss updates: on the first update, C<ev_stat> detects a change and |
2002 | easily miss updates: on the first update, C<ev_stat> detects a change and |
1989 | calls your callback, which does something. When there is another update |
2003 | calls your callback, which does something. When there is another update |
1990 | within the same second, C<ev_stat> will be unable to detect unless the |
2004 | within the same second, C<ev_stat> will be unable to detect unless the |