epoll原理与实战

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Process blocks
Process datagram Return ok
Copy data to user
Copy complete
Signal driven I/O
Application Operating system System call No datagram ready
Establish SIGIO Signal handler
EWOULDBLOCK
recvfrom() System call datagram ready
Copy datagram
Process blocks
Process datagram Return ok
Copy data to user
Copy complete
什么是I/O Multiplexing
Since Linux 2.6.8, the size argument is unused. (The kernel dynamically sizes
the required data structures without needing this initial hint.) Linux-2.6.32/fs/eventpoll.c: SYSCALL_DEFINE1(epoll_create, int, size) { if (size <= 0) return -EINVAL; return sys_epoll_create1(0); }
Process datagram Return ok Copy complete
Non-Blocking I/O
Application Operating system System call No datagram ready
recvfrom()
EWOULDBLOCK
recvfrom() System call No datagram ready
---淘宝叔度
目前没有up to date的测试表明ET更快 ---Google陈硕
epoll_wait
ep_poll
ep_send_events_proc
for_each_rdlist(…….) revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL) & epi->event.events; list_del_init(&epi->rdllink); if (revents) { if (__put_user(revents, &uevent->events) || __put_user(epi->event.data, &uevent->data)) {
epoll是什么?
epoll是多路复用IO(I/O Multiplexing)中的一种方式 于Linux 2.5.44首度登场的epoll是Linux核心的可扩展I/O事件通知机制。它设计目 的只在取代既有POSIX select(2)与poll(2)系统函式,让需要大量操作档案描述子的 程式得以发挥更优异的性能(举例来说:旧有的系统函式所花费的时间复杂度为O(n), epoll则耗时O(1))。epoll与FreeBSD的kqueue类似,底层都是由可组态的操作系 统核心物件建构而成,并以档案描述符(file descriptor)的形式呈现于使用者空间。
Proactor实例
使用同步I/O模拟Procator模式
使用场景
Boost asio和ACE都支持Proactor模式,而ACE也支持Reactor 模式 Windows下IOCP就是典型的Proactor,linux下的应用由于内 核支持方面的问题,多数是Reactor 《Comparing Two High-Performance I/O Design Patterns》
epoll_ctl
Man epoll_ctl #include <sys/epoll.h> int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); EPOLL_CTL_ADD Register the target file descriptor fd on the epoll instance referred to by the file descriptor epfd and associate the event event with the internal file linked to fd. EPOLL_CTL_MOD Change the event event associated with the target file descriptor fd. EPOLL_CTL_DEL
Reactor实例
Proactor模式
应用程序初始化一个异步读取操作,然后注册相应的事件处理器,此时事件 处理器不关注读取就绪事件,而是关注读取完成事件,这是区别于Reactor 的关键。
事件分离器等待读取操作完成事件
在事件分离器等待读取操作完成的时候,操作系统调用内核线程完成读取操 作(异步IO都是操作系统负责将数据读写到应用传递进来的缓冲区供应用 程序操作,操作系统扮演了重要角色),并将读取的内容放入用户传递过来 的缓存区中。这也是区别于Reactor的一点,Proactor中,应用程序需要 传递缓存区。 事件分离器捕获到读取完成事件后,激活应用程序注册的事件处理器,事件 处理器直接从缓存区读取数据,而不需要进行实际的读取操作。
Process continues
Signal Handler
recvfrom()
Wait for data
Deliver SIGIO
System call
datagram ready
Copy datagram
Process blocks
Process datagram Return ok
Copy data to user
标准示例:
epoll的原理
epoll file 设备等待队 列节点
监控的目 标文件
epoll_create
epoll_ctl
Edge Trigger与Level Trigger
ET理论上可以比LT少带来一些系统调用,所以更省一些。具体 的性能提升有多少,要看应用场景。不过绝大多数情景下,LT 是足够的。
异步非阻塞IO

在此种模式下,用户进程只需要发起一个IO操作然后 立即返回,等IO操作真正的完成以后,应用程序会得 到IO操作完成的通知,此时用户进程只需要对数据进 行处理就好了,不需要进行实际的IO读写操作,因为 真正的IO读取或者写入操作已经由内核完成了。目前 Java中还没有支持此种IO模型。
Copy data to user
Return ok Copy complete
Signal handler Process datagram
高性能I/O设计模式Reactor和Proactor
同步阻塞IO
在此种方式下,用户进程在发起一个IO操作以后,必 须等待IO操作的完成,只有当真正完成了IO操作以后, 用户进程才能运行。JAVA传统的IO模型属于此种方 式!
Copy complete
Asynchronous I/O
Application Operating system System call return Wait for data No datagram ready
aio_read()
Process continues
datagram ready Copy datagram
http://zh.wikipedia.org/wiki/Epoll
man epoll
epoll的使用
epoll_create
epoll_wait
epoll_ctl ET 与 LT
Biblioteka Baidu
epoll_create
Man epoll_create: #include <sys/epoll.h> int epoll_create(int size); …
Remove (deregister) the target file descriptor fd from the epoll instance referred to by epfd. The event is ignored and can be.
epoll_wait
#include <sys/epoll.h> int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); The epoll_wait() system call waits for events on the epoll instance referred to by the file descriptor epfd. The memory area pointed to by events will contain the events that will be available for the caller. Up to maxevents are returned by epoll_wait(). The maxevents argument must be greater than zero. The call waits for a maximum time of timeout milliseconds. Specifying a timeout of -1 makes epoll_wait() wait indefinitely, while specifying a timeout equal to zero makes epoll_wait() to return immediately even if no events are available
同步非阻塞IO
在此种方式下,用户进程发起一个IO操作以后边可返 回做其它事情,但是用户进程需要时不时的询问IO操 作是否就绪,这就要求用户进程不停的去询问,从而 引入不必要的CPU资源浪费。其中目前JAVA的NIO就 属于同步非阻塞IO。
异步阻塞IO
此种方式下是指应用发起一个IO操作以后,不等待内核IO操作 的完成,等内核完成IO操作以后会通知应用程序,这其实就是 同步和异步最关键的区别,同步必须等待或者主动的去询问IO 是否完成,那么为什么说是阻塞的呢? 因为此时是通过select系统调用来完成的,而select函数本身 的实现方式是阻塞的,而采用select函数有个好处就是它可以 同时监听多个文件句柄,从而提高系统的并发性!
Reactor模式
应用程序注册读就绪事件和相关联的事件处理器 事件分离器等待事件的发生 当发生读就绪事件的时候,事件分离器调用第一步注册的事件处理器 事件处理器首先执行实际的读取操作,然后根据读取到的内容进行进一步的 处理 写入操作类似于读取操作,只不过第一步注册的是写就绪事件。
Blocking I/O
Application Operating system System call No datagram ready
recvfrom()
Wait for data
Process blocks
datagram ready
Copy datagram
Copy data to user
Application Operating system System call No datagram ready
select()
Process blocks
Return readable
recvfrom()
System call
Wait for data
datagram ready
Copy datagram
When an application needs to handle multiple I/O descriptors at the same time.
E.g. file and socket descriptors, multiple socket descriptors
I/O Multiplexing
linux高性能服务器编程之
epoll原理与实战
系统研发部 丁玉杰
IO模型(I/O Models)
Blocking I/O Non-blocking I/O I/O multiplexing – select(),poll(),epoll(),kqueue(),…. Signal driven I/O Asynchronous I/O
相关文档
最新文档