Rate This Document
Findability
Accuracy
Completeness
Readability

Using epoll Instead of select

Principles

The epoll mechanism is an extensible I/O event processing mechanism in the Linux kernel. It can be used to replace the POSIX select system call to improve performance in high concurrency scenarios.

The select mechanism has the following disadvantages:

  • The kernel supports a maximum of 1024 file handles by default.
  • The select mechanism scans file handles in polling mode, which results in poor performance.

Optimized epoll mechanism:

  • There is no limit on the maximum number of concurrent connections. The maximum number of file handles that can be opened is far greater than 1024, which facilitates processing of more requests in a thread.
  • The event notification mode is used, which reduces the polling overhead.

Modification Method

Use epoll functions instead of select. The epoll functions include epoll_create, epoll_ctl, and epoll_wait. Linux 2.6.19 introduces epoll_pwait, which can be used to mask specified signals.

Function description:

  1. Create an epoll handle.
    1
    int epoll_create(int size);
    

    In the preceding command, size indicates the maximum number of file handles that can be monitored.

  2. Register the types of events to be monitored.
    1
    int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
    

    The parameters are described as follows:

    • epfd: file handle returned by epoll_create.
    • op: operation to be performed, including EPOLL_CTL_ADD, EPOLL_CTL_MOD, and EPOLL_CTL_DEL.
    • fd: file handle to be operated.
    • event: event description. Common events are as follows:
      • EPOLLIN: There is data that can be read in the file descriptor.
      • EPOLLOUT: Data can be written to the file descriptor.
      • EPOLLERR: An error occurs in the corresponding file descriptor.
  3. Wait for the event to occur.
    1
    2
    3
    4
    int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
    int epoll_pwait(int epfd, struct epoll_event *events,
    int maxevents, int timeout,
    const sigset_t *sigmask);
    

    The parameters are described as follows:

    • epfd: file handle returned by epoll_create.
    • events: array of events to be processed.
    • maxevents: length of the events array.
    • timeout: timeout duration (unit: ms).
    • sigmask: masked signal.

In high-concurrency scenarios, select can be replaced by epoll.

You can run the following commands to check whether the epoll function is called:

View the system call information of a process.

1
2
# strace -p $TID -T -tt
18:25:47.902439 epoll_pwait(716, [{EPOLLIN, {u32=1052576880, u64=281463144385648}}, {EPOLLIN, {u32=1052693569, u64=281463144502337}}, {EPOLLOUT, {u32=1052638657, u64=281463144447425}}, {EPOLLIN|EPOLLOUT|EPOLLRDHUP, {u32=1052673241, u64=281463144482009}}, {EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP, {u32=1052636016, u64=281463144444784}}], 512, 1, NULL, 8) = 5 <0.000038>