[SOLVED] Linux for loop 1 million measurement execution time, jitter phenomenon

Issue

This Content is from Stack Overflow. Question asked by user10249913

hardware::
Linux xxx 5.4.0-65-generic #Ubuntu x86_64 GNU/Linux
cpu 1 core
environment vwmare

software
thread:
policy = SCHED_FIFO;
param.sched_priority = 90;//highest pri

unsigned long getUstime()
{
    struct timespec start_time;
    clock_gettime(CLOCK_MONOTONIC, &start_time);
    return start_time.tv_nsec/1000 + start_time.tv_sec*1e6;
}

#if 1
int  testclocktime()  {
    std::cout << "-----test clocktime------"<<std::endl;

    unsigned long long start = 0;
    unsigned long long end = 0;

    const static double  LOOP_TIMES = 100000.;
    double timesumCall = 0;
    for(int j = 0; j < 10; ++j) // loop1 million test;
    {
        volatile unsigned long long sum = 0;

        for(volatile  long i= 0; i< LOOP_TIMES; ++i)
        {
            start = getUstime(); // start time

            // test somethings

            end = getUstime(); // end time

            double now_diff =  end - start;  //calucate elapsed time
            sum += end - start;
            
            if(now_diff > 500) //judge
            {
                printf("overload 1000 us, now is %fn", now_diff);
            }
        }
        double times= sum / LOOP_TIMES;
        timesumCall+=times;
        printf("AVG_CYCLE : %lf us n", times);
    }

    timesumCall = timesumCall/10;
    printf("total AVG_CYCLE : %lf us n", timesumCall);

    return 0;
}
#endif

output:

-----testclocktime------
AVG_CYCLE : 1.591390 us 
AVG_CYCLE : 1.586720 us 
AVG_CYCLE : 1.579920 us 
now diff time is 109.000000
now diff time is 104.000000
AVG_CYCLE : 1.580130 us 
AVG_CYCLE : 1.581860 us 
now diff time is 114.000000
now diff time is 101.000000
now diff time is 104.000000
AVG_CYCLE : 1.584920 us 
now diff time is 106.000000
now diff time is 51880.000000
AVG_CYCLE : 2.102190 us 
now diff time is 106.000000
AVG_CYCLE : 1.563000 us 
AVG_CYCLE : 1.596560 us 
AVG_CYCLE : 1.579730 us 
total AVG_CYCLE : 1.634642 us 
total Maxrdtime : 51880.000000 us

As shown in the output, in the program, the normal operation time is about 2US, and the average time is within 2US, but there will be a time jump greater than 100us in the cycle process.

I have set the thread priority to the highest level and the thread scheduling mode to FIFO in the execution function.

Why is this happening? Or to put it this way, what if I make sure that I make sure that I execute functions without jitter in a real-time thread?



Solution

Why is this happening?

Jitter is to be expected in Linux, FreeBSD, macOS, and other general purpose operating systems.

What if I make sure that I execute functions without jitter in a real-time thread?

The only way to really make sure is to utilize a real time version of Linux.


This Question was asked in StackOverflow by user10249913 and Answered by James Risner It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?