sched_clock.c 8.3 KB

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