epoll

Determine which signal caused EINTR?

这一生的挚爱 提交于 2019-12-22 08:34:19
问题 I am running an epoll loop and sometimes my call to epoll_wait returns -1 with errno set to EINTR. Sometimes, I want this to end the epoll loop, like in the case of SIGTERM or SIGINT. But I have this code compiled with the -pg flag, so periodic SIGPROF (27) signals are raised that stop my loop. So... is it possible to switch on the signum so that I can determine when to exit vs. continue? I would like to avoid anything that employs the use of a global to keep track of the most recent signal

epoll

十年热恋 提交于 2019-12-22 08:33:38
本质上 epoll 还是一种 I/O 多路复用技术, epoll 通过监控注册的多个描述字,来进行 I/O 事件的分发处理。不同于 poll 的是,epoll 不仅提供了默认的 level-triggered(条件触发)机制,还提供了性能更为强劲的 edge-triggered(边缘触发)机制。 epoll使用 epoll_create epoll_create() 方法创建了一个 epoll 实例, int epoll_create ( int size ) ; int epoll_create ( int flags ) ; 若成功返回大于 0 的值, - 1 表示出错 参数 size 被自动忽略,但是该值仍需要一个大于 0 的整数,关于参数 size,在一开始的 epoll_create 实现中,是用来告知内核期望监控的文件描述字大小,然后内核使用这部分的信息来初始化内核数据结构,在新的实现中,这个参数不再被需要,因为内核可以动态分配需要的内核数据结构。我们只需要注意,每次将 size 设置成一个大于 0 的整数就可以了。 epoll_create1() 的用法和 epoll_create() 基本一致,如果 epoll_create1() 的输入 flags 为 0,则和 epoll_create() 一样,内核自动忽略。可以增加如 EPOLL_CLOEXEC 的额外选项。

深入理解 tornado 之底层 ioloop 实现

∥☆過路亽.° 提交于 2019-12-21 10:39:41
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近打算学习 tornado 的源码,所以就建立一个系列主题 “深入理解 tornado” 。 在此记录学习经历及个人见解与大家分享。文中一定会出现理解不到位或理解错误的地方,还请大家多多指教 进入正题: tornado 优秀的大并发处理能力得益于它的 web server 从底层开始就自己实现了一整套基于 epoll 的单线程异步架构(其他 python web 框架的自带 server 基本是基于 wsgi 写的简单服务器,并没有自己实现底层结构。 关于 wsgi 详见之前的文章: 自己写一个 wsgi 服务器运行 Django 、Tornado 应用 )。 那么 tornado.ioloop 就是 tornado web server 最底层的实现。 看 ioloop 之前,我们需要了解一些预备知识,有助于我们理解 ioloop。 epoll ioloop 的实现基于 epoll ,那么什么是 epoll? epoll 是Linux内核为处理大批量文件描述符而作了改进的 poll 。 那么什么又是 poll ? 首先,我们回顾一下, socket 通信时的服务端,当它接受( accept )一个连接并建立通信后( connection )就进行通信,而此时我们并不知道连接的客户端有没有信息发完。

Why having to use non-blocking fd in a edge triggered epoll function?

元气小坏坏 提交于 2019-12-21 05:23:05
问题 I read document abount edge triggered epoll function in web as follows: 1. The file descriptor that represents the read side of a pipe (rfd) is registered on the epoll instance. 2. A pipe writer writes 2 kB of data on the write side of the pipe. 3. A call to epoll_wait(2) is done that will return rfd as a ready file descriptor. 4. The pipe reader reads 1 kB of data from rfd. 5. A call to epoll_wait(2) is done. ....... ....... The suggested way to use epoll as an edge-triggered (EPOLLET)

Why exactly does ePoll scale better than Poll?

爷,独闯天下 提交于 2019-12-21 03:42:54
问题 Short question but for me its difficult to understand. Why exactly does ePoll scale better than Poll? 回答1: The poll system call needs to copy your list of file descriptors to the kernel each time. This happens only once with epoll_ctl , but not every time you call epoll_wait . Also, epoll_wait is O(1) in respect of the number of descriptors watched 1 , which means it does not matter whether you wait on one descriptor or on 5,000 or 50,000 descriptors. poll , while being more efficient than

TCP: When is EPOLLHUP generated?

巧了我就是萌 提交于 2019-12-21 02:33:07
问题 Also see this question, unanswered as of now. There is a lot of confusion about EPOLLHUP , even in the man and Kernel docs. People seem to believe it is returned when polling on a descriptor locally closed for writing , i.e. shutdown(SHUT_WR) , i.e. the same call that causes an EPOLLRDHUP at the peer . But this is not true, in my experiments I get EPOLLOUT , and no EPOLLHUP , after shutdown(SHUT_WR) (yes, it's counterintuitive to get writable , as the writing half is closed, but this is not

revisiting “how do you use aio and epoll together”

不羁的心 提交于 2019-12-20 12:27:08
问题 following the discussion at How do you use AIO and epoll together in a single event loop?. There are in fact 2 "aio" APIs in linux. There's POSIX aio (the aio_* family of functions), included in glibc and libaio developed I believe by RedHat (?), the io_* family. The first one allows registration of notification requests via aio_sigevent aiocb member. That can be easily integrated with ppoll()/pselect() event loops. If you want to integrate POSIX aio with epoll() then you need to translate

Boost Asio single threaded performance

泄露秘密 提交于 2019-12-20 09:24:01
问题 I am implementing custom server that needs to maintain very large number (100K or more) of long lived connections. Server simply passes messages between sockets and it doesn't do any serious data processing. Messages are small, but many of them are received/send every second. Reducing latency is one of the goals. I realize that using multiple cores won't improve performance and therefore I decided to run the server in a single thread by calling run_one or poll methods of io_service object.

redis相关

旧街凉风 提交于 2019-12-20 01:28:53
redis结构: String,Hash,List,Set,Zet 持久化: RDB 快照形式 AOF 追加 1,redis既然是单线程的,为啥处理速度很快 redis每秒处理100000+的QPS,基于内存操作,只是我们在处理网络请求的时候,是单线程处理,redis Server在运行的时候肯定不止一个线程,单线程省去了很多上下文切换的时间,redis使用多路复用技术,可以处理并发的连接。非阻塞IO 内部实现采用epoll,采用了epoll+自己实现的简单的事件框架。epoll中的读、写、关闭、连接都转化成了事件,然后利用epoll的多路复用特性,绝不在io上浪费一点时间。 2.分布式锁 加锁使用setnx(); key可以为当前线程id,value随便,当执行完后返回1,表明没有加锁,可以使用,当返回0的时候,表明当key已经存在,加锁失败,解锁的话del(key), 锁超时:当线程获取锁之后,挂掉了,那么锁就无法释放,这个时候需要设置expire()超时时间,保证到期释放锁 问题一: setnx 和expire操作非原子性,解决:redis高版本有set()方法设置超时时间 问题二: 线程执行时间太长,超时时间过了,其他线程获取锁执行,redis误删其他的锁,解决:删除锁时校验是否为当前线程 校验和删除非原子性操作,可以通过LUA脚本来操作, String luaS

IOCP模型与EPOLL模型的比较

眉间皱痕 提交于 2019-12-19 10:22:11
一:IOCP和Epoll之间的异同。 异: 1:IOCP是WINDOWS系统下使用。Epoll是Linux系统下使用。 2:IOCP是IO操作完毕之后,通过Get函数获得一个完成的事件通知。 Epoll是当你希望进行一个IO操作时,向Epoll查询是否可读或者可写,若处于可读或可写状态后,Epoll会通过epoll_wait进行通知。 3:IOCP封装了异步的消息事件的通知机制,同时封装了部分IO操作。但Epoll仅仅封装了一个异步事件的通知机制,并不负责IO读写操作。Epoll保持了事件通知和IO操作间的独立性,更加简单灵活。 4: 基于上面的描述,我们可以知道Epoll不负责IO操作,所以它只告诉你当前可读可写了,并且将协议读写缓冲填充,由用户去读写控制,此时我们可以做出额 外的许多操作。IOCP则直接将IO通道里的读写操作都做完了才通知用户,当IO通道里发生了堵塞等状况我们是无法控制的。 同: 1:它们都是异步的事件驱动的网络模型。 2:它们都可以向底层进行指针数据传递,当返回事件时,除可通知事件类型外,还可以通知事件相关数据。 二:描述一下IOCP: 扯远点。首先传统服务器的网络IO流程如下: 接到一个客户端连接->创建一个线程负责这个连接的IO操作->持续对新线程进行数据处理->全部数据处理完毕->终止线程。 但是这样的设计代价是: 1:每个连接创建一个线程