hrtimer.h 14 KB

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