162306a36Sopenharmony_ciktime accessors 262306a36Sopenharmony_ci=============== 362306a36Sopenharmony_ci 462306a36Sopenharmony_ciDevice drivers can read the current time using ktime_get() and the many 562306a36Sopenharmony_cirelated functions declared in linux/timekeeping.h. As a rule of thumb, 662306a36Sopenharmony_ciusing an accessor with a shorter name is preferred over one with a longer 762306a36Sopenharmony_ciname if both are equally fit for a particular use case. 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciBasic ktime_t based interfaces 1062306a36Sopenharmony_ci------------------------------ 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciThe recommended simplest form returns an opaque ktime_t, with variants 1362306a36Sopenharmony_cithat return time for different clock references: 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci.. c:function:: ktime_t ktime_get( void ) 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci CLOCK_MONOTONIC 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci Useful for reliable timestamps and measuring short time intervals 2162306a36Sopenharmony_ci accurately. Starts at system boot time but stops during suspend. 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci.. c:function:: ktime_t ktime_get_boottime( void ) 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci CLOCK_BOOTTIME 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci Like ktime_get(), but does not stop when suspended. This can be 2862306a36Sopenharmony_ci used e.g. for key expiration times that need to be synchronized 2962306a36Sopenharmony_ci with other machines across a suspend operation. 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci.. c:function:: ktime_t ktime_get_real( void ) 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci CLOCK_REALTIME 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci Returns the time in relative to the UNIX epoch starting in 1970 3662306a36Sopenharmony_ci using the Coordinated Universal Time (UTC), same as gettimeofday() 3762306a36Sopenharmony_ci user space. This is used for all timestamps that need to 3862306a36Sopenharmony_ci persist across a reboot, like inode times, but should be avoided 3962306a36Sopenharmony_ci for internal uses, since it can jump backwards due to a leap 4062306a36Sopenharmony_ci second update, NTP adjustment settimeofday() operation from user 4162306a36Sopenharmony_ci space. 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci.. c:function:: ktime_t ktime_get_clocktai( void ) 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci CLOCK_TAI 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci Like ktime_get_real(), but uses the International Atomic Time (TAI) 4862306a36Sopenharmony_ci reference instead of UTC to avoid jumping on leap second updates. 4962306a36Sopenharmony_ci This is rarely useful in the kernel. 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci.. c:function:: ktime_t ktime_get_raw( void ) 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci CLOCK_MONOTONIC_RAW 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci Like ktime_get(), but runs at the same rate as the hardware 5662306a36Sopenharmony_ci clocksource without (NTP) adjustments for clock drift. This is 5762306a36Sopenharmony_ci also rarely needed in the kernel. 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cinanosecond, timespec64, and second output 6062306a36Sopenharmony_ci----------------------------------------- 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ciFor all of the above, there are variants that return the time in a 6362306a36Sopenharmony_cidifferent format depending on what is required by the user: 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci.. c:function:: u64 ktime_get_ns( void ) 6662306a36Sopenharmony_ci u64 ktime_get_boottime_ns( void ) 6762306a36Sopenharmony_ci u64 ktime_get_real_ns( void ) 6862306a36Sopenharmony_ci u64 ktime_get_clocktai_ns( void ) 6962306a36Sopenharmony_ci u64 ktime_get_raw_ns( void ) 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci Same as the plain ktime_get functions, but returning a u64 number 7262306a36Sopenharmony_ci of nanoseconds in the respective time reference, which may be 7362306a36Sopenharmony_ci more convenient for some callers. 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci.. c:function:: void ktime_get_ts64( struct timespec64 * ) 7662306a36Sopenharmony_ci void ktime_get_boottime_ts64( struct timespec64 * ) 7762306a36Sopenharmony_ci void ktime_get_real_ts64( struct timespec64 * ) 7862306a36Sopenharmony_ci void ktime_get_clocktai_ts64( struct timespec64 * ) 7962306a36Sopenharmony_ci void ktime_get_raw_ts64( struct timespec64 * ) 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci Same above, but returns the time in a 'struct timespec64', split 8262306a36Sopenharmony_ci into seconds and nanoseconds. This can avoid an extra division 8362306a36Sopenharmony_ci when printing the time, or when passing it into an external 8462306a36Sopenharmony_ci interface that expects a 'timespec' or 'timeval' structure. 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci.. c:function:: time64_t ktime_get_seconds( void ) 8762306a36Sopenharmony_ci time64_t ktime_get_boottime_seconds( void ) 8862306a36Sopenharmony_ci time64_t ktime_get_real_seconds( void ) 8962306a36Sopenharmony_ci time64_t ktime_get_clocktai_seconds( void ) 9062306a36Sopenharmony_ci time64_t ktime_get_raw_seconds( void ) 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci Return a coarse-grained version of the time as a scalar 9362306a36Sopenharmony_ci time64_t. This avoids accessing the clock hardware and rounds 9462306a36Sopenharmony_ci down the seconds to the full seconds of the last timer tick 9562306a36Sopenharmony_ci using the respective reference. 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ciCoarse and fast_ns access 9862306a36Sopenharmony_ci------------------------- 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciSome additional variants exist for more specialized cases: 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci.. c:function:: ktime_t ktime_get_coarse( void ) 10362306a36Sopenharmony_ci ktime_t ktime_get_coarse_boottime( void ) 10462306a36Sopenharmony_ci ktime_t ktime_get_coarse_real( void ) 10562306a36Sopenharmony_ci ktime_t ktime_get_coarse_clocktai( void ) 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci.. c:function:: u64 ktime_get_coarse_ns( void ) 10862306a36Sopenharmony_ci u64 ktime_get_coarse_boottime_ns( void ) 10962306a36Sopenharmony_ci u64 ktime_get_coarse_real_ns( void ) 11062306a36Sopenharmony_ci u64 ktime_get_coarse_clocktai_ns( void ) 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci.. c:function:: void ktime_get_coarse_ts64( struct timespec64 * ) 11362306a36Sopenharmony_ci void ktime_get_coarse_boottime_ts64( struct timespec64 * ) 11462306a36Sopenharmony_ci void ktime_get_coarse_real_ts64( struct timespec64 * ) 11562306a36Sopenharmony_ci void ktime_get_coarse_clocktai_ts64( struct timespec64 * ) 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci These are quicker than the non-coarse versions, but less accurate, 11862306a36Sopenharmony_ci corresponding to CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE 11962306a36Sopenharmony_ci in user space, along with the equivalent boottime/tai/raw 12062306a36Sopenharmony_ci timebase not available in user space. 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci The time returned here corresponds to the last timer tick, which 12362306a36Sopenharmony_ci may be as much as 10ms in the past (for CONFIG_HZ=100), same as 12462306a36Sopenharmony_ci reading the 'jiffies' variable. These are only useful when called 12562306a36Sopenharmony_ci in a fast path and one still expects better than second accuracy, 12662306a36Sopenharmony_ci but can't easily use 'jiffies', e.g. for inode timestamps. 12762306a36Sopenharmony_ci Skipping the hardware clock access saves around 100 CPU cycles 12862306a36Sopenharmony_ci on most modern machines with a reliable cycle counter, but 12962306a36Sopenharmony_ci up to several microseconds on older hardware with an external 13062306a36Sopenharmony_ci clocksource. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci.. c:function:: u64 ktime_get_mono_fast_ns( void ) 13362306a36Sopenharmony_ci u64 ktime_get_raw_fast_ns( void ) 13462306a36Sopenharmony_ci u64 ktime_get_boot_fast_ns( void ) 13562306a36Sopenharmony_ci u64 ktime_get_tai_fast_ns( void ) 13662306a36Sopenharmony_ci u64 ktime_get_real_fast_ns( void ) 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci These variants are safe to call from any context, including from 13962306a36Sopenharmony_ci a non-maskable interrupt (NMI) during a timekeeper update, and 14062306a36Sopenharmony_ci while we are entering suspend with the clocksource powered down. 14162306a36Sopenharmony_ci This is useful in some tracing or debugging code as well as 14262306a36Sopenharmony_ci machine check reporting, but most drivers should never call them, 14362306a36Sopenharmony_ci since the time is allowed to jump under certain conditions. 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciDeprecated time interfaces 14662306a36Sopenharmony_ci-------------------------- 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ciOlder kernels used some other interfaces that are now being phased out 14962306a36Sopenharmony_cibut may appear in third-party drivers being ported here. In particular, 15062306a36Sopenharmony_ciall interfaces returning a 'struct timeval' or 'struct timespec' have 15162306a36Sopenharmony_cibeen replaced because the tv_sec member overflows in year 2038 on 32-bit 15262306a36Sopenharmony_ciarchitectures. These are the recommended replacements: 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci.. c:function:: void ktime_get_ts( struct timespec * ) 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci Use ktime_get() or ktime_get_ts64() instead. 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci.. c:function:: void do_gettimeofday( struct timeval * ) 15962306a36Sopenharmony_ci void getnstimeofday( struct timespec * ) 16062306a36Sopenharmony_ci void getnstimeofday64( struct timespec64 * ) 16162306a36Sopenharmony_ci void ktime_get_real_ts( struct timespec * ) 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci ktime_get_real_ts64() is a direct replacement, but consider using 16462306a36Sopenharmony_ci monotonic time (ktime_get_ts64()) and/or a ktime_t based interface 16562306a36Sopenharmony_ci (ktime_get()/ktime_get_real()). 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci.. c:function:: struct timespec current_kernel_time( void ) 16862306a36Sopenharmony_ci struct timespec64 current_kernel_time64( void ) 16962306a36Sopenharmony_ci struct timespec get_monotonic_coarse( void ) 17062306a36Sopenharmony_ci struct timespec64 get_monotonic_coarse64( void ) 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci These are replaced by ktime_get_coarse_real_ts64() and 17362306a36Sopenharmony_ci ktime_get_coarse_ts64(). However, A lot of code that wants 17462306a36Sopenharmony_ci coarse-grained times can use the simple 'jiffies' instead, while 17562306a36Sopenharmony_ci some drivers may actually want the higher resolution accessors 17662306a36Sopenharmony_ci these days. 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci.. c:function:: struct timespec getrawmonotonic( void ) 17962306a36Sopenharmony_ci struct timespec64 getrawmonotonic64( void ) 18062306a36Sopenharmony_ci struct timespec timekeeping_clocktai( void ) 18162306a36Sopenharmony_ci struct timespec64 timekeeping_clocktai64( void ) 18262306a36Sopenharmony_ci struct timespec get_monotonic_boottime( void ) 18362306a36Sopenharmony_ci struct timespec64 get_monotonic_boottime64( void ) 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci These are replaced by ktime_get_raw()/ktime_get_raw_ts64(), 18662306a36Sopenharmony_ci ktime_get_clocktai()/ktime_get_clocktai_ts64() as well 18762306a36Sopenharmony_ci as ktime_get_boottime()/ktime_get_boottime_ts64(). 18862306a36Sopenharmony_ci However, if the particular choice of clock source is not 18962306a36Sopenharmony_ci important for the user, consider converting to 19062306a36Sopenharmony_ci ktime_get()/ktime_get_ts64() instead for consistency. 191