2 * include/linux/hrtimer.h
4 * hrtimers - High-resolution kernel timers
6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
9 * data type definitions, declarations, prototypes
11 * Started by: Thomas Gleixner and Ingo Molnar
13 * For licencing details see kernel-base/COPYING
15 #ifndef _LINUX_HRTIMER_H
16 #define _LINUX_HRTIMER_H
18 #include <linux/rbtree.h>
19 #include <linux/ktime.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/wait.h>
23 #include <linux/percpu.h>
26 struct hrtimer_clock_base
;
27 struct hrtimer_cpu_base
;
30 * Mode arguments of xxx_hrtimer functions:
33 HRTIMER_MODE_ABS
, /* Time value is absolute */
34 HRTIMER_MODE_REL
, /* Time value is relative to now */
38 * Return values for the callback function
40 enum hrtimer_restart
{
41 HRTIMER_NORESTART
, /* Timer is not restarted */
42 HRTIMER_RESTART
, /* Timer must be restarted */
46 * Values to track state of the timer
51 * 0x01 enqueued into rbtree
52 * 0x02 callback function running
55 * 0x03 callback function running and enqueued
56 * (was requeued on another CPU)
57 * 0x09 timer was migrated on CPU hotunplug
58 * The "callback function running and enqueued" status is only possible on
59 * SMP. It happens for example when a posix timer expired and the callback
60 * queued a signal. Between dropping the lock which protects the posix timer
61 * and reacquiring the base lock of the hrtimer, another CPU can deliver the
62 * signal and rearm the timer. We have to preserve the callback running state,
63 * as otherwise the timer could be removed before the softirq code finishes the
64 * the handling of the timer.
66 * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
67 * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
69 * All state transitions are protected by cpu_base->lock.
71 #define HRTIMER_STATE_INACTIVE 0x00
72 #define HRTIMER_STATE_ENQUEUED 0x01
73 #define HRTIMER_STATE_CALLBACK 0x02
74 #define HRTIMER_STATE_MIGRATE 0x04
77 * struct hrtimer - the basic hrtimer structure
78 * @node: red black tree node for time ordered insertion
79 * @_expires: the absolute expiry time in the hrtimers internal
80 * representation. The time is related to the clock on
81 * which the timer is based. Is setup by adding
82 * slack to the _softexpires value. For non range timers
83 * identical to _softexpires.
84 * @_softexpires: the absolute earliest expiry time of the hrtimer.
85 * The time which was given as expiry time when the timer
87 * @function: timer expiry callback function
88 * @base: pointer to the timer base (per cpu and per clock)
89 * @state: state information (See bit values above)
90 * @cb_entry: list head to enqueue an expired timer into the callback list
91 * @start_site: timer statistics field to store the site where the timer
93 * @start_comm: timer statistics field to store the name of the process which
95 * @start_pid: timer statistics field to store the pid of the task which
98 * The hrtimer structure must be initialized by hrtimer_init()
103 ktime_t _softexpires
;
104 enum hrtimer_restart (*function
)(struct hrtimer
*);
105 struct hrtimer_clock_base
*base
;
107 struct list_head cb_entry
;
108 #ifdef CONFIG_TIMER_STATS
116 * struct hrtimer_sleeper - simple sleeper structure
117 * @timer: embedded timer structure
118 * @task: task to wake up
120 * task is set to NULL, when the timer expires.
122 struct hrtimer_sleeper
{
123 struct hrtimer timer
;
124 struct task_struct
*task
;
128 * struct hrtimer_clock_base - the timer base for a specific clock
129 * @cpu_base: per cpu clock base
130 * @index: clock type index for per_cpu support when moving a
131 * timer to a base on another cpu.
132 * @active: red black tree root node for the active timers
133 * @first: pointer to the timer node which expires first
134 * @resolution: the resolution of the clock, in nanoseconds
135 * @get_time: function to retrieve the current time of the clock
136 * @softirq_time: the time when running the hrtimer queue in the softirq
137 * @offset: offset of this clock to the monotonic base
139 struct hrtimer_clock_base
{
140 struct hrtimer_cpu_base
*cpu_base
;
142 struct rb_root active
;
143 struct rb_node
*first
;
145 ktime_t (*get_time
)(void);
146 ktime_t softirq_time
;
147 #ifdef CONFIG_HIGH_RES_TIMERS
152 #define HRTIMER_MAX_CLOCK_BASES 2
155 * struct hrtimer_cpu_base - the per cpu clock bases
156 * @lock: lock protecting the base and associated clock bases
158 * @clock_base: array of clock bases for this cpu
159 * @curr_timer: the timer which is executing a callback right now
160 * @expires_next: absolute time of the next event which was scheduled
161 * via clock_set_next_event()
162 * @hres_active: State of high resolution mode
163 * @check_clocks: Indictator, when set evaluate time source and clock
164 * event devices whether high resolution mode can be
166 * @nr_events: Total number of timer interrupt events
168 struct hrtimer_cpu_base
{
170 struct hrtimer_clock_base clock_base
[HRTIMER_MAX_CLOCK_BASES
];
171 #ifdef CONFIG_HIGH_RES_TIMERS
172 ktime_t expires_next
;
174 unsigned long nr_events
;
178 static inline void hrtimer_set_expires(struct hrtimer
*timer
, ktime_t time
)
180 timer
->_expires
= time
;
181 timer
->_softexpires
= time
;
184 static inline void hrtimer_set_expires_range(struct hrtimer
*timer
, ktime_t time
, ktime_t delta
)
186 timer
->_softexpires
= time
;
187 timer
->_expires
= ktime_add_safe(time
, delta
);
190 static inline void hrtimer_set_expires_range_ns(struct hrtimer
*timer
, ktime_t time
, unsigned long delta
)
192 timer
->_softexpires
= time
;
193 timer
->_expires
= ktime_add_safe(time
, ns_to_ktime(delta
));
196 static inline void hrtimer_set_expires_tv64(struct hrtimer
*timer
, s64 tv64
)
198 timer
->_expires
.tv64
= tv64
;
199 timer
->_softexpires
.tv64
= tv64
;
202 static inline void hrtimer_add_expires(struct hrtimer
*timer
, ktime_t time
)
204 timer
->_expires
= ktime_add_safe(timer
->_expires
, time
);
205 timer
->_softexpires
= ktime_add_safe(timer
->_softexpires
, time
);
208 static inline void hrtimer_add_expires_ns(struct hrtimer
*timer
, u64 ns
)
210 timer
->_expires
= ktime_add_ns(timer
->_expires
, ns
);
211 timer
->_softexpires
= ktime_add_ns(timer
->_softexpires
, ns
);
214 static inline ktime_t
hrtimer_get_expires(const struct hrtimer
*timer
)
216 return timer
->_expires
;
219 static inline ktime_t
hrtimer_get_softexpires(const struct hrtimer
*timer
)
221 return timer
->_softexpires
;
224 static inline s64
hrtimer_get_expires_tv64(const struct hrtimer
*timer
)
226 return timer
->_expires
.tv64
;
228 static inline s64
hrtimer_get_softexpires_tv64(const struct hrtimer
*timer
)
230 return timer
->_softexpires
.tv64
;
233 static inline s64
hrtimer_get_expires_ns(const struct hrtimer
*timer
)
235 return ktime_to_ns(timer
->_expires
);
238 static inline ktime_t
hrtimer_expires_remaining(const struct hrtimer
*timer
)
240 return ktime_sub(timer
->_expires
, timer
->base
->get_time());
243 #ifdef CONFIG_HIGH_RES_TIMERS
244 struct clock_event_device
;
246 extern void clock_was_set(void);
247 extern void hres_timers_resume(void);
248 extern void hrtimer_interrupt(struct clock_event_device
*dev
);
251 * In high resolution mode the time reference must be read accurate
253 static inline ktime_t
hrtimer_cb_get_time(struct hrtimer
*timer
)
255 return timer
->base
->get_time();
258 static inline int hrtimer_is_hres_active(struct hrtimer
*timer
)
260 return timer
->base
->cpu_base
->hres_active
;
263 extern void hrtimer_peek_ahead_timers(void);
266 * The resolution of the clocks. The resolution value is returned in
267 * the clock_getres() system call to give application programmers an
268 * idea of the (in)accuracy of timers. Timer values are rounded up to
269 * this resolution values.
271 # define HIGH_RES_NSEC 1
272 # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC }
273 # define MONOTONIC_RES_NSEC HIGH_RES_NSEC
274 # define KTIME_MONOTONIC_RES KTIME_HIGH_RES
278 # define MONOTONIC_RES_NSEC LOW_RES_NSEC
279 # define KTIME_MONOTONIC_RES KTIME_LOW_RES
282 * clock_was_set() is a NOP for non- high-resolution systems. The
283 * time-sorted order guarantees that a timer does not expire early and
284 * is expired in the next softirq when the clock was advanced.
286 static inline void clock_was_set(void) { }
287 static inline void hrtimer_peek_ahead_timers(void) { }
289 static inline void hres_timers_resume(void) { }
292 * In non high resolution mode the time reference is taken from
293 * the base softirq time variable.
295 static inline ktime_t
hrtimer_cb_get_time(struct hrtimer
*timer
)
297 return timer
->base
->softirq_time
;
300 static inline int hrtimer_is_hres_active(struct hrtimer
*timer
)
306 extern ktime_t
ktime_get(void);
307 extern ktime_t
ktime_get_real(void);
310 DECLARE_PER_CPU(struct tick_device
, tick_cpu_device
);
313 /* Exported timer functions: */
315 /* Initialize timers: */
316 extern void hrtimer_init(struct hrtimer
*timer
, clockid_t which_clock
,
317 enum hrtimer_mode mode
);
319 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
320 extern void hrtimer_init_on_stack(struct hrtimer
*timer
, clockid_t which_clock
,
321 enum hrtimer_mode mode
);
323 extern void destroy_hrtimer_on_stack(struct hrtimer
*timer
);
325 static inline void hrtimer_init_on_stack(struct hrtimer
*timer
,
326 clockid_t which_clock
,
327 enum hrtimer_mode mode
)
329 hrtimer_init(timer
, which_clock
, mode
);
331 static inline void destroy_hrtimer_on_stack(struct hrtimer
*timer
) { }
334 /* Basic timer operations: */
335 extern int hrtimer_start(struct hrtimer
*timer
, ktime_t tim
,
336 const enum hrtimer_mode mode
);
337 extern int hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
338 unsigned long range_ns
, const enum hrtimer_mode mode
);
340 __hrtimer_start_range_ns(struct hrtimer
*timer
, ktime_t tim
,
341 unsigned long delta_ns
,
342 const enum hrtimer_mode mode
, int wakeup
);
344 extern int hrtimer_cancel(struct hrtimer
*timer
);
345 extern int hrtimer_try_to_cancel(struct hrtimer
*timer
);
347 static inline int hrtimer_start_expires(struct hrtimer
*timer
,
348 enum hrtimer_mode mode
)
352 soft
= hrtimer_get_softexpires(timer
);
353 hard
= hrtimer_get_expires(timer
);
354 delta
= ktime_to_ns(ktime_sub(hard
, soft
));
355 return hrtimer_start_range_ns(timer
, soft
, delta
, mode
);
358 static inline int hrtimer_restart(struct hrtimer
*timer
)
360 return hrtimer_start_expires(timer
, HRTIMER_MODE_ABS
);
364 extern ktime_t
hrtimer_get_remaining(const struct hrtimer
*timer
);
365 extern int hrtimer_get_res(const clockid_t which_clock
, struct timespec
*tp
);
367 extern ktime_t
hrtimer_get_next_event(void);
370 * A timer is active, when it is enqueued into the rbtree or the callback
371 * function is running.
373 static inline int hrtimer_active(const struct hrtimer
*timer
)
375 return timer
->state
!= HRTIMER_STATE_INACTIVE
;
379 * Helper function to check, whether the timer is on one of the queues
381 static inline int hrtimer_is_queued(struct hrtimer
*timer
)
383 return timer
->state
& HRTIMER_STATE_ENQUEUED
;
387 * Helper function to check, whether the timer is running the callback
390 static inline int hrtimer_callback_running(struct hrtimer
*timer
)
392 return timer
->state
& HRTIMER_STATE_CALLBACK
;
395 /* Forward a hrtimer so it expires after now: */
397 hrtimer_forward(struct hrtimer
*timer
, ktime_t now
, ktime_t interval
);
399 /* Forward a hrtimer so it expires after the hrtimer's current now */
400 static inline u64
hrtimer_forward_now(struct hrtimer
*timer
,
403 return hrtimer_forward(timer
, timer
->base
->get_time(), interval
);
407 extern long hrtimer_nanosleep(struct timespec
*rqtp
,
408 struct timespec __user
*rmtp
,
409 const enum hrtimer_mode mode
,
410 const clockid_t clockid
);
411 extern long hrtimer_nanosleep_restart(struct restart_block
*restart_block
);
413 extern void hrtimer_init_sleeper(struct hrtimer_sleeper
*sl
,
414 struct task_struct
*tsk
);
416 extern int schedule_hrtimeout_range(ktime_t
*expires
, unsigned long delta
,
417 const enum hrtimer_mode mode
);
418 extern int schedule_hrtimeout(ktime_t
*expires
, const enum hrtimer_mode mode
);
420 /* Soft interrupt function to run the hrtimer queues: */
421 extern void hrtimer_run_queues(void);
422 extern void hrtimer_run_pending(void);
424 /* Bootup initialization: */
425 extern void __init
hrtimers_init(void);
427 #if BITS_PER_LONG < 64
428 extern u64
ktime_divns(const ktime_t kt
, s64 div
);
429 #else /* BITS_PER_LONG < 64 */
430 # define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
433 /* Show pending timers: */
434 extern void sysrq_timer_list_show(void);
437 * Timer-statistics info:
439 #ifdef CONFIG_TIMER_STATS
441 extern void timer_stats_update_stats(void *timer
, pid_t pid
, void *startf
,
442 void *timerf
, char *comm
,
443 unsigned int timer_flag
);
445 static inline void timer_stats_account_hrtimer(struct hrtimer
*timer
)
447 timer_stats_update_stats(timer
, timer
->start_pid
, timer
->start_site
,
448 timer
->function
, timer
->start_comm
, 0);
451 extern void __timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
,
454 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
)
456 __timer_stats_hrtimer_set_start_info(timer
, __builtin_return_address(0));
459 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer
*timer
)
461 timer
->start_site
= NULL
;
464 static inline void timer_stats_account_hrtimer(struct hrtimer
*timer
)
468 static inline void timer_stats_hrtimer_set_start_info(struct hrtimer
*timer
)
472 static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer
*timer
)