关于epoll和splice的问题

发布于 2024-10-02 10:43:21 字数 814 浏览 3 评论 0原文

我的应用程序将通过网络发送大量数据,因此我决定(因为我使用的是 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 套接字。我想问你两件事:

  1. 将会有相当多的 epoll_ctl 调用,当我有这么多套接字时,会不会很慢?
  2. 文件描述符必须首先变得可读,并且在套接字变得可写之前会有一些间隔。我可以确定,当套接字变为可写文件描述符时仍然可读(以避免阻塞调用)吗?

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:

  1. There will be quite a lot of epoll_ctl calls, won't wit be slow when I will have so many sockets?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-10-09 10:43:21

第一个问题

  1. 您可以使用边缘触发而不是触发轮询,因此您不必每次都删除套接字。
  2. 您可以使用 EPOLLONESHOT 来防止删除套接字

文件描述符必须首先变得可读,并且在套接字变得可写之前会有一些间隔。

什么样的文件描述符?如果文件系统上的此文件无法使用选择/轮询或其他工具来实现此目的,则无论磁盘和缓存的状态如何,文件都将始终可读或可写。如果您需要异步工作,您可以使用aio_* API,但通常只是从文件读取写入文件并假设它是非阻塞的。

如果是 TCP 套接字,那么大多数情况下它是可写的。最好使用
非阻塞调用,并在获得 EWOULDBLOCK 时将套接字放入 epoll。

1st question

  1. You can use edge triggered rather then even triggered polling thus you do not have to delete socket each time.
  2. You can use EPOLLONESHOT to prevent removing socket

File descriptor has to become readable first and there will be some interval before socket will become writable.

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.

玉环 2024-10-09 10:43:21

考虑使用 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文