0

I wrote a very simple program that one PC doing udp sendto, and another PC doing recvfrom. On 1Gbps ethernet link, the sender side cost 13% CPU but the receiver side cost only 5%.
Anyone know why sendto cost so much higher than recvfrom,and any good idea to reduce the sender cost, thanks very much!
main code :

while (1)
{
    static int sendLen = 0;
    sendLen = sendto(socketfd, buffer, buflen, 0, (struct sockaddr *)&dest, sizeof(dest));

    totalSize += sendLen;
    ++loopcnt;
    totalsend++;

    if (loopcnt == COUNTNUM)
    {
        clock_gettime(0, &end);
        unsigned int timecost = 1000 * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000; //ms
        double timecost_double = 1000 * (end.tv_sec - start.tv_sec) + ((double)(end.tv_nsec - start.tv_nsec)) / 1000000;

        printf("UDP APP TX %.1f M bps. PPS %.1f pps  packet size:%d, timecost=%.lf\n", (float)(totalSize * 8) / (timecost_double * 1000),
            (float)(loopcnt*1000) / (timecost), buflen, timecost_double);                     //timecost ms

        start = end;
        totalSize = 0;
        loopcnt = 0;

    }


}
Patrick
  • 51
  • 1
  • 2
  • 6
  • How did you profile this to get the CPU usage, and how did you adjust for total usage relative to duration? This may be a case of local cache allowing you to write faster before a send, while your CPU isn't as busy since it has to wait for each packet received. Also, it may help to post the code, in case there are additional reasons based on how you manage your connection. – Matt Jordan May 09 '16 at 19:00
  • I just use 'top -H' to check CPU usage. – Patrick May 10 '16 at 04:42

1 Answers1

0

If you don't call connect in the sender, every call to sendto has to do an implicit temporary connect operation, making it much more expensive. Add a connect call in the sender and watch the CPU use drop.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • I add a connect and use send instead of sendto, now the CPU Usage go down to 11% from 13%, but still much higher than recvfrom – Patrick May 09 '16 at 18:09
  • I use 'perf top' to show the hot functions, the tops are :13.90% [kernel] [k] csum_partial_copy_generic 9.97% [kernel] [k] __ticket_spin_lock 3.97% [kernel] [k] fib_table_lookup 3.56% [tg3] [k] tg3_start_xmit – Patrick May 10 '16 at 03:35