trace_benchmark.c 4.7 KB

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