Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLOCK_MONOTONIC vs CLOCK_MONOTONIC_RAW truncated values

I am writing some test code where I need nanosecond resolution. When I use clock_gettime with CLOCK_MONOTONIC, i get a value I expect: 3327.874384321. When i use clock_gettime with CLOCK_MONOTONIC_RAW, i get a value that i do not expect: 3327.875723000

I've run this in a loop, and ALL of the values returned have the nanosecond resolution "truncated", 000.

Output from uname -a: Linux raspberrypi 3.12.22+ #691 PREEMPT Wed Jun 18 18:29:58 BST 2014 armv6l GNU/Linux

Thoughts on what is happening? How to address? I am currently considering disabling NTP so I can use CLOCK_MONOTONIC

like image 696
Nick Avatar asked Dec 12 '25 01:12

Nick


1 Answers

I think your conclusion that CLOCK_MONOTONIC_RAW is "truncated" is wrong. Rather, the resolution of the hardware clock source is probably just microseconds. The nonzero low digits you're seeing in CLOCK_MONOTONIC are because the timestamps from the hardware clock source are being scaled, per adjustments made via adjtime/NTP, to correct for imprecision in the hardware clock rate that would otherwise make it drift relative to real time.

To test this hypothesis, you should take a large number of timer samples with CLOCK_MONOTONIC and look for a pattern in the low digits. I suspect you'll find that all your timestamps differ by a multiple of some number of nanoseconds close to but not exactly 1000, e.g. maybe 995 or 1005 or so.

like image 50
R.. GitHub STOP HELPING ICE Avatar answered Dec 13 '25 14:12

R.. GitHub STOP HELPING ICE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!