ktime accessors¶
Device drivers can read the current time usingktime_get() and the manyrelated functions declared in linux/timekeeping.h. As a rule of thumb,using an accessor with a shorter name is preferred over one with a longername if both are equally fit for a particular use case.
Basic ktime_t based interfaces¶
The recommended simplest form returns an opaque ktime_t, with variantsthat return time for different clock references:
- ktime_t
ktime_get(void)¶ CLOCK_MONOTONIC
Useful for reliable timestamps and measuring short time intervalsaccurately. Starts at system boot time but stops during suspend.
- ktime_t
ktime_get_boottime(void)¶ CLOCK_BOOTTIME
Like
ktime_get(), but does not stop when suspended. This can beused e.g. for key expiration times that need to be synchronizedwith other machines across a suspend operation.
- ktime_t
ktime_get_real(void)¶ CLOCK_REALTIME
Returns the time in relative to the UNIX epoch starting in 1970using the Coordinated Universal Time (UTC), same as gettimeofday()user space. This is used for all timestamps that need topersist across a reboot, like inode times, but should be avoidedfor internal uses, since it can jump backwards due to a leapsecond update, NTP adjustment settimeofday() operation from userspace.
- ktime_t
ktime_get_clocktai(void)¶ - CLOCK_TAI
Like
ktime_get_real(), but uses the International Atomic Time (TAI)reference instead of UTC to avoid jumping on leap second updates.This is rarely useful in the kernel.
- ktime_t
ktime_get_raw(void)¶ CLOCK_MONOTONIC_RAW
Like
ktime_get(), but runs at the same rate as the hardwareclocksource without (NTP) adjustments for clock drift. This isalso rarely needed in the kernel.
nanosecond, timespec64, and second output¶
For all of the above, there are variants that return the time in adifferent format depending on what is required by the user:
- u64
ktime_get_ns(void)¶ - u64
ktime_get_boottime_ns(void)¶ - u64
ktime_get_real_ns(void)¶ - u64
ktime_get_clocktai_ns(void)¶ - u64
ktime_get_raw_ns(void)¶ Same as the plain ktime_get functions, but returning a u64 numberof nanoseconds in the respective time reference, which may bemore convenient for some callers.
- void
ktime_get_ts64(struct timespec64 *)¶ - void
ktime_get_boottime_ts64(struct timespec64 *)¶ - void
ktime_get_real_ts64(struct timespec64 *)¶ - void
ktime_get_clocktai_ts64(struct timespec64 *)¶ - void
ktime_get_raw_ts64(struct timespec64 *)¶ Same above, but returns the time in a ‘struct timespec64’, splitinto seconds and nanoseconds. This can avoid an extra divisionwhen printing the time, or when passing it into an externalinterface that expects a ‘timespec’ or ‘timeval’ structure.
- time64_t
ktime_get_seconds(void)¶ - time64_t
ktime_get_boottime_seconds(void)¶ - time64_t
ktime_get_real_seconds(void)¶ - time64_t
ktime_get_clocktai_seconds(void)¶ - time64_t
ktime_get_raw_seconds(void)¶ Return a coarse-grained version of the time as a scalartime64_t. This avoids accessing the clock hardware and roundsdown the seconds to the full seconds of the last timer tickusing the respective reference.
Coarse and fast_ns access¶
Some additional variants exist for more specialized cases:
- ktime_t
ktime_get_coarse(void)¶ - ktime_t
ktime_get_coarse_boottime(void)¶ - ktime_t
ktime_get_coarse_real(void)¶ - ktime_t
ktime_get_coarse_clocktai(void)¶
- u64
ktime_get_coarse_ns(void)¶ - u64
ktime_get_coarse_boottime_ns(void)¶ - u64
ktime_get_coarse_real_ns(void)¶ - u64
ktime_get_coarse_clocktai_ns(void)¶
- void
ktime_get_coarse_ts64(struct timespec64 *)¶ - void
ktime_get_coarse_boottime_ts64(struct timespec64 *)¶ - void
ktime_get_coarse_real_ts64(struct timespec64 *)¶ - void
ktime_get_coarse_clocktai_ts64(struct timespec64 *)¶ These are quicker than the non-coarse versions, but less accurate,corresponding to CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSEin user space, along with the equivalent boottime/tai/rawtimebase not available in user space.
The time returned here corresponds to the last timer tick, whichmay be as much as 10ms in the past (for CONFIG_HZ=100), same asreading the ‘jiffies’ variable. These are only useful when calledin a fast path and one still expects better than second accuracy,but can’t easily use ‘jiffies’, e.g. for inode timestamps.Skipping the hardware clock access saves around 100 CPU cycleson most modern machines with a reliable cycle counter, butup to several microseconds on older hardware with an externalclocksource.
- u64
ktime_get_mono_fast_ns(void)¶ - u64
ktime_get_raw_fast_ns(void)¶ - u64
ktime_get_boot_fast_ns(void)¶ - u64
ktime_get_real_fast_ns(void)¶ These variants are safe to call from any context, including froma non-maskable interrupt (NMI) during a timekeeper update, andwhile we are entering suspend with the clocksource powered down.This is useful in some tracing or debugging code as well asmachine check reporting, but most drivers should never call them,since the time is allowed to jump under certain conditions.
Deprecated time interfaces¶
Older kernels used some other interfaces that are now being phased outbut may appear in third-party drivers being ported here. In particular,all interfaces returning a ‘struct timeval’ or ‘struct timespec’ havebeen replaced because the tv_sec member overflows in year 2038 on 32-bitarchitectures. These are the recommended replacements:
- void
ktime_get_ts(struct timespec *)¶ Use
ktime_get()orktime_get_ts64()instead.
- void
do_gettimeofday(struct timeval *)¶ - void
getnstimeofday(struct timespec *)¶ - void
getnstimeofday64(struct timespec64 *)¶ - void
ktime_get_real_ts(struct timespec *)¶ ktime_get_real_ts64()is a direct replacement, but consider usingmonotonic time (ktime_get_ts64()) and/or a ktime_t based interface(ktime_get()/ktime_get_real()).
- struct timespec
current_kernel_time(void)¶ - struct timespec64
current_kernel_time64(void)¶ - struct timespec
get_monotonic_coarse(void)¶ - struct timespec64
get_monotonic_coarse64(void)¶ These are replaced by
ktime_get_coarse_real_ts64()andktime_get_coarse_ts64(). However, A lot of code that wantscoarse-grained times can use the simple ‘jiffies’ instead, whilesome drivers may actually want the higher resolution accessorsthese days.
- struct timespec
getrawmonotonic(void)¶ - struct timespec64
getrawmonotonic64(void)¶ - struct timespec
timekeeping_clocktai(void)¶ - struct timespec64
timekeeping_clocktai64(void)¶ - struct timespec
get_monotonic_boottime(void)¶ - struct timespec64
get_monotonic_boottime64(void)¶ These are replaced by
ktime_get_raw()/ktime_get_raw_ts64(),ktime_get_clocktai()/ktime_get_clocktai_ts64()as wellasktime_get_boottime()/ktime_get_boottime_ts64().However, if the particular choice of clock source is notimportant for the user, consider converting toktime_get()/ktime_get_ts64()instead for consistency.