hrtimer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * include/linux/hrtimer.h
  3. *
  4. * hrtimers - High-resolution kernel timers
  5. *
  6. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
  8. *
  9. * data type definitions, declarations, prototypes
  10. *
  11. * Started by: Thomas Gleixner and Ingo Molnar
  12. *
  13. * For licencing details see kernel-base/COPYING
  14. */
  15. #ifndef _LINUX_HRTIMER_H
  16. #define _LINUX_HRTIMER_H
  17. #include <linux/rbtree.h>
  18. #include <linux/ktime.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/wait.h>
  22. #include <linux/percpu.h>
  23. #include <linux/timer.h>
  24. #include <linux/timerqueue.h>
  25. struct hrtimer_clock_base;
  26. struct hrtimer_cpu_base;
  27. /*
  28. * Mode arguments of xxx_hrtimer functions:
  29. */
  30. enum hrtimer_mode {
  31. HRTIMER_MODE_ABS = 0x0, /* Time value is absolute */
  32. HRTIMER_MODE_REL = 0x1, /* Time value is relative to now */
  33. HRTIMER_MODE_PINNED = 0x02, /* Timer is bound to CPU */
  34. HRTIMER_MODE_ABS_PINNED = 0x02,
  35. HRTIMER_MODE_REL_PINNED = 0x03,
  36. };
  37. /*
  38. * Return values for the callback function
  39. */
  40. enum hrtimer_restart {
  41. HRTIMER_NORESTART, /* Timer is not restarted */
  42. HRTIMER_RESTART, /* Timer must be restarted */
  43. };
  44. /*
  45. * Values to track state of the timer
  46. *
  47. * Possible states:
  48. *
  49. * 0x00 inactive
  50. * 0x01 enqueued into rbtree
  51. * 0x02 callback function running
  52. *
  53. * Special cases:
  54. * 0x03 callback function running and enqueued
  55. * (was requeued on another CPU)
  56. * 0x09 timer was migrated on CPU hotunplug
  57. * The "callback function running and enqueued" status is only possible on
  58. * SMP. It happens for example when a posix timer expired and the callback
  59. * queued a signal. Between dropping the lock which protects the posix timer
  60. * and reacquiring the base lock of the hrtimer, another CPU can deliver the
  61. * signal and rearm the timer. We have to preserve the callback running state,
  62. * as otherwise the timer could be removed before the softirq code finishes the
  63. * the handling of the timer.
  64. *
  65. * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
  66. * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
  67. *
  68. * All state transitions are protected by cpu_base->lock.
  69. */
  70. #define HRTIMER_STATE_INACTIVE 0x00
  71. #define HRTIMER_STATE_ENQUEUED 0x01
  72. #define HRTIMER_STATE_CALLBACK 0x02
  73. #define HRTIMER_STATE_MIGRATE 0x04
  74. /**
  75. * struct hrtimer - the basic hrtimer structure
  76. * @node: timerqueue node, which also manages node.expires,
  77. * the absolute expiry time in the hrtimers internal
  78. * representation. The time is related to the clock on
  79. * which the timer is based. Is setup by adding
  80. * slack to the _softexpires value. For non range timers
  81. * identical to _softexpires.
  82. * @_softexpires: the absolute earliest expiry time of the hrtimer.
  83. * The time which was given as expiry time when the timer
  84. * was armed.
  85. * @function: timer expiry callback function
  86. * @base: pointer to the timer base (per cpu and per clock)
  87. * @state: state information (See bit values above)
  88. * @start_site: timer statistics field to store the site where the timer
  89. * was started
  90. * @start_comm: timer statistics field to store the name of the process which
  91. * started the timer
  92. * @start_pid: timer statistics field to store the pid of the task which
  93. * started the timer
  94. *
  95. * The hrtimer structure must be initialized by hrtimer_init()
  96. */
  97. struct hrtimer {
  98. struct timerqueue_node node;
  99. ktime_t _softexpires;
  100. enum hrtimer_restart (*function)(struct hrtimer *);
  101. struct hrtimer_clock_base *base;
  102. unsigned long state;
  103. #ifdef CONFIG_TIMER_STATS
  104. int start_pid;
  105. void *start_site;
  106. char start_comm[16];
  107. #endif
  108. };
  109. /**
  110. * struct hrtimer_sleeper - simple sleeper structure
  111. * @timer: embedded timer structure
  112. * @task: task to wake up
  113. *
  114. * task is set to NULL, when the timer expires.
  115. */
  116. struct hrtimer_sleeper {
  117. struct hrtimer timer;
  118. struct task_struct *task;
  119. };
  120. /**
  121. * struct hrtimer_clock_base - the timer base for a specific clock
  122. * @cpu_base: per cpu clock base
  123. * @index: clock type index for per_cpu support when moving a
  124. * timer to a base on another cpu.
  125. * @active: red black tree root node for the active timers
  126. * @resolution: the resolution of the clock, in nanoseconds
  127. * @get_time: function to retrieve the current time of the clock
  128. * @softirq_time: the time when running the hrtimer queue in the softirq
  129. * @offset: offset of this clock to the monotonic base
  130. */
  131. struct hrtimer_clock_base {
  132. struct hrtimer_cpu_base *cpu_base;
  133. clockid_t index;
  134. struct timerqueue_head active;
  135. ktime_t resolution;
  136. ktime_t (*get_time)(void);
  137. ktime_t softirq_time;
  138. #ifdef CONFIG_HIGH_RES_TIMERS
  139. ktime_t offset;
  140. #endif
  141. };
  142. enum hrtimer_base_type {
  143. HRTIMER_BASE_REALTIME,
  144. HRTIMER_BASE_MONOTONIC,
  145. HRTIMER_MAX_CLOCK_BASES,
  146. };
  147. /*
  148. * struct hrtimer_cpu_base - the per cpu clock bases
  149. * @lock: lock protecting the base and associated clock bases
  150. * and timers
  151. * @clock_base: array of clock bases for this cpu
  152. * @expires_next: absolute time of the next event which was scheduled
  153. * via clock_set_next_event()
  154. * @hres_active: State of high resolution mode
  155. * @hang_detected: The last hrtimer interrupt detected a hang
  156. * @nr_events: Total number of hrtimer interrupt events
  157. * @nr_retries: Total number of hrtimer interrupt retries
  158. * @nr_hangs: Total number of hrtimer interrupt hangs
  159. * @max_hang_time: Maximum time spent in hrtimer_interrupt
  160. */
  161. struct hrtimer_cpu_base {
  162. raw_spinlock_t lock;
  163. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  164. #ifdef CONFIG_HIGH_RES_TIMERS
  165. ktime_t expires_next;
  166. int hres_active;
  167. int hang_detected;
  168. unsigned long nr_events;
  169. unsigned long nr_retries;
  170. unsigned long nr_hangs;
  171. ktime_t max_hang_time;
  172. #endif
  173. };
  174. static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
  175. {
  176. timer->node.expires = time;
  177. timer->_softexpires = time;
  178. }
  179. static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
  180. {
  181. timer->_softexpires = time;
  182. timer->node.expires = ktime_add_safe(time, delta);
  183. }
  184. static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
  185. {
  186. timer->_softexpires = time;
  187. timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
  188. }
  189. static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
  190. {
  191. timer->node.expires.tv64 = tv64;
  192. timer->_softexpires.tv64 = tv64;
  193. }
  194. static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
  195. {
  196. timer->node.expires = ktime_add_safe(timer->node.expires, time);
  197. timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
  198. }
  199. static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
  200. {
  201. timer->node.expires = ktime_add_ns(timer->node.expires, ns);
  202. timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
  203. }
  204. static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
  205. {
  206. return timer->node.expires;
  207. }
  208. static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
  209. {
  210. return timer->_softexpires;
  211. }
  212. static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
  213. {
  214. return timer->node.expires.tv64;
  215. }
  216. static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
  217. {
  218. return timer->_softexpires.tv64;
  219. }
  220. static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
  221. {
  222. return ktime_to_ns(timer->node.expires);
  223. }
  224. static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
  225. {
  226. return ktime_sub(timer->node.expires, timer->base->get_time());
  227. }
  228. #ifdef CONFIG_HIGH_RES_TIMERS
  229. struct clock_event_device;
  230. extern void clock_was_set(void);
  231. extern void hres_timers_resume(void);
  232. extern void hrtimer_interrupt(struct clock_event_device *dev);
  233. /*
  234. * In high resolution mode the time reference must be read accurate
  235. */
  236. static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
  237. {
  238. return timer->base->get_time();
  239. }
  240. static inline int hrtimer_is_hres_active(struct hrtimer *timer)
  241. {
  242. return timer->base->cpu_base->hres_active;
  243. }
  244. extern void hrtimer_peek_ahead_timers(void);
  245. /*
  246. * The resolution of the clocks. The resolution value is returned in
  247. * the clock_getres() system call to give application programmers an
  248. * idea of the (in)accuracy of timers. Timer values are rounded up to
  249. * this resolution values.
  250. */
  251. # define HIGH_RES_NSEC 1
  252. # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC }
  253. # define MONOTONIC_RES_NSEC HIGH_RES_NSEC
  254. # define KTIME_MONOTONIC_RES KTIME_HIGH_RES
  255. #else
  256. # define MONOTONIC_RES_NSEC LOW_RES_NSEC
  257. # define KTIME_MONOTONIC_RES KTIME_LOW_RES
  258. /*
  259. * clock_was_set() is a NOP for non- high-resolution systems. The
  260. * time-sorted order guarantees that a timer does not expire early and
  261. * is expired in the next softirq when the clock was advanced.
  262. */
  263. static inline void clock_was_set(void) { }
  264. static inline void hrtimer_peek_ahead_timers(void) { }
  265. static inline void hres_timers_resume(void) { }
  266. /*
  267. * In non high resolution mode the time reference is taken from
  268. * the base softirq time variable.
  269. */
  270. static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
  271. {
  272. return timer->base->softirq_time;
  273. }
  274. static inline int hrtimer_is_hres_active(struct hrtimer *timer)
  275. {
  276. return 0;
  277. }
  278. #endif
  279. extern ktime_t ktime_get(void);
  280. extern ktime_t ktime_get_real(void);
  281. extern ktime_t ktime_get_boottime(void);
  282. DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
  283. /* Exported timer functions: */
  284. /* Initialize timers: */
  285. extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
  286. enum hrtimer_mode mode);
  287. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  288. extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
  289. enum hrtimer_mode mode);
  290. extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
  291. #else
  292. static inline void hrtimer_init_on_stack(struct hrtimer *timer,
  293. clockid_t which_clock,
  294. enum hrtimer_mode mode)
  295. {
  296. hrtimer_init(timer, which_clock, mode);
  297. }
  298. static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
  299. #endif
  300. /* Basic timer operations: */
  301. extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
  302. const enum hrtimer_mode mode);
  303. extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  304. unsigned long range_ns, const enum hrtimer_mode mode);
  305. extern int
  306. __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  307. unsigned long delta_ns,
  308. const enum hrtimer_mode mode, int wakeup);
  309. extern int hrtimer_cancel(struct hrtimer *timer);
  310. extern int hrtimer_try_to_cancel(struct hrtimer *timer);
  311. static inline int hrtimer_start_expires(struct hrtimer *timer,
  312. enum hrtimer_mode mode)
  313. {
  314. unsigned long delta;
  315. ktime_t soft, hard;
  316. soft = hrtimer_get_softexpires(timer);
  317. hard = hrtimer_get_expires(timer);
  318. delta = ktime_to_ns(ktime_sub(hard, soft));
  319. return hrtimer_start_range_ns(timer, soft, delta, mode);
  320. }
  321. static inline int hrtimer_restart(struct hrtimer *timer)
  322. {
  323. return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  324. }
  325. /* Query timers: */
  326. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  327. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  328. extern ktime_t hrtimer_get_next_event(void);
  329. /*
  330. * A timer is active, when it is enqueued into the rbtree or the callback
  331. * function is running.
  332. */
  333. static inline int hrtimer_active(const struct hrtimer *timer)
  334. {
  335. return timer->state != HRTIMER_STATE_INACTIVE;
  336. }
  337. /*
  338. * Helper function to check, whether the timer is on one of the queues
  339. */
  340. static inline int hrtimer_is_queued(struct hrtimer *timer)
  341. {
  342. return timer->state & HRTIMER_STATE_ENQUEUED;
  343. }
  344. /*
  345. * Helper function to check, whether the timer is running the callback
  346. * function
  347. */
  348. static inline int hrtimer_callback_running(struct hrtimer *timer)
  349. {
  350. return timer->state & HRTIMER_STATE_CALLBACK;
  351. }
  352. /* Forward a hrtimer so it expires after now: */
  353. extern u64
  354. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
  355. /* Forward a hrtimer so it expires after the hrtimer's current now */
  356. static inline u64 hrtimer_forward_now(struct hrtimer *timer,
  357. ktime_t interval)
  358. {
  359. return hrtimer_forward(timer, timer->base->get_time(), interval);
  360. }
  361. /* Precise sleep: */
  362. extern long hrtimer_nanosleep(struct timespec *rqtp,
  363. struct timespec __user *rmtp,
  364. const enum hrtimer_mode mode,
  365. const clockid_t clockid);
  366. extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
  367. extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  368. struct task_struct *tsk);
  369. extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  370. const enum hrtimer_mode mode);
  371. extern int schedule_hrtimeout_range_clock(ktime_t *expires,
  372. unsigned long delta, const enum hrtimer_mode mode, int clock);
  373. extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
  374. /* Soft interrupt function to run the hrtimer queues: */
  375. extern void hrtimer_run_queues(void);
  376. extern void hrtimer_run_pending(void);
  377. /* Bootup initialization: */
  378. extern void __init hrtimers_init(void);
  379. #if BITS_PER_LONG < 64
  380. extern u64 ktime_divns(const ktime_t kt, s64 div);
  381. #else /* BITS_PER_LONG < 64 */
  382. # define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
  383. #endif
  384. /* Show pending timers: */
  385. extern void sysrq_timer_list_show(void);
  386. #endif