hrtimer.h 14 KB

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