ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.150 by root, Tue May 6 23:34:16 2008 UTC vs.
Revision 1.156 by root, Tue May 20 20:00:34 2008 UTC

64 64
65=head1 DESCRIPTION 65=head1 DESCRIPTION
66 66
67The newest version of this document is also available as an html-formatted 67The newest version of this document is also available as an html-formatted
68web page you might find easier to navigate when reading it for the first 68web page you might find easier to navigate when reading it for the first
69time: L<http://cvs.schmorp.de/libev/ev.html>. 69time: L<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>.
70 70
71Libev is an event loop: you register interest in certain events (such as a 71Libev is an event loop: you register interest in certain events (such as a
72file descriptor being readable or a timeout occurring), and it will manage 72file descriptor being readable or a timeout occurring), and it will manage
73these event sources and provide your program with events. 73these event sources and provide your program with events.
74 74
336To get good performance out of this backend you need a high amount of 336To get good performance out of this backend you need a high amount of
337parallelity (most of the file descriptors should be busy). If you are 337parallelity (most of the file descriptors should be busy). If you are
338writing a server, you should C<accept ()> in a loop to accept as many 338writing a server, you should C<accept ()> in a loop to accept as many
339connections as possible during one iteration. You might also want to have 339connections as possible during one iteration. You might also want to have
340a look at C<ev_set_io_collect_interval ()> to increase the amount of 340a look at C<ev_set_io_collect_interval ()> to increase the amount of
341readyness notifications you get per iteration. 341readiness notifications you get per iteration.
342 342
343=item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) 343=item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows)
344 344
345And this is your standard poll(2) backend. It's more complicated 345And this is your standard poll(2) backend. It's more complicated
346than select, but handles sparse fds better and has no artificial 346than select, but handles sparse fds better and has no artificial
425While this backend scales well, it requires one system call per active 425While this backend scales well, it requires one system call per active
426file descriptor per loop iteration. For small and medium numbers of file 426file descriptor per loop iteration. For small and medium numbers of file
427descriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend 427descriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend
428might perform better. 428might perform better.
429 429
430On the positive side, ignoring the spurious readyness notifications, this 430On the positive side, ignoring the spurious readiness notifications, this
431backend actually performed to specification in all tests and is fully 431backend actually performed to specification in all tests and is fully
432embeddable, which is a rare feat among the OS-specific backends. 432embeddable, which is a rare feat among the OS-specific backends.
433 433
434=item C<EVBACKEND_ALL> 434=item C<EVBACKEND_ALL>
435 435
1032If you must do this, then force the use of a known-to-be-good backend 1032If you must do this, then force the use of a known-to-be-good backend
1033(at the time of this writing, this includes only C<EVBACKEND_SELECT> and 1033(at the time of this writing, this includes only C<EVBACKEND_SELECT> and
1034C<EVBACKEND_POLL>). 1034C<EVBACKEND_POLL>).
1035 1035
1036Another thing you have to watch out for is that it is quite easy to 1036Another thing you have to watch out for is that it is quite easy to
1037receive "spurious" readyness notifications, that is your callback might 1037receive "spurious" readiness notifications, that is your callback might
1038be called with C<EV_READ> but a subsequent C<read>(2) will actually block 1038be called with C<EV_READ> but a subsequent C<read>(2) will actually block
1039because there is no data. Not only are some backends known to create a 1039because there is no data. Not only are some backends known to create a
1040lot of those (for example solaris ports), it is very easy to get into 1040lot of those (for example solaris ports), it is very easy to get into
1041this situation even with a relatively standard program structure. Thus 1041this situation even with a relatively standard program structure. Thus
1042it is best to always use non-blocking I/O: An extra C<read>(2) returning 1042it is best to always use non-blocking I/O: An extra C<read>(2) returning
1657calls your callback, which does something. When there is another update 1657calls your callback, which does something. When there is another update
1658within the same second, C<ev_stat> will be unable to detect it as the stat 1658within the same second, C<ev_stat> will be unable to detect it as the stat
1659data does not change. 1659data does not change.
1660 1660
1661The solution to this is to delay acting on a change for slightly more 1661The solution to this is to delay acting on a change for slightly more
1662than second (or till slightly after the next full second boundary), using 1662than a second (or till slightly after the next full second boundary), using
1663a roughly one-second-delay C<ev_timer> (e.g. C<ev_timer_set (w, 0., 1.02); 1663a roughly one-second-delay C<ev_timer> (e.g. C<ev_timer_set (w, 0., 1.02);
1664ev_timer_again (loop, w)>). 1664ev_timer_again (loop, w)>).
1665 1665
1666The C<.02> offset is added to work around small timing inconsistencies 1666The C<.02> offset is added to work around small timing inconsistencies
1667of some operating systems (where the second counter of the current time 1667of some operating systems (where the second counter of the current time
2980defined to be C<0>, then they are not. 2980defined to be C<0>, then they are not.
2981 2981
2982=item EV_MINIMAL 2982=item EV_MINIMAL
2983 2983
2984If you need to shave off some kilobytes of code at the expense of some 2984If you need to shave off some kilobytes of code at the expense of some
2985speed, define this symbol to C<1>. Currently only used for gcc to override 2985speed, define this symbol to C<1>. Currently this is used to override some
2986some inlining decisions, saves roughly 30% codesize of amd64. 2986inlining decisions, saves roughly 30% codesize of amd64. It also selects a
2987much smaller 2-heap for timer management over the default 4-heap.
2987 2988
2988=item EV_PID_HASHSIZE 2989=item EV_PID_HASHSIZE
2989 2990
2990C<ev_child> watchers use a small hash table to distribute workload by 2991C<ev_child> watchers use a small hash table to distribute workload by
2991pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more 2992pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more
2997C<ev_stat> watchers use a small hash table to distribute workload by 2998C<ev_stat> watchers use a small hash table to distribute workload by
2998inotify watch id. The default size is C<16> (or C<1> with C<EV_MINIMAL>), 2999inotify watch id. The default size is C<16> (or C<1> with C<EV_MINIMAL>),
2999usually more than enough. If you need to manage thousands of C<ev_stat> 3000usually more than enough. If you need to manage thousands of C<ev_stat>
3000watchers you might want to increase this value (I<must> be a power of 3001watchers you might want to increase this value (I<must> be a power of
3001two). 3002two).
3003
3004=item EV_USE_4HEAP
3005
3006Heaps are not very cache-efficient. To improve the cache-efficiency of the
3007timer and periodics heap, libev uses a 4-heap when this symbol is defined
3008to C<1>. The 4-heap uses more complicated (longer) code but has
3009noticably faster performance with many (thousands) of watchers.
3010
3011The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0>
3012(disabled).
3013
3014=item EV_HEAP_CACHE_AT
3015
3016Heaps are not very cache-efficient. To improve the cache-efficiency of the
3017timer and periodics heap, libev can cache the timestamp (I<at>) within
3018the heap structure (selected by defining C<EV_HEAP_CACHE_AT> to C<1>),
3019which uses 8-12 bytes more per watcher and a few hundred bytes more code,
3020but avoids random read accesses on heap changes. This improves performance
3021noticably with with many (hundreds) of watchers.
3022
3023The default is C<1> unless C<EV_MINIMAL> is set in which case it is C<0>
3024(disabled).
3002 3025
3003=item EV_COMMON 3026=item EV_COMMON
3004 3027
3005By default, all watchers have a C<void *data> member. By redefining 3028By default, all watchers have a C<void *data> member. By redefining
3006this macro to a something else you can include more and other types of 3029this macro to a something else you can include more and other types of
3176correct watcher to remove. The lists are usually short (you don't usually 3199correct watcher to remove. The lists are usually short (you don't usually
3177have many watchers waiting for the same fd or signal). 3200have many watchers waiting for the same fd or signal).
3178 3201
3179=item Finding the next timer in each loop iteration: O(1) 3202=item Finding the next timer in each loop iteration: O(1)
3180 3203
3181By virtue of using a binary heap, the next timer is always found at the 3204By virtue of using a binary or 4-heap, the next timer is always found at a
3182beginning of the storage array. 3205fixed position in the storage array.
3183 3206
3184=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) 3207=item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
3185 3208
3186A change means an I/O watcher gets started or stopped, which requires 3209A change means an I/O watcher gets started or stopped, which requires
3187libev to recalculate its status (and possibly tell the kernel, depending 3210libev to recalculate its status (and possibly tell the kernel, depending
3228 3251
3229Due to the many, low, and arbitrary limits on the win32 platform and 3252Due to the many, low, and arbitrary limits on the win32 platform and
3230the abysmal performance of winsockets, using a large number of sockets 3253the abysmal performance of winsockets, using a large number of sockets
3231is not recommended (and not reasonable). If your program needs to use 3254is not recommended (and not reasonable). If your program needs to use
3232more than a hundred or so sockets, then likely it needs to use a totally 3255more than a hundred or so sockets, then likely it needs to use a totally
3233different implementation for windows, as libev offers the POSIX readyness 3256different implementation for windows, as libev offers the POSIX readiness
3234notification model, which cannot be implemented efficiently on windows 3257notification model, which cannot be implemented efficiently on windows
3235(microsoft monopoly games). 3258(microsoft monopoly games).
3236 3259
3237=over 4 3260=over 4
3238 3261
3319is still at least 31 bits everywhere, which is enough for hundreds of 3342is still at least 31 bits everywhere, which is enough for hundreds of
3320millions of watchers. 3343millions of watchers.
3321 3344
3322=item C<double> must hold a time value in seconds with enough accuracy 3345=item C<double> must hold a time value in seconds with enough accuracy
3323 3346
3324The type C<double> is used to represent timestamps. It is required to have 3347The type C<double> is used to represent timestamps. It is required to
3325at least 51 bits of mantissa, which is good enough for at least into the 3348have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3326year 4000. This requirement is fulfilled by implementations implementing 3349enough for at least into the year 4000. This requirement is fulfilled by
3327IEEE 754 (basically all existing ones). 3350implementations implementing IEEE 754 (basically all existing ones).
3328 3351
3329=back 3352=back
3330 3353
3331If you know of other additional requirements drop me a note. 3354If you know of other additional requirements drop me a note.
3332 3355
3333 3356
3357=head1 VALGRIND
3358
3359Valgrind has a special section here because it is a popular tool that is
3360highly useful, but valgrind reports are very hard to interpret.
3361
3362If you think you found a bug (memory leak, uninitialised data access etc.)
3363in libev, then check twice: If valgrind reports something like:
3364
3365 ==2274== definitely lost: 0 bytes in 0 blocks.
3366 ==2274== possibly lost: 0 bytes in 0 blocks.
3367 ==2274== still reachable: 256 bytes in 1 blocks.
3368
3369then there is no memory leak. Similarly, under some circumstances,
3370valgrind might report kernel bugs as if it were a bug in libev, or it
3371might be confused (it is a very good tool, but only a tool).
3372
3373If you are unsure about something, feel free to contact the mailing list
3374with the full valgrind report and an explanation on why you think this is
3375a bug in libev. However, don't be annoyed when you get a brisk "this is
3376no bug" answer and take the chance of learning how to interpret valgrind
3377properly.
3378
3379If you need, for some reason, empty reports from valgrind for your project
3380I suggest using suppression lists.
3381
3382
3334=head1 AUTHOR 3383=head1 AUTHOR
3335 3384
3336Marc Lehmann <libev@schmorp.de>. 3385Marc Lehmann <libev@schmorp.de>.
3337 3386

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines