Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What does the accuracy of timing functions depend on? The computer hardware? Simultaneously running programs? #8

Open
mavavilj opened this issue Apr 19, 2022 · 4 comments
Labels
question Further information is requested

Comments

@mavavilj
Copy link

mavavilj commented Apr 19, 2022

What does the accuracy of timing functions depend on? The computer hardware? Simultaneously running programs? Something else?

I'm running https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/timinglib_timestamps_basic.c on an Elitebook 8470P and I see:

~/Documents/codings/c/gettime> ./hello_world
Hello World.

millis() = 74693423 ms;    micros() = 74693423313 us;    nanos() = 74693423313140 ns
millis() = 74693423 ms;    micros() = 74693423341 us;    nanos() = 74693423341053 ns
millis() = 74693423 ms;    micros() = 74693423346 us;    nanos() = 74693423346025 ns
millis() = 74693423 ms;    micros() = 74693423353 us;    nanos() = 74693423353034 ns
millis() = 74693423 ms;    micros() = 74693423357 us;    nanos() = 74693423357136 ns
millis() = 74693423 ms;    micros() = 74693423361 us;    nanos() = 74693423361226 ns
millis() = 74693423 ms;    micros() = 74693423365 us;    nanos() = 74693423365412 ns
millis() = 74693423 ms;    micros() = 74693423369 us;    nanos() = 74693423369428 ns
millis() = 74693423 ms;    micros() = 74693423378 us;    nanos() = 74693423378081 ns
millis() = 74693423 ms;    micros() = 74693423384 us;    nanos() = 74693423384539 ns
millis() = 74693423 ms;    micros() = 74693423390 us;    nanos() = 74693423390749 ns
millis() = 74693423 ms;    micros() = 74693423396 us;    nanos() = 74693423396853 ns
millis() = 74693423 ms;    micros() = 74693423402 us;    nanos() = 74693423402182 ns
millis() = 74693423 ms;    micros() = 74693423408 us;    nanos() = 74693423408212 ns
millis() = 74693423 ms;    micros() = 74693423413 us;    nanos() = 74693423413594 ns
millis() = 74693423 ms;    micros() = 74693423419 us;    nanos() = 74693423419628 ns
millis() = 74693423 ms;    micros() = 74693423426 us;    nanos() = 74693423426006 ns
millis() = 74693423 ms;    micros() = 74693423432 us;    nanos() = 74693423432297 ns
millis() = 74693423 ms;    micros() = 74693423438 us;    nanos() = 74693423438391 ns
millis() = 74693423 ms;    micros() = 74693423444 us;    nanos() = 74693423444672 ns

which seems to be 1) less accurate and 2) more jittery than the sample output.

@mavavilj
Copy link
Author

mavavilj commented Apr 19, 2022

But I also guess these are still negligible errors, since the header talks about e.g. sleep being at best at 55 \mu s accuracy:

https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/timinglib.h#L114

@ElectricRCAircraftGuy
Copy link
Owner

ElectricRCAircraftGuy commented Apr 19, 2022

What does the accuracy of timing functions depend on? The computer hardware? Simultaneously running programs?

Yes, both of those--probably mostly your hardware. Also, the Linux scheduler chosen to run this thread makes a huge difference. One more factor that makes a really big difference is the clock you choose to obtain timestamps. I specify CLOCK_MONOTONIC here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/timinglib.c#L33

You can see the various clock options here: https://man7.org/linux/man-pages/man3/clock_gettime.3.html

For a rough estimate of the resolution for your computer with the concurrent events running and with the default SCHED_OTHER Linux scheduler, run get_estimated_resolution() repeatedly and print out the output: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/timinglib.h#L91-L95:

/// Obtain an estimate, in nanoseconds, of the resolution of the underlying clock used to obtain all
/// of the timestamps above. This estimate is determined empirically through testing.
/// - Testing on my x86-64 Linux Ubuntu system shows my estimated timestamp resolution to be
///   ~15~30 ns. Run your own tests via "timinglib_get_resolution.c".
uint64_t get_estimated_resolution();

To turn on the Linux SCHED_RR soft real-time round robin scheduler in order to get better resolution (and an improved ~4 us nanosleep resolution on my system, rather than ~55 us nanosleep resolution), run use_realtime_scheduler() once at the start of your program: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/timinglib.h#L183-L189

// Call this function to turn ON the Linux soft realtime scheduler for the
// calling thread, in order to get minimum sleep intervals and resolution of
// ~4 us with worst-case results being 100~400 us (0.1~0.4 ms), rather than the
// default minimum sleep time of ~55 us with worst-case results being ~8000 us
// (8 ms)! Calling this function allows for much better sleep resolutions and
// timing accuracies than the default `SCHED_OTHER` scheduler can provide!
void use_realtime_scheduler();

@ElectricRCAircraftGuy ElectricRCAircraftGuy added the question Further information is requested label Apr 19, 2022
@ElectricRCAircraftGuy
Copy link
Owner

@mavavilj , how did you find this code by the way?

@mavavilj
Copy link
Author

https://stackoverflow.com/a/67731965

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants