sched_clock.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * sched_clock for unstable cpu clocks
  3. *
  4. * Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  5. *
  6. * Updates and enhancements:
  7. * Copyright (C) 2008 Red Hat, Inc. Steven Rostedt <srostedt@redhat.com>
  8. *
  9. * Based on code by:
  10. * Ingo Molnar <mingo@redhat.com>
  11. * Guillaume Chazarain <guichaz@gmail.com>
  12. *
  13. * Create a semi stable clock from a mixture of other events, including:
  14. * - gtod
  15. * - sched_clock()
  16. * - explicit idle events
  17. *
  18. * We use gtod as base and the unstable clock deltas. The deltas are filtered,
  19. * making it monotonic and keeping it within an expected window.
  20. *
  21. * Furthermore, explicit sleep and wakeup hooks allow us to account for time
  22. * that is otherwise invisible (TSC gets stopped).
  23. *
  24. * The clock: sched_clock_cpu() is monotonic per cpu, and should be somewhat
  25. * consistent between cpus (never more than 2 jiffies difference).
  26. */
  27. #include <linux/spinlock.h>
  28. #include <linux/hardirq.h>
  29. #include <linux/module.h>
  30. #include <linux/percpu.h>
  31. #include <linux/ktime.h>
  32. #include <linux/sched.h>
  33. /*
  34. * Scheduler clock - returns current time in nanosec units.
  35. * This is default implementation.
  36. * Architectures and sub-architectures can override this.
  37. */
  38. unsigned long long __attribute__((weak)) sched_clock(void)
  39. {
  40. return (unsigned long long)(jiffies - INITIAL_JIFFIES)
  41. * (NSEC_PER_SEC / HZ);
  42. }
  43. static __read_mostly int sched_clock_running;
  44. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  45. __read_mostly int sched_clock_stable;
  46. struct sched_clock_data {
  47. /*
  48. * Raw spinlock - this is a special case: this might be called
  49. * from within instrumentation code so we dont want to do any
  50. * instrumentation ourselves.
  51. */
  52. raw_spinlock_t lock;
  53. u64 tick_raw;
  54. u64 tick_gtod;
  55. u64 clock;
  56. };
  57. static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
  58. static inline struct sched_clock_data *this_scd(void)
  59. {
  60. return &__get_cpu_var(sched_clock_data);
  61. }
  62. static inline struct sched_clock_data *cpu_sdc(int cpu)
  63. {
  64. return &per_cpu(sched_clock_data, cpu);
  65. }
  66. void sched_clock_init(void)
  67. {
  68. u64 ktime_now = ktime_to_ns(ktime_get());
  69. int cpu;
  70. for_each_possible_cpu(cpu) {
  71. struct sched_clock_data *scd = cpu_sdc(cpu);
  72. scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  73. scd->tick_raw = 0;
  74. scd->tick_gtod = ktime_now;
  75. scd->clock = ktime_now;
  76. }
  77. sched_clock_running = 1;
  78. }
  79. /*
  80. * min, max except they take wrapping into account
  81. */
  82. static inline u64 wrap_min(u64 x, u64 y)
  83. {
  84. return (s64)(x - y) < 0 ? x : y;
  85. }
  86. static inline u64 wrap_max(u64 x, u64 y)
  87. {
  88. return (s64)(x - y) > 0 ? x : y;
  89. }
  90. /*
  91. * update the percpu scd from the raw @now value
  92. *
  93. * - filter out backward motion
  94. * - use the GTOD tick value to create a window to filter crazy TSC values
  95. */
  96. static u64 __update_sched_clock(struct sched_clock_data *scd, u64 now)
  97. {
  98. s64 delta = now - scd->tick_raw;
  99. u64 clock, min_clock, max_clock;
  100. if (unlikely(delta < 0))
  101. delta = 0;
  102. /*
  103. * scd->clock = clamp(scd->tick_gtod + delta,
  104. * max(scd->tick_gtod, scd->clock),
  105. * scd->tick_gtod + TICK_NSEC);
  106. */
  107. clock = scd->tick_gtod + delta;
  108. min_clock = wrap_max(scd->tick_gtod, scd->clock);
  109. max_clock = wrap_max(scd->clock, scd->tick_gtod + TICK_NSEC);
  110. clock = wrap_max(clock, min_clock);
  111. clock = wrap_min(clock, max_clock);
  112. scd->clock = clock;
  113. return scd->clock;
  114. }
  115. static void lock_double_clock(struct sched_clock_data *data1,
  116. struct sched_clock_data *data2)
  117. {
  118. if (data1 < data2) {
  119. __raw_spin_lock(&data1->lock);
  120. __raw_spin_lock(&data2->lock);
  121. } else {
  122. __raw_spin_lock(&data2->lock);
  123. __raw_spin_lock(&data1->lock);
  124. }
  125. }
  126. u64 sched_clock_cpu(int cpu)
  127. {
  128. u64 now, clock, this_clock, remote_clock;
  129. struct sched_clock_data *scd;
  130. if (sched_clock_stable)
  131. return sched_clock();
  132. scd = cpu_sdc(cpu);
  133. /*
  134. * Normally this is not called in NMI context - but if it is,
  135. * trying to do any locking here is totally lethal.
  136. */
  137. if (unlikely(in_nmi()))
  138. return scd->clock;
  139. if (unlikely(!sched_clock_running))
  140. return 0ull;
  141. WARN_ON_ONCE(!irqs_disabled());
  142. now = sched_clock();
  143. if (cpu != raw_smp_processor_id()) {
  144. struct sched_clock_data *my_scd = this_scd();
  145. lock_double_clock(scd, my_scd);
  146. this_clock = __update_sched_clock(my_scd, now);
  147. remote_clock = scd->clock;
  148. /*
  149. * Use the opportunity that we have both locks
  150. * taken to couple the two clocks: we take the
  151. * larger time as the latest time for both
  152. * runqueues. (this creates monotonic movement)
  153. */
  154. if (likely((s64)(remote_clock - this_clock) < 0)) {
  155. clock = this_clock;
  156. scd->clock = clock;
  157. } else {
  158. /*
  159. * Should be rare, but possible:
  160. */
  161. clock = remote_clock;
  162. my_scd->clock = remote_clock;
  163. }
  164. __raw_spin_unlock(&my_scd->lock);
  165. } else {
  166. __raw_spin_lock(&scd->lock);
  167. clock = __update_sched_clock(scd, now);
  168. }
  169. __raw_spin_unlock(&scd->lock);
  170. return clock;
  171. }
  172. void sched_clock_tick(void)
  173. {
  174. struct sched_clock_data *scd;
  175. u64 now, now_gtod;
  176. if (sched_clock_stable)
  177. return;
  178. if (unlikely(!sched_clock_running))
  179. return;
  180. WARN_ON_ONCE(!irqs_disabled());
  181. scd = this_scd();
  182. now_gtod = ktime_to_ns(ktime_get());
  183. now = sched_clock();
  184. __raw_spin_lock(&scd->lock);
  185. scd->tick_raw = now;
  186. scd->tick_gtod = now_gtod;
  187. __update_sched_clock(scd, now);
  188. __raw_spin_unlock(&scd->lock);
  189. }
  190. /*
  191. * We are going deep-idle (irqs are disabled):
  192. */
  193. void sched_clock_idle_sleep_event(void)
  194. {
  195. sched_clock_cpu(smp_processor_id());
  196. }
  197. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  198. /*
  199. * We just idled delta nanoseconds (called with irqs disabled):
  200. */
  201. void sched_clock_idle_wakeup_event(u64 delta_ns)
  202. {
  203. if (timekeeping_suspended)
  204. return;
  205. sched_clock_tick();
  206. touch_softlockup_watchdog();
  207. }
  208. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  209. #else /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
  210. void sched_clock_init(void)
  211. {
  212. sched_clock_running = 1;
  213. }
  214. u64 sched_clock_cpu(int cpu)
  215. {
  216. if (unlikely(!sched_clock_running))
  217. return 0;
  218. return sched_clock();
  219. }
  220. #endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
  221. unsigned long long cpu_clock(int cpu)
  222. {
  223. unsigned long long clock;
  224. unsigned long flags;
  225. local_irq_save(flags);
  226. clock = sched_clock_cpu(cpu);
  227. local_irq_restore(flags);
  228. return clock;
  229. }
  230. EXPORT_SYMBOL_GPL(cpu_clock);