This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AM3874: Time resolution possible to achieve in Linux running on AM3874

Part Number: AM3874

Hello,

I would like to put my app to sleep for 100 microseconds. I've used usleep function but I've noticed that it sleeps for about 200 microseconds. I wrote the following program to check what's what:

#include <stdio.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char **argv) {
    struct timespec res, tp1, tp2;


    clock_getres(CLOCK_MONOTONIC, &res);
    clock_gettime(CLOCK_MONOTONIC, &tp1);
    usleep(100);
    clock_gettime(CLOCK_MONOTONIC, &tp2);

    printf("resolution: %ld sec %ld ns\n", res.tv_sec, res.tv_nsec);
    printf("time1: %ld / %ld\n", tp1.tv_sec, tp1.tv_nsec);
    printf("time2: %ld / %ld\n", tp2.tv_sec, tp2.tv_nsec);
    printf("diff: %ld ns\n", (tp2.tv_sec - tp1.tv_sec) * 1000000000 + tp2.tv_nsec - tp1.tv_nsec);
}

The results are:

resolution: 0 sec 1 ns
time1: 136 / 905181953
time2: 136 / 905369318
187365 ns

So I get additional 87 microseconds. Is it possible to improve this and get as close to 100 us sleep as possible? Do I have to change something in my Linux configuration and recompile it?

  • Hi,

    100us sleep on a Linux kernel on this platform may not be consistently achievable without RT Linux, and that may not completely solve this problem.

    Please see this data:

    https://www.osadl.org/Latency-plot-of-system-in-rack-7-slot.qa-latencyplot-r7s8.0.html?shadow=1

    This is from a Beaglebone Black, which is also Cortex-A8 running at 1GHz. As you can see, it does not achieve this latency a lot of the time.

    So, you can switch to a RT kernel and see if that will meet your needs, but as Linux is not a RTOS, this latency may simply be more than it can deliver on worst case basis. 

    If you application can handle the jitter, you can certainly improve what you are seeing with scheduling techniques like using "nice" to increase the priority of this task.

    I hope this is helpful to you. Thanks for your post.