简体   繁体   English

以最低的延迟从Linux中的多播套接字接收数据

[英]receive data from multicast socket in linux with lowest latency

In HFT trading application I need to receive data from udp multicast socket. 在HFT交易应用程序中,我需要从udp多播套接字接收数据。 The only requirement is latency - this is so important that I can "spent" one CPU core. 唯一的要求是延迟-这是如此重要,以至于我可以“花费”一个CPU内核。 It's ok to spin or whatever. 可以旋转或其他任何方式。 This is what I currently have in Windows: 这是我目前在Windows中拥有的:

void Receiver::ThreadMethod() {
    //UINT32 seq;
    sockaddr_in Sender;
    int SenderAddrSize = sizeof(Sender);

    while (stayConnected) {
        int res=recvfrom(socketId,buf,sizeof(char) * RECEIVE_BUFFER_SIZE,0, (SOCKADDR *)& Sender, &SenderAddrSize);
        if (res == SOCKET_ERROR) {
            printf("recvfrom failed, WSAGetLastError: %d\n", WSAGetLastError());
            continue;
        }
        //seq = *(UINT32*)buf;
        //printf("%12s:seq=%6d:len=%4d\n", inet_ntoa(Sender.sin_addr), seq, res);
        unsigned char* buf2 = reinterpret_cast<unsigned char*>(buf);
        feed->ProcessMessage(res, buf2);
    }
}

recvfrom blocks, so it will be likely very slow (or i'm wrong?). recvfrom块,所以它可能会很慢(或者我错了?)。 I should rewrite this for Linux and achieve the best latency. 我应该针对Linux重写此代码并获得最佳延迟。 I need to process just ONE socket per thread, so I assume I should NOT use epoll as it designed more to process many sockets. 我只需要每个线程处理一个套接字,所以我认为我不应该使用epoll因为它的设计目的是处理更多的套接字。 What should I use? 我应该使用什么?

upd i've found similar question Low-latency read of UDP port UPD我已经找到了类似的问题低延迟读取UDP端口

In UNIX, you should use fcntl to set your socket non blocking : 在UNIX中,应该使用fcntl来设置套接字非阻塞状态:

fcntl(socket, F_SETFL, O_NONBLOCK);

Additionally, if you client needs to process multiple sockets (eg to aggregate multiple feeds), you should use a select call to process multiple file descriptors at once, and see which socket has available data, if any (this will, among other things, avoid to loop through all the sockets for nothing) 此外,如果您的客户需要处理多个套接字(例如,聚合多个提要),则应使用select调用一次处理多个文件描述符,并查看哪个套接字具有可用数据(如果有)(除其他外,避免不加循环地遍历所有套接字)

As for the latency, other factors such as the NIC type and configuration, the kernel settings (possibly having a NIC that bypass the kernel) will have considerable impacts on the latency (to be measured). 至于延迟,其他因素(例如NIC类型和配置,内核设置(可能具有绕过内核的NIC))将对延迟(待测)产生重大影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM