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 itEvent triggering.2.5.xAfter the kernel, introduceEpoll.
epoll is > Linux kernel improvements for handling large volumes of handles poll , yes Linux multiplexing Io interface select/poll , it can significantly reduce Programs System CPU utilization rate.
because of this The file descriptor set is not reused to pass the result, 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 an event is obtained, it does not need to traverse the entire listener descriptor set , as long as traverse those kernels Io Add events to asynchronous wakeup ready set of queue descriptors . epoll not only select/poll Io event Level Trigger ( level triggered ) , edge trigger ( edge triggered ) is also provided) , this makes it possible for the user space program to cache Io Status and reduce epoll_wait/epoll_pwait to improve application efficiency.
InLinux/posix_types.hThe header file has the following statement:
# DEFINE _ fd_setsize 1024
IndicatesSelectSimultaneous listening at most1024ItemsFDOf 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 aboveCallback Function implementation. Then, only "active"SocketAutomatically callCallbackFunction, OtherIdleStatusSocketNo, at this point,EpollImplements a "pseudo"AIOBecause the driver isOSKernel. In someBenchmarkIf allSocketBasically, they are all active.---For example, a high-speedLanEnvironment,EpollNot comparableSelect/pollWhat is the efficiency? On the contrary, if you use too muchEpoll_ctl, The efficiency is slightly lower than that. HoweverIdle connectionsSimulationWanEnvironment,EpollThe efficiency is far awaySelect/pollAbove.
3. Working Mode
LT ( level triggered ) is the default working method, and supports both block and NO-block socket. in this case, the kernel tells you whether a file descriptor is ready, then you can FD Io . 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. Traditional select/poll are representative of this model.
Et (Edge-triggered)Is a high-speed way of working, Only supportsNo-block socket. In this mode, when the descriptor is never ready, the kernel passes throughEpollTell 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 amountEwouldblock/eagainErrors ). However, please note that if this is not always the caseFDWorkIoThe 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.
To sum up:
et mode is notified only when the status changes, the so-called state changes do not include the unprocessed data in the buffer, that is, if you want to use the et mode, you must keep read/write until an error occurs, many people reflect why the et mode only receives a portion of the data and no notification is received; in lt mode, notifications are continuously sent if any data is not processed.