Nick
Nick

Reputation: 2913

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

Upvotes: 3

Views: 7980

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215387

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.

Upvotes: 5

Related Questions