If this article can not tell epoll of nature, then come to strangle me! (3)

If this article can not tell epoll of nature, then come to strangle me! (3)

Luopei Yu  https://zhuanlan.zhihu.com/p/64746509

Author of "Unity3D online game combat."

 

linux epoll is to develop the necessary technology to high-performance servers, epoll nature, must master knowledge of server-side programmers.

Text / Luopei Yu

Part Review

Fourth, the whole process of data received by the core network

Fifth, a simple method while monitoring a plurality of socket

Sixth, the design ideas of epoll

Seven, epoll principles and processes

This section will be examples and diagrams to explain epoll principles and processes.

Objects created epoll

As shown below, when a process calls epoll_create method, the kernel creates a eventpoll objects (ie represented by the object program epfd). eventpoll the object is a file system, and socket as it will have to wait queue.

Kernel objects create eventpoll

The object is to create a representative of eventpoll epoll is necessary, because the kernel to maintain "ready list" data, "ready list" as a member of eventpoll.

Maintenance Watchlist

After you create epoll object, you can use epoll_ctl additions or deletions to listen socket. Socket to add as an example, as shown below, is added to monitor if sock1, sock2 and sock3 by epoll_ctl, the kernel will eventpoll added to the queue in three socket.

Adding to be listening socket

When the socket receives the data, the program will interrupt the operation eventpoll object, rather than directly manipulate the process.

Receive data

When the socket receives the data, interrupt program will eventpoll the "ready list" add socket reference. The following figure shows that sock2 and sock3 after receiving the data, interrupt program lets rdlist references both socket.

Add a reference to the ready list

eventpoll target is equivalent to an intermediary between the socket and processes data received socket does not directly affect the process, but to change the process by changing the state of the ready list eventpoll.

当程序执行到epoll_wait时,如果rdlist已经引用了socket,那么epoll_wait直接返回,如果rdlist为空,阻塞进程。

阻塞和唤醒进程

假设计算机中正在运行进程A和进程B,在某时刻进程A运行到了epoll_wait语句。如下图所示,内核会将进程A放入eventpoll的等待队列中,阻塞进程。

epoll_wait阻塞进程

当socket接收到数据,中断程序一方面修改rdlist,另一方面唤醒eventpoll等待队列中的进程,进程A再次进入运行状态(如下图)。也因为rdlist的存在,进程A可以知道哪些socket发生了变化。

epoll唤醒进程

八、epoll的实现细节

至此,相信读者对epoll的本质已经有一定的了解。但我们还留有一个问题,eventpoll的数据结构是什么样子?

再留两个问题,就绪队列应该应使用什么数据结构?eventpoll应使用什么数据结构来管理通过epoll_ctl添加或删除的socket?

 

(——我是分割线,想好了才能往下看哦~)

 

如下图所示,eventpoll包含了lock、mtx、wq(等待队列)、rdlist等成员。rdlist和rbr是我们所关心的。

epoll原理示意图,图片来源:《深入理解Nginx:模块开发与架构解析(第二版)》,陶辉

 

就绪列表的数据结构

就绪列表引用着就绪的socket,所以它应能够快速的插入数据。

程序可能随时调用epoll_ctl添加监视socket,也可能随时删除。当删除时,若该socket已经存放在就绪列表中,它也应该被移除。

所以就绪列表应是一种能够快速插入和删除的数据结构。双向链表就是这样一种数据结构,epoll使用双向链表来实现就绪队列(对应上图的rdllist)。

索引结构

既然epoll将“维护监视队列”和“进程阻塞”分离,也意味着需要有个数据结构来保存监视的socket。至少要方便的添加和移除,还要便于搜索,以避免重复添加。红黑树是一种自平衡二叉查找树,搜索、插入和删除时间复杂度都是O(log(N)),效率较好。epoll使用了红黑树作为索引结构(对应上图的rbr)。

 

ps:因为操作系统要兼顾多种功能,以及由更多需要保存的数据,rdlist并非直接引用socket,而是通过epitem间接引用,红黑树的节点也是epitem对象。同样,文件系统也并非直接引用着socket。为方便理解,本文中省略了一些间接结构。

九、结论

epoll在select和poll(poll和select基本一样,有少量改进)的基础引入了eventpoll作为中间层,使用了先进的数据结构,是一种高效的多路复用技术。

再留一点作业

下表是个很常见的表,描述了select、poll和epoll的区别。读完本文,读者能否解释select和epoll的时间复杂度为什么是O(n)和O(1)?

select、poll和epoll的区别。图片来源《Linux高性能服务器编程》

Guess you like

Origin blog.csdn.net/xclshwd/article/details/90401550