sched_clock.c 7.7 KB

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