timings.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * linux/kernel/irq/timings.c
  3. *
  4. * Copyright (C) 2016, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/percpu.h>
  13. #include <linux/slab.h>
  14. #include <linux/static_key.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/idr.h>
  17. #include <linux/irq.h>
  18. #include <linux/math64.h>
  19. #include <trace/events/irq.h>
  20. #include "internals.h"
  21. DEFINE_STATIC_KEY_FALSE(irq_timing_enabled);
  22. DEFINE_PER_CPU(struct irq_timings, irq_timings);
  23. struct irqt_stat {
  24. u64 next_evt;
  25. u64 last_ts;
  26. u64 variance;
  27. u32 avg;
  28. u32 nr_samples;
  29. int anomalies;
  30. int valid;
  31. };
  32. static DEFINE_IDR(irqt_stats);
  33. void irq_timings_enable(void)
  34. {
  35. static_branch_enable(&irq_timing_enabled);
  36. }
  37. void irq_timings_disable(void)
  38. {
  39. static_branch_disable(&irq_timing_enabled);
  40. }
  41. /**
  42. * irqs_update - update the irq timing statistics with a new timestamp
  43. *
  44. * @irqs: an irqt_stat struct pointer
  45. * @ts: the new timestamp
  46. *
  47. * The statistics are computed online, in other words, the code is
  48. * designed to compute the statistics on a stream of values rather
  49. * than doing multiple passes on the values to compute the average,
  50. * then the variance. The integer division introduces a loss of
  51. * precision but with an acceptable error margin regarding the results
  52. * we would have with the double floating precision: we are dealing
  53. * with nanosec, so big numbers, consequently the mantisse is
  54. * negligeable, especially when converting the time in usec
  55. * afterwards.
  56. *
  57. * The computation happens at idle time. When the CPU is not idle, the
  58. * interrupts' timestamps are stored in the circular buffer, when the
  59. * CPU goes idle and this routine is called, all the buffer's values
  60. * are injected in the statistical model continuying to extend the
  61. * statistics from the previous busy-idle cycle.
  62. *
  63. * The observations showed a device will trigger a burst of periodic
  64. * interrupts followed by one or two peaks of longer time, for
  65. * instance when a SD card device flushes its cache, then the periodic
  66. * intervals occur again. A one second inactivity period resets the
  67. * stats, that gives us the certitude the statistical values won't
  68. * exceed 1x10^9, thus the computation won't overflow.
  69. *
  70. * Basically, the purpose of the algorithm is to watch the periodic
  71. * interrupts and eliminate the peaks.
  72. *
  73. * An interrupt is considered periodically stable if the interval of
  74. * its occurences follow the normal distribution, thus the values
  75. * comply with:
  76. *
  77. * avg - 3 x stddev < value < avg + 3 x stddev
  78. *
  79. * Which can be simplified to:
  80. *
  81. * -3 x stddev < value - avg < 3 x stddev
  82. *
  83. * abs(value - avg) < 3 x stddev
  84. *
  85. * In order to save a costly square root computation, we use the
  86. * variance. For the record, stddev = sqrt(variance). The equation
  87. * above becomes:
  88. *
  89. * abs(value - avg) < 3 x sqrt(variance)
  90. *
  91. * And finally we square it:
  92. *
  93. * (value - avg) ^ 2 < (3 x sqrt(variance)) ^ 2
  94. *
  95. * (value - avg) x (value - avg) < 9 x variance
  96. *
  97. * Statistically speaking, any values out of this interval is
  98. * considered as an anomaly and is discarded. However, a normal
  99. * distribution appears when the number of samples is 30 (it is the
  100. * rule of thumb in statistics, cf. "30 samples" on Internet). When
  101. * there are three consecutive anomalies, the statistics are resetted.
  102. *
  103. */
  104. static void irqs_update(struct irqt_stat *irqs, u64 ts)
  105. {
  106. u64 old_ts = irqs->last_ts;
  107. u64 variance = 0;
  108. u64 interval;
  109. s64 diff;
  110. /*
  111. * The timestamps are absolute time values, we need to compute
  112. * the timing interval between two interrupts.
  113. */
  114. irqs->last_ts = ts;
  115. /*
  116. * The interval type is u64 in order to deal with the same
  117. * type in our computation, that prevent mindfuck issues with
  118. * overflow, sign and division.
  119. */
  120. interval = ts - old_ts;
  121. /*
  122. * The interrupt triggered more than one second apart, that
  123. * ends the sequence as predictible for our purpose. In this
  124. * case, assume we have the beginning of a sequence and the
  125. * timestamp is the first value. As it is impossible to
  126. * predict anything at this point, return.
  127. *
  128. * Note the first timestamp of the sequence will always fall
  129. * in this test because the old_ts is zero. That is what we
  130. * want as we need another timestamp to compute an interval.
  131. */
  132. if (interval >= NSEC_PER_SEC) {
  133. memset(irqs, 0, sizeof(*irqs));
  134. irqs->last_ts = ts;
  135. return;
  136. }
  137. /*
  138. * Pre-compute the delta with the average as the result is
  139. * used several times in this function.
  140. */
  141. diff = interval - irqs->avg;
  142. /*
  143. * Increment the number of samples.
  144. */
  145. irqs->nr_samples++;
  146. /*
  147. * Online variance divided by the number of elements if there
  148. * is more than one sample. Normally the formula is division
  149. * by nr_samples - 1 but we assume the number of element will be
  150. * more than 32 and dividing by 32 instead of 31 is enough
  151. * precise.
  152. */
  153. if (likely(irqs->nr_samples > 1))
  154. variance = irqs->variance >> IRQ_TIMINGS_SHIFT;
  155. /*
  156. * The rule of thumb in statistics for the normal distribution
  157. * is having at least 30 samples in order to have the model to
  158. * apply. Values outside the interval are considered as an
  159. * anomaly.
  160. */
  161. if ((irqs->nr_samples >= 30) && ((diff * diff) > (9 * variance))) {
  162. /*
  163. * After three consecutive anomalies, we reset the
  164. * stats as it is no longer stable enough.
  165. */
  166. if (irqs->anomalies++ >= 3) {
  167. memset(irqs, 0, sizeof(*irqs));
  168. irqs->last_ts = ts;
  169. return;
  170. }
  171. } else {
  172. /*
  173. * The anomalies must be consecutives, so at this
  174. * point, we reset the anomalies counter.
  175. */
  176. irqs->anomalies = 0;
  177. }
  178. /*
  179. * The interrupt is considered stable enough to try to predict
  180. * the next event on it.
  181. */
  182. irqs->valid = 1;
  183. /*
  184. * Online average algorithm:
  185. *
  186. * new_average = average + ((value - average) / count)
  187. *
  188. * The variance computation depends on the new average
  189. * to be computed here first.
  190. *
  191. */
  192. irqs->avg = irqs->avg + (diff >> IRQ_TIMINGS_SHIFT);
  193. /*
  194. * Online variance algorithm:
  195. *
  196. * new_variance = variance + (value - average) x (value - new_average)
  197. *
  198. * Warning: irqs->avg is updated with the line above, hence
  199. * 'interval - irqs->avg' is no longer equal to 'diff'
  200. */
  201. irqs->variance = irqs->variance + (diff * (interval - irqs->avg));
  202. /*
  203. * Update the next event
  204. */
  205. irqs->next_evt = ts + irqs->avg;
  206. }
  207. /**
  208. * irq_timings_next_event - Return when the next event is supposed to arrive
  209. *
  210. * During the last busy cycle, the number of interrupts is incremented
  211. * and stored in the irq_timings structure. This information is
  212. * necessary to:
  213. *
  214. * - know if the index in the table wrapped up:
  215. *
  216. * If more than the array size interrupts happened during the
  217. * last busy/idle cycle, the index wrapped up and we have to
  218. * begin with the next element in the array which is the last one
  219. * in the sequence, otherwise it is a the index 0.
  220. *
  221. * - have an indication of the interrupts activity on this CPU
  222. * (eg. irq/sec)
  223. *
  224. * The values are 'consumed' after inserting in the statistical model,
  225. * thus the count is reinitialized.
  226. *
  227. * The array of values **must** be browsed in the time direction, the
  228. * timestamp must increase between an element and the next one.
  229. *
  230. * Returns a nanosec time based estimation of the earliest interrupt,
  231. * U64_MAX otherwise.
  232. */
  233. u64 irq_timings_next_event(u64 now)
  234. {
  235. struct irq_timings *irqts = this_cpu_ptr(&irq_timings);
  236. struct irqt_stat *irqs;
  237. struct irqt_stat __percpu *s;
  238. u64 ts, next_evt = U64_MAX;
  239. int i, irq = 0;
  240. /*
  241. * This function must be called with the local irq disabled in
  242. * order to prevent the timings circular buffer to be updated
  243. * while we are reading it.
  244. */
  245. WARN_ON_ONCE(!irqs_disabled());
  246. /*
  247. * Number of elements in the circular buffer: If it happens it
  248. * was flushed before, then the number of elements could be
  249. * smaller than IRQ_TIMINGS_SIZE, so the count is used,
  250. * otherwise the array size is used as we wrapped. The index
  251. * begins from zero when we did not wrap. That could be done
  252. * in a nicer way with the proper circular array structure
  253. * type but with the cost of extra computation in the
  254. * interrupt handler hot path. We choose efficiency.
  255. *
  256. * Inject measured irq/timestamp to the statistical model
  257. * while decrementing the counter because we consume the data
  258. * from our circular buffer.
  259. */
  260. for (i = irqts->count & IRQ_TIMINGS_MASK,
  261. irqts->count = min(IRQ_TIMINGS_SIZE, irqts->count);
  262. irqts->count > 0; irqts->count--, i = (i + 1) & IRQ_TIMINGS_MASK) {
  263. irq = irq_timing_decode(irqts->values[i], &ts);
  264. s = idr_find(&irqt_stats, irq);
  265. if (s) {
  266. irqs = this_cpu_ptr(s);
  267. irqs_update(irqs, ts);
  268. }
  269. }
  270. /*
  271. * Look in the list of interrupts' statistics, the earliest
  272. * next event.
  273. */
  274. idr_for_each_entry(&irqt_stats, s, i) {
  275. irqs = this_cpu_ptr(s);
  276. if (!irqs->valid)
  277. continue;
  278. if (irqs->next_evt <= now) {
  279. irq = i;
  280. next_evt = now;
  281. /*
  282. * This interrupt mustn't use in the future
  283. * until new events occur and update the
  284. * statistics.
  285. */
  286. irqs->valid = 0;
  287. break;
  288. }
  289. if (irqs->next_evt < next_evt) {
  290. irq = i;
  291. next_evt = irqs->next_evt;
  292. }
  293. }
  294. return next_evt;
  295. }
  296. void irq_timings_free(int irq)
  297. {
  298. struct irqt_stat __percpu *s;
  299. s = idr_find(&irqt_stats, irq);
  300. if (s) {
  301. free_percpu(s);
  302. idr_remove(&irqt_stats, irq);
  303. }
  304. }
  305. int irq_timings_alloc(int irq)
  306. {
  307. struct irqt_stat __percpu *s;
  308. int id;
  309. /*
  310. * Some platforms can have the same private interrupt per cpu,
  311. * so this function may be be called several times with the
  312. * same interrupt number. Just bail out in case the per cpu
  313. * stat structure is already allocated.
  314. */
  315. s = idr_find(&irqt_stats, irq);
  316. if (s)
  317. return 0;
  318. s = alloc_percpu(*s);
  319. if (!s)
  320. return -ENOMEM;
  321. idr_preload(GFP_KERNEL);
  322. id = idr_alloc(&irqt_stats, s, irq, irq + 1, GFP_NOWAIT);
  323. idr_preload_end();
  324. if (id < 0) {
  325. free_percpu(s);
  326. return id;
  327. }
  328. return 0;
  329. }