timekeeping.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ktime accessors
  2. ===============
  3. Device drivers can read the current time using ktime_get() and the many
  4. related functions declared in linux/timekeeping.h. As a rule of thumb,
  5. using an accessor with a shorter name is preferred over one with a longer
  6. name if both are equally fit for a particular use case.
  7. Basic ktime_t based interfaces
  8. ------------------------------
  9. The recommended simplest form returns an opaque ktime_t, with variants
  10. that return time for different clock references:
  11. .. c:function:: ktime_t ktime_get( void )
  12. CLOCK_MONOTONIC
  13. Useful for reliable timestamps and measuring short time intervals
  14. accurately. Starts at system boot time but stops during suspend.
  15. .. c:function:: ktime_t ktime_get_boottime( void )
  16. CLOCK_BOOTTIME
  17. Like ktime_get(), but does not stop when suspended. This can be
  18. used e.g. for key expiration times that need to be synchronized
  19. with other machines across a suspend operation.
  20. .. c:function:: ktime_t ktime_get_real( void )
  21. CLOCK_REALTIME
  22. Returns the time in relative to the UNIX epoch starting in 1970
  23. using the Coordinated Universal Time (UTC), same as gettimeofday()
  24. user space. This is used for all timestamps that need to
  25. persist across a reboot, like inode times, but should be avoided
  26. for internal uses, since it can jump backwards due to a leap
  27. second update, NTP adjustment settimeofday() operation from user
  28. space.
  29. .. c:function:: ktime_t ktime_get_clocktai( void )
  30. CLOCK_TAI
  31. Like ktime_get_real(), but uses the International Atomic Time (TAI)
  32. reference instead of UTC to avoid jumping on leap second updates.
  33. This is rarely useful in the kernel.
  34. .. c:function:: ktime_t ktime_get_raw( void )
  35. CLOCK_MONOTONIC_RAW
  36. Like ktime_get(), but runs at the same rate as the hardware
  37. clocksource without (NTP) adjustments for clock drift. This is
  38. also rarely needed in the kernel.
  39. nanosecond, timespec64, and second output
  40. -----------------------------------------
  41. For all of the above, there are variants that return the time in a
  42. different format depending on what is required by the user:
  43. .. c:function:: u64 ktime_get_ns( void )
  44. u64 ktime_get_boottime_ns( void )
  45. u64 ktime_get_real_ns( void )
  46. u64 ktime_get_clocktai_ns( void )
  47. u64 ktime_get_raw_ns( void )
  48. Same as the plain ktime_get functions, but returning a u64 number
  49. of nanoseconds in the respective time reference, which may be
  50. more convenient for some callers.
  51. .. c:function:: void ktime_get_ts64( struct timespec64 * )
  52. void ktime_get_boottime_ts64( struct timespec64 * )
  53. void ktime_get_real_ts64( struct timespec64 * )
  54. void ktime_get_clocktai_ts64( struct timespec64 * )
  55. void ktime_get_raw_ts64( struct timespec64 * )
  56. Same above, but returns the time in a 'struct timespec64', split
  57. into seconds and nanoseconds. This can avoid an extra division
  58. when printing the time, or when passing it into an external
  59. interface that expects a 'timespec' or 'timeval' structure.
  60. .. c:function:: time64_t ktime_get_seconds( void )
  61. time64_t ktime_get_boottime_seconds( void )
  62. time64_t ktime_get_real_seconds( void )
  63. time64_t ktime_get_clocktai_seconds( void )
  64. time64_t ktime_get_raw_seconds( void )
  65. Return a coarse-grained version of the time as a scalar
  66. time64_t. This avoids accessing the clock hardware and rounds
  67. down the seconds to the full seconds of the last timer tick
  68. using the respective reference.
  69. Coarse and fast_ns access
  70. -------------------------
  71. Some additional variants exist for more specialized cases:
  72. .. c:function:: ktime_t ktime_get_coarse( void )
  73. ktime_t ktime_get_coarse_boottime( void )
  74. ktime_t ktime_get_coarse_real( void )
  75. ktime_t ktime_get_coarse_clocktai( void )
  76. .. c:function:: u64 ktime_get_coarse_ns( void )
  77. u64 ktime_get_coarse_boottime_ns( void )
  78. u64 ktime_get_coarse_real_ns( void )
  79. u64 ktime_get_coarse_clocktai_ns( void )
  80. .. c:function:: void ktime_get_coarse_ts64( struct timespec64 * )
  81. void ktime_get_coarse_boottime_ts64( struct timespec64 * )
  82. void ktime_get_coarse_real_ts64( struct timespec64 * )
  83. void ktime_get_coarse_clocktai_ts64( struct timespec64 * )
  84. These are quicker than the non-coarse versions, but less accurate,
  85. corresponding to CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE
  86. in user space, along with the equivalent boottime/tai/raw
  87. timebase not available in user space.
  88. The time returned here corresponds to the last timer tick, which
  89. may be as much as 10ms in the past (for CONFIG_HZ=100), same as
  90. reading the 'jiffies' variable. These are only useful when called
  91. in a fast path and one still expects better than second accuracy,
  92. but can't easily use 'jiffies', e.g. for inode timestamps.
  93. Skipping the hardware clock access saves around 100 CPU cycles
  94. on most modern machines with a reliable cycle counter, but
  95. up to several microseconds on older hardware with an external
  96. clocksource.
  97. .. c:function:: u64 ktime_get_mono_fast_ns( void )
  98. u64 ktime_get_raw_fast_ns( void )
  99. u64 ktime_get_boot_fast_ns( void )
  100. u64 ktime_get_tai_fast_ns( void )
  101. u64 ktime_get_real_fast_ns( void )
  102. These variants are safe to call from any context, including from
  103. a non-maskable interrupt (NMI) during a timekeeper update, and
  104. while we are entering suspend with the clocksource powered down.
  105. This is useful in some tracing or debugging code as well as
  106. machine check reporting, but most drivers should never call them,
  107. since the time is allowed to jump under certain conditions.
  108. Deprecated time interfaces
  109. --------------------------
  110. Older kernels used some other interfaces that are now being phased out
  111. but may appear in third-party drivers being ported here. In particular,
  112. all interfaces returning a 'struct timeval' or 'struct timespec' have
  113. been replaced because the tv_sec member overflows in year 2038 on 32-bit
  114. architectures. These are the recommended replacements:
  115. .. c:function:: void ktime_get_ts( struct timespec * )
  116. Use ktime_get() or ktime_get_ts64() instead.
  117. .. c:function:: void do_gettimeofday( struct timeval * )
  118. void getnstimeofday( struct timespec * )
  119. void getnstimeofday64( struct timespec64 * )
  120. void ktime_get_real_ts( struct timespec * )
  121. ktime_get_real_ts64() is a direct replacement, but consider using
  122. monotonic time (ktime_get_ts64()) and/or a ktime_t based interface
  123. (ktime_get()/ktime_get_real()).
  124. .. c:function:: struct timespec current_kernel_time( void )
  125. struct timespec64 current_kernel_time64( void )
  126. struct timespec get_monotonic_coarse( void )
  127. struct timespec64 get_monotonic_coarse64( void )
  128. These are replaced by ktime_get_coarse_real_ts64() and
  129. ktime_get_coarse_ts64(). However, A lot of code that wants
  130. coarse-grained times can use the simple 'jiffies' instead, while
  131. some drivers may actually want the higher resolution accessors
  132. these days.
  133. .. c:function:: struct timespec getrawmonotonic( void )
  134. struct timespec64 getrawmonotonic64( void )
  135. struct timespec timekeeping_clocktai( void )
  136. struct timespec64 timekeeping_clocktai64( void )
  137. struct timespec get_monotonic_boottime( void )
  138. struct timespec64 get_monotonic_boottime64( void )
  139. These are replaced by ktime_get_raw()/ktime_get_raw_ts64(),
  140. ktime_get_clocktai()/ktime_get_clocktai_ts64() as well
  141. as ktime_get_boottime()/ktime_get_boottime_ts64().
  142. However, if the particular choice of clock source is not
  143. important for the user, consider converting to
  144. ktime_get()/ktime_get_ts64() instead for consistency.