0

I am using hrtimer to set a timer for 10 ms(need accuracy in ns). So while trying to check the timer accuracy I tried using getnstimeofday() and was printing the time and was comparing with the time of next timer call but the time difference between the dmesg time stamp was not matching with difference between the time that I am getting using getnstimeofday() in two successive timer call. Later while exploring found that getnstimeofday() uses CLOCK_REALTIME and function that uses CLOCK_MONOTONIC is better way to measure time difference between two event.

So tried both Ktime_get() and Ktime_get_ts() sepeartely but in both cases time difference between two consecutive print was less than 10 ms (~10,000 u sec less for 100 iteration i.e. for 1 sec(10 ms *100)) where as difference between two consecutive dmesg time stamp is more than 10 ms(~200 u sec more for 100 iteration i.e. for 1 sec(10 ms * 100)) for same senario. Can anyone help me what is wrong in my understanding.

Used below functions:
getnstimeofday() --> CLOCK_REALTIME
Ktime_get_ts() --> CLOCK_MONOTONIC
Ktime_get --> CLOCK_MONOTONIC

to calculate the timer accuracy.

Below is just one snippet of kernel logs

< idle>-0 [003] d.h. 6271.497363[= A]: : current time = 6261 sec, current time = 602118348[=C] ns
< idle>-0 [003] d.h. 6272.498933[= B]: : current time = 6262 sec, current time = 602108850[=D] ns
< idle>-0 [003] d.h. 6273.500501: : current time = 6263 sec, current time = 602097738 ns
< idle>-0 [003] d.h. 6274.502080: : current time = 6264 sec, current time = 602098777 ns
< idle>-0 [003] d.h. 6275.503647: : current time = 6265 sec, current time = 602087231 ns
< idle>-0 [003] d.h. 6276.505345: : current time = 6266 sec, current time = 602205944 ns
< idle>-0 [003] d.h. 6277.506775: : current time = 6267 sec, current time = 602057258 ns
< idle>-0 [003] d.h. 6278.508343: : current time = 6268 sec, current time = 602045768 ns
< idle>-0 [003] d.h. 6279.509911: : current time = 6269 sec, current time = 602035489 ns
< idle>-0 [003] d.h. 6280.511597: : current time = 6270 sec, current time = 602143379 ns

A --> dmesg time stamp at T
B --> dmesg time stamp at T+1 sec
C --> time calculated using ktime_get_ts() at time stamp at T
D --> time calculated using ktime_get_ts() at time stamp at T+1 sec

B-A = 1 sec + 157 u sec
D-C = 1 sec - 9498 ns

0 Answers0