关于epoll和splice的问题
我的应用程序将通过网络发送大量数据,因此我决定(因为我使用的是 Linux)使用 epoll 和 splice。以下是我的看法(伪代码):
epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event
while(1)
{
epoll_wait (tmp_structure);
if (tmp_structure->fd == file_descriptor)
{
epoll_ctl (file_fd, EPOLL_CTL_DEL);
epoll_ctl (tcp_socket_fd, EPOLL_CTL_ADD); // wait for EPOLLOUT event
}
if (tmp_structure->fd == tcp_socket_descriptor)
{
splice (file_fd, tcp_socket_fd);
epoll_ctl (tcp_socket_fd, EPOLL_CTL_DEL);
epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event
}
}
我假设我的应用程序将打开最多 2000 个 TCP 套接字。我想问你两件事:
- 将会有相当多的 epoll_ctl 调用,当我有这么多套接字时,会不会很慢?
- 文件描述符必须首先变得可读,并且在套接字变得可写之前会有一些间隔。我可以确定,当套接字变为可写文件描述符时仍然可读(以避免阻塞调用)吗?
My application is going to send huge amount of data over network, so I decided (because I'm using Linux) to use epoll and splice. Here's how I see it (pseudocode):
epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event
while(1)
{
epoll_wait (tmp_structure);
if (tmp_structure->fd == file_descriptor)
{
epoll_ctl (file_fd, EPOLL_CTL_DEL);
epoll_ctl (tcp_socket_fd, EPOLL_CTL_ADD); // wait for EPOLLOUT event
}
if (tmp_structure->fd == tcp_socket_descriptor)
{
splice (file_fd, tcp_socket_fd);
epoll_ctl (tcp_socket_fd, EPOLL_CTL_DEL);
epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event
}
}
I assume, that my application will open up to 2000 TCP sockets. I want o ask you about two things:
- There will be quite a lot of epoll_ctl calls, won't wit be slow when I will have so many sockets?
- File descriptor has to become readable first and there will be some interval before socket will become writable. Can I be sure, that at the moment when socket becomes writable file descriptor is still readable (to avoid blocking call)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个问题
什么样的文件描述符?如果文件系统上的此文件无法使用选择/轮询或其他工具来实现此目的,则无论磁盘和缓存的状态如何,文件都将始终可读或可写。如果您需要异步工作,您可以使用
aio_*
API,但通常只是从文件读取写入文件并假设它是非阻塞的。如果是 TCP 套接字,那么大多数情况下它是可写的。最好使用
非阻塞调用,并在获得 EWOULDBLOCK 时将套接字放入 epoll。
1st question
What kind of file descriptor? If this file on file system you can't use select/poll or other tools for this purpose, file will be always readable or writeable regardless the state if disk and cache. If you need to do staff asynchronous you may use
aio_*
API but generally just read from file write to file and assume it is non-blocking.If it is TCP socket then it would be writeable most of the time. It is better to use
non-blocking calls and put sockets to epoll when you get EWOULDBLOCK.
考虑使用 EPOLLET 标志。这绝对是针对这种情况的。使用此标志时,您可以以正确的方式使用事件循环,而无需从 epoll 中首次注册后取消注册(或修改模式)文件描述符。 :) 享受!
Consider using EPOLLET flag. This is definitely for that case. When using this flag you can use event loop in a proper way without deregistering (or modifying mode on) file descriptors since first registration in epoll. :) enjoy!