Reputation: 67
Is it possible in linux to change the rate at which it ticks for finer time resolution?
The default resolution is set to 1000hz in my system, meaning that I have a minimum latency of 1ms, I need a much more percise system with latency at 1us or even 1ns, but I don't know if it is even possible to change this beyond 1000hz.
if it's possible to change, where/how can this be accomplished?
Is there possible any other workaround considering im writing a c script?
Upvotes: 3
Views: 3740
Reputation: 6786
Of course it depends on hardware (if specific embedded) and on the kernel version, however in modern Linux sleep time is not controlled by Jiffies. Linux kernels above 2.6.24 can have high precision event timer (HPET) to trigger some events.
It is not related to application sleep. It is not needed to set CONFIG_HZ_1000
if you want to sleep 1ms or less.
In Linux you can have functions usleep()
and nanosleep()
. The clock resolution is already 1 ns in a desktop PC running Ubuntu. However it does not mean that it is possible to perfectly sleep for few nanoseconds in not real time systems.
Check this example (compiled with gcc clock.c -O3 -Wall -lrt -o clock
):
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main()
{
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);
return 0;
}
By default it occurs that I have additional 60 usec delays above the desired sleep time. Such accuracy can be acceptable for sleep time for hundreds microseconds (CONFIG_HZ=250
in my system).
To reduce that delay the process should be run with higher priority, for example:
sudo chrt --rr 99 ./clock
In that case I can have the error less than 20 us.
Without any sleep function between subsequent calls to clock_gettime
I can see delays in range 200 - 900 ns
. So, it is possible to measure sleep with usec
precision by busy loop:
clock_gettime(CLOCK_MONOTONIC, &tp1);
clock_gettime(CLOCK_MONOTONIC, &tp2);
while (get_diff(&tp1, &tp2) < ns_time_to_sleep)
clock_gettime(CLOCK_MONOTONIC, &tp2);
Upvotes: 2