Wikipedia says
unlike the older system calls, which operate at O(n), epoll operates in O(1) [2]).
http://en.wikipedia.org/wiki/Epoll
However, the source code at fs/eventpoll.c on Linux-2.6.38, seems it is implemented with an RB tree for searching, which has O(logN)
/*
* Search the file inside the eventpoll tree. The RB tree operations
* are protected by the "mtx" mutex, and ep_find() must be called with
* "mtx" held.
*/
static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
{
In fact, I couldn't see any man page saying the complexity of epoll() is O(1). Why is it known as O(1)?
epoll
executes inO(1)
, as the number of file descriptors it needs to watch isn't constant. There has to be some kind of repetition construct. – Ariosoepoll()
function people keep talking about? I can't find any function with that name.epoll
is a whole family of functions, not all of which have the same complexity. – Sustentationepoll()
must scan the file descriptors to find ready ones.epoll()
can also wait on the descriptors to become ready... – Criminateepoll
is scanning anything. What I meant was that if you're waiting on events that can happen at anytime thenO(1)
operation is not guaranteed by definition. Either that or everyone talking about theepoll
API meant something else byO(1)
. – AriosoO(asleep)
. :) – Criminateepoll
operates at O(1).epoll
operates at O(log(n)) in respect to adding and removing descriptors, at O(n) in respect of ready descriptors, and at O(1) in respect of monitored descriptors -- which is the whole point ofepoll
. Adding/removing descriptors is a rare thing, waiting for readiness is frequent, and you usually have many more descriptors in your set than are actually ready. Insofar, whileepoll
does not really operate at O(1), it does so where it matters. The common case is fast, the uncommon case is not. – LabializedO(n) for ready descriptors
signify? I thought it would be different from the input set. O(m) where m is the number of ready events. – Gramicidin