tick.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* linux/include/linux/tick.h
  2. *
  3. * This file contains the structure definitions for tick related functions
  4. *
  5. */
  6. #ifndef _LINUX_TICK_H
  7. #define _LINUX_TICK_H
  8. #include <linux/clockchips.h>
  9. #include <linux/irqflags.h>
  10. #include <linux/percpu.h>
  11. #include <linux/hrtimer.h>
  12. #include <linux/context_tracking_state.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/sched.h>
  15. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  16. enum tick_device_mode {
  17. TICKDEV_MODE_PERIODIC,
  18. TICKDEV_MODE_ONESHOT,
  19. };
  20. struct tick_device {
  21. struct clock_event_device *evtdev;
  22. enum tick_device_mode mode;
  23. };
  24. enum tick_nohz_mode {
  25. NOHZ_MODE_INACTIVE,
  26. NOHZ_MODE_LOWRES,
  27. NOHZ_MODE_HIGHRES,
  28. };
  29. /**
  30. * struct tick_sched - sched tick emulation and no idle tick control/stats
  31. * @sched_timer: hrtimer to schedule the periodic tick in high
  32. * resolution mode
  33. * @last_tick: Store the last tick expiry time when the tick
  34. * timer is modified for nohz sleeps. This is necessary
  35. * to resume the tick timer operation in the timeline
  36. * when the CPU returns from nohz sleep.
  37. * @tick_stopped: Indicator that the idle tick has been stopped
  38. * @idle_jiffies: jiffies at the entry to idle for idle time accounting
  39. * @idle_calls: Total number of idle calls
  40. * @idle_sleeps: Number of idle calls, where the sched tick was stopped
  41. * @idle_entrytime: Time when the idle call was entered
  42. * @idle_waketime: Time when the idle was interrupted
  43. * @idle_exittime: Time when the idle state was left
  44. * @idle_sleeptime: Sum of the time slept in idle with sched tick stopped
  45. * @iowait_sleeptime: Sum of the time slept in idle with sched tick stopped, with IO outstanding
  46. * @sleep_length: Duration of the current idle sleep
  47. * @do_timer_lst: CPU was the last one doing do_timer before going idle
  48. */
  49. struct tick_sched {
  50. struct hrtimer sched_timer;
  51. unsigned long check_clocks;
  52. enum tick_nohz_mode nohz_mode;
  53. ktime_t last_tick;
  54. int inidle;
  55. int tick_stopped;
  56. unsigned long idle_jiffies;
  57. unsigned long idle_calls;
  58. unsigned long idle_sleeps;
  59. int idle_active;
  60. ktime_t idle_entrytime;
  61. ktime_t idle_waketime;
  62. ktime_t idle_exittime;
  63. ktime_t idle_sleeptime;
  64. ktime_t iowait_sleeptime;
  65. ktime_t sleep_length;
  66. unsigned long last_jiffies;
  67. unsigned long next_jiffies;
  68. ktime_t idle_expires;
  69. int do_timer_last;
  70. };
  71. extern void __init tick_init(void);
  72. extern int tick_is_oneshot_available(void);
  73. extern struct tick_device *tick_get_device(int cpu);
  74. # ifdef CONFIG_HIGH_RES_TIMERS
  75. extern int tick_init_highres(void);
  76. extern int tick_program_event(ktime_t expires, int force);
  77. extern void tick_setup_sched_timer(void);
  78. # endif
  79. # if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
  80. extern void tick_cancel_sched_timer(int cpu);
  81. # else
  82. static inline void tick_cancel_sched_timer(int cpu) { }
  83. # endif
  84. # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  85. extern struct tick_device *tick_get_broadcast_device(void);
  86. extern struct cpumask *tick_get_broadcast_mask(void);
  87. # ifdef CONFIG_TICK_ONESHOT
  88. extern struct cpumask *tick_get_broadcast_oneshot_mask(void);
  89. # endif
  90. # endif /* BROADCAST */
  91. # ifdef CONFIG_TICK_ONESHOT
  92. extern void tick_clock_notify(void);
  93. extern int tick_check_oneshot_change(int allow_nohz);
  94. extern struct tick_sched *tick_get_tick_sched(int cpu);
  95. extern void tick_irq_enter(void);
  96. extern int tick_oneshot_mode_active(void);
  97. # ifndef arch_needs_cpu
  98. # define arch_needs_cpu(cpu) (0)
  99. # endif
  100. # else
  101. static inline void tick_clock_notify(void) { }
  102. static inline int tick_check_oneshot_change(int allow_nohz) { return 0; }
  103. static inline void tick_irq_enter(void) { }
  104. static inline int tick_oneshot_mode_active(void) { return 0; }
  105. # endif
  106. #else /* CONFIG_GENERIC_CLOCKEVENTS */
  107. static inline void tick_init(void) { }
  108. static inline void tick_cancel_sched_timer(int cpu) { }
  109. static inline void tick_clock_notify(void) { }
  110. static inline int tick_check_oneshot_change(int allow_nohz) { return 0; }
  111. static inline void tick_irq_enter(void) { }
  112. static inline int tick_oneshot_mode_active(void) { return 0; }
  113. #endif /* !CONFIG_GENERIC_CLOCKEVENTS */
  114. # ifdef CONFIG_NO_HZ_COMMON
  115. DECLARE_PER_CPU(struct tick_sched, tick_cpu_sched);
  116. static inline int tick_nohz_tick_stopped(void)
  117. {
  118. return __this_cpu_read(tick_cpu_sched.tick_stopped);
  119. }
  120. extern void tick_nohz_idle_enter(void);
  121. extern void tick_nohz_idle_exit(void);
  122. extern void tick_nohz_irq_exit(void);
  123. extern ktime_t tick_nohz_get_sleep_length(void);
  124. extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
  125. extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
  126. # else /* !CONFIG_NO_HZ_COMMON */
  127. static inline int tick_nohz_tick_stopped(void)
  128. {
  129. return 0;
  130. }
  131. static inline void tick_nohz_idle_enter(void) { }
  132. static inline void tick_nohz_idle_exit(void) { }
  133. static inline ktime_t tick_nohz_get_sleep_length(void)
  134. {
  135. ktime_t len = { .tv64 = NSEC_PER_SEC/HZ };
  136. return len;
  137. }
  138. static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
  139. static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
  140. # endif /* !CONFIG_NO_HZ_COMMON */
  141. #ifdef CONFIG_NO_HZ_FULL
  142. extern bool tick_nohz_full_running;
  143. extern cpumask_var_t tick_nohz_full_mask;
  144. extern cpumask_var_t housekeeping_mask;
  145. static inline bool tick_nohz_full_enabled(void)
  146. {
  147. if (!context_tracking_is_enabled())
  148. return false;
  149. return tick_nohz_full_running;
  150. }
  151. static inline bool tick_nohz_full_cpu(int cpu)
  152. {
  153. if (!tick_nohz_full_enabled())
  154. return false;
  155. return cpumask_test_cpu(cpu, tick_nohz_full_mask);
  156. }
  157. extern void tick_nohz_init(void);
  158. extern void __tick_nohz_full_check(void);
  159. extern void tick_nohz_full_kick(void);
  160. extern void tick_nohz_full_kick_cpu(int cpu);
  161. extern void tick_nohz_full_kick_all(void);
  162. extern void __tick_nohz_task_switch(struct task_struct *tsk);
  163. #else
  164. static inline void tick_nohz_init(void) { }
  165. static inline bool tick_nohz_full_enabled(void) { return false; }
  166. static inline bool tick_nohz_full_cpu(int cpu) { return false; }
  167. static inline void __tick_nohz_full_check(void) { }
  168. static inline void tick_nohz_full_kick_cpu(int cpu) { }
  169. static inline void tick_nohz_full_kick(void) { }
  170. static inline void tick_nohz_full_kick_all(void) { }
  171. static inline void __tick_nohz_task_switch(struct task_struct *tsk) { }
  172. #endif
  173. static inline bool is_housekeeping_cpu(int cpu)
  174. {
  175. #ifdef CONFIG_NO_HZ_FULL
  176. if (tick_nohz_full_enabled())
  177. return cpumask_test_cpu(cpu, housekeeping_mask);
  178. #endif
  179. return true;
  180. }
  181. static inline void housekeeping_affine(struct task_struct *t)
  182. {
  183. #ifdef CONFIG_NO_HZ_FULL
  184. if (tick_nohz_full_enabled())
  185. set_cpus_allowed_ptr(t, housekeeping_mask);
  186. #endif
  187. }
  188. static inline void tick_nohz_full_check(void)
  189. {
  190. if (tick_nohz_full_enabled())
  191. __tick_nohz_full_check();
  192. }
  193. static inline void tick_nohz_task_switch(struct task_struct *tsk)
  194. {
  195. if (tick_nohz_full_enabled())
  196. __tick_nohz_task_switch(tsk);
  197. }
  198. #endif