sched_clock.c 5.4 KB

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