Questions tagged [epoll]

epoll is a Linux 2.6 readiness notification API for sockets, pipes, and special event-, signal-, and timer descriptors which can operate both in level- and edge-triggered mode, although presently only level-triggered behaviour is in accordance with the documentation. As opposed to poll or select, epoll scales O(1) in respect to the number of descriptors and O(N) in respect realized events.

The epoll API is built around 3 functions:

  • epoll_create creates a new epoll instance and returns a file descriptor that refers to it. This descriptor can be operated on with the other epoll functions and can be added to a different epoll instance
  • epoll_ctl allows file descriptors (sockets, pipes, eventfd, timerfd, signalfd, and epoll) being added and removed to an epoll's set of monitored descriptors, as well as flags of existing descriptors being modified
  • epoll_wait will return up to maxevents queued events. If no events are available, it will return zero. If a timeout is provided and no events are available, epoll_wait will block for the duration of the timeout (a value of -1 means forever).

The conceptual idea behind the API is that applications usually have a certain set of descriptors that changes rarely if ever, but which needs to be observed for readiness many times. Also, typically a lot fewer descriptors are ready than open. epoll therefore separates copying the list of descriptors to watch from the actual watching and notifies registered listeners instead of iterating a list of descriptors.

The operation of level-triggered mode (default) is easy, since it is identical of how poll/select works. As long as the resource is ready (e.g. as long as there remains data to be read), every call to epoll_wait will return an event.

The operation of edge-triggered mode (EPOLLET flag) is more complicated, more error-prone, inconsistenly documented, and inconsistently implemented. In epoll(7), it is explained in terms of reading partial data causing the next call to epoll_wait to block until new data arrives, but not while some data remains in the buffers. It is therefore recommended to use non-blocking descriptors and reading until EAGAIN is received.
According to The Linux Programming Interface, edge-triggered mode only reports events that happened since the last call to epoll_wait.
In reality, it does a mixture of both (i.e. both reads and epoll_wait reset the status to "not ready"), and it does not work as indicated in respect of several epoll instances listening to the same socket or several threads waiting on the same epoll instance (observed under kernel 2.6.38 with timerfd and eventfd). Although epoll is supposed to signal all waiters upon arrival of an event, in edge-triggered mode it only ever signals a single waiter.

792 questions
14
votes
1 answer

about epoll_ctl()

when using epoll_ctl(), I found that the third parameter "fd" is another file descriptor besides the epoll file descriptor "epfd". And I saw an example like this: event.data.fd = sfd; //sfd is a fd for listening event.events = EPOLLIN | EPOLLET; s =…
realjin
  • 1,485
  • 1
  • 19
  • 38
14
votes
1 answer

What is epoll_pwait in node.js application and what can I do about it?

My application is a web version of the board game Settlers of Catan. I'm using node --prof app.js to profile the app and node --prof-process ISOLATE_LOG_FILE > processed.txt to turn them into a processed file. I do get a lot of Code move event for…
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
14
votes
3 answers

With a single file descriptor, Is there any performance difference between select, poll and epoll and ...?

The title really says it all. The and ... means also include pselect and ppoll.. The server project I'm working on basically structured with multiple threads. Each thread handles one or more sessions. All the threads are identical. The protocol…
hookenz
  • 36,432
  • 45
  • 177
  • 286
14
votes
3 answers

C: epoll and multithreading

I need to create specialized HTTP server, for this I plan to use epoll sycall, but I want to utilize multiple processors/cores and I can't come up with architecture solution. ATM my idea is followng: create multiple threads with own epoll…
Daniel
  • 4,272
  • 8
  • 35
  • 48
14
votes
3 answers

Why do we need to call poll_wait in poll?

In LDD3, i saw such codes static unsigned int scull_p_poll(struct file *filp, poll_table *wait) { struct scull_pipe *dev = filp->private_data; unsigned int mask = 0; /* * The buffer is circular; it is considered full * if "wp"…
demonguy
  • 1,977
  • 5
  • 22
  • 34
14
votes
1 answer

How can a single-threaded NGINX handle so many connections?

NGNIX uses epoll notification to know if there is any data on the socket to read. Let assume: There are two requests to the server. nginx is notificated about this two requests and starts to: receive the first request parse ist headers check the…
user2624744
  • 693
  • 8
  • 23
14
votes
2 answers

epoll_wait on several Threads faster?

I am thinking of programming a tcp server based on epoll. To achieve the best performance i want to implement multi core support too. But during my researches the following question came up: Is it faster to call two epoll_wait()-Calls from two…
Filipe Santos
  • 1,629
  • 3
  • 18
  • 27
13
votes
1 answer

Epoll and remote 1-way shutdown

Assume a TCP socket on the local linux host is in a connected state with a remote host. The local host is using epoll_wait to be notified of events on the socket with the remote host. If the remote host were to call: shutdown(s,SHUT_WR); on its…
selbie
  • 100,020
  • 15
  • 103
  • 173
13
votes
4 answers

Scalable server framework in C++

I am looking to write a server application in C++ that is meant to handle tens of thousands of clients simultaneously. It should run under Windows and Linux. I have been looking around for frameworks and libraries and have come across Boost Asio,…
Philip Bennefall
  • 1,477
  • 5
  • 20
  • 33
13
votes
2 answers

Why exactly does ePoll scale better than Poll?

Short question but for me its difficult to understand. Why exactly does ePoll scale better than Poll?
Filipe Santos
  • 1,629
  • 3
  • 18
  • 27
13
votes
3 answers

What does poll() do with a timeout of 0?

I'm looking at the poll() man page, and it tells me the behavior of poll() when positive and negative values are passed in for the timeout parameter. It doesn't doesn't tell me what happens if timeout is 0. Any ideas? Looking at the epoll_wait() man…
Paul Holden
  • 850
  • 1
  • 8
  • 24
13
votes
1 answer

Why do we need EPOLLRDHUP when EPOLLHUP seems enough?

According to the linux man page, EPOLLHUP When reading from a channel such as a pipe or a stream socket, this event merely indicates that the peer closed its end of the channel. EPOLLRDHUP Stream socket peer closed connection, or shut down…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
13
votes
2 answers

Boost Asio single threaded performance

I am implementing custom server that needs to maintain very large number (100K or more) of long lived connections. Server simply passes messages between sockets and it doesn't do any serious data processing. Messages are small, but many of them are…
pic11
  • 14,267
  • 21
  • 83
  • 119
13
votes
5 answers

multithreaded epoll

I am creating a multithreaded server using epoll (edge-triggered) and non-blocking sockets. Currently I'm creating an event loop on the main thread and waiting for notifications and it works correctly I have to choose between two approaches to make…
goodolddays
  • 2,595
  • 4
  • 34
  • 51
13
votes
2 answers

Does OS X not support epoll function?

I'm learning to use epoll function. But my OS X, Mountain Lion doesn't have a header file, sys/epoll.h. I'd like to use epoll function on OS X. How Can I use epoll function?
inherithandle
  • 2,614
  • 4
  • 31
  • 53
1 2
3
52 53