1 ============================================== 2 Clock sources, Clock events, sched_clock() and 3 ============================================== 4 5 This document tries to briefly explain some ba 6 abstractions. It partly pertains to the driver 7 drivers/clocksource in the kernel tree, but th 8 across the kernel. 9 10 If you grep through the kernel source you will 11 specific implementations of clock sources, clo 12 architecture-specific overrides of the sched_c 13 delay timers. 14 15 To provide timekeeping for your platform, the 16 the basic timeline, whereas clock events shoot 17 on this timeline, providing facilities such as 18 sched_clock() is used for scheduling and times 19 provide an accurate delay source using hardwar 20 21 22 Clock sources 23 ------------- 24 25 The purpose of the clock source is to provide 26 tells you where you are in time. For example i 27 a Linux system will eventually read the clock 28 what time it is. 29 30 Typically the clock source is a monotonic, ato 31 n bits which count from 0 to (2^n)-1 and then 32 It will ideally NEVER stop ticking as long as 33 may stop during system suspend. 34 35 The clock source shall have as high resolution 36 shall be as stable and correct as possible as 37 clock. It should not move unpredictably back a 38 cycles here and there. 39 40 It must be immune to the kind of effects that 41 the counter register is read in two phases on 42 and the higher 16 bits in a second bus cycle w 43 potentially being updated in between leading t 44 values from the counter. 45 46 When the wall-clock accuracy of the clock sour 47 are various quirks and layers in the timekeepi 48 the user-visible time to RTC clocks in the sys 49 servers using NTP, but all they do basically i 50 the clock source, which provides the fundament 51 These measures does not affect the clock sourc 52 system to the shortcomings of it. 53 54 The clock source struct shall provide means to 55 into a nanosecond value as an unsigned long lo 56 Since this operation may be invoked very often 57 mathematical sense is not desirable: instead t 58 possible to a nanosecond value using only the 59 multiply and shift, so in clocksource_cyc2ns() 60 61 ns ~= (clocksource * mult) >> shift 62 63 You will find a number of helper functions in 64 to aid in providing these mult and shift value 65 clocksource_khz2mult(), clocksource_hz2mult() 66 mult factor from a fixed shift, and clocksourc 67 clocksource_register_khz() which will help out 68 factors using the frequency of the clock sourc 69 70 For real simple clock sources accessed from a 71 there is nowadays even clocksource_mmio_init() 72 location, bit width, a parameter telling wheth 73 register counts up or down, and the timer cloc 74 necessary parameters. 75 76 Since a 32-bit counter at say 100 MHz will wra 77 seconds, the code handling the clock source wi 78 That is the reason why the clock source struct 79 member telling how many bits of the source are 80 code knows when the counter will wrap around a 81 compensation code on both sides of the wrap po 82 remains monotonic. 83 84 85 Clock events 86 ------------ 87 88 Clock events are the conceptual reverse of clo 89 desired time specification value and calculate 90 hardware timer registers. 91 92 Clock events are orthogonal to clock sources. 93 and register range may be used for the clock e 94 a different thing. The hardware driving clock 95 fire interrupts, so as to trigger events on th 96 system, it is ideal (and customary) to have on 97 CPU core, so that each core can trigger events 98 core. 99 100 You will notice that the clock event device co 101 idea about translating counters to nanoseconds 102 arithmetic, and you find the same family of he 103 assigning these values. The clock event driver 104 attribute however: the system will not try to 105 horizon of the clock event. 106 107 108 sched_clock() 109 ------------- 110 111 In addition to the clock sources and clock eve 112 function in the kernel called sched_clock(). T 113 number of nanoseconds since the system was sta 114 may not provide an implementation of sched_clo 115 implementation is not provided, the system jif 116 sched_clock(). 117 118 As the name suggests, sched_clock() is used fo 119 determining the absolute timeslice for a certa 120 for example. It is also used for printk timest 121 include time information in printk for things 122 123 Compared to clock sources, sched_clock() has t 124 much more often, especially by the scheduler. 125 between accuracy compared to the clock source, 126 for speed in sched_clock(). It however require 127 characteristics as the clock source, i.e. it s 128 129 The sched_clock() function may wrap only on un 130 i.e. after 64 bits. Since this is a nanosecond 131 after circa 585 years. (For most practical sys 132 133 If an architecture does not provide its own im 134 it will fall back to using jiffies, making its 135 jiffy frequency for the architecture. This wil 136 and will likely show up in system benchmarks. 137 138 The clock driving sched_clock() may stop or re 139 suspend/sleep. This does not matter to the fun 140 events on the system. However it may result in 141 printk(). 142 143 The sched_clock() function should be callable 144 NMI-safe and return a sane value in any contex 145 146 Some architectures may have a limited set of t 147 counter to derive a 64-bit nanosecond value, s 148 architecture, special helper functions have be 149 sched_clock() nanosecond base from a 16- or 32 150 same counter that is also used as clock source 151 152 On SMP systems, it is crucial for performance 153 independently on each CPU without any synchron 154 Some hardware (such as the x86 TSC) will cause 155 drift between the CPUs on the system. The kern 156 enabling the CONFIG_HAVE_UNSTABLE_SCHED_CLOCK 157 that makes sched_clock() different from the or 158 159 160 Delay timers (some architectures only) 161 -------------------------------------- 162 163 On systems with variable CPU frequency, the va 164 will sometimes behave strangely. Basically the 165 loop to delay a certain number of jiffy fracti 166 jiffy) value, calibrated on boot. 167 168 Let's hope that your system is running on maxi 169 is calibrated: as an effect when the frequency 170 full frequency, any delay() will be twice as l 171 hurt, as you're commonly requesting that amoun 172 basically the semantics are quite unpredictabl 173 174 Enter timer-based delays. Using these, a timer 175 a hard-coded loop for providing the desired de 176 177 This is done by declaring a struct delay_timer 178 function pointers and rate settings for this d 179 180 This is available on some architectures like O
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.