xref: /linux-6.15/kernel/trace/trace_benchmark.c (revision 81dc9f0e)
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 	s64 stddev;
37 	u64 seed;
38 	u64 seedsq;
39 	u64 last_seed;
40 	unsigned int avg;
41 	unsigned int std = 0;
42 
43 	/* Only run if the tracepoint is actually active */
44 	if (!trace_benchmark_event_enabled())
45 		return;
46 
47 	local_irq_disable();
48 	start = trace_clock_local();
49 	trace_benchmark_event(bm_str);
50 	stop = trace_clock_local();
51 	local_irq_enable();
52 
53 	bm_cnt++;
54 
55 	delta = stop - start;
56 
57 	/*
58 	 * The first read is cold cached, keep it separate from the
59 	 * other calculations.
60 	 */
61 	if (bm_cnt == 1) {
62 		bm_first = delta;
63 		scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
64 			  "first=%llu [COLD CACHED]", bm_first);
65 		return;
66 	}
67 
68 	bm_last = delta;
69 
70 	bm_total += delta;
71 	bm_totalsq += delta * delta;
72 
73 	if (delta > bm_max)
74 		bm_max = delta;
75 	if (!bm_min || delta < bm_min)
76 		bm_min = delta;
77 
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, bm_cnt);
85 		do_div(stddev, bm_cnt - 1);
86 	} else
87 		stddev = 0;
88 
89 	delta = bm_total;
90 	do_div(delta, bm_cnt);
91 	avg = delta;
92 
93 	if (stddev > 0) {
94 		int i = 0;
95 		/*
96 		 * stddev is the square of standard deviation but
97 		 * we want the actualy number. Use the average
98 		 * as our seed to find the std.
99 		 *
100 		 * The next try is:
101 		 *  x = (x + N/x) / 2
102 		 *
103 		 * Where N is the squared number to find the square
104 		 * root of.
105 		 */
106 		seed = avg;
107 		do {
108 			last_seed = seed;
109 			seed = stddev;
110 			if (!last_seed)
111 				break;
112 			do_div(seed, last_seed);
113 			seed += last_seed;
114 			do_div(seed, 2);
115 		} while (i++ < 10 && last_seed != seed);
116 
117 		std = seed;
118 	}
119 
120 	scnprintf(bm_str, BENCHMARK_EVENT_STRLEN,
121 		  "last=%llu first=%llu max=%llu min=%llu avg=%u std=%d std^2=%lld",
122 		  bm_last, bm_first, bm_max, bm_min, avg, std, stddev);
123 }
124 
125 static int benchmark_event_kthread(void *arg)
126 {
127 	/* sleep a bit to make sure the tracepoint gets activated */
128 	msleep(100);
129 
130 	while (!kthread_should_stop()) {
131 
132 		trace_do_benchmark();
133 
134 		/*
135 		 * We don't go to sleep, but let others
136 		 * run as well.
137 		 */
138 		cond_resched();
139 	}
140 
141 	return 0;
142 }
143 
144 /*
145  * When the benchmark tracepoint is enabled, it calls this
146  * function and the thread that calls the tracepoint is created.
147  */
148 void trace_benchmark_reg(void)
149 {
150 	bm_event_thread = kthread_run(benchmark_event_kthread,
151 				      NULL, "event_benchmark");
152 	WARN_ON(!bm_event_thread);
153 }
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 
165 	kthread_stop(bm_event_thread);
166 
167 	strcpy(bm_str, "START");
168 	bm_total = 0;
169 	bm_totalsq = 0;
170 	bm_last = 0;
171 	bm_max = 0;
172 	bm_min = 0;
173 	bm_cnt = 0;
174 	/* bm_first doesn't need to be reset but reset it anyway */
175 	bm_first = 0;
176 }
177