sched_clock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/module.h>
  29. #include <linux/percpu.h>
  30. #include <linux/ktime.h>
  31. #include <linux/sched.h>
  32. /*
  33. * Scheduler clock - returns current time in nanosec units.
  34. * This is default implementation.
  35. * Architectures and sub-architectures can override this.
  36. */
  37. unsigned long long __attribute__((weak)) sched_clock(void)
  38. {
  39. return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
  40. }
  41. static __read_mostly int sched_clock_running;
  42. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  43. __read_mostly int sched_clock_stable;
  44. #else
  45. static const int sched_clock_stable = 1;
  46. #endif
  47. struct sched_clock_data {
  48. /*
  49. * Raw spinlock - this is a special case: this might be called
  50. * from within instrumentation code so we dont want to do any
  51. * instrumentation ourselves.
  52. */
  53. raw_spinlock_t lock;
  54. u64 tick_raw;
  55. u64 tick_gtod;
  56. u64 clock;
  57. };
  58. static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
  59. static inline struct sched_clock_data *this_scd(void)
  60. {
  61. return &__get_cpu_var(sched_clock_data);
  62. }
  63. static inline struct sched_clock_data *cpu_sdc(int cpu)
  64. {
  65. return &per_cpu(sched_clock_data, cpu);
  66. }
  67. void sched_clock_init(void)
  68. {
  69. u64 ktime_now = ktime_to_ns(ktime_get());
  70. int cpu;
  71. for_each_possible_cpu(cpu) {
  72. struct sched_clock_data *scd = cpu_sdc(cpu);
  73. scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  74. scd->tick_raw = 0;
  75. scd->tick_gtod = ktime_now;
  76. scd->clock = ktime_now;
  77. }
  78. sched_clock_running = 1;
  79. }
  80. /*
  81. * min, max except they take wrapping into account
  82. */
  83. static inline u64 wrap_min(u64 x, u64 y)
  84. {
  85. return (s64)(x - y) < 0 ? x : y;
  86. }
  87. static inline u64 wrap_max(u64 x, u64 y)
  88. {
  89. return (s64)(x - y) > 0 ? x : y;
  90. }
  91. /*
  92. * update the percpu scd from the raw @now value
  93. *
  94. * - filter out backward motion
  95. * - use the GTOD tick value to create a window to filter crazy TSC values
  96. */
  97. static u64 __update_sched_clock(struct sched_clock_data *scd, u64 now)
  98. {
  99. s64 delta = now - scd->tick_raw;
  100. u64 clock, min_clock, max_clock;
  101. WARN_ON_ONCE(!irqs_disabled());
  102. if (unlikely(delta < 0))
  103. delta = 0;
  104. if (unlikely(!sched_clock_running))
  105. return 0ull;
  106. /*
  107. * scd->clock = clamp(scd->tick_gtod + delta,
  108. * max(scd->tick_gtod, scd->clock),
  109. * scd->tick_gtod + TICK_NSEC);
  110. */
  111. clock = scd->tick_gtod + delta;
  112. min_clock = wrap_max(scd->tick_gtod, scd->clock);
  113. max_clock = wrap_max(scd->clock, scd->tick_gtod + TICK_NSEC);
  114. clock = wrap_max(clock, min_clock);
  115. clock = wrap_min(clock, max_clock);
  116. scd->clock = clock;
  117. return scd->clock;
  118. }
  119. static void lock_double_clock(struct sched_clock_data *data1,
  120. struct sched_clock_data *data2)
  121. {
  122. if (data1 < data2) {
  123. __raw_spin_lock(&data1->lock);
  124. __raw_spin_lock(&data2->lock);
  125. } else {
  126. __raw_spin_lock(&data2->lock);
  127. __raw_spin_lock(&data1->lock);
  128. }
  129. }
  130. u64 sched_clock_cpu(int cpu)
  131. {
  132. u64 now, clock, this_clock, remote_clock;
  133. struct sched_clock_data *scd;
  134. if (sched_clock_stable)
  135. return sched_clock();
  136. scd = cpu_sdc(cpu);
  137. WARN_ON_ONCE(!irqs_disabled());
  138. now = sched_clock();
  139. if (cpu != raw_smp_processor_id()) {
  140. struct sched_clock_data *my_scd = this_scd();
  141. lock_double_clock(scd, my_scd);
  142. this_clock = __update_sched_clock(my_scd, now);
  143. remote_clock = scd->clock;
  144. /*
  145. * Use the opportunity that we have both locks
  146. * taken to couple the two clocks: we take the
  147. * larger time as the latest time for both
  148. * runqueues. (this creates monotonic movement)
  149. */
  150. if (likely((s64)(remote_clock - this_clock) < 0)) {
  151. clock = this_clock;
  152. scd->clock = clock;
  153. } else {
  154. /*
  155. * Should be rare, but possible:
  156. */
  157. clock = remote_clock;
  158. my_scd->clock = remote_clock;
  159. }
  160. __raw_spin_unlock(&my_scd->lock);
  161. } else {
  162. __raw_spin_lock(&scd->lock);
  163. clock = __update_sched_clock(scd, now);
  164. }
  165. __raw_spin_unlock(&scd->lock);
  166. return clock;
  167. }
  168. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  169. void sched_clock_tick(void)
  170. {
  171. struct sched_clock_data *scd = this_scd();
  172. u64 now, now_gtod;
  173. if (unlikely(!sched_clock_running))
  174. return;
  175. WARN_ON_ONCE(!irqs_disabled());
  176. now_gtod = ktime_to_ns(ktime_get());
  177. now = sched_clock();
  178. __raw_spin_lock(&scd->lock);
  179. scd->tick_raw = now;
  180. scd->tick_gtod = now_gtod;
  181. __update_sched_clock(scd, now);
  182. __raw_spin_unlock(&scd->lock);
  183. }
  184. /*
  185. * We are going deep-idle (irqs are disabled):
  186. */
  187. void sched_clock_idle_sleep_event(void)
  188. {
  189. sched_clock_cpu(smp_processor_id());
  190. }
  191. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  192. /*
  193. * We just idled delta nanoseconds (called with irqs disabled):
  194. */
  195. void sched_clock_idle_wakeup_event(u64 delta_ns)
  196. {
  197. if (timekeeping_suspended)
  198. return;
  199. sched_clock_tick();
  200. touch_softlockup_watchdog();
  201. }
  202. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  203. #endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
  204. unsigned long long cpu_clock(int cpu)
  205. {
  206. unsigned long long clock;
  207. unsigned long flags;
  208. local_irq_save(flags);
  209. clock = sched_clock_cpu(cpu);
  210. local_irq_restore(flags);
  211. return clock;
  212. }
  213. EXPORT_SYMBOL_GPL(cpu_clock);