Linux NIO series (04-4) select, poll, epoll contrast

Linux NIO series (04-4) select, poll, epoll contrast

Netty series catalog ( https://www.cnblogs.com/binarylei/p/10117436.html )

Now select / poll / epoll is a specific implementation I / O multiplexing, why now exist, in fact, they are also the product of different historical periods

  • select appear in 1984 inside the BSD implementation
  • That is, after 14 years in 1997, was able to achieve a poll, in fact, for waiting so long is not efficiency, but the hardware of that era too weak, a server process over 1,000 links simply as God exists, select a long period of time needs have been met
  • 2002, Great God Davide Libenzi realized epoll

A, API comparison

1.1 select API

int select(int maxfdp, fd_set *readset, fd_set *writeset, fd_set *exceptset,struct timeval *timeout);
int FD_ZERO(int fd, fd_set *fdset);     // 一个 fd_set 类型变量的所有位都设为 0
int FD_CLR(int fd, fd_set *fdset);      // 清除某个位时可以使用
int FD_SET(int fd, fd_set *fd_set);     // 设置变量的某个位置位
int FD_ISSET(int fd, fd_set *fdset);    // 测试某个位是否被置位

provides select () mechanism of one data structure fd_set is actually a type of array Long , each array element can be opened with a file handle to establish contact (this link needs its own complete), when the call to select ( ), the content fd_set according IO state by the kernel, thereby notify the execution of select () or a process which Socket-readable file.

Issue select mechanism

  1. Each time the select call, you need to copy fd_set collection from user mode to kernel mode, if fd_set collection is large, then this is also a great cost
  2. At the same time each call fd_set select all need to traverse passed in the kernel, if fd_set collection is large, then this is also a great cost
  3. In order to reduce damage to the performance data copying brought fd_set kernel set size being monitored do limit (default is 1024)

1.2 poll API

int poll(struct pollfd *fds, nfds_t nfds, int timeout);
struct pollfd {
    int fd;         // 文件描述符
    short events;   // 感兴趣的事件
    short revents;  // 实际发生的事件
};

Poll and select a similar mechanism, not much different in nature to select, manage a plurality of descriptors is polling, processing descriptor in accordance with the state, but there is no maximum number of file descriptors poll limit. In other words, poll only solve the above problem 3, does not solve the problem of performance overhead issues 1 and 2.

1.3 epoll API

// 函数创建一个 epoll 句柄,实际上是一棵红黑树
int epoll_create(int size);
// 函数注册要监听的事件类型,op 表示红黑树进行增删改
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
// 函数等待事件的就绪,成功时返回就绪的事件数目,调用失败时返回 -1,等待超时返回 0
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);

epoll formally proposed in Linux2.6 kernel, is based on event-driven I / O mode , it is relative to the select, epoll descriptor is no limit to the number, use a file descriptor to manage multiple descriptors, describing the files of interest to users Fu events stored in the kernel of an event table so that only one copy in the user space and kernel space.

Second, summary

I / O multiplexing the I / O programming process, when it is necessary to handle multiple clients access request can be processed using multi-threading or I / O multiplexing. I / O multiplexing by the plurality of I / O congestion multiplexed onto the same blocking select, so that the system can handle the case where a plurality of single-threaded client requests simultaneously. With the traditional multi-threaded / multi-process model ratio, I / O multiplexing biggest advantage is the small system overhead, the system does not need to create new and additional processes or threads, do not need to run these maintenance processes and threads, reducing the system maintenance activity, saving system resources, I / O multiplexing main scenarios are as follows:

  • A plurality of servers need to process the plurality of sockets in listening state or a connected state
  • Socket server needs to handle a variety of network protocols.

Currently supports I / O multiplexing system call select, pselect, poll, epoll, in Linux network programming, for a long time to make use of select polling and network event notification, however, some of the inherent flaws select the lead its use has been greatly restricted, and ultimately had to find alternatives to select Linux in the new kernel version, the final choice epoll. Principle epoll and select a more similar, in order to overcome the shortcomings of select, epoll made a lot of significant improvements, are summarized below.

2.1 supports a process to open a socket descriptor (FD) unrestricted (maximum file only limited by the operating system handles)

select single biggest flaw is the process of being opened FD is a certain limit, which consists of FD_SETSIZE set, the default value is 1024. For those who need to support tens of thousands of large-scale server TCP connection is obviously too little. This macro can choose to modify and recompile the kernel, but this will bring down the network efficiency. We can solve this problem by selecting a multi-process program (traditional Apache program), but although the process of creating on the cost of Linux is relatively small, but still can not be ignored. In addition, data exchange between process cumbersome for Java, because there is no shared memory, the need for data synchronization Socket communication or other means, which brings additional performance loss, a program to increase in complexity, so do not be a kind of the perfect solution. Fortunately, epoll does not have this limitation, FD upper limit is the maximum that it supports operating system file handles, a figure far greater than 1024. For example, on a machine with 1GB of RAM is about 100 000 handles specific values ​​can be cat proc / sys / fs / file-max view, the relationship between the value of memory with the system usually relatively large.

# (所有进程)当前计算机所能打开的最大文件个数。受硬件影响,这个值可以改(通过limits.conf)
cat /proc/sys/fs/file-max

# (单个进程)查看一个进程可以打开的socket描述符上限。缺省为1024
ulimit -a 
# 修改为默认的最大文件个数。【注销用户,使其生效】
ulimit -n 2000

# soft软限制 hard硬限制。所谓软限制是可以用命令的方式修改该上限值,但不能大于硬限制
vi /etc/security/limits.conf
* soft nofile 3000      # 设置默认值。可直接使用命令修改
* hard nofile 20000     # 最大上限值

2.2 I / O efficiency is not as to increase in the number of linearly decreased FD

Traditional select / poll another fatal weakness, it is that when you have a great set of socket, due to network delays or the link is idle at any one time only a small part of the socket is "active", but select / poll each invocation will scan the entire set of linear, resulting in decreased efficiency linear presentation. epoll not have this problem, it only would be "active" socket operates-this is because the kernel implementation, epoll fd is implemented according to each of the above callback function. So, only the "active" socket Bio will take the initiative to call the callback function, socket other idle state will not. At this point, epoll implements a pseudo-AIO. For contrast and select epoll performance benchmark tests show that: If all of the socket are in active state - for example, a high-speed LAN environment, epoll no better than select / poll much higher efficiency; the contrary, if compared to the excessive use of epoll_ctl, as well as efficiency By using slightly lowered but idle connections simulated WAN environment, the efficiency of far above epoll would select / poll a.

2.3 mmap acceleration kernel and user space messaging

Whether select, poll or epoll kernel needs to FD message notification to the user space, how to avoid unnecessary memory copy is very important, epoll by kernel and user space mmap the same piece of memory to achieve.

2.4 epoll API easier

Including the creation of a epoll descriptor, add an event listener, listening block waiting for the event to close the epoll descriptor and so on.

It should be noted that the method used to overcome the select / poll shortcomings not only epoll, epoll is just a Linux implementation. There are at freeBSD kqueue, and dev / poll is the oldest Solaris programs, in ascending order of difficulty of use. kqueue is freeBSD darling, it's actually a fairly feature-rich kernel event queue, it is not just upgrade select / poll, and can handle multiple event signal, change the directory structure, process and so on. kqueue is edge-triggered. / Dev / poll Solaris is a product of this series of high-performance API first appeared. Kernel provides a special device file / dev / poll, the application opens the file handle to the obtained operation fd_set, modify it by writing Polled, a specific ioctl calls to replace select. However, because of earlier years it appeared, so the / dev / poll interface is primitive.

Table 1: select / poll / epoll difference

Compare select poll epoll
Operation method Traversal Traversal Callback
The underlying implementation Array List Red-black tree
IO efficiency Linear traversing each call,
the time complexity is O (n)
Linear traversing each call,
the time complexity is O (n)

Event Notification way, whenever fd ready,
the system registered callback function will be called,
will be ready fd into readyList inside,
the time complexity of O (1)
The maximum number of connections | 1024 | unlimited | unlimited
fd copy | every time call select,
need to fd collection copy from user mode to kernel mode | each call poll,
need to fd collection copy from user mode to kernel mode | copied into the kernel when calling epoll_ctl and saved
after each epoll_wait not copy

Summary : epoll Linux is the preferred model for large-scale network currently concurrent program development. In most cases performance than select and poll. Popular high-performance web server Nginx official relies on efficient network socket epoll polling services provided. However, in the case of concurrent connections is not high, multi-threaded + blocking I / O performance may be better ways.

reference:

  1. IO multiplexing three mechanisms Select, Poll, Epoll

The intentions of recording a little bit every day. Perhaps the content is not important, but the habit is very important!

Guess you like

Origin www.cnblogs.com/binarylei/p/11130079.html