xref: /linux-6.15/kernel/trace/trace_benchmark.c (revision 72e2fe38)
1 #include <linux/delay.h>
2 #include <linux/module.h>
3 #include <linux/kthread.h>
4 #include <linux/trace_clock.h>
5 
6 #define CREATE_TRACE_POINTS
7 #include "trace_benchmark.h"
8 
9 static struct task_struct *bm_event_thread;
10 
11 static char bm_str[BENCHMARK_EVENT_STRLEN] = "START";
12 
13 static u64 bm_total;
14 static u64 bm_totalsq;
15 static u64 bm_last;
16 static u64 bm_max;
17 static u64 bm_min;
18 static u64 bm_first;
19 static s64 bm_cnt;
20 
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 
42 	/* Only run if the tracepoint is actually active */
43 	if (!trace_benchmark_event_enabled())
44 		return;
45 
46 	local_irq_disable();
47 	start = trace_clock_local();
48 	trace_benchmark_event(bm_str);
49 	stop = trace_clock_local();
50 	local_irq_enable();
51 
52 	bm_cnt++;
53 
54 	delta = stop - start;
55 
56 	/*
57 	 * The first read is cold cached, keep it separate from the
58 	 * other calculations.
59 	 */
60 	if (bm_cnt == 1) {
61 		bm_first = delta;
62 		scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
63 			  "first=%llu [COLD CACHED]", bm_first);
64 		return;
65 	}
66 
67 	bm_last = delta;
68 
69 	bm_total += delta;
70 	bm_totalsq += delta * delta;
71 
72 	if (delta > bm_max)
73 		bm_max = delta;
74 	if (!bm_min || delta < bm_min)
75 		bm_min = delta;
76 
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, bm_cnt);
84 		do_div(stddev, bm_cnt - 1);
85 	} else
86 		stddev = 0;
87 
88 	delta = bm_total;
89 	do_div(delta, bm_cnt);
90 	avg = delta;
91 
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 
116 		std = seed;
117 	}
118 
119 	scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
120 		  "last=%llu first=%llu max=%llu min=%llu avg=%u std=%d std^2=%lld",
121 		  bm_last, bm_first, bm_max, bm_min, avg, std, stddev);
122 }
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 
129 	while (!kthread_should_stop()) {
130 
131 		trace_do_benchmark();
132 
133 		/*
134 		 * We don't go to sleep, but let others
135 		 * run as well.
136 		 */
137 		cond_resched();
138 	}
139 
140 	return 0;
141 }
142 
143 /*
144  * When the benchmark tracepoint is enabled, it calls this
145  * function and the thread that calls the tracepoint is created.
146  */
147 void trace_benchmark_reg(void)
148 {
149 	bm_event_thread = kthread_run(benchmark_event_kthread,
150 				      NULL, "event_benchmark");
151 	WARN_ON(!bm_event_thread);
152 }
153 
154 /*
155  * When the benchmark tracepoint is disabled, it calls this
156  * function and the thread that calls the tracepoint is deleted
157  * and all the numbers are reset.
158  */
159 void trace_benchmark_unreg(void)
160 {
161 	if (!bm_event_thread)
162 		return;
163 
164 	kthread_stop(bm_event_thread);
165 
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 	/* bm_first doesn't need to be reset but reset it anyway */
174 	bm_first = 0;
175 }
176