epoll性能
谁能帮我回答有关 epoll_wait 的问题。
使用多个线程在同一个 fd 上调用 epoll_wait 来为大约 100K 活动套接字提供服务是否太过分了?或者只创建 1 个线程来执行 epoll_wait 就足够了吗?
例如,当只有一个套接字准备好读取数据时,有多少线程将从 epoll_wait 中唤醒?我的意思是,是否会出现这样的情况:2 个或更多线程将从 epoll_wait 中唤醒,但在结果事件中具有相同的 fd?
在与许多活动客户端(例如 50K+)一起使用的服务器中组织线程的最佳方式是什么。我认为最好的方法是:1 个 I/O 工作线程,它执行 epoll_wait 和 i/o 操作。 + 许多数据处理线程,它们将处理从 I/O 工作线程接收的数据(可能需要很长时间,例如任何游戏逻辑)并为 I/O 工作线程编写新数据以发送到客户。我的这种方法是否正确,或者任何人都可以帮助我找出组织此方法的最佳方法?
预先感谢,瓦伦丁
Can anyone please help me to answer the questions about epoll_wait.
Is it overkill to use many threads that call epoll_wait on the same fds set to serve at about 100K active sockets? or will it just be enough to create only 1 thread to perform epoll_wait?
How many threads will wake up from epoll_wait when for example only one socket is ready to read data? i mean, can there be situation when 2 or more threads will wake up from epoll_wait but will have same fds in resulted events?
What is the best way to organize threads in server that works with many active clients (e.g. 50K+). The best way i think is: 1 I/O Worker Thread which perfroms epoll_wait and i/o operations. + Many Data processing threads which will process the data received from I/O worker thread (can take a long time, such as any game logic) and compose new data for I/O worker thread to send to client. Am I right in this approach, or can anyone help me to find out the best way to organize this?
Thanks in advance, Valentin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 epoll 时,您希望将线程总数调整为要用于处理的物理 CPU 核心(或超线程调度单元)的数量。仅使用一个线程进行工作意味着一次最多有一个核心处于活动状态。
这取决于epoll文件描述符的模式。事件可以是“边缘触发”,这意味着它们仅原子地发生一次,也可以是“级别触发”,这意味着如果缓冲区中有空间,任何调用者都会收到事件。
没有足够的信息可说。为了简单起见,我建议根本不要有特殊用途的线程,而只需在接收事件的线程中处理每个事件的“命令”。但显然这取决于您的应用程序的性质。
When using epoll, you want to size your thread total to the number of physical CPU cores (or hyperthread dispatch units) which you want to use for processing. Using only one thread for work means that at most one core will be active at a time.
It depends on the mode of the epoll file descriptor. Events can be "edge triggered", meaning that they only happen once atomically, or "level triggered" meaning that any caller gets an event if there is space in the buffer.
Not enough information to say. I'd suggest not having special purpose threads at all, for simplicity, and simply handling each event's "command" in the thread in which it is received. But obviously that depends on the nature of your application.
我建议您阅读 2006 年的这篇文章:http://www.kegel.com/c10k.html
I recommend you read this article from 2006: http://www.kegel.com/c10k.html
实际上,这是 epoll 的错误用例。
绝对不能在线程之间共享 epoll fd。否则,一个线程可能会读取一个 fd 上的部分传入数据,而另一个线程也会读取同一个 fd 上的部分传入数据,而无法知道哪一部分数据在另一部分之前。
只需在每个调用 epoll_wait 的线程中调用 epoll_create 即可。否则 I/O 损坏。
Actually this is a wrong use case of epoll.
You must absolutely not share the epoll fd between threads. Otherwise you have the possibility that one thread read part of incoming data on one fd and another thread too on the same fd without any way to know which part of the data was before the other.
Just call epoll_create in each and every thread that calls epoll_wait. Otherwise the I/O is broken.