sched_clock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. EXPORT_SYMBOL_GPL(sched_clock);
  44. static __read_mostly int sched_clock_running;
  45. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  46. __read_mostly int sched_clock_stable;
  47. struct sched_clock_data {
  48. u64 tick_raw;
  49. u64 tick_gtod;
  50. u64 clock;
  51. };
  52. static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
  53. static inline struct sched_clock_data *this_scd(void)
  54. {
  55. return &__get_cpu_var(sched_clock_data);
  56. }
  57. static inline struct sched_clock_data *cpu_sdc(int cpu)
  58. {
  59. return &per_cpu(sched_clock_data, cpu);
  60. }
  61. void sched_clock_init(void)
  62. {
  63. u64 ktime_now = ktime_to_ns(ktime_get());
  64. int cpu;
  65. for_each_possible_cpu(cpu) {
  66. struct sched_clock_data *scd = cpu_sdc(cpu);
  67. scd->tick_raw = 0;
  68. scd->tick_gtod = ktime_now;
  69. scd->clock = ktime_now;
  70. }
  71. sched_clock_running = 1;
  72. }
  73. /*
  74. * min, max except they take wrapping into account
  75. */
  76. static inline u64 wrap_min(u64 x, u64 y)
  77. {
  78. return (s64)(x - y) < 0 ? x : y;
  79. }
  80. static inline u64 wrap_max(u64 x, u64 y)
  81. {
  82. return (s64)(x - y) > 0 ? x : y;
  83. }
  84. /*
  85. * update the percpu scd from the raw @now value
  86. *
  87. * - filter out backward motion
  88. * - use the GTOD tick value to create a window to filter crazy TSC values
  89. */
  90. static u64 sched_clock_local(struct sched_clock_data *scd)
  91. {
  92. u64 now, clock, old_clock, min_clock, max_clock;
  93. s64 delta;
  94. again:
  95. now = sched_clock();
  96. delta = now - scd->tick_raw;
  97. if (unlikely(delta < 0))
  98. delta = 0;
  99. old_clock = scd->clock;
  100. /*
  101. * scd->clock = clamp(scd->tick_gtod + delta,
  102. * max(scd->tick_gtod, scd->clock),
  103. * scd->tick_gtod + TICK_NSEC);
  104. */
  105. clock = scd->tick_gtod + delta;
  106. min_clock = wrap_max(scd->tick_gtod, old_clock);
  107. max_clock = wrap_max(old_clock, scd->tick_gtod + TICK_NSEC);
  108. clock = wrap_max(clock, min_clock);
  109. clock = wrap_min(clock, max_clock);
  110. if (cmpxchg64(&scd->clock, old_clock, clock) != old_clock)
  111. goto again;
  112. return clock;
  113. }
  114. static u64 sched_clock_remote(struct sched_clock_data *scd)
  115. {
  116. struct sched_clock_data *my_scd = this_scd();
  117. u64 this_clock, remote_clock;
  118. u64 *ptr, old_val, val;
  119. sched_clock_local(my_scd);
  120. again:
  121. this_clock = my_scd->clock;
  122. remote_clock = scd->clock;
  123. /*
  124. * Use the opportunity that we have both locks
  125. * taken to couple the two clocks: we take the
  126. * larger time as the latest time for both
  127. * runqueues. (this creates monotonic movement)
  128. */
  129. if (likely((s64)(remote_clock - this_clock) < 0)) {
  130. ptr = &scd->clock;
  131. old_val = remote_clock;
  132. val = this_clock;
  133. } else {
  134. /*
  135. * Should be rare, but possible:
  136. */
  137. ptr = &my_scd->clock;
  138. old_val = this_clock;
  139. val = remote_clock;
  140. }
  141. if (cmpxchg64(ptr, old_val, val) != old_val)
  142. goto again;
  143. return val;
  144. }
  145. u64 sched_clock_cpu(int cpu)
  146. {
  147. struct sched_clock_data *scd;
  148. u64 clock;
  149. WARN_ON_ONCE(!irqs_disabled());
  150. if (sched_clock_stable)
  151. return sched_clock();
  152. if (unlikely(!sched_clock_running))
  153. return 0ull;
  154. scd = cpu_sdc(cpu);
  155. if (cpu != smp_processor_id())
  156. clock = sched_clock_remote(scd);
  157. else
  158. clock = sched_clock_local(scd);
  159. return clock;
  160. }
  161. void sched_clock_tick(void)
  162. {
  163. struct sched_clock_data *scd;
  164. u64 now, now_gtod;
  165. if (sched_clock_stable)
  166. return;
  167. if (unlikely(!sched_clock_running))
  168. return;
  169. WARN_ON_ONCE(!irqs_disabled());
  170. scd = this_scd();
  171. now_gtod = ktime_to_ns(ktime_get());
  172. now = sched_clock();
  173. scd->tick_raw = now;
  174. scd->tick_gtod = now_gtod;
  175. sched_clock_local(scd);
  176. }
  177. /*
  178. * We are going deep-idle (irqs are disabled):
  179. */
  180. void sched_clock_idle_sleep_event(void)
  181. {
  182. sched_clock_cpu(smp_processor_id());
  183. }
  184. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  185. /*
  186. * We just idled delta nanoseconds (called with irqs disabled):
  187. */
  188. void sched_clock_idle_wakeup_event(u64 delta_ns)
  189. {
  190. if (timekeeping_suspended)
  191. return;
  192. sched_clock_tick();
  193. touch_softlockup_watchdog();
  194. }
  195. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  196. unsigned long long cpu_clock(int cpu)
  197. {
  198. unsigned long long clock;
  199. unsigned long flags;
  200. local_irq_save(flags);
  201. clock = sched_clock_cpu(cpu);
  202. local_irq_restore(flags);
  203. return clock;
  204. }
  205. #else /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
  206. void sched_clock_init(void)
  207. {
  208. sched_clock_running = 1;
  209. }
  210. u64 sched_clock_cpu(int cpu)
  211. {
  212. if (unlikely(!sched_clock_running))
  213. return 0;
  214. return sched_clock();
  215. }
  216. unsigned long long cpu_clock(int cpu)
  217. {
  218. return sched_clock_cpu(cpu);
  219. }
  220. #endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
  221. EXPORT_SYMBOL_GPL(cpu_clock);