recvfrom的延迟随其被称为频率而变化

时间:2018-07-24 14:32:44

标签: c linux sockets latency

在以下情况下,我不了解recvfrom的行为: 我有一个循环,该循环发送和接收udp数据包并使其休眠。取决于接收(接收)延迟的位会有所不同。 如果我睡眠22毫秒或更长时间,则延迟约为170微秒;如果我睡眠21毫秒或更短时间,则延迟约为1毫秒。 我想念什么?为什么延迟变化如此之大?我曾考虑过中断合并,但是我使用ethtool进行了检查,但已将其禁用。

该代码来自https://gist.github.com/suyash/0f100b1518334fcf650bbefd54556df9,并稍加修改。

client.c:

#include <arpa/inet.h> 
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <inttypes.h>

#define TIMESPEC_NSEC(ts) ((ts)->tv_sec * 1000000000ULL + (ts)->tv_nsec)

uint64_t realtime_now()
{
   struct timespec now_ts;
   clock_gettime(CLOCK_MONOTONIC, &now_ts);
   return TIMESPEC_NSEC(&now_ts);
}

int main() {
   const char* server_name = "192.168.1.240";
   const int server_port = 8877;

   struct sockaddr_in server_address;
   memset(&server_address, 0, sizeof(server_address));
   server_address.sin_family = AF_INET;
   inet_pton(AF_INET, server_name, &server_address.sin_addr);
   server_address.sin_port = htons(server_port);
   int sock;
   if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
       printf("could not create socket\n");
       return 1;
   }

  const char* data_to_send = "...";
  struct timespec ts;
  ts.tv_nsec = 21 *1000*1000;
  ts.tv_sec =  0;
  uint64_t t0,t1;
  while(1)
  {
    int len =sendto(sock, data_to_send, strlen(data_to_send), 0,
                    (struct sockaddr*)&server_address,sizeof(server_address));

    char buffer[100];
    t0 = realtime_now();
    recvfrom(sock, buffer, len, 0, NULL, NULL);
    t1 = realtime_now();
    printf("%" PRIu64 "\n",t1 - t0);
    buffer[len] = '\0';
    printf("recieved: '%s'\n", buffer);
    int err = nanosleep(&ts, NULL);
    if (err == EINTR)
    {
        err = 0;
    }
    if(err!=0)
    {
        perror("nanosleep failed");
        break;
    }
  }
  close(sock);
  return 0;
 }

server.c

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    int SERVER_PORT = 8877;
    struct sockaddr_in server_address;
    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(SERVER_PORT);
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);

    int sock;
    if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("could not create socket\n");
        return 1;
    }

    if ((bind(sock, (struct sockaddr *)&server_address,
              sizeof(server_address))) < 0) {
        printf("could not bind socket\n");
        return 1;
    }
    struct sockaddr_in client_address;
    socklen_t client_address_len = sizeof(client_address);
    while (true) {
        char buffer[100];
        int len = recvfrom(sock, buffer, sizeof(buffer), 0,
                           (struct sockaddr *)&client_address,
                           &client_address_len);

        buffer[len] = '\0';
        sendto(sock, buffer, len, 0, (struct sockaddr *)&client_address,
               sizeof(client_address));
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

请注意,您是通过有线网络通过本地网络将数据包发送到192.168.1.240的(除非您设置了主机本地虚拟网络)。数据包首先通过您的交换机/路由器,然后再到达您的服务器。和相反的方式。因此,您可以在此处有效地测量往返时间,而不是recvfrom的延迟。