trace_benchmark.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/delay.h>
  3. #include <linux/module.h>
  4. #include <linux/kthread.h>
  5. #include <linux/trace_clock.h>
  6. #define CREATE_TRACE_POINTS
  7. #include "trace_benchmark.h"
  8. static struct task_struct *bm_event_thread;
  9. static char bm_str[BENCHMARK_EVENT_STRLEN] = "START";
  10. static u64 bm_total;
  11. static u64 bm_totalsq;
  12. static u64 bm_last;
  13. static u64 bm_max;
  14. static u64 bm_min;
  15. static u64 bm_first;
  16. static u64 bm_cnt;
  17. static u64 bm_stddev;
  18. static unsigned int bm_avg;
  19. static unsigned int bm_std;
  20. static bool ok_to_run;
  21. /*
  22. * This gets called in a loop recording the time it took to write
  23. * the tracepoint. What it writes is the time statistics of the last
  24. * tracepoint write. As there is nothing to write the first time
  25. * it simply writes "START". As the first write is cold cache and
  26. * the rest is hot, we save off that time in bm_first and it is
  27. * reported as "first", which is shown in the second write to the
  28. * tracepoint. The "first" field is writen within the statics from
  29. * then on but never changes.
  30. */
  31. static void trace_do_benchmark(void)
  32. {
  33. u64 start;
  34. u64 stop;
  35. u64 delta;
  36. u64 stddev;
  37. u64 seed;
  38. u64 last_seed;
  39. unsigned int avg;
  40. unsigned int std = 0;
  41. /* Only run if the tracepoint is actually active */
  42. if (!trace_benchmark_event_enabled() || !tracing_is_on())
  43. return;
  44. local_irq_disable();
  45. start = trace_clock_local();
  46. trace_benchmark_event(bm_str);
  47. stop = trace_clock_local();
  48. local_irq_enable();
  49. bm_cnt++;
  50. delta = stop - start;
  51. /*
  52. * The first read is cold cached, keep it separate from the
  53. * other calculations.
  54. */
  55. if (bm_cnt == 1) {
  56. bm_first = delta;
  57. scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
  58. "first=%llu [COLD CACHED]", bm_first);
  59. return;
  60. }
  61. bm_last = delta;
  62. if (delta > bm_max)
  63. bm_max = delta;
  64. if (!bm_min || delta < bm_min)
  65. bm_min = delta;
  66. /*
  67. * When bm_cnt is greater than UINT_MAX, it breaks the statistics
  68. * accounting. Freeze the statistics when that happens.
  69. * We should have enough data for the avg and stddev anyway.
  70. */
  71. if (bm_cnt > UINT_MAX) {
  72. scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
  73. "last=%llu first=%llu max=%llu min=%llu ** avg=%u std=%d std^2=%lld",
  74. bm_last, bm_first, bm_max, bm_min, bm_avg, bm_std, bm_stddev);
  75. return;
  76. }
  77. bm_total += delta;
  78. bm_totalsq += delta * delta;
  79. if (bm_cnt > 1) {
  80. /*
  81. * Apply Welford's method to calculate standard deviation:
  82. * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
  83. */
  84. stddev = (u64)bm_cnt * bm_totalsq - bm_total * bm_total;
  85. do_div(stddev, (u32)bm_cnt);
  86. do_div(stddev, (u32)bm_cnt - 1);
  87. } else
  88. stddev = 0;
  89. delta = bm_total;
  90. do_div(delta, bm_cnt);
  91. avg = delta;
  92. if (stddev > 0) {
  93. int i = 0;
  94. /*
  95. * stddev is the square of standard deviation but
  96. * we want the actualy number. Use the average
  97. * as our seed to find the std.
  98. *
  99. * The next try is:
  100. * x = (x + N/x) / 2
  101. *
  102. * Where N is the squared number to find the square
  103. * root of.
  104. */
  105. seed = avg;
  106. do {
  107. last_seed = seed;
  108. seed = stddev;
  109. if (!last_seed)
  110. break;
  111. do_div(seed, last_seed);
  112. seed += last_seed;
  113. do_div(seed, 2);
  114. } while (i++ < 10 && last_seed != seed);
  115. std = seed;
  116. }
  117. scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
  118. "last=%llu first=%llu max=%llu min=%llu avg=%u std=%d std^2=%lld",
  119. bm_last, bm_first, bm_max, bm_min, avg, std, stddev);
  120. bm_std = std;
  121. bm_avg = avg;
  122. bm_stddev = stddev;
  123. }
  124. static int benchmark_event_kthread(void *arg)
  125. {
  126. /* sleep a bit to make sure the tracepoint gets activated */
  127. msleep(100);
  128. while (!kthread_should_stop()) {
  129. trace_do_benchmark();
  130. /*
  131. * We don't go to sleep, but let others run as well.
  132. * This is bascially a "yield()" to let any task that
  133. * wants to run, schedule in, but if the CPU is idle,
  134. * we'll keep burning cycles.
  135. *
  136. * Note the tasks_rcu_qs() version of cond_resched() will
  137. * notify synchronize_rcu_tasks() that this thread has
  138. * passed a quiescent state for rcu_tasks. Otherwise
  139. * this thread will never voluntarily schedule which would
  140. * block synchronize_rcu_tasks() indefinitely.
  141. */
  142. cond_resched_tasks_rcu_qs();
  143. }
  144. return 0;
  145. }
  146. /*
  147. * When the benchmark tracepoint is enabled, it calls this
  148. * function and the thread that calls the tracepoint is created.
  149. */
  150. int trace_benchmark_reg(void)
  151. {
  152. if (!ok_to_run) {
  153. pr_warning("trace benchmark cannot be started via kernel command line\n");
  154. return -EBUSY;
  155. }
  156. bm_event_thread = kthread_run(benchmark_event_kthread,
  157. NULL, "event_benchmark");
  158. if (IS_ERR(bm_event_thread)) {
  159. pr_warning("trace benchmark failed to create kernel thread\n");
  160. return PTR_ERR(bm_event_thread);
  161. }
  162. return 0;
  163. }
  164. /*
  165. * When the benchmark tracepoint is disabled, it calls this
  166. * function and the thread that calls the tracepoint is deleted
  167. * and all the numbers are reset.
  168. */
  169. void trace_benchmark_unreg(void)
  170. {
  171. if (!bm_event_thread)
  172. return;
  173. kthread_stop(bm_event_thread);
  174. bm_event_thread = NULL;
  175. strcpy(bm_str, "START");
  176. bm_total = 0;
  177. bm_totalsq = 0;
  178. bm_last = 0;
  179. bm_max = 0;
  180. bm_min = 0;
  181. bm_cnt = 0;
  182. /* These don't need to be reset but reset them anyway */
  183. bm_first = 0;
  184. bm_std = 0;
  185. bm_avg = 0;
  186. bm_stddev = 0;
  187. }
  188. static __init int ok_to_run_trace_benchmark(void)
  189. {
  190. ok_to_run = true;
  191. return 0;
  192. }
  193. early_initcall(ok_to_run_trace_benchmark);