Diff between times in kernel-space and user-space
i need to exchange a struct timeval between kernl-space and user-space (through a netlink message) in order to calculate the elapsed time between the creation of the message and the elaboration in userspace.
i'm using the function jiffies_to_timeval
in k开发者_运维问答ernel-space; in user-space i call gettimeofday
...but if i print the number of seconds of both structures i obtain a difference of 200 seconds...i can't figure out the reason for this.
Any ideas?
Thank you all!
There are several clocks inside of Linux. The differences boil down to whether they tick while the machine is suspended, whether they try to track "wall clock time", and how high-resolution they are.
do_gettimeofday
is probably not what you want to use, since that clock tries to track "wall clock time"; that is, it can actually jump forwards or backwards. What if someone -- or something (like NTP) -- decided to set the system clock between the two times you read it?
For timing, I would recommend using the POSIX clock_gettime
function with the CLOCK_MONOTONIC
clock ID. (You might have to compile your user-space application with -lrt
, though.)
I believe clock_gettime
also exists as a kernel function, although I do not have my kernel sources handy at the moment...
精彩评论