Regarding Monotonic clock

390 views
Skip to first unread message

Somya Anand

unread,
Oct 11, 2014, 9:32:49 AM10/11/14
to Arnd Bergmann, opw-k...@googlegroups.com
Hi Arnd,

I have again a conceptual doubt how will you decide when to use monotonic clock and when real_time clock?

As far as I enquired it says If you want to compute the elapsed time between two events observed on the one machine without an intervening reboot, CLOCK_MONOTONIC is the best option.
he
But still, my concepts are not very clear. Kindly help me out.

Thanks
--
Somya

Arnd Bergmann

unread,
Oct 11, 2014, 4:16:47 PM10/11/14
to Somya Anand, opw-k...@googlegroups.com
Let me start by confusing you some more, before we try to make it
clearer. There are actually several more clock sources, each with
its own use cases, see 'man clock_gettime', kernel/time/posix-timers.c
and include/linux/timekeeping.h:

- real time (also called wall clock), using ktime_get_real, ktime_get_real_ts64,
ktime_get_real_ts64, getnstimeofday64 or do_gettimeofday is most useful
for reporting the absolute time to users. You have to use this for any
kind of file system time stamps, or anything that gets copied to user
space as part of an informational interface. This time is expected
to be as close as possible to the international time standard, but
because any clock can be set wrong, or can drift, code using this must
be prepared for the time to just forward or backward, go faster or
slower or get leap second adjustments

- monotonic time, using ktime_get, ktime_get_ts or ktime_get_ns:
this one will never jump backwards, and ticks at the same rate
as real time. This is as close as it gets to telling you the
elapsed time when you are comparing two time values, so it's normally
the right choice within code that doesn't communicate the time values
to the outside (user space, file system, network). If you need to
communicate a monotonic time to user space, first convert it
using ktime_mono_to_real.

- tai: like real time, but does not regard leap seconds. This is
rarely used, don't worry about it.

- boot time: ktime_get_boottime, ktime_get_boot_ns: like monotonic time,
but does not stop when the system is suspended, so you can use this
if you want to compare times relative to an outside event (e.g.
network socket timeouts) rather than events inside of the system
that also stop during suspend (e.g. user processes).

- coarse real or monotonic time: using current_kernel_time() or
get_monotonic_coarse(): much faster to read, but only changes
at the same rate as jiffies (between 1 and 10 ms normally),
rarely used in the kernel because usually you can just as well
use the seconds portion (get_seconds). As mentioned, we should
probably add a monotonic version of get_seconds.

- raw monotonic time: ktime_get_raw/getrawmonotonic like monotonic,
but ticks at the nominal hardware rate, which can drift more than
real or monotonic do. This is the right one to use if you need to
synchronize against hardware in the system that is based on the
same hardware clock signal as the system time.

Arnd
Reply all
Reply to author
Forward
0 new messages