sched_clock.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * sched_clock.c: Generic sched_clock() support, to extend low level
  3. * hardware time counters to full 64-bit ns values.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clocksource.h>
  10. #include <linux/init.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/ktime.h>
  13. #include <linux/kernel.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/sched.h>
  16. #include <linux/syscore_ops.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/sched_clock.h>
  19. #include <linux/seqlock.h>
  20. #include <linux/bitops.h>
  21. /**
  22. * struct clock_read_data - data required to read from sched_clock()
  23. *
  24. * @epoch_ns: sched_clock() value at last update
  25. * @epoch_cyc: Clock cycle value at last update.
  26. * @sched_clock_mask: Bitmask for two's complement subtraction of non 64bit
  27. * clocks.
  28. * @read_sched_clock: Current clock source (or dummy source when suspended).
  29. * @mult: Multipler for scaled math conversion.
  30. * @shift: Shift value for scaled math conversion.
  31. *
  32. * Care must be taken when updating this structure; it is read by
  33. * some very hot code paths. It occupies <=40 bytes and, when combined
  34. * with the seqcount used to synchronize access, comfortably fits into
  35. * a 64 byte cache line.
  36. */
  37. struct clock_read_data {
  38. u64 epoch_ns;
  39. u64 epoch_cyc;
  40. u64 sched_clock_mask;
  41. u64 (*read_sched_clock)(void);
  42. u32 mult;
  43. u32 shift;
  44. };
  45. /**
  46. * struct clock_data - all data needed for sched_clock() (including
  47. * registration of a new clock source)
  48. *
  49. * @seq: Sequence counter for protecting updates. The lowest
  50. * bit is the index for @read_data.
  51. * @read_data: Data required to read from sched_clock.
  52. * @wrap_kt: Duration for which clock can run before wrapping.
  53. * @rate: Tick rate of the registered clock.
  54. * @actual_read_sched_clock: Registered hardware level clock read function.
  55. *
  56. * The ordering of this structure has been chosen to optimize cache
  57. * performance. In particular 'seq' and 'read_data[0]' (combined) should fit
  58. * into a single 64-byte cache line.
  59. */
  60. struct clock_data {
  61. seqcount_t seq;
  62. struct clock_read_data read_data[2];
  63. ktime_t wrap_kt;
  64. unsigned long rate;
  65. u64 (*actual_read_sched_clock)(void);
  66. };
  67. static struct hrtimer sched_clock_timer;
  68. static int irqtime = -1;
  69. core_param(irqtime, irqtime, int, 0400);
  70. static u64 notrace jiffy_sched_clock_read(void)
  71. {
  72. /*
  73. * We don't need to use get_jiffies_64 on 32-bit arches here
  74. * because we register with BITS_PER_LONG
  75. */
  76. return (u64)(jiffies - INITIAL_JIFFIES);
  77. }
  78. static struct clock_data cd ____cacheline_aligned = {
  79. .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
  80. .read_sched_clock = jiffy_sched_clock_read, },
  81. .actual_read_sched_clock = jiffy_sched_clock_read,
  82. };
  83. static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
  84. {
  85. return (cyc * mult) >> shift;
  86. }
  87. unsigned long long notrace sched_clock(void)
  88. {
  89. u64 cyc, res;
  90. unsigned long seq;
  91. struct clock_read_data *rd;
  92. do {
  93. seq = raw_read_seqcount(&cd.seq);
  94. rd = cd.read_data + (seq & 1);
  95. cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
  96. rd->sched_clock_mask;
  97. res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
  98. } while (read_seqcount_retry(&cd.seq, seq));
  99. return res;
  100. }
  101. /*
  102. * Updating the data required to read the clock.
  103. *
  104. * sched_clock() will never observe mis-matched data even if called from
  105. * an NMI. We do this by maintaining an odd/even copy of the data and
  106. * steering sched_clock() to one or the other using a sequence counter.
  107. * In order to preserve the data cache profile of sched_clock() as much
  108. * as possible the system reverts back to the even copy when the update
  109. * completes; the odd copy is used *only* during an update.
  110. */
  111. static void update_clock_read_data(struct clock_read_data *rd)
  112. {
  113. /* update the backup (odd) copy with the new data */
  114. cd.read_data[1] = *rd;
  115. /* steer readers towards the odd copy */
  116. raw_write_seqcount_latch(&cd.seq);
  117. /* now its safe for us to update the normal (even) copy */
  118. cd.read_data[0] = *rd;
  119. /* switch readers back to the even copy */
  120. raw_write_seqcount_latch(&cd.seq);
  121. }
  122. /*
  123. * Atomically update the sched_clock() epoch.
  124. */
  125. static void update_sched_clock(void)
  126. {
  127. u64 cyc;
  128. u64 ns;
  129. struct clock_read_data rd;
  130. rd = cd.read_data[0];
  131. cyc = cd.actual_read_sched_clock();
  132. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  133. rd.epoch_ns = ns;
  134. rd.epoch_cyc = cyc;
  135. update_clock_read_data(&rd);
  136. }
  137. static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
  138. {
  139. update_sched_clock();
  140. hrtimer_forward_now(hrt, cd.wrap_kt);
  141. return HRTIMER_RESTART;
  142. }
  143. void __init
  144. sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
  145. {
  146. u64 res, wrap, new_mask, new_epoch, cyc, ns;
  147. u32 new_mult, new_shift;
  148. unsigned long r;
  149. char r_unit;
  150. struct clock_read_data rd;
  151. if (cd.rate > rate)
  152. return;
  153. WARN_ON(!irqs_disabled());
  154. /* Calculate the mult/shift to convert counter ticks to ns. */
  155. clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
  156. new_mask = CLOCKSOURCE_MASK(bits);
  157. cd.rate = rate;
  158. /* Calculate how many nanosecs until we risk wrapping */
  159. wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
  160. cd.wrap_kt = ns_to_ktime(wrap);
  161. rd = cd.read_data[0];
  162. /* Update epoch for new counter and update 'epoch_ns' from old counter*/
  163. new_epoch = read();
  164. cyc = cd.actual_read_sched_clock();
  165. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  166. cd.actual_read_sched_clock = read;
  167. rd.read_sched_clock = read;
  168. rd.sched_clock_mask = new_mask;
  169. rd.mult = new_mult;
  170. rd.shift = new_shift;
  171. rd.epoch_cyc = new_epoch;
  172. rd.epoch_ns = ns;
  173. update_clock_read_data(&rd);
  174. if (sched_clock_timer.function != NULL) {
  175. /* update timeout for clock wrap */
  176. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
  177. }
  178. r = rate;
  179. if (r >= 4000000) {
  180. r /= 1000000;
  181. r_unit = 'M';
  182. } else {
  183. if (r >= 1000) {
  184. r /= 1000;
  185. r_unit = 'k';
  186. } else {
  187. r_unit = ' ';
  188. }
  189. }
  190. /* Calculate the ns resolution of this counter */
  191. res = cyc_to_ns(1ULL, new_mult, new_shift);
  192. pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
  193. bits, r, r_unit, res, wrap);
  194. /* Enable IRQ time accounting if we have a fast enough sched_clock() */
  195. if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
  196. enable_sched_clock_irqtime();
  197. pr_debug("Registered %pF as sched_clock source\n", read);
  198. }
  199. void __init sched_clock_postinit(void)
  200. {
  201. /*
  202. * If no sched_clock() function has been provided at that point,
  203. * make it the final one one.
  204. */
  205. if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
  206. sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
  207. update_sched_clock();
  208. /*
  209. * Start the timer to keep sched_clock() properly updated and
  210. * sets the initial epoch.
  211. */
  212. hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  213. sched_clock_timer.function = sched_clock_poll;
  214. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
  215. }
  216. /*
  217. * Clock read function for use when the clock is suspended.
  218. *
  219. * This function makes it appear to sched_clock() as if the clock
  220. * stopped counting at its last update.
  221. *
  222. * This function must only be called from the critical
  223. * section in sched_clock(). It relies on the read_seqcount_retry()
  224. * at the end of the critical section to be sure we observe the
  225. * correct copy of 'epoch_cyc'.
  226. */
  227. static u64 notrace suspended_sched_clock_read(void)
  228. {
  229. unsigned long seq = raw_read_seqcount(&cd.seq);
  230. return cd.read_data[seq & 1].epoch_cyc;
  231. }
  232. static int sched_clock_suspend(void)
  233. {
  234. struct clock_read_data *rd = &cd.read_data[0];
  235. update_sched_clock();
  236. hrtimer_cancel(&sched_clock_timer);
  237. rd->read_sched_clock = suspended_sched_clock_read;
  238. return 0;
  239. }
  240. static void sched_clock_resume(void)
  241. {
  242. struct clock_read_data *rd = &cd.read_data[0];
  243. rd->epoch_cyc = cd.actual_read_sched_clock();
  244. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
  245. rd->read_sched_clock = cd.actual_read_sched_clock;
  246. }
  247. static struct syscore_ops sched_clock_ops = {
  248. .suspend = sched_clock_suspend,
  249. .resume = sched_clock_resume,
  250. };
  251. static int __init sched_clock_syscore_init(void)
  252. {
  253. register_syscore_ops(&sched_clock_ops);
  254. return 0;
  255. }
  256. device_initcall(sched_clock_syscore_init);