sched_clock.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * sched_clock for unstable cpu clocks
  3. *
  4. * Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  5. *
  6. * Based on code by:
  7. * Ingo Molnar <mingo@redhat.com>
  8. * Guillaume Chazarain <guichaz@gmail.com>
  9. *
  10. * Create a semi stable clock from a mixture of other events, including:
  11. * - gtod
  12. * - jiffies
  13. * - sched_clock()
  14. * - explicit idle events
  15. *
  16. * We use gtod as base and the unstable clock deltas. The deltas are filtered,
  17. * making it monotonic and keeping it within an expected window. This window
  18. * is set up using jiffies.
  19. *
  20. * Furthermore, explicit sleep and wakeup hooks allow us to account for time
  21. * that is otherwise invisible (TSC gets stopped).
  22. *
  23. * The clock: sched_clock_cpu() is monotonic per cpu, and should be somewhat
  24. * consistent between cpus (never more than 1 jiffies difference).
  25. */
  26. #include <linux/sched.h>
  27. #include <linux/percpu.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/ktime.h>
  30. #include <linux/module.h>
  31. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  32. struct sched_clock_data {
  33. /*
  34. * Raw spinlock - this is a special case: this might be called
  35. * from within instrumentation code so we dont want to do any
  36. * instrumentation ourselves.
  37. */
  38. raw_spinlock_t lock;
  39. unsigned long tick_jiffies;
  40. u64 prev_raw;
  41. u64 tick_raw;
  42. u64 tick_gtod;
  43. u64 clock;
  44. #ifdef CONFIG_NO_HZ
  45. int check_max;
  46. #endif
  47. };
  48. static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
  49. static inline struct sched_clock_data *this_scd(void)
  50. {
  51. return &__get_cpu_var(sched_clock_data);
  52. }
  53. static inline struct sched_clock_data *cpu_sdc(int cpu)
  54. {
  55. return &per_cpu(sched_clock_data, cpu);
  56. }
  57. static __read_mostly int sched_clock_running;
  58. void sched_clock_init(void)
  59. {
  60. u64 ktime_now = ktime_to_ns(ktime_get());
  61. unsigned long now_jiffies = jiffies;
  62. int cpu;
  63. for_each_possible_cpu(cpu) {
  64. struct sched_clock_data *scd = cpu_sdc(cpu);
  65. scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  66. scd->tick_jiffies = now_jiffies;
  67. scd->prev_raw = 0;
  68. scd->tick_raw = 0;
  69. scd->tick_gtod = ktime_now;
  70. scd->clock = ktime_now;
  71. #ifdef CONFIG_NO_HZ
  72. scd->check_max = 1;
  73. #endif
  74. }
  75. sched_clock_running = 1;
  76. }
  77. #ifdef CONFIG_NO_HZ
  78. /*
  79. * The dynamic ticks makes the delta jiffies inaccurate. This
  80. * prevents us from checking the maximum time update.
  81. * Disable the maximum check during stopped ticks.
  82. */
  83. void sched_clock_tick_stop(int cpu)
  84. {
  85. struct sched_clock_data *scd = cpu_sdc(cpu);
  86. scd->check_max = 0;
  87. }
  88. void sched_clock_tick_start(int cpu)
  89. {
  90. struct sched_clock_data *scd = cpu_sdc(cpu);
  91. scd->check_max = 1;
  92. }
  93. static int check_max(struct sched_clock_data *scd)
  94. {
  95. return scd->check_max;
  96. }
  97. #else
  98. static int check_max(struct sched_clock_data *scd)
  99. {
  100. return 1;
  101. }
  102. #endif /* CONFIG_NO_HZ */
  103. /*
  104. * update the percpu scd from the raw @now value
  105. *
  106. * - filter out backward motion
  107. * - use jiffies to generate a min,max window to clip the raw values
  108. */
  109. static void __update_sched_clock(struct sched_clock_data *scd, u64 now)
  110. {
  111. unsigned long now_jiffies = jiffies;
  112. long delta_jiffies = now_jiffies - scd->tick_jiffies;
  113. u64 clock = scd->clock;
  114. u64 min_clock, max_clock;
  115. s64 delta = now - scd->prev_raw;
  116. WARN_ON_ONCE(!irqs_disabled());
  117. min_clock = scd->tick_gtod +
  118. (delta_jiffies ? delta_jiffies - 1 : 0) * TICK_NSEC;
  119. if (unlikely(delta < 0)) {
  120. clock++;
  121. goto out;
  122. }
  123. /*
  124. * The clock must stay within a jiffie of the gtod.
  125. * But since we may be at the start of a jiffy or the end of one
  126. * we add another jiffy buffer.
  127. */
  128. max_clock = scd->tick_gtod + (2 + delta_jiffies) * TICK_NSEC;
  129. if (unlikely(clock + delta > max_clock) && check_max(scd)) {
  130. if (clock < max_clock)
  131. clock = max_clock;
  132. else
  133. clock++;
  134. } else {
  135. clock += delta;
  136. }
  137. out:
  138. if (unlikely(clock < min_clock))
  139. clock = min_clock;
  140. scd->prev_raw = now;
  141. scd->clock = clock;
  142. }
  143. static void lock_double_clock(struct sched_clock_data *data1,
  144. struct sched_clock_data *data2)
  145. {
  146. if (data1 < data2) {
  147. __raw_spin_lock(&data1->lock);
  148. __raw_spin_lock(&data2->lock);
  149. } else {
  150. __raw_spin_lock(&data2->lock);
  151. __raw_spin_lock(&data1->lock);
  152. }
  153. }
  154. u64 sched_clock_cpu(int cpu)
  155. {
  156. struct sched_clock_data *scd = cpu_sdc(cpu);
  157. u64 now, clock;
  158. if (unlikely(!sched_clock_running))
  159. return 0ull;
  160. WARN_ON_ONCE(!irqs_disabled());
  161. now = sched_clock();
  162. if (cpu != raw_smp_processor_id()) {
  163. /*
  164. * in order to update a remote cpu's clock based on our
  165. * unstable raw time rebase it against:
  166. * tick_raw (offset between raw counters)
  167. * tick_gotd (tick offset between cpus)
  168. */
  169. struct sched_clock_data *my_scd = this_scd();
  170. lock_double_clock(scd, my_scd);
  171. now -= my_scd->tick_raw;
  172. now += scd->tick_raw;
  173. now += my_scd->tick_gtod;
  174. now -= scd->tick_gtod;
  175. __raw_spin_unlock(&my_scd->lock);
  176. } else {
  177. __raw_spin_lock(&scd->lock);
  178. }
  179. __update_sched_clock(scd, now);
  180. clock = scd->clock;
  181. __raw_spin_unlock(&scd->lock);
  182. return clock;
  183. }
  184. void sched_clock_tick(void)
  185. {
  186. struct sched_clock_data *scd = this_scd();
  187. unsigned long now_jiffies = jiffies;
  188. u64 now, now_gtod;
  189. if (unlikely(!sched_clock_running))
  190. return;
  191. WARN_ON_ONCE(!irqs_disabled());
  192. now = sched_clock();
  193. now_gtod = ktime_to_ns(ktime_get());
  194. __raw_spin_lock(&scd->lock);
  195. __update_sched_clock(scd, now);
  196. /*
  197. * update tick_gtod after __update_sched_clock() because that will
  198. * already observe 1 new jiffy; adding a new tick_gtod to that would
  199. * increase the clock 2 jiffies.
  200. */
  201. scd->tick_jiffies = now_jiffies;
  202. scd->tick_raw = now;
  203. scd->tick_gtod = now_gtod;
  204. __raw_spin_unlock(&scd->lock);
  205. }
  206. /*
  207. * We are going deep-idle (irqs are disabled):
  208. */
  209. void sched_clock_idle_sleep_event(void)
  210. {
  211. sched_clock_cpu(smp_processor_id());
  212. }
  213. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  214. /*
  215. * We just idled delta nanoseconds (called with irqs disabled):
  216. */
  217. void sched_clock_idle_wakeup_event(u64 delta_ns)
  218. {
  219. struct sched_clock_data *scd = this_scd();
  220. u64 now = sched_clock();
  221. /*
  222. * Override the previous timestamp and ignore all
  223. * sched_clock() deltas that occured while we idled,
  224. * and use the PM-provided delta_ns to advance the
  225. * rq clock:
  226. */
  227. __raw_spin_lock(&scd->lock);
  228. scd->prev_raw = now;
  229. scd->clock += delta_ns;
  230. __raw_spin_unlock(&scd->lock);
  231. touch_softlockup_watchdog();
  232. }
  233. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  234. #endif
  235. /*
  236. * Scheduler clock - returns current time in nanosec units.
  237. * This is default implementation.
  238. * Architectures and sub-architectures can override this.
  239. */
  240. unsigned long long __attribute__((weak)) sched_clock(void)
  241. {
  242. return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
  243. }