1. concept-based
Epoll is a mechanism for processing the read/write sequence of a large number of concurrent connections to current events.
InLinuxNetwork Programming, used for a long timeSelectDo it Event triggering .2.5.xAfter the kernel, introduceEpoll.
epoll is Linux improved kernel for handling large volumes of handles poll , yes Linux multiplexing Io interface select/poll enhanced version, It can significantly reduceProgramSystems with a large number of concurrent connections and only a small number of activeCPUUtilization.
Because itNot ReusedThe file descriptor set is used to pass the results, forcing the developer to re-Prepare the file descriptor set to be listened every time before waiting for the event. Another reason is that when obtaining the event,It does not need to traverse the entire intercepted descriptor set, As long Traverse those kernelsIoEvent asynchronously wake up and addReadyThe descriptor set of the queue is enough. .EpollIn addition to providingSelect/pollThatIoEvent Level Trigger (Level triggered) Edge triggering is also provided. (Edge triggered) This makes it possible for the user space program to cacheIoStatus, reduceEpoll_wait/epoll_pwaitTo improve application efficiency.
InLinux/posix_types.hThe header file has the following statement:
# DEFINE _ fd_setsize 1024
representation select simultaneous listening 1024 FD Of course, you can increase the number by modifying the header file and re-compiling the kernel.
Supported by epollFDThe upper limit is the maximum number of files that can be opened. , In1 GBThe memory on the machine is about10Tens of thousands or so.CAT/proc/sys/fs/file-maxView.
2. Efficiency
The fatal weakness of select/poll is that when there is a largeSocketHowever, due to network latencySocketIs "active, HoweverSelect/pollEach call will linearly scan all sets, resulting in a linear decline in efficiency. HoweverEpollThis problem does not exist. It only applies to "active"SocketOperation --- This is because in kernel implementationEpollIs based onFDThe above Callback Function implementation. Then, only "active" Socket Automatically call Callback Function, Other Idle Status Socket No, at this point, Epoll Implements a "pseudo" AIO Because the driver is OS Kernel. In some Benchmark If all Socket Basically, they are all active. --- For example, a high-speed Lan Environment, Epoll Not comparable Select/poll What is the efficiency? On the contrary, if you use too much Epoll_ctl , The efficiency is slightly lower than that. However Idle connections Simulation Wan Environment, Epoll The efficiency is far away Select/poll Above.
3. Working Mode
LT ( Level triggered) Is the default working method, and supports bothBlockAndNo-block socket.In this way, the kernel tells you whether a file descriptor is ready, and then you canFDProceedIoOperation. If you do not perform any operation, the kernel will continue to inform you, so the possibility of programming errors in this mode is lower. TraditionalSelect/poll Are representative of this model.
Et (Edge-triggered) Is a high-speed way of working , Only supports No-block socket . In this mode, when the descriptor is never ready, the kernel passes through Epoll Tell You. Then it will assume that you know that the file descriptor is ready and will not send more ready notifications for that file descriptor, until you do some operations, the file descriptor is no longer in the ready state (for example, continuing to send data will lead to another ready state; all the data in the cache is read; or when the number of data sent and received is less than a certain amount Ewouldblock/eagain Errors ). However, please note that if this is not always the case FD Work Io The kernel will not send more notifications ( Only once ), That is, each continuous data stream is only notified once, HoweverTCPProtocol,EtThe acceleration effect of the mode still requires moreBenchmarkConfirm.
summary :
the et mode is notified only when the State changes. The so-called state changes do not include any unprocessed data in the buffer, that is, if you want to use et mode, always read/write until an error occurs, many people reflect why et In this mode, only a part of data is received and no notification is received. Most of them are because of this. lt the mode is always notified if any data is not processed.