sched_clock.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  35. #define MULTI_SHIFT 15
  36. /* Max is double, Min is 1/2 */
  37. #define MAX_MULTI (2LL << MULTI_SHIFT)
  38. #define MIN_MULTI (1LL << (MULTI_SHIFT-1))
  39. struct sched_clock_data {
  40. /*
  41. * Raw spinlock - this is a special case: this might be called
  42. * from within instrumentation code so we dont want to do any
  43. * instrumentation ourselves.
  44. */
  45. raw_spinlock_t lock;
  46. unsigned long tick_jiffies;
  47. u64 prev_raw;
  48. u64 tick_raw;
  49. u64 tick_gtod;
  50. u64 clock;
  51. s64 multi;
  52. #ifdef CONFIG_NO_HZ
  53. int check_max;
  54. #endif
  55. };
  56. static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data);
  57. static inline struct sched_clock_data *this_scd(void)
  58. {
  59. return &__get_cpu_var(sched_clock_data);
  60. }
  61. static inline struct sched_clock_data *cpu_sdc(int cpu)
  62. {
  63. return &per_cpu(sched_clock_data, cpu);
  64. }
  65. static __read_mostly int sched_clock_running;
  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->prev_raw = 0;
  76. scd->tick_raw = 0;
  77. scd->tick_gtod = ktime_now;
  78. scd->clock = ktime_now;
  79. scd->multi = 1 << MULTI_SHIFT;
  80. #ifdef CONFIG_NO_HZ
  81. scd->check_max = 1;
  82. #endif
  83. }
  84. sched_clock_running = 1;
  85. }
  86. #ifdef CONFIG_NO_HZ
  87. /*
  88. * The dynamic ticks makes the delta jiffies inaccurate. This
  89. * prevents us from checking the maximum time update.
  90. * Disable the maximum check during stopped ticks.
  91. */
  92. void sched_clock_tick_stop(int cpu)
  93. {
  94. struct sched_clock_data *scd = cpu_sdc(cpu);
  95. scd->check_max = 0;
  96. }
  97. void sched_clock_tick_start(int cpu)
  98. {
  99. struct sched_clock_data *scd = cpu_sdc(cpu);
  100. scd->check_max = 1;
  101. }
  102. static int check_max(struct sched_clock_data *scd)
  103. {
  104. return scd->check_max;
  105. }
  106. #else
  107. static int check_max(struct sched_clock_data *scd)
  108. {
  109. return 1;
  110. }
  111. #endif /* CONFIG_NO_HZ */
  112. /*
  113. * update the percpu scd from the raw @now value
  114. *
  115. * - filter out backward motion
  116. * - use jiffies to generate a min,max window to clip the raw values
  117. */
  118. static void __update_sched_clock(struct sched_clock_data *scd, u64 now, u64 *time)
  119. {
  120. unsigned long now_jiffies = jiffies;
  121. long delta_jiffies = now_jiffies - scd->tick_jiffies;
  122. u64 clock = scd->clock;
  123. u64 min_clock, max_clock;
  124. s64 delta = now - scd->prev_raw;
  125. WARN_ON_ONCE(!irqs_disabled());
  126. /*
  127. * At schedule tick the clock can be just under the gtod. We don't
  128. * want to push it too prematurely.
  129. */
  130. min_clock = scd->tick_gtod + (delta_jiffies * TICK_NSEC);
  131. if (min_clock > TICK_NSEC)
  132. min_clock -= TICK_NSEC / 2;
  133. if (unlikely(delta < 0)) {
  134. clock++;
  135. goto out;
  136. }
  137. /*
  138. * The clock must stay within a jiffie of the gtod.
  139. * But since we may be at the start of a jiffy or the end of one
  140. * we add another jiffy buffer.
  141. */
  142. max_clock = scd->tick_gtod + (2 + delta_jiffies) * TICK_NSEC;
  143. delta *= scd->multi;
  144. delta >>= MULTI_SHIFT;
  145. if (unlikely(clock + delta > max_clock) && check_max(scd)) {
  146. if (clock < max_clock)
  147. clock = max_clock;
  148. else
  149. clock++;
  150. } else {
  151. clock += delta;
  152. }
  153. out:
  154. if (unlikely(clock < min_clock))
  155. clock = min_clock;
  156. if (time)
  157. *time = clock;
  158. else {
  159. scd->prev_raw = now;
  160. scd->clock = clock;
  161. }
  162. }
  163. static void lock_double_clock(struct sched_clock_data *data1,
  164. struct sched_clock_data *data2)
  165. {
  166. if (data1 < data2) {
  167. __raw_spin_lock(&data1->lock);
  168. __raw_spin_lock(&data2->lock);
  169. } else {
  170. __raw_spin_lock(&data2->lock);
  171. __raw_spin_lock(&data1->lock);
  172. }
  173. }
  174. u64 sched_clock_cpu(int cpu)
  175. {
  176. struct sched_clock_data *scd = cpu_sdc(cpu);
  177. u64 now, clock;
  178. if (unlikely(!sched_clock_running))
  179. return 0ull;
  180. WARN_ON_ONCE(!irqs_disabled());
  181. now = sched_clock();
  182. if (cpu != raw_smp_processor_id()) {
  183. /*
  184. * in order to update a remote cpu's clock based on our
  185. * unstable raw time rebase it against:
  186. * tick_raw (offset between raw counters)
  187. * tick_gotd (tick offset between cpus)
  188. */
  189. struct sched_clock_data *my_scd = this_scd();
  190. lock_double_clock(scd, my_scd);
  191. now -= my_scd->tick_raw;
  192. now += scd->tick_raw;
  193. now += my_scd->tick_gtod;
  194. now -= scd->tick_gtod;
  195. __raw_spin_unlock(&my_scd->lock);
  196. __update_sched_clock(scd, now, &clock);
  197. __raw_spin_unlock(&scd->lock);
  198. } else {
  199. __raw_spin_lock(&scd->lock);
  200. __update_sched_clock(scd, now, NULL);
  201. clock = scd->clock;
  202. __raw_spin_unlock(&scd->lock);
  203. }
  204. return clock;
  205. }
  206. void sched_clock_tick(void)
  207. {
  208. struct sched_clock_data *scd = this_scd();
  209. unsigned long now_jiffies = jiffies;
  210. s64 mult, delta_gtod, delta_raw;
  211. u64 now, now_gtod;
  212. if (unlikely(!sched_clock_running))
  213. return;
  214. WARN_ON_ONCE(!irqs_disabled());
  215. now_gtod = ktime_to_ns(ktime_get());
  216. now = sched_clock();
  217. __raw_spin_lock(&scd->lock);
  218. __update_sched_clock(scd, now, NULL);
  219. /*
  220. * update tick_gtod after __update_sched_clock() because that will
  221. * already observe 1 new jiffy; adding a new tick_gtod to that would
  222. * increase the clock 2 jiffies.
  223. */
  224. delta_gtod = now_gtod - scd->tick_gtod;
  225. delta_raw = now - scd->tick_raw;
  226. if ((long)delta_raw > 0) {
  227. mult = delta_gtod << MULTI_SHIFT;
  228. do_div(mult, delta_raw);
  229. scd->multi = mult;
  230. if (scd->multi > MAX_MULTI)
  231. scd->multi = MAX_MULTI;
  232. else if (scd->multi < MIN_MULTI)
  233. scd->multi = MIN_MULTI;
  234. } else
  235. scd->multi = 1 << MULTI_SHIFT;
  236. scd->tick_raw = now;
  237. scd->tick_gtod = now_gtod;
  238. scd->tick_jiffies = now_jiffies;
  239. __raw_spin_unlock(&scd->lock);
  240. }
  241. /*
  242. * We are going deep-idle (irqs are disabled):
  243. */
  244. void sched_clock_idle_sleep_event(void)
  245. {
  246. sched_clock_cpu(smp_processor_id());
  247. }
  248. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  249. /*
  250. * We just idled delta nanoseconds (called with irqs disabled):
  251. */
  252. void sched_clock_idle_wakeup_event(u64 delta_ns)
  253. {
  254. struct sched_clock_data *scd = this_scd();
  255. u64 now = sched_clock();
  256. /*
  257. * Override the previous timestamp and ignore all
  258. * sched_clock() deltas that occured while we idled,
  259. * and use the PM-provided delta_ns to advance the
  260. * rq clock:
  261. */
  262. __raw_spin_lock(&scd->lock);
  263. scd->prev_raw = now;
  264. scd->clock += delta_ns;
  265. scd->multi = 1 << MULTI_SHIFT;
  266. __raw_spin_unlock(&scd->lock);
  267. touch_softlockup_watchdog();
  268. }
  269. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  270. #endif
  271. /*
  272. * Scheduler clock - returns current time in nanosec units.
  273. * This is default implementation.
  274. * Architectures and sub-architectures can override this.
  275. */
  276. unsigned long long __attribute__((weak)) sched_clock(void)
  277. {
  278. return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
  279. }
  280. unsigned long long cpu_clock(int cpu)
  281. {
  282. unsigned long long clock;
  283. unsigned long flags;
  284. local_irq_save(flags);
  285. clock = sched_clock_cpu(cpu);
  286. local_irq_restore(flags);
  287. return clock;
  288. }
  289. EXPORT_SYMBOL_GPL(cpu_clock);