aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/timers
diff options
context:
space:
mode:
authorThomas Gleixner <tglx@linutronix.de>2008-03-05 12:28:15 -0500
committerThomas Gleixner <tglx@linutronix.de>2008-04-17 06:22:31 -0400
commit3833eecc183ce052e9ac96b39b45121a2d11ac16 (patch)
tree58844d33c1006c6e11d9cdbed822c6aa89d9dfcc /Documentation/timers
parent903b8a8d4835a796f582033802c83283886f4a3d (diff)
Documentation: move timer related documentation to a single place
We have two directories with timer related information in Documentation/: hrtimers/ and hrtimer/. timer_stats are not restricted to hrtimers. Move all those files into Documentation/timers where we can pile up other timer related docs as well. Pointed-out-by: Randy Dunlap <randy@oracle.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'Documentation/timers')
-rw-r--r--Documentation/timers/highres.txt249
-rw-r--r--Documentation/timers/hrtimers.txt178
-rw-r--r--Documentation/timers/timer_stats.txt73
3 files changed, 500 insertions, 0 deletions
diff --git a/Documentation/timers/highres.txt b/Documentation/timers/highres.txt
new file mode 100644
index 000000000000..a73ecf5b4bdb
--- /dev/null
+++ b/Documentation/timers/highres.txt
@@ -0,0 +1,249 @@
1High resolution timers and dynamic ticks design notes
2-----------------------------------------------------
3
4Further information can be found in the paper of the OLS 2006 talk "hrtimers
5and beyond". The paper is part of the OLS 2006 Proceedings Volume 1, which can
6be found on the OLS website:
7http://www.linuxsymposium.org/2006/linuxsymposium_procv1.pdf
8
9The slides to this talk are available from:
10http://tglx.de/projects/hrtimers/ols2006-hrtimers.pdf
11
12The slides contain five figures (pages 2, 15, 18, 20, 22), which illustrate the
13changes in the time(r) related Linux subsystems. Figure #1 (p. 2) shows the
14design of the Linux time(r) system before hrtimers and other building blocks
15got merged into mainline.
16
17Note: the paper and the slides are talking about "clock event source", while we
18switched to the name "clock event devices" in meantime.
19
20The design contains the following basic building blocks:
21
22- hrtimer base infrastructure
23- timeofday and clock source management
24- clock event management
25- high resolution timer functionality
26- dynamic ticks
27
28
29hrtimer base infrastructure
30---------------------------
31
32The hrtimer base infrastructure was merged into the 2.6.16 kernel. Details of
33the base implementation are covered in Documentation/hrtimers/hrtimer.txt. See
34also figure #2 (OLS slides p. 15)
35
36The main differences to the timer wheel, which holds the armed timer_list type
37timers are:
38 - time ordered enqueueing into a rb-tree
39 - independent of ticks (the processing is based on nanoseconds)
40
41
42timeofday and clock source management
43-------------------------------------
44
45John Stultz's Generic Time Of Day (GTOD) framework moves a large portion of
46code out of the architecture-specific areas into a generic management
47framework, as illustrated in figure #3 (OLS slides p. 18). The architecture
48specific portion is reduced to the low level hardware details of the clock
49sources, which are registered in the framework and selected on a quality based
50decision. The low level code provides hardware setup and readout routines and
51initializes data structures, which are used by the generic time keeping code to
52convert the clock ticks to nanosecond based time values. All other time keeping
53related functionality is moved into the generic code. The GTOD base patch got
54merged into the 2.6.18 kernel.
55
56Further information about the Generic Time Of Day framework is available in the
57OLS 2005 Proceedings Volume 1:
58http://www.linuxsymposium.org/2005/linuxsymposium_procv1.pdf
59
60The paper "We Are Not Getting Any Younger: A New Approach to Time and
61Timers" was written by J. Stultz, D.V. Hart, & N. Aravamudan.
62
63Figure #3 (OLS slides p.18) illustrates the transformation.
64
65
66clock event management
67----------------------
68
69While clock sources provide read access to the monotonically increasing time
70value, clock event devices are used to schedule the next event
71interrupt(s). The next event is currently defined to be periodic, with its
72period defined at compile time. The setup and selection of the event device
73for various event driven functionalities is hardwired into the architecture
74dependent code. This results in duplicated code across all architectures and
75makes it extremely difficult to change the configuration of the system to use
76event interrupt devices other than those already built into the
77architecture. Another implication of the current design is that it is necessary
78to touch all the architecture-specific implementations in order to provide new
79functionality like high resolution timers or dynamic ticks.
80
81The clock events subsystem tries to address this problem by providing a generic
82solution to manage clock event devices and their usage for the various clock
83event driven kernel functionalities. The goal of the clock event subsystem is
84to minimize the clock event related architecture dependent code to the pure
85hardware related handling and to allow easy addition and utilization of new
86clock event devices. It also minimizes the duplicated code across the
87architectures as it provides generic functionality down to the interrupt
88service handler, which is almost inherently hardware dependent.
89
90Clock event devices are registered either by the architecture dependent boot
91code or at module insertion time. Each clock event device fills a data
92structure with clock-specific property parameters and callback functions. The
93clock event management decides, by using the specified property parameters, the
94set of system functions a clock event device will be used to support. This
95includes the distinction of per-CPU and per-system global event devices.
96
97System-level global event devices are used for the Linux periodic tick. Per-CPU
98event devices are used to provide local CPU functionality such as process
99accounting, profiling, and high resolution timers.
100
101The management layer assigns one or more of the following functions to a clock
102event device:
103 - system global periodic tick (jiffies update)
104 - cpu local update_process_times
105 - cpu local profiling
106 - cpu local next event interrupt (non periodic mode)
107
108The clock event device delegates the selection of those timer interrupt related
109functions completely to the management layer. The clock management layer stores
110a function pointer in the device description structure, which has to be called
111from the hardware level handler. This removes a lot of duplicated code from the
112architecture specific timer interrupt handlers and hands the control over the
113clock event devices and the assignment of timer interrupt related functionality
114to the core code.
115
116The clock event layer API is rather small. Aside from the clock event device
117registration interface it provides functions to schedule the next event
118interrupt, clock event device notification service and support for suspend and
119resume.
120
121The framework adds about 700 lines of code which results in a 2KB increase of
122the kernel binary size. The conversion of i386 removes about 100 lines of
123code. The binary size decrease is in the range of 400 byte. We believe that the
124increase of flexibility and the avoidance of duplicated code across
125architectures justifies the slight increase of the binary size.
126
127The conversion of an architecture has no functional impact, but allows to
128utilize the high resolution and dynamic tick functionalites without any change
129to the clock event device and timer interrupt code. After the conversion the
130enabling of high resolution timers and dynamic ticks is simply provided by
131adding the kernel/time/Kconfig file to the architecture specific Kconfig and
132adding the dynamic tick specific calls to the idle routine (a total of 3 lines
133added to the idle function and the Kconfig file)
134
135Figure #4 (OLS slides p.20) illustrates the transformation.
136
137
138high resolution timer functionality
139-----------------------------------
140
141During system boot it is not possible to use the high resolution timer
142functionality, while making it possible would be difficult and would serve no
143useful function. The initialization of the clock event device framework, the
144clock source framework (GTOD) and hrtimers itself has to be done and
145appropriate clock sources and clock event devices have to be registered before
146the high resolution functionality can work. Up to the point where hrtimers are
147initialized, the system works in the usual low resolution periodic mode. The
148clock source and the clock event device layers provide notification functions
149which inform hrtimers about availability of new hardware. hrtimers validates
150the usability of the registered clock sources and clock event devices before
151switching to high resolution mode. This ensures also that a kernel which is
152configured for high resolution timers can run on a system which lacks the
153necessary hardware support.
154
155The high resolution timer code does not support SMP machines which have only
156global clock event devices. The support of such hardware would involve IPI
157calls when an interrupt happens. The overhead would be much larger than the
158benefit. This is the reason why we currently disable high resolution and
159dynamic ticks on i386 SMP systems which stop the local APIC in C3 power
160state. A workaround is available as an idea, but the problem has not been
161tackled yet.
162
163The time ordered insertion of timers provides all the infrastructure to decide
164whether the event device has to be reprogrammed when a timer is added. The
165decision is made per timer base and synchronized across per-cpu timer bases in
166a support function. The design allows the system to utilize separate per-CPU
167clock event devices for the per-CPU timer bases, but currently only one
168reprogrammable clock event device per-CPU is utilized.
169
170When the timer interrupt happens, the next event interrupt handler is called
171from the clock event distribution code and moves expired timers from the
172red-black tree to a separate double linked list and invokes the softirq
173handler. An additional mode field in the hrtimer structure allows the system to
174execute callback functions directly from the next event interrupt handler. This
175is restricted to code which can safely be executed in the hard interrupt
176context. This applies, for example, to the common case of a wakeup function as
177used by nanosleep. The advantage of executing the handler in the interrupt
178context is the avoidance of up to two context switches - from the interrupted
179context to the softirq and to the task which is woken up by the expired
180timer.
181
182Once a system has switched to high resolution mode, the periodic tick is
183switched off. This disables the per system global periodic clock event device -
184e.g. the PIT on i386 SMP systems.
185
186The periodic tick functionality is provided by an per-cpu hrtimer. The callback
187function is executed in the next event interrupt context and updates jiffies
188and calls update_process_times and profiling. The implementation of the hrtimer
189based periodic tick is designed to be extended with dynamic tick functionality.
190This allows to use a single clock event device to schedule high resolution
191timer and periodic events (jiffies tick, profiling, process accounting) on UP
192systems. This has been proved to work with the PIT on i386 and the Incrementer
193on PPC.
194
195The softirq for running the hrtimer queues and executing the callbacks has been
196separated from the tick bound timer softirq to allow accurate delivery of high
197resolution timer signals which are used by itimer and POSIX interval
198timers. The execution of this softirq can still be delayed by other softirqs,
199but the overall latencies have been significantly improved by this separation.
200
201Figure #5 (OLS slides p.22) illustrates the transformation.
202
203
204dynamic ticks
205-------------
206
207Dynamic ticks are the logical consequence of the hrtimer based periodic tick
208replacement (sched_tick). The functionality of the sched_tick hrtimer is
209extended by three functions:
210
211- hrtimer_stop_sched_tick
212- hrtimer_restart_sched_tick
213- hrtimer_update_jiffies
214
215hrtimer_stop_sched_tick() is called when a CPU goes into idle state. The code
216evaluates the next scheduled timer event (from both hrtimers and the timer
217wheel) and in case that the next event is further away than the next tick it
218reprograms the sched_tick to this future event, to allow longer idle sleeps
219without worthless interruption by the periodic tick. The function is also
220called when an interrupt happens during the idle period, which does not cause a
221reschedule. The call is necessary as the interrupt handler might have armed a
222new timer whose expiry time is before the time which was identified as the
223nearest event in the previous call to hrtimer_stop_sched_tick.
224
225hrtimer_restart_sched_tick() is called when the CPU leaves the idle state before
226it calls schedule(). hrtimer_restart_sched_tick() resumes the periodic tick,
227which is kept active until the next call to hrtimer_stop_sched_tick().
228
229hrtimer_update_jiffies() is called from irq_enter() when an interrupt happens
230in the idle period to make sure that jiffies are up to date and the interrupt
231handler has not to deal with an eventually stale jiffy value.
232
233The dynamic tick feature provides statistical values which are exported to
234userspace via /proc/stats and can be made available for enhanced power
235management control.
236
237The implementation leaves room for further development like full tickless
238systems, where the time slice is controlled by the scheduler, variable
239frequency profiling, and a complete removal of jiffies in the future.
240
241
242Aside the current initial submission of i386 support, the patchset has been
243extended to x86_64 and ARM already. Initial (work in progress) support is also
244available for MIPS and PowerPC.
245
246 Thomas, Ingo
247
248
249
diff --git a/Documentation/timers/hrtimers.txt b/Documentation/timers/hrtimers.txt
new file mode 100644
index 000000000000..ce31f65e12e7
--- /dev/null
+++ b/Documentation/timers/hrtimers.txt
@@ -0,0 +1,178 @@
1
2hrtimers - subsystem for high-resolution kernel timers
3----------------------------------------------------
4
5This patch introduces a new subsystem for high-resolution kernel timers.
6
7One might ask the question: we already have a timer subsystem
8(kernel/timers.c), why do we need two timer subsystems? After a lot of
9back and forth trying to integrate high-resolution and high-precision
10features into the existing timer framework, and after testing various
11such high-resolution timer implementations in practice, we came to the
12conclusion that the timer wheel code is fundamentally not suitable for
13such an approach. We initially didn't believe this ('there must be a way
14to solve this'), and spent a considerable effort trying to integrate
15things into the timer wheel, but we failed. In hindsight, there are
16several reasons why such integration is hard/impossible:
17
18- the forced handling of low-resolution and high-resolution timers in
19 the same way leads to a lot of compromises, macro magic and #ifdef
20 mess. The timers.c code is very "tightly coded" around jiffies and
21 32-bitness assumptions, and has been honed and micro-optimized for a
22 relatively narrow use case (jiffies in a relatively narrow HZ range)
23 for many years - and thus even small extensions to it easily break
24 the wheel concept, leading to even worse compromises. The timer wheel
25 code is very good and tight code, there's zero problems with it in its
26 current usage - but it is simply not suitable to be extended for
27 high-res timers.
28
29- the unpredictable [O(N)] overhead of cascading leads to delays which
30 necessitate a more complex handling of high resolution timers, which
31 in turn decreases robustness. Such a design still led to rather large
32 timing inaccuracies. Cascading is a fundamental property of the timer
33 wheel concept, it cannot be 'designed out' without unevitably
34 degrading other portions of the timers.c code in an unacceptable way.
35
36- the implementation of the current posix-timer subsystem on top of
37 the timer wheel has already introduced a quite complex handling of
38 the required readjusting of absolute CLOCK_REALTIME timers at
39 settimeofday or NTP time - further underlying our experience by
40 example: that the timer wheel data structure is too rigid for high-res
41 timers.
42
43- the timer wheel code is most optimal for use cases which can be
44 identified as "timeouts". Such timeouts are usually set up to cover
45 error conditions in various I/O paths, such as networking and block
46 I/O. The vast majority of those timers never expire and are rarely
47 recascaded because the expected correct event arrives in time so they
48 can be removed from the timer wheel before any further processing of
49 them becomes necessary. Thus the users of these timeouts can accept
50 the granularity and precision tradeoffs of the timer wheel, and
51 largely expect the timer subsystem to have near-zero overhead.
52 Accurate timing for them is not a core purpose - in fact most of the
53 timeout values used are ad-hoc. For them it is at most a necessary
54 evil to guarantee the processing of actual timeout completions
55 (because most of the timeouts are deleted before completion), which
56 should thus be as cheap and unintrusive as possible.
57
58The primary users of precision timers are user-space applications that
59utilize nanosleep, posix-timers and itimer interfaces. Also, in-kernel
60users like drivers and subsystems which require precise timed events
61(e.g. multimedia) can benefit from the availability of a separate
62high-resolution timer subsystem as well.
63
64While this subsystem does not offer high-resolution clock sources just
65yet, the hrtimer subsystem can be easily extended with high-resolution
66clock capabilities, and patches for that exist and are maturing quickly.
67The increasing demand for realtime and multimedia applications along
68with other potential users for precise timers gives another reason to
69separate the "timeout" and "precise timer" subsystems.
70
71Another potential benefit is that such a separation allows even more
72special-purpose optimization of the existing timer wheel for the low
73resolution and low precision use cases - once the precision-sensitive
74APIs are separated from the timer wheel and are migrated over to
75hrtimers. E.g. we could decrease the frequency of the timeout subsystem
76from 250 Hz to 100 HZ (or even smaller).
77
78hrtimer subsystem implementation details
79----------------------------------------
80
81the basic design considerations were:
82
83- simplicity
84
85- data structure not bound to jiffies or any other granularity. All the
86 kernel logic works at 64-bit nanoseconds resolution - no compromises.
87
88- simplification of existing, timing related kernel code
89
90another basic requirement was the immediate enqueueing and ordering of
91timers at activation time. After looking at several possible solutions
92such as radix trees and hashes, we chose the red black tree as the basic
93data structure. Rbtrees are available as a library in the kernel and are
94used in various performance-critical areas of e.g. memory management and
95file systems. The rbtree is solely used for time sorted ordering, while
96a separate list is used to give the expiry code fast access to the
97queued timers, without having to walk the rbtree.
98
99(This separate list is also useful for later when we'll introduce
100high-resolution clocks, where we need separate pending and expired
101queues while keeping the time-order intact.)
102
103Time-ordered enqueueing is not purely for the purposes of
104high-resolution clocks though, it also simplifies the handling of
105absolute timers based on a low-resolution CLOCK_REALTIME. The existing
106implementation needed to keep an extra list of all armed absolute
107CLOCK_REALTIME timers along with complex locking. In case of
108settimeofday and NTP, all the timers (!) had to be dequeued, the
109time-changing code had to fix them up one by one, and all of them had to
110be enqueued again. The time-ordered enqueueing and the storage of the
111expiry time in absolute time units removes all this complex and poorly
112scaling code from the posix-timer implementation - the clock can simply
113be set without having to touch the rbtree. This also makes the handling
114of posix-timers simpler in general.
115
116The locking and per-CPU behavior of hrtimers was mostly taken from the
117existing timer wheel code, as it is mature and well suited. Sharing code
118was not really a win, due to the different data structures. Also, the
119hrtimer functions now have clearer behavior and clearer names - such as
120hrtimer_try_to_cancel() and hrtimer_cancel() [which are roughly
121equivalent to del_timer() and del_timer_sync()] - so there's no direct
1221:1 mapping between them on the algorithmical level, and thus no real
123potential for code sharing either.
124
125Basic data types: every time value, absolute or relative, is in a
126special nanosecond-resolution type: ktime_t. The kernel-internal
127representation of ktime_t values and operations is implemented via
128macros and inline functions, and can be switched between a "hybrid
129union" type and a plain "scalar" 64bit nanoseconds representation (at
130compile time). The hybrid union type optimizes time conversions on 32bit
131CPUs. This build-time-selectable ktime_t storage format was implemented
132to avoid the performance impact of 64-bit multiplications and divisions
133on 32bit CPUs. Such operations are frequently necessary to convert
134between the storage formats provided by kernel and userspace interfaces
135and the internal time format. (See include/linux/ktime.h for further
136details.)
137
138hrtimers - rounding of timer values
139-----------------------------------
140
141the hrtimer code will round timer events to lower-resolution clocks
142because it has to. Otherwise it will do no artificial rounding at all.
143
144one question is, what resolution value should be returned to the user by
145the clock_getres() interface. This will return whatever real resolution
146a given clock has - be it low-res, high-res, or artificially-low-res.
147
148hrtimers - testing and verification
149----------------------------------
150
151We used the high-resolution clock subsystem ontop of hrtimers to verify
152the hrtimer implementation details in praxis, and we also ran the posix
153timer tests in order to ensure specification compliance. We also ran
154tests on low-resolution clocks.
155
156The hrtimer patch converts the following kernel functionality to use
157hrtimers:
158
159 - nanosleep
160 - itimers
161 - posix-timers
162
163The conversion of nanosleep and posix-timers enabled the unification of
164nanosleep and clock_nanosleep.
165
166The code was successfully compiled for the following platforms:
167
168 i386, x86_64, ARM, PPC, PPC64, IA64
169
170The code was run-tested on the following platforms:
171
172 i386(UP/SMP), x86_64(UP/SMP), ARM, PPC
173
174hrtimers were also integrated into the -rt tree, along with a
175hrtimers-based high-resolution clock implementation, so the hrtimers
176code got a healthy amount of testing and use in practice.
177
178 Thomas Gleixner, Ingo Molnar
diff --git a/Documentation/timers/timer_stats.txt b/Documentation/timers/timer_stats.txt
new file mode 100644
index 000000000000..20d368c59814
--- /dev/null
+++ b/Documentation/timers/timer_stats.txt
@@ -0,0 +1,73 @@
1timer_stats - timer usage statistics
2------------------------------------
3
4timer_stats is a debugging facility to make the timer (ab)usage in a Linux
5system visible to kernel and userspace developers. If enabled in the config
6but not used it has almost zero runtime overhead, and a relatively small
7data structure overhead. Even if collection is enabled runtime all the
8locking is per-CPU and lookup is hashed.
9
10timer_stats should be used by kernel and userspace developers to verify that
11their code does not make unduly use of timers. This helps to avoid unnecessary
12wakeups, which should be avoided to optimize power consumption.
13
14It can be enabled by CONFIG_TIMER_STATS in the "Kernel hacking" configuration
15section.
16
17timer_stats collects information about the timer events which are fired in a
18Linux system over a sample period:
19
20- the pid of the task(process) which initialized the timer
21- the name of the process which initialized the timer
22- the function where the timer was intialized
23- the callback function which is associated to the timer
24- the number of events (callbacks)
25
26timer_stats adds an entry to /proc: /proc/timer_stats
27
28This entry is used to control the statistics functionality and to read out the
29sampled information.
30
31The timer_stats functionality is inactive on bootup.
32
33To activate a sample period issue:
34# echo 1 >/proc/timer_stats
35
36To stop a sample period issue:
37# echo 0 >/proc/timer_stats
38
39The statistics can be retrieved by:
40# cat /proc/timer_stats
41
42The readout of /proc/timer_stats automatically disables sampling. The sampled
43information is kept until a new sample period is started. This allows multiple
44readouts.
45
46Sample output of /proc/timer_stats:
47
48Timerstats sample period: 3.888770 s
49 12, 0 swapper hrtimer_stop_sched_tick (hrtimer_sched_tick)
50 15, 1 swapper hcd_submit_urb (rh_timer_func)
51 4, 959 kedac schedule_timeout (process_timeout)
52 1, 0 swapper page_writeback_init (wb_timer_fn)
53 28, 0 swapper hrtimer_stop_sched_tick (hrtimer_sched_tick)
54 22, 2948 IRQ 4 tty_flip_buffer_push (delayed_work_timer_fn)
55 3, 3100 bash schedule_timeout (process_timeout)
56 1, 1 swapper queue_delayed_work_on (delayed_work_timer_fn)
57 1, 1 swapper queue_delayed_work_on (delayed_work_timer_fn)
58 1, 1 swapper neigh_table_init_no_netlink (neigh_periodic_timer)
59 1, 2292 ip __netdev_watchdog_up (dev_watchdog)
60 1, 23 events/1 do_cache_clean (delayed_work_timer_fn)
6190 total events, 30.0 events/sec
62
63The first column is the number of events, the second column the pid, the third
64column is the name of the process. The forth column shows the function which
65initialized the timer and in parantheses the callback function which was
66executed on expiry.
67
68 Thomas, Ingo
69
70Added flag to indicate 'deferrable timer' in /proc/timer_stats. A deferrable
71timer will appear as follows
72 10D, 1 swapper queue_delayed_work_on (delayed_work_timer_fn)
73