--- libev/ev.pod 2008/10/27 12:20:32 1.205 +++ libev/ev.pod 2009/02/06 20:17:43 1.224 @@ -11,6 +11,8 @@ // a single header file is required #include + #include // for puts + // every watcher type has its own typedef'd struct // with the name ev_TYPE ev_io stdin_watcher; @@ -43,7 +45,7 @@ main (void) { // use the default event loop unless you have special needs - ev_loop *loop = ev_default_loop (0); + struct ev_loop *loop = ev_default_loop (0); // initialise an io watcher, then start it // this one will watch for stdin to become readable @@ -300,7 +302,7 @@ Note that this function is I thread-safe, so if you want to use it from multiple threads, you have to lock (note also that this is unlikely, -as loops cannot bes hared easily between threads anyway). +as loops cannot be shared easily between threads anyway). The default loop is the only loop that can handle C and C watchers, and to do this, it always registers a handler @@ -388,30 +390,41 @@ like O(total_fds) where n is the total number of fds (or the highest fd), epoll scales either O(1) or O(active_fds). -The epoll syscalls are the most misdesigned of the more advanced -event mechanisms: probelsm include silently dropping events in some -hard-to-detect cases, requiring a system call per fd change, no fork -support, problems with dup and so on. +The epoll mechanism deserves honorable mention as the most misdesigned +of the more advanced event mechanisms: mere annoyances include silently +dropping file descriptors, requiring a system call per change per file +descriptor (and unnecessary guessing of parameters), problems with dup and +so on. The biggest issue is fork races, however - if a program forks then +I parent and child process have to recreate the epoll set, which can +take considerable time (one syscall per file descriptor) and is of course +hard to detect. -Epoll is also notoriously buggy - embedding epoll fds should work, but -of course doesn't, and epoll just loves to report events for totally +Epoll is also notoriously buggy - embedding epoll fds I work, but +of course I, and epoll just loves to report events for totally I file descriptors (even already closed ones, so one cannot even remove them from the set) than registered in the set (especially on SMP systems). Libev tries to counter these spurious notifications by employing an additional generation counter and comparing that against the -events to filter out spurious ones. +events to filter out spurious ones, recreating the set when required. While stopping, setting and starting an I/O watcher in the same iteration -will result in some caching, there is still a system call per such incident -(because the fd could point to a different file description now), so its -best to avoid that. Also, C'ed file descriptors might not work -very well if you register events for both fds. +will result in some caching, there is still a system call per such +incident (because the same I could point to a different +I now), so its best to avoid that. Also, C'ed +file descriptors might not work very well if you register events for both +file descriptors. Best performance from this backend is achieved by not unregistering all watchers for a file descriptor until it has been closed, if possible, i.e. keep at least one watcher active per fd at all times. Stopping and starting a watcher (without re-setting it) also usually doesn't cause -extra overhead. +extra overhead. A fork can both result in spurious notifications as well +as in libev having to destroy and recreate the epoll object, which can +take considerable time and thus should be avoided. + +All this means that, in practice, C can be as fast or +faster than epoll for maybe up to a hundred file descriptors, depending on +the usage. So sad. While nominally embeddable in other event loops, this feature is broken in all kernel versions tested so far. @@ -421,12 +434,15 @@ =item C (value 8, most BSD clones) -Kqueue deserves special mention, as at the time of this writing, it was -broken on all BSDs except NetBSD (usually it doesn't work reliably with -anything but sockets and pipes, except on Darwin, where of course it's -completely useless). For this reason it's not being "auto-detected" unless -you explicitly specify it in the flags (i.e. using C) or -libev was compiled on a known-to-be-good (-enough) system like NetBSD. +Kqueue deserves special mention, as at the time of this writing, it +was broken on all BSDs except NetBSD (usually it doesn't work reliably +with anything but sockets and pipes, except on Darwin, where of course +it's completely useless). Unlike epoll, however, whose brokenness +is by design, these kqueue bugs can (and eventually will) be fixed +without API changes to existing programs. For this reason it's not being +"auto-detected" unless you explicitly specify it in the flags (i.e. using +C) or libev was compiled on a known-to-be-good (-enough) +system like NetBSD. You still can embed kqueue into a normal poll or select backend and use it only for sockets (after having made sure that sockets work with kqueue on @@ -436,8 +452,9 @@ kernel is more efficient (which says nothing about its actual speed, of course). While stopping, setting and starting an I/O watcher does never cause an extra system call as with C, it still adds up to -two event changes per incident. Support for C is very bad and it -drops fds silently in similarly hard-to-detect cases. +two event changes per incident. Support for C is very bad (but +sane, unlike epoll) and it drops fds silently in similarly hard-to-detect +cases This backend usually performs well under most conditions. @@ -445,8 +462,8 @@ everywhere, so you might need to test for this. And since it is broken almost everywhere, you should only use it when you have a lot of sockets (for which it usually works), by embedding it into another event loop -(e.g. C or C) and, did I mention it, -using it only for sockets. +(e.g. C or C (but C is of course +also broken on OS X)) and, did I mention it, using it only for sockets. This backend maps C into an C kevent with C, and C into an C kevent with @@ -476,7 +493,7 @@ On the positive side, with the exception of the spurious readiness notifications, this backend actually performed fully to specification in all tests and is fully embeddable, which is a rare feat among the -OS-specific backends. +OS-specific backends (I vastly prefer correctness over speed hacks). This backend maps C and C in the same way as C. @@ -643,7 +660,7 @@ A flags value of C will look for new events (waiting if necessary) and will handle those and any already outstanding ones. It will block your process until at least one new event arrives (which could -be an event internal to libev itself, so there is no guarentee that a +be an event internal to libev itself, so there is no guarantee that a user-registered callback will be called), and will return after one iteration of the loop. @@ -1406,7 +1423,7 @@ // callback was invoked, but there was some activity, re-arm // the watcher to fire in last_activity + 60, which is // guaranteed to be in the future, so "again" is positive: - w->again = timeout - now; + w->repeat = timeout - now; ev_timer_again (EV_A_ w); } } @@ -1918,35 +1935,38 @@ =head2 C - did the file attributes just change? This watches a file system path for attribute changes. That is, it calls -C regularly (or when the OS says it changed) and sees if it changed -compared to the last time, invoking the callback if it did. +C on that path in regular intervals (or when the OS says it changed) +and sees if it changed compared to the last time, invoking the callback if +it did. The path does not need to exist: changing from "path exists" to "path does -not exist" is a status change like any other. The condition "path does -not exist" is signified by the C field being zero (which is -otherwise always forced to be at least one) and all the other fields of -the stat buffer having unspecified contents. - -The path I be absolute and I end in a slash. If it is -relative and your working directory changes, the behaviour is undefined. - -Since there is no standard kernel interface to do this, the portable -implementation simply calls C regularly on the path to see if -it changed somehow. You can specify a recommended polling interval for -this case. If you specify a polling interval of C<0> (highly recommended!) -then a I value will be used (which -you can expect to be around five seconds, although this might change -dynamically). Libev will also impose a minimum interval which is currently -around C<0.1>, but thats usually overkill. +not exist" is a status change like any other. The condition "path does not +exist" (or more correctly "path cannot be stat'ed") is signified by the +C field being zero (which is otherwise always forced to be at +least one) and all the other fields of the stat buffer having unspecified +contents. + +The path I end in a slash or contain special components such as +C<.> or C<..>. The path I be absolute: If it is relative and +your working directory changes, then the behaviour is undefined. + +Since there is no portable change notification interface available, the +portable implementation simply calls C regularly on the path +to see if it changed somehow. You can specify a recommended polling +interval for this case. If you specify a polling interval of C<0> (highly +recommended!) then a I value will be used +(which you can expect to be around five seconds, although this might +change dynamically). Libev will also impose a minimum interval which is +currently around C<0.1>, but that's usually overkill. This watcher type is not meant for massive numbers of stat watchers, as even with OS-supported change notifications, this can be resource-intensive. At the time of this writing, the only OS-specific interface implemented -is the Linux inotify interface (implementing kqueue support is left as -an exercise for the reader. Note, however, that the author sees no way -of implementing C semantics with kqueue). +is the Linux inotify interface (implementing kqueue support is left as an +exercise for the reader. Note, however, that the author sees no way of +implementing C semantics with kqueue, except as a hint). =head3 ABI Issues (Largefile Support) @@ -1957,7 +1977,7 @@ use 64 bit file offsets the programs will fail. In that case you have to compile libev with the same flags to get binary compatibility. This is obviously the case with any flags that change the ABI, but the problem is -most noticeably disabled with ev_stat and large file support. +most noticeably displayed with ev_stat and large file support. The solution for this is to lobby your distribution maker to make large file interfaces available by default (as e.g. FreeBSD does) and not @@ -1967,28 +1987,48 @@ =head3 Inotify and Kqueue -When C support has been compiled into libev (generally -only available with Linux 2.6.25 or above due to bugs in earlier -implementations) and present at runtime, it will be used to speed up -change detection where possible. The inotify descriptor will be created -lazily when the first C watcher is being started. +When C support has been compiled into libev and present at +runtime, it will be used to speed up change detection where possible. The +inotify descriptor will be created lazily when the first C +watcher is being started. Inotify presence does not change the semantics of C watchers except that changes might be detected earlier, and in some cases, to avoid making regular C calls. Even in the presence of inotify support there are many cases where libev has to resort to regular C polling, -but as long as the path exists, libev usually gets away without polling. +but as long as kernel 2.6.25 or newer is used (2.6.24 and older have too +many bugs), the path exists (i.e. stat succeeds), and the path resides on +a local filesystem (libev currently assumes only ext2/3, jfs, reiserfs and +xfs are fully working) libev usually gets away without polling. There is no support for kqueue, as apparently it cannot be used to implement this functionality, due to the requirement of having a file descriptor open on the object at all times, and detecting renames, unlinks etc. is difficult. +=head3 C is a synchronous operation + +Libev doesn't normally do any kind of I/O itself, and so is not blocking +the process. The exception are C watchers - those call C, which is a synchronous operation. + +For local paths, this usually doesn't matter: unless the system is very +busy or the intervals between stat's are large, a stat call will be fast, +as the path data is usually in memory already (except when starting the +watcher). + +For networked file systems, calling C can block an indefinite +time due to network issues, and even under good conditions, a stat call +often takes multiple milliseconds. + +Therefore, it is best to avoid using C watchers on networked +paths, although this is fully supported by libev. + =head3 The special problem of stat time resolution -The C system call only supports full-second resolution portably, and -even on systems where the resolution is higher, most file systems still -only support whole seconds. +The C system call only supports full-second resolution portably, +and even on systems where the resolution is higher, most file systems +still only support whole seconds. That means that, if the time is the only thing that changes, you can easily miss updates: on the first update, C detects a change and @@ -2390,24 +2430,20 @@ this case you would put all the high priority stuff in one loop and all the rest in a second one, and embed the second one in the first. -As long as the watcher is active, the callback will be invoked every time -there might be events pending in the embedded loop. The callback must then -call C to make a single sweep and invoke -their callbacks (you could also start an idle watcher to give the embedded -loop strictly lower priority for example). You can also set the callback -to C<0>, in which case the embed watcher will automatically execute the -embedded loop sweep. - -As long as the watcher is started it will automatically handle events. The -callback will be invoked whenever some events have been handled. You can -set the callback to C<0> to avoid having to specify one if you are not -interested in that. - -Also, there have not currently been made special provisions for forking: -when you fork, you not only have to call C on both loops, -but you will also have to stop and restart any C watchers -yourself - but you can use a fork watcher to handle this automatically, -and future versions of libev might do just that. +As long as the watcher is active, the callback will be invoked every +time there might be events pending in the embedded loop. The callback +must then call C to make a single +sweep and invoke their callbacks (the callback doesn't need to invoke the +C function directly, it could also start an idle watcher +to give the embedded loop strictly lower priority for example). + +You can also set the callback to C<0>, in which case the embed watcher +will automatically execute the embedded loop sweep whenever necessary. + +Fork detection will be handled transparently while the C watcher +is active, i.e., the embedded loop will automatically be forked when the +embedding loop forks. In other cases, the user is responsible for calling +C on the embedded loop. Unfortunately, not all backends are embeddable: only the ones returned by C are, which, unfortunately, does not include any @@ -2637,7 +2673,7 @@ =item ev_async_init (ev_async *, callback) Initialises and configures the async watcher - it has no parameters of any -kind. There is a C macro, but using it is utterly pointless, +kind. There is a C macro, but using it is utterly pointless, trust me. =item ev_async_send (loop, ev_async *) @@ -2853,6 +2889,36 @@ ev::io iow; iow.set (&obj); +=item w->set (object *) + +This is an B feature that might go away in a future version. + +This is a variation of a method callback - leaving out the method to call +will default the method to C, which makes it possible to use +functor objects without having to manually specify the C all +the time. Incidentally, you can then also leave out the template argument +list. + +The C method prototype must be C. + +See the method-C above for more details. + +Example: use a functor object as callback. + + struct myfunctor + { + void operator() (ev::io &w, int revents) + { + ... + } + } + + myfunctor f; + + ev::io w; + w.set (&f); + =item w->set (void *data = 0) Also sets a callback, but uses a static method or plain function as @@ -2961,6 +3027,9 @@ more on top of it. It can be found via gem servers. Its homepage is at L. +Roger Pack reports that using the link order C<-lws2_32 -lmsvcrt-ruby-190> +makes rev work even on mingw. + =item D Leandro Lucarella has written a D language binding (F) for libev, to @@ -3080,7 +3149,7 @@ #include "ev.h" Both header files and implementation files can be compiled with a C++ -compiler (at least, thats a stated goal, and breakage will be treated +compiler (at least, that's a stated goal, and breakage will be treated as a bug). You need the following files in your source tree, or in a directory @@ -3146,24 +3215,40 @@ supported). It will also not define any of the structs usually found in F that are not directly supported by the libev core alone. +In stanbdalone mode, libev will still try to automatically deduce the +configuration, but has to be more conservative. + =item EV_USE_MONOTONIC If defined to be C<1>, libev will try to detect the availability of the -monotonic clock option at both compile time and runtime. Otherwise no use -of the monotonic clock option will be attempted. If you enable this, you -usually have to link against librt or something similar. Enabling it when -the functionality isn't available is safe, though, although you have +monotonic clock option at both compile time and runtime. Otherwise no +use of the monotonic clock option will be attempted. If you enable this, +you usually have to link against librt or something similar. Enabling it +when the functionality isn't available is safe, though, although you have to make sure you link against any libraries where the C -function is hiding in (often F<-lrt>). +function is hiding in (often F<-lrt>). See also C. =item EV_USE_REALTIME If defined to be C<1>, libev will try to detect the availability of the -real-time clock option at compile time (and assume its availability at -runtime if successful). Otherwise no use of the real-time clock option will -be attempted. This effectively replaces C by C and will not normally affect correctness. See the -note about libraries in the description of C, though. +real-time clock option at compile time (and assume its availability +at runtime if successful). Otherwise no use of the real-time clock +option will be attempted. This effectively replaces C +by C and will not normally affect +correctness. See the note about libraries in the description of +C, though. Defaults to the opposite value of +C. + +=item EV_USE_CLOCK_SYSCALL + +If defined to be C<1>, libev will try to use a direct syscall instead +of calling the system-provided C function. This option +exists because on GNU/Linux, C is in C, but C +unconditionally pulls in C, slowing down single-threaded +programs needlessly. Using a direct syscall is slightly slower (in +theory), because no optimised vdso implementation can be used, but avoids +the pthread dependency. Defaults to C<1> on GNU/Linux with glibc 2.x or +higher, as it simplifies linking (no need for C<-lrt>). =item EV_USE_NANOSLEEP @@ -3189,11 +3274,11 @@ If defined to C<1>, then the select backend will use the system C structure. This is useful if libev doesn't compile due to a missing -C or C definition or it mis-guesses the bitset layout on -exotic systems. This usually limits the range of file descriptors to some -low limit such as 1024 or might have other limitations (winsocket only -allows 64 sockets). The C macro, set before compilation, might -influence the size of the C used. +C or C definition or it mis-guesses the bitset layout +on exotic systems. This usually limits the range of file descriptors to +some low limit such as 1024 or might have other limitations (winsocket +only allows 64 sockets). The C macro, set before compilation, +configures the maximum size of the C. =item EV_SELECT_IS_WINSOCKET @@ -3562,7 +3647,7 @@ Care has been taken to ensure that libev does not keep local state inside C, and other calls do not usually allow for coroutine switches as -they do not clal any callbacks. +they do not call any callbacks. =head2 COMPILER WARNINGS @@ -3606,7 +3691,7 @@ ==2274== still reachable: 256 bytes in 1 blocks. Then there is no memory leak, just as memory accounted to global variables -is not a memleak - the memory is still being refernced, and didn't leak. +is not a memleak - the memory is still being referenced, and didn't leak. Similarly, under some circumstances, valgrind might report kernel bugs as if it were a bug in libev (e.g. in realloc or in the poll backend, @@ -3854,5 +3939,5 @@ =head1 AUTHOR -Marc Lehmann . +Marc Lehmann , with repeated corrections by Mikael Magnusson.