trace_benchmark.c 4.3 KB

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