timekeeping.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. Clock sources, Clock events, sched_clock() and delay timers
  2. -----------------------------------------------------------
  3. This document tries to briefly explain some basic kernel timekeeping
  4. abstractions. It partly pertains to the drivers usually found in
  5. drivers/clocksource in the kernel tree, but the code may be spread out
  6. across the kernel.
  7. If you grep through the kernel source you will find a number of architecture-
  8. specific implementations of clock sources, clockevents and several likewise
  9. architecture-specific overrides of the sched_clock() function and some
  10. delay timers.
  11. To provide timekeeping for your platform, the clock source provides
  12. the basic timeline, whereas clock events shoot interrupts on certain points
  13. on this timeline, providing facilities such as high-resolution timers.
  14. sched_clock() is used for scheduling and timestamping, and delay timers
  15. provide an accurate delay source using hardware counters.
  16. Clock sources
  17. -------------
  18. The purpose of the clock source is to provide a timeline for the system that
  19. tells you where you are in time. For example issuing the command 'date' on
  20. a Linux system will eventually read the clock source to determine exactly
  21. what time it is.
  22. Typically the clock source is a monotonic, atomic counter which will provide
  23. n bits which count from 0 to 2^(n-1) and then wraps around to 0 and start over.
  24. It will ideally NEVER stop ticking as long as the system is running. It
  25. may stop during system suspend.
  26. The clock source shall have as high resolution as possible, and the frequency
  27. shall be as stable and correct as possible as compared to a real-world wall
  28. clock. It should not move unpredictably back and forth in time or miss a few
  29. cycles here and there.
  30. It must be immune to the kind of effects that occur in hardware where e.g.
  31. the counter register is read in two phases on the bus lowest 16 bits first
  32. and the higher 16 bits in a second bus cycle with the counter bits
  33. potentially being updated in between leading to the risk of very strange
  34. values from the counter.
  35. When the wall-clock accuracy of the clock source isn't satisfactory, there
  36. are various quirks and layers in the timekeeping code for e.g. synchronizing
  37. the user-visible time to RTC clocks in the system or against networked time
  38. servers using NTP, but all they do basically is update an offset against
  39. the clock source, which provides the fundamental timeline for the system.
  40. These measures does not affect the clock source per se, they only adapt the
  41. system to the shortcomings of it.
  42. The clock source struct shall provide means to translate the provided counter
  43. into a nanosecond value as an unsigned long long (unsigned 64 bit) number.
  44. Since this operation may be invoked very often, doing this in a strict
  45. mathematical sense is not desirable: instead the number is taken as close as
  46. possible to a nanosecond value using only the arithmetic operations
  47. multiply and shift, so in clocksource_cyc2ns() you find:
  48. ns ~= (clocksource * mult) >> shift
  49. You will find a number of helper functions in the clock source code intended
  50. to aid in providing these mult and shift values, such as
  51. clocksource_khz2mult(), clocksource_hz2mult() that help determine the
  52. mult factor from a fixed shift, and clocksource_register_hz() and
  53. clocksource_register_khz() which will help out assigning both shift and mult
  54. factors using the frequency of the clock source as the only input.
  55. For real simple clock sources accessed from a single I/O memory location
  56. there is nowadays even clocksource_mmio_init() which will take a memory
  57. location, bit width, a parameter telling whether the counter in the
  58. register counts up or down, and the timer clock rate, and then conjure all
  59. necessary parameters.
  60. Since a 32-bit counter at say 100 MHz will wrap around to zero after some 43
  61. seconds, the code handling the clock source will have to compensate for this.
  62. That is the reason why the clock source struct also contains a 'mask'
  63. member telling how many bits of the source are valid. This way the timekeeping
  64. code knows when the counter will wrap around and can insert the necessary
  65. compensation code on both sides of the wrap point so that the system timeline
  66. remains monotonic.
  67. Clock events
  68. ------------
  69. Clock events are the conceptual reverse of clock sources: they take a
  70. desired time specification value and calculate the values to poke into
  71. hardware timer registers.
  72. Clock events are orthogonal to clock sources. The same hardware
  73. and register range may be used for the clock event, but it is essentially
  74. a different thing. The hardware driving clock events has to be able to
  75. fire interrupts, so as to trigger events on the system timeline. On an SMP
  76. system, it is ideal (and customary) to have one such event driving timer per
  77. CPU core, so that each core can trigger events independently of any other
  78. core.
  79. You will notice that the clock event device code is based on the same basic
  80. idea about translating counters to nanoseconds using mult and shift
  81. arithmetic, and you find the same family of helper functions again for
  82. assigning these values. The clock event driver does not need a 'mask'
  83. attribute however: the system will not try to plan events beyond the time
  84. horizon of the clock event.
  85. sched_clock()
  86. -------------
  87. In addition to the clock sources and clock events there is a special weak
  88. function in the kernel called sched_clock(). This function shall return the
  89. number of nanoseconds since the system was started. An architecture may or
  90. may not provide an implementation of sched_clock() on its own. If a local
  91. implementation is not provided, the system jiffy counter will be used as
  92. sched_clock().
  93. As the name suggests, sched_clock() is used for scheduling the system,
  94. determining the absolute timeslice for a certain process in the CFS scheduler
  95. for example. It is also used for printk timestamps when you have selected to
  96. include time information in printk for things like bootcharts.
  97. Compared to clock sources, sched_clock() has to be very fast: it is called
  98. much more often, especially by the scheduler. If you have to do trade-offs
  99. between accuracy compared to the clock source, you may sacrifice accuracy
  100. for speed in sched_clock(). It however requires some of the same basic
  101. characteristics as the clock source, i.e. it should be monotonic.
  102. The sched_clock() function may wrap only on unsigned long long boundaries,
  103. i.e. after 64 bits. Since this is a nanosecond value this will mean it wraps
  104. after circa 585 years. (For most practical systems this means "never".)
  105. If an architecture does not provide its own implementation of this function,
  106. it will fall back to using jiffies, making its maximum resolution 1/HZ of the
  107. jiffy frequency for the architecture. This will affect scheduling accuracy
  108. and will likely show up in system benchmarks.
  109. The clock driving sched_clock() may stop or reset to zero during system
  110. suspend/sleep. This does not matter to the function it serves of scheduling
  111. events on the system. However it may result in interesting timestamps in
  112. printk().
  113. The sched_clock() function should be callable in any context, IRQ- and
  114. NMI-safe and return a sane value in any context.
  115. Some architectures may have a limited set of time sources and lack a nice
  116. counter to derive a 64-bit nanosecond value, so for example on the ARM
  117. architecture, special helper functions have been created to provide a
  118. sched_clock() nanosecond base from a 16- or 32-bit counter. Sometimes the
  119. same counter that is also used as clock source is used for this purpose.
  120. On SMP systems, it is crucial for performance that sched_clock() can be called
  121. independently on each CPU without any synchronization performance hits.
  122. Some hardware (such as the x86 TSC) will cause the sched_clock() function to
  123. drift between the CPUs on the system. The kernel can work around this by
  124. enabling the CONFIG_HAVE_UNSTABLE_SCHED_CLOCK option. This is another aspect
  125. that makes sched_clock() different from the ordinary clock source.
  126. Delay timers (some architectures only)
  127. --------------------------------------
  128. On systems with variable CPU frequency, the various kernel delay() functions
  129. will sometimes behave strangely. Basically these delays usually use a hard
  130. loop to delay a certain number of jiffy fractions using a "lpj" (loops per
  131. jiffy) value, calibrated on boot.
  132. Let's hope that your system is running on maximum frequency when this value
  133. is calibrated: as an effect when the frequency is geared down to half the
  134. full frequency, any delay() will be twice as long. Usually this does not
  135. hurt, as you're commonly requesting that amount of delay *or more*. But
  136. basically the semantics are quite unpredictable on such systems.
  137. Enter timer-based delays. Using these, a timer read may be used instead of
  138. a hard-coded loop for providing the desired delay.
  139. This is done by declaring a struct delay_timer and assigning the appropriate
  140. function pointers and rate settings for this delay timer.
  141. This is available on some architectures like OpenRISC or ARM.