sched_clock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 prev_jiffies;
  40. u64 prev_raw;
  41. u64 tick_raw;
  42. u64 tick_gtod;
  43. u64 clock;
  44. };
  45. static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
  46. static inline struct sched_clock_data *this_scd(void)
  47. {
  48. return &__get_cpu_var(sched_clock_data);
  49. }
  50. static inline struct sched_clock_data *cpu_sdc(int cpu)
  51. {
  52. return &per_cpu(sched_clock_data, cpu);
  53. }
  54. static __read_mostly int sched_clock_running;
  55. void sched_clock_init(void)
  56. {
  57. u64 ktime_now = ktime_to_ns(ktime_get());
  58. unsigned long now_jiffies = jiffies;
  59. int cpu;
  60. for_each_possible_cpu(cpu) {
  61. struct sched_clock_data *scd = cpu_sdc(cpu);
  62. scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  63. scd->prev_jiffies = now_jiffies;
  64. scd->prev_raw = 0;
  65. scd->tick_raw = 0;
  66. scd->tick_gtod = ktime_now;
  67. scd->clock = ktime_now;
  68. }
  69. sched_clock_running = 1;
  70. }
  71. /*
  72. * update the percpu scd from the raw @now value
  73. *
  74. * - filter out backward motion
  75. * - use jiffies to generate a min,max window to clip the raw values
  76. */
  77. static void __update_sched_clock(struct sched_clock_data *scd, u64 now)
  78. {
  79. unsigned long now_jiffies = jiffies;
  80. long delta_jiffies = now_jiffies - scd->prev_jiffies;
  81. u64 clock = scd->clock;
  82. u64 min_clock, max_clock;
  83. s64 delta = now - scd->prev_raw;
  84. WARN_ON_ONCE(!irqs_disabled());
  85. min_clock = scd->tick_gtod + delta_jiffies * TICK_NSEC;
  86. if (unlikely(delta < 0)) {
  87. clock++;
  88. goto out;
  89. }
  90. max_clock = min_clock + TICK_NSEC;
  91. if (unlikely(clock + delta > max_clock)) {
  92. if (clock < max_clock)
  93. clock = max_clock;
  94. else
  95. clock++;
  96. } else {
  97. clock += delta;
  98. }
  99. out:
  100. if (unlikely(clock < min_clock))
  101. clock = min_clock;
  102. scd->prev_raw = now;
  103. scd->prev_jiffies = now_jiffies;
  104. scd->clock = clock;
  105. }
  106. static void lock_double_clock(struct sched_clock_data *data1,
  107. struct sched_clock_data *data2)
  108. {
  109. if (data1 < data2) {
  110. __raw_spin_lock(&data1->lock);
  111. __raw_spin_lock(&data2->lock);
  112. } else {
  113. __raw_spin_lock(&data2->lock);
  114. __raw_spin_lock(&data1->lock);
  115. }
  116. }
  117. u64 sched_clock_cpu(int cpu)
  118. {
  119. struct sched_clock_data *scd = cpu_sdc(cpu);
  120. u64 now, clock;
  121. if (unlikely(!sched_clock_running))
  122. return 0ull;
  123. WARN_ON_ONCE(!irqs_disabled());
  124. now = sched_clock();
  125. if (cpu != raw_smp_processor_id()) {
  126. /*
  127. * in order to update a remote cpu's clock based on our
  128. * unstable raw time rebase it against:
  129. * tick_raw (offset between raw counters)
  130. * tick_gotd (tick offset between cpus)
  131. */
  132. struct sched_clock_data *my_scd = this_scd();
  133. lock_double_clock(scd, my_scd);
  134. now -= my_scd->tick_raw;
  135. now += scd->tick_raw;
  136. now -= my_scd->tick_gtod;
  137. now += scd->tick_gtod;
  138. __raw_spin_unlock(&my_scd->lock);
  139. } else {
  140. __raw_spin_lock(&scd->lock);
  141. }
  142. __update_sched_clock(scd, now);
  143. clock = scd->clock;
  144. __raw_spin_unlock(&scd->lock);
  145. return clock;
  146. }
  147. void sched_clock_tick(void)
  148. {
  149. struct sched_clock_data *scd = this_scd();
  150. u64 now, now_gtod;
  151. if (unlikely(!sched_clock_running))
  152. return;
  153. WARN_ON_ONCE(!irqs_disabled());
  154. now = sched_clock();
  155. now_gtod = ktime_to_ns(ktime_get());
  156. __raw_spin_lock(&scd->lock);
  157. __update_sched_clock(scd, now);
  158. /*
  159. * update tick_gtod after __update_sched_clock() because that will
  160. * already observe 1 new jiffy; adding a new tick_gtod to that would
  161. * increase the clock 2 jiffies.
  162. */
  163. scd->tick_raw = now;
  164. scd->tick_gtod = now_gtod;
  165. __raw_spin_unlock(&scd->lock);
  166. }
  167. /*
  168. * We are going deep-idle (irqs are disabled):
  169. */
  170. void sched_clock_idle_sleep_event(void)
  171. {
  172. sched_clock_cpu(smp_processor_id());
  173. }
  174. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  175. /*
  176. * We just idled delta nanoseconds (called with irqs disabled):
  177. */
  178. void sched_clock_idle_wakeup_event(u64 delta_ns)
  179. {
  180. struct sched_clock_data *scd = this_scd();
  181. u64 now = sched_clock();
  182. /*
  183. * Override the previous timestamp and ignore all
  184. * sched_clock() deltas that occured while we idled,
  185. * and use the PM-provided delta_ns to advance the
  186. * rq clock:
  187. */
  188. __raw_spin_lock(&scd->lock);
  189. scd->prev_raw = now;
  190. scd->clock += delta_ns;
  191. __raw_spin_unlock(&scd->lock);
  192. touch_softlockup_watchdog();
  193. }
  194. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  195. #endif
  196. /*
  197. * Scheduler clock - returns current time in nanosec units.
  198. * This is default implementation.
  199. * Architectures and sub-architectures can override this.
  200. */
  201. unsigned long long __attribute__((weak)) sched_clock(void)
  202. {
  203. return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
  204. }
  205. unsigned long long cpu_clock(int cpu)
  206. {
  207. unsigned long long clock;
  208. unsigned long flags;
  209. local_irq_save(flags);
  210. clock = sched_clock_cpu(cpu);
  211. local_irq_restore(flags);
  212. return clock;
  213. }
  214. EXPORT_SYMBOL_GPL(cpu_clock);