sched_clock.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * - jiffies
  16. * - sched_clock()
  17. * - explicit idle events
  18. *
  19. * We use gtod as base and the unstable clock deltas. The deltas are filtered,
  20. * making it monotonic and keeping it within an expected window. This window
  21. * is set up using jiffies.
  22. *
  23. * Furthermore, explicit sleep and wakeup hooks allow us to account for time
  24. * that is otherwise invisible (TSC gets stopped).
  25. *
  26. * The clock: sched_clock_cpu() is monotonic per cpu, and should be somewhat
  27. * consistent between cpus (never more than 1 jiffies difference).
  28. */
  29. #include <linux/sched.h>
  30. #include <linux/percpu.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/ktime.h>
  33. #include <linux/module.h>
  34. /*
  35. * Scheduler clock - returns current time in nanosec units.
  36. * This is default implementation.
  37. * Architectures and sub-architectures can override this.
  38. */
  39. unsigned long long __attribute__((weak)) sched_clock(void)
  40. {
  41. return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
  42. }
  43. static __read_mostly int sched_clock_running;
  44. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  45. struct sched_clock_data {
  46. /*
  47. * Raw spinlock - this is a special case: this might be called
  48. * from within instrumentation code so we dont want to do any
  49. * instrumentation ourselves.
  50. */
  51. raw_spinlock_t lock;
  52. unsigned long tick_jiffies;
  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. unsigned long now_jiffies = jiffies;
  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_jiffies = now_jiffies;
  75. scd->tick_raw = 0;
  76. scd->tick_gtod = ktime_now;
  77. scd->clock = ktime_now;
  78. }
  79. sched_clock_running = 1;
  80. }
  81. /*
  82. * update the percpu scd from the raw @now value
  83. *
  84. * - filter out backward motion
  85. * - use jiffies to generate a min,max window to clip the raw values
  86. */
  87. static u64 __update_sched_clock(struct sched_clock_data *scd, u64 now)
  88. {
  89. unsigned long now_jiffies = jiffies;
  90. long delta_jiffies = now_jiffies - scd->tick_jiffies;
  91. u64 clock = scd->clock;
  92. u64 min_clock, max_clock;
  93. s64 delta = now - scd->tick_raw;
  94. WARN_ON_ONCE(!irqs_disabled());
  95. min_clock = scd->tick_gtod + delta_jiffies * TICK_NSEC;
  96. if (unlikely(delta < 0)) {
  97. clock++;
  98. goto out;
  99. }
  100. max_clock = min_clock + TICK_NSEC;
  101. if (unlikely(clock + delta > max_clock)) {
  102. if (clock < max_clock)
  103. clock = max_clock;
  104. else
  105. clock++;
  106. } else {
  107. clock += delta;
  108. }
  109. out:
  110. if (unlikely(clock < min_clock))
  111. clock = min_clock;
  112. scd->tick_jiffies = now_jiffies;
  113. scd->clock = clock;
  114. return clock;
  115. }
  116. static void lock_double_clock(struct sched_clock_data *data1,
  117. struct sched_clock_data *data2)
  118. {
  119. if (data1 < data2) {
  120. __raw_spin_lock(&data1->lock);
  121. __raw_spin_lock(&data2->lock);
  122. } else {
  123. __raw_spin_lock(&data2->lock);
  124. __raw_spin_lock(&data1->lock);
  125. }
  126. }
  127. u64 sched_clock_cpu(int cpu)
  128. {
  129. struct sched_clock_data *scd = cpu_sdc(cpu);
  130. u64 now, clock, this_clock, remote_clock;
  131. if (unlikely(!sched_clock_running))
  132. return 0ull;
  133. WARN_ON_ONCE(!irqs_disabled());
  134. now = sched_clock();
  135. if (cpu != raw_smp_processor_id()) {
  136. struct sched_clock_data *my_scd = this_scd();
  137. lock_double_clock(scd, my_scd);
  138. this_clock = __update_sched_clock(my_scd, now);
  139. remote_clock = scd->clock;
  140. /*
  141. * Use the opportunity that we have both locks
  142. * taken to couple the two clocks: we take the
  143. * larger time as the latest time for both
  144. * runqueues. (this creates monotonic movement)
  145. */
  146. if (likely(remote_clock < this_clock)) {
  147. clock = this_clock;
  148. scd->clock = clock;
  149. } else {
  150. /*
  151. * Should be rare, but possible:
  152. */
  153. clock = remote_clock;
  154. my_scd->clock = remote_clock;
  155. }
  156. __raw_spin_unlock(&my_scd->lock);
  157. } else {
  158. __raw_spin_lock(&scd->lock);
  159. clock = __update_sched_clock(scd, now);
  160. }
  161. __raw_spin_unlock(&scd->lock);
  162. return clock;
  163. }
  164. void sched_clock_tick(void)
  165. {
  166. struct sched_clock_data *scd = this_scd();
  167. u64 now, now_gtod;
  168. if (unlikely(!sched_clock_running))
  169. return;
  170. WARN_ON_ONCE(!irqs_disabled());
  171. now_gtod = ktime_to_ns(ktime_get());
  172. now = sched_clock();
  173. __raw_spin_lock(&scd->lock);
  174. __update_sched_clock(scd, now);
  175. /*
  176. * update tick_gtod after __update_sched_clock() because that will
  177. * already observe 1 new jiffy; adding a new tick_gtod to that would
  178. * increase the clock 2 jiffies.
  179. */
  180. scd->tick_raw = now;
  181. scd->tick_gtod = now_gtod;
  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. struct sched_clock_data *scd = this_scd();
  198. /*
  199. * Override the previous timestamp and ignore all
  200. * sched_clock() deltas that occured while we idled,
  201. * and use the PM-provided delta_ns to advance the
  202. * rq clock:
  203. */
  204. __raw_spin_lock(&scd->lock);
  205. scd->clock += delta_ns;
  206. __raw_spin_unlock(&scd->lock);
  207. touch_softlockup_watchdog();
  208. }
  209. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  210. #else /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
  211. void sched_clock_init(void)
  212. {
  213. sched_clock_running = 1;
  214. }
  215. u64 sched_clock_cpu(int cpu)
  216. {
  217. if (unlikely(!sched_clock_running))
  218. return 0;
  219. return sched_clock();
  220. }
  221. #endif
  222. unsigned long long cpu_clock(int cpu)
  223. {
  224. unsigned long long clock;
  225. unsigned long flags;
  226. local_irq_save(flags);
  227. clock = sched_clock_cpu(cpu);
  228. local_irq_restore(flags);
  229. return clock;
  230. }
  231. EXPORT_SYMBOL_GPL(cpu_clock);