clinuxtimenetwork-programminggettimeofday

why struct timeval return 0 after first use? and how to over come this problem?


I am making a simple Sender and Reciever to check the time difference between using Reno CC algorithm and a Cubic CC algorithm. in order to make an accurate comparison between them I am using struct timeval instead of clock, and I am also adding a check for packet loss with sudo tc qdisc add dev lo root netem loss 10%

my code sends 2 parts of the file one part in cubic second part in reno, for a couple of times, I am asking it to do. for some reason the timeval return 0 on the second loop I tried checking with gdb and didn't found why and how to fix it.

enter image description here

relevant code

while (1)
    {
        struct timeval start_t_cubic, end_t_cubic, tval_result_cubic; // will use them to check the timing
        struct timeval start_t_reno, end_t_reno, tval_result_reno; // will use them to check the timing

        // set the algorithm to cubic
        char *cc = "cubic";
        if (setsockopt(client_socket, IPPROTO_TCP, TCP_CONGESTION, cc, strlen(cc)) != 0)
        {
            printf("setsockopt failed \n");
            return;
        }

        gettimeofday(&start_t_cubic, NULL); // start the time

        while (num_of_bytes < BUFSIZE / 2)
        {
            recv(client_socket, client_message, 1, 0);
            num_of_bytes++;
        }

        gettimeofday(&end_t_cubic, NULL); // finish count for first part of the file

        timersub(&end_t_cubic, &start_t_cubic, &tval_result_cubic); // the total time cubic

             printf("algo: cubic, time: %ld.%06ld, iter num: %d\n",
               (long int)tval_result_cubic.tv_sec,
               (long int)tval_result_cubic.tv_usec,
               iteration_number);
     
        // change the algorithm to reno
        char *cc_algo = "reno"; // the CC algorithm to use (in this case, "reno")
        check(setsockopt(server_socket, IPPROTO_TCP, TCP_CONGESTION, cc_algo, strlen(cc_algo)),
              "setsockopt failed");



        gettimeofday(&start_t_reno, NULL); // start the time

        // recive a file of half mega bytes
        while (num_of_bytes < BUFSIZE)
        {
            recv(client_socket, client_message, 1, 0);
            num_of_bytes++;
        }

        gettimeofday(&end_t_reno, NULL);                         // finish count for first part of the file
        timersub(&end_t_reno, &start_t_reno, &tval_result_reno); // the total time reno
        printf("algo: reno, time: %ld.%06ld, iter num: %d\n", (long int)tval_result_reno.tv_sec, (long int)tval_result_reno.tv_usec, iteration_number);
        // store the time elapsed in a variable
        long int time_elapsed_reno = tval_result_reno.tv_sec * 1000000 + tval_result_reno.tv_usec;

       
    }

if someone had come across this issue I will be glad for any help or hint please

full source code: https://github.com/dolev146/networking


Solution

  • The problem was that in the second iteration the function time was too fast there fore the output was 0

    Somehow the PC optimizes the code

    I added usleep(1000) to so that I not get 0 value for the time difference