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>
7 #define CREATE_TRACE_POINTS
8 #include "trace_benchmark.h"
10 static struct task_struct *bm_event_thread;
12 static char bm_str[BENCHMARK_EVENT_STRLEN] = "START";
15 static u64 bm_totalsq;
22 static unsigned int bm_avg;
23 static unsigned int bm_std;
25 static bool ok_to_run;
28 * This gets called in a loop recording the time it took to write
29 * the tracepoint. What it writes is the time statistics of the last
30 * tracepoint write. As there is nothing to write the first time
31 * it simply writes "START". As the first write is cold cache and
32 * the rest is hot, we save off that time in bm_first and it is
33 * reported as "first", which is shown in the second write to the
34 * tracepoint. The "first" field is written within the statics from
35 * then on but never changes.
37 static void trace_do_benchmark(void)
48 /* Only run if the tracepoint is actually active */
49 if (!trace_benchmark_event_enabled() || !tracing_is_on())
53 start = trace_clock_local();
54 trace_benchmark_event(bm_str);
55 stop = trace_clock_local();
63 * The first read is cold cached, keep it separate from the
68 scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
69 "first=%llu [COLD CACHED]", bm_first);
77 if (!bm_min || delta < bm_min)
81 * When bm_cnt is greater than UINT_MAX, it breaks the statistics
82 * accounting. Freeze the statistics when that happens.
83 * We should have enough data for the avg and stddev anyway.
85 if (bm_cnt > UINT_MAX) {
86 scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
87 "last=%llu first=%llu max=%llu min=%llu ** avg=%u std=%d std^2=%lld",
88 bm_last, bm_first, bm_max, bm_min, bm_avg, bm_std, bm_stddev);
93 bm_totalsq += delta * delta;
98 * Apply Welford's method to calculate standard deviation:
99 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
101 stddev = (u64)bm_cnt * bm_totalsq - bm_total * bm_total;
102 do_div(stddev, (u32)bm_cnt);
103 do_div(stddev, (u32)bm_cnt - 1);
108 do_div(delta, bm_cnt);
114 * stddev is the square of standard deviation but
115 * we want the actually number. Use the average
116 * as our seed to find the std.
121 * Where N is the squared number to find the square
130 do_div(seed, last_seed);
133 } while (i++ < 10 && last_seed != seed);
138 scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
139 "last=%llu first=%llu max=%llu min=%llu avg=%u std=%d std^2=%lld",
140 bm_last, bm_first, bm_max, bm_min, avg, std, stddev);
147 static int benchmark_event_kthread(void *arg)
149 /* sleep a bit to make sure the tracepoint gets activated */
152 while (!kthread_should_stop()) {
154 trace_do_benchmark();
157 * We don't go to sleep, but let others run as well.
158 * This is basically a "yield()" to let any task that
159 * wants to run, schedule in, but if the CPU is idle,
160 * we'll keep burning cycles.
162 * Note the tasks_rcu_qs() version of cond_resched() will
163 * notify synchronize_rcu_tasks() that this thread has
164 * passed a quiescent state for rcu_tasks. Otherwise
165 * this thread will never voluntarily schedule which would
166 * block synchronize_rcu_tasks() indefinitely.
168 cond_resched_tasks_rcu_qs();
175 * When the benchmark tracepoint is enabled, it calls this
176 * function and the thread that calls the tracepoint is created.
178 int trace_benchmark_reg(void)
181 pr_warn("trace benchmark cannot be started via kernel command line\n");
185 bm_event_thread = kthread_run(benchmark_event_kthread,
186 NULL, "event_benchmark");
187 if (IS_ERR(bm_event_thread)) {
188 pr_warn("trace benchmark failed to create kernel thread\n");
189 return PTR_ERR(bm_event_thread);
196 * When the benchmark tracepoint is disabled, it calls this
197 * function and the thread that calls the tracepoint is deleted
198 * and all the numbers are reset.
200 void trace_benchmark_unreg(void)
202 if (!bm_event_thread)
205 kthread_stop(bm_event_thread);
206 bm_event_thread = NULL;
208 strcpy(bm_str, "START");
215 /* These don't need to be reset but reset them anyway */
222 static __init int ok_to_run_trace_benchmark(void)
229 early_initcall(ok_to_run_trace_benchmark);