xref: /openbmc/linux/kernel/trace/synth_event_gen_test.c (revision 9fe41efaca08416657efa8731c0d47ccb6a3f3eb)
1*9fe41efaSTom Zanussi // SPDX-License-Identifier: GPL-2.0
2*9fe41efaSTom Zanussi /*
3*9fe41efaSTom Zanussi  * Test module for in-kernel sythetic event creation and generation.
4*9fe41efaSTom Zanussi  *
5*9fe41efaSTom Zanussi  * Copyright (C) 2019 Tom Zanussi <zanussi@kernel.org>
6*9fe41efaSTom Zanussi  */
7*9fe41efaSTom Zanussi 
8*9fe41efaSTom Zanussi #include <linux/module.h>
9*9fe41efaSTom Zanussi #include <linux/trace_events.h>
10*9fe41efaSTom Zanussi 
11*9fe41efaSTom Zanussi /*
12*9fe41efaSTom Zanussi  * This module is a simple test of basic functionality for in-kernel
13*9fe41efaSTom Zanussi  * synthetic event creation and generation, the first and second tests
14*9fe41efaSTom Zanussi  * using synth_event_gen_cmd_start() and synth_event_add_field(), the
15*9fe41efaSTom Zanussi  * third uses synth_event_create() to do it all at once with a static
16*9fe41efaSTom Zanussi  * field array.
17*9fe41efaSTom Zanussi  *
18*9fe41efaSTom Zanussi  * Following that are a few examples using the created events to test
19*9fe41efaSTom Zanussi  * various ways of tracing a synthetic event.
20*9fe41efaSTom Zanussi  *
21*9fe41efaSTom Zanussi  * To test, select CONFIG_SYNTH_EVENT_GEN_TEST and build the module.
22*9fe41efaSTom Zanussi  * Then:
23*9fe41efaSTom Zanussi  *
24*9fe41efaSTom Zanussi  * # insmod kernel/trace/synth_event_gen_test.ko
25*9fe41efaSTom Zanussi  * # cat /sys/kernel/debug/tracing/trace
26*9fe41efaSTom Zanussi  *
27*9fe41efaSTom Zanussi  * You should see several events in the trace buffer -
28*9fe41efaSTom Zanussi  * "create_synth_test", "empty_synth_test", and several instances of
29*9fe41efaSTom Zanussi  * "gen_synth_test".
30*9fe41efaSTom Zanussi  *
31*9fe41efaSTom Zanussi  * To remove the events, remove the module:
32*9fe41efaSTom Zanussi  *
33*9fe41efaSTom Zanussi  * # rmmod synth_event_gen_test
34*9fe41efaSTom Zanussi  *
35*9fe41efaSTom Zanussi  */
36*9fe41efaSTom Zanussi 
37*9fe41efaSTom Zanussi static struct trace_event_file *create_synth_test;
38*9fe41efaSTom Zanussi static struct trace_event_file *empty_synth_test;
39*9fe41efaSTom Zanussi static struct trace_event_file *gen_synth_test;
40*9fe41efaSTom Zanussi 
41*9fe41efaSTom Zanussi /*
42*9fe41efaSTom Zanussi  * Test to make sure we can create a synthetic event, then add more
43*9fe41efaSTom Zanussi  * fields.
44*9fe41efaSTom Zanussi  */
45*9fe41efaSTom Zanussi static int __init test_gen_synth_cmd(void)
46*9fe41efaSTom Zanussi {
47*9fe41efaSTom Zanussi 	struct dynevent_cmd cmd;
48*9fe41efaSTom Zanussi 	u64 vals[7];
49*9fe41efaSTom Zanussi 	char *buf;
50*9fe41efaSTom Zanussi 	int ret;
51*9fe41efaSTom Zanussi 
52*9fe41efaSTom Zanussi 	/* Create a buffer to hold the generated command */
53*9fe41efaSTom Zanussi 	buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
54*9fe41efaSTom Zanussi 	if (!buf)
55*9fe41efaSTom Zanussi 		return -ENOMEM;
56*9fe41efaSTom Zanussi 
57*9fe41efaSTom Zanussi 	/* Before generating the command, initialize the cmd object */
58*9fe41efaSTom Zanussi 	synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
59*9fe41efaSTom Zanussi 
60*9fe41efaSTom Zanussi 	/*
61*9fe41efaSTom Zanussi 	 * Create the empty gen_synth_test synthetic event with the
62*9fe41efaSTom Zanussi 	 * first 4 fields.
63*9fe41efaSTom Zanussi 	 */
64*9fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_start(&cmd, "gen_synth_test", THIS_MODULE,
65*9fe41efaSTom Zanussi 					"pid_t", "next_pid_field",
66*9fe41efaSTom Zanussi 					"char[16]", "next_comm_field",
67*9fe41efaSTom Zanussi 					"u64", "ts_ns",
68*9fe41efaSTom Zanussi 					"u64", "ts_ms");
69*9fe41efaSTom Zanussi 	if (ret)
70*9fe41efaSTom Zanussi 		goto free;
71*9fe41efaSTom Zanussi 
72*9fe41efaSTom Zanussi 	/* Use synth_event_add_field to add the rest of the fields */
73*9fe41efaSTom Zanussi 
74*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "unsigned int", "cpu");
75*9fe41efaSTom Zanussi 	if (ret)
76*9fe41efaSTom Zanussi 		goto free;
77*9fe41efaSTom Zanussi 
78*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "char[64]", "my_string_field");
79*9fe41efaSTom Zanussi 	if (ret)
80*9fe41efaSTom Zanussi 		goto free;
81*9fe41efaSTom Zanussi 
82*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "int", "my_int_field");
83*9fe41efaSTom Zanussi 	if (ret)
84*9fe41efaSTom Zanussi 		goto free;
85*9fe41efaSTom Zanussi 
86*9fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_end(&cmd);
87*9fe41efaSTom Zanussi 	if (ret)
88*9fe41efaSTom Zanussi 		goto free;
89*9fe41efaSTom Zanussi 
90*9fe41efaSTom Zanussi 	/*
91*9fe41efaSTom Zanussi 	 * Now get the gen_synth_test event file.  We need to prevent
92*9fe41efaSTom Zanussi 	 * the instance and event from disappearing from underneath
93*9fe41efaSTom Zanussi 	 * us, which trace_get_event_file() does (though in this case
94*9fe41efaSTom Zanussi 	 * we're using the top-level instance which never goes away).
95*9fe41efaSTom Zanussi 	 */
96*9fe41efaSTom Zanussi 	gen_synth_test = trace_get_event_file(NULL, "synthetic",
97*9fe41efaSTom Zanussi 					      "gen_synth_test");
98*9fe41efaSTom Zanussi 	if (IS_ERR(gen_synth_test)) {
99*9fe41efaSTom Zanussi 		ret = PTR_ERR(gen_synth_test);
100*9fe41efaSTom Zanussi 		goto delete;
101*9fe41efaSTom Zanussi 	}
102*9fe41efaSTom Zanussi 
103*9fe41efaSTom Zanussi 	/* Enable the event or you won't see anything */
104*9fe41efaSTom Zanussi 	ret = trace_array_set_clr_event(gen_synth_test->tr,
105*9fe41efaSTom Zanussi 					"synthetic", "gen_synth_test", true);
106*9fe41efaSTom Zanussi 	if (ret) {
107*9fe41efaSTom Zanussi 		trace_put_event_file(gen_synth_test);
108*9fe41efaSTom Zanussi 		goto delete;
109*9fe41efaSTom Zanussi 	}
110*9fe41efaSTom Zanussi 
111*9fe41efaSTom Zanussi 	/* Create some bogus values just for testing */
112*9fe41efaSTom Zanussi 
113*9fe41efaSTom Zanussi 	vals[0] = 777;			/* next_pid_field */
114*9fe41efaSTom Zanussi 	vals[1] = (u64)"hula hoops";	/* next_comm_field */
115*9fe41efaSTom Zanussi 	vals[2] = 1000000;		/* ts_ns */
116*9fe41efaSTom Zanussi 	vals[3] = 1000;			/* ts_ms */
117*9fe41efaSTom Zanussi 	vals[4] = smp_processor_id();	/* cpu */
118*9fe41efaSTom Zanussi 	vals[5] = (u64)"thneed";	/* my_string_field */
119*9fe41efaSTom Zanussi 	vals[6] = 598;			/* my_int_field */
120*9fe41efaSTom Zanussi 
121*9fe41efaSTom Zanussi 	/* Now generate a gen_synth_test event */
122*9fe41efaSTom Zanussi 	ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals));
123*9fe41efaSTom Zanussi  out:
124*9fe41efaSTom Zanussi 	return ret;
125*9fe41efaSTom Zanussi  delete:
126*9fe41efaSTom Zanussi 	/* We got an error after creating the event, delete it */
127*9fe41efaSTom Zanussi 	synth_event_delete("gen_synth_test");
128*9fe41efaSTom Zanussi  free:
129*9fe41efaSTom Zanussi 	kfree(buf);
130*9fe41efaSTom Zanussi 
131*9fe41efaSTom Zanussi 	goto out;
132*9fe41efaSTom Zanussi }
133*9fe41efaSTom Zanussi 
134*9fe41efaSTom Zanussi /*
135*9fe41efaSTom Zanussi  * Test to make sure we can create an initially empty synthetic event,
136*9fe41efaSTom Zanussi  * then add all the fields.
137*9fe41efaSTom Zanussi  */
138*9fe41efaSTom Zanussi static int __init test_empty_synth_event(void)
139*9fe41efaSTom Zanussi {
140*9fe41efaSTom Zanussi 	struct dynevent_cmd cmd;
141*9fe41efaSTom Zanussi 	u64 vals[7];
142*9fe41efaSTom Zanussi 	char *buf;
143*9fe41efaSTom Zanussi 	int ret;
144*9fe41efaSTom Zanussi 
145*9fe41efaSTom Zanussi 	/* Create a buffer to hold the generated command */
146*9fe41efaSTom Zanussi 	buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
147*9fe41efaSTom Zanussi 	if (!buf)
148*9fe41efaSTom Zanussi 		return -ENOMEM;
149*9fe41efaSTom Zanussi 
150*9fe41efaSTom Zanussi 	/* Before generating the command, initialize the cmd object */
151*9fe41efaSTom Zanussi 	synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
152*9fe41efaSTom Zanussi 
153*9fe41efaSTom Zanussi 	/*
154*9fe41efaSTom Zanussi 	 * Create the empty_synth_test synthetic event with no fields.
155*9fe41efaSTom Zanussi 	 */
156*9fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_start(&cmd, "empty_synth_test", THIS_MODULE);
157*9fe41efaSTom Zanussi 	if (ret)
158*9fe41efaSTom Zanussi 		goto free;
159*9fe41efaSTom Zanussi 
160*9fe41efaSTom Zanussi 	/* Use synth_event_add_field to add all of the fields */
161*9fe41efaSTom Zanussi 
162*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "pid_t", "next_pid_field");
163*9fe41efaSTom Zanussi 	if (ret)
164*9fe41efaSTom Zanussi 		goto free;
165*9fe41efaSTom Zanussi 
166*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "char[16]", "next_comm_field");
167*9fe41efaSTom Zanussi 	if (ret)
168*9fe41efaSTom Zanussi 		goto free;
169*9fe41efaSTom Zanussi 
170*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "u64", "ts_ns");
171*9fe41efaSTom Zanussi 	if (ret)
172*9fe41efaSTom Zanussi 		goto free;
173*9fe41efaSTom Zanussi 
174*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "u64", "ts_ms");
175*9fe41efaSTom Zanussi 	if (ret)
176*9fe41efaSTom Zanussi 		goto free;
177*9fe41efaSTom Zanussi 
178*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "unsigned int", "cpu");
179*9fe41efaSTom Zanussi 	if (ret)
180*9fe41efaSTom Zanussi 		goto free;
181*9fe41efaSTom Zanussi 
182*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "char[64]", "my_string_field");
183*9fe41efaSTom Zanussi 	if (ret)
184*9fe41efaSTom Zanussi 		goto free;
185*9fe41efaSTom Zanussi 
186*9fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "int", "my_int_field");
187*9fe41efaSTom Zanussi 	if (ret)
188*9fe41efaSTom Zanussi 		goto free;
189*9fe41efaSTom Zanussi 
190*9fe41efaSTom Zanussi 	/* All fields have been added, close and register the synth event */
191*9fe41efaSTom Zanussi 
192*9fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_end(&cmd);
193*9fe41efaSTom Zanussi 	if (ret)
194*9fe41efaSTom Zanussi 		goto free;
195*9fe41efaSTom Zanussi 
196*9fe41efaSTom Zanussi 	/*
197*9fe41efaSTom Zanussi 	 * Now get the empty_synth_test event file.  We need to
198*9fe41efaSTom Zanussi 	 * prevent the instance and event from disappearing from
199*9fe41efaSTom Zanussi 	 * underneath us, which trace_get_event_file() does (though in
200*9fe41efaSTom Zanussi 	 * this case we're using the top-level instance which never
201*9fe41efaSTom Zanussi 	 * goes away).
202*9fe41efaSTom Zanussi 	 */
203*9fe41efaSTom Zanussi 	empty_synth_test = trace_get_event_file(NULL, "synthetic",
204*9fe41efaSTom Zanussi 						"empty_synth_test");
205*9fe41efaSTom Zanussi 	if (IS_ERR(empty_synth_test)) {
206*9fe41efaSTom Zanussi 		ret = PTR_ERR(empty_synth_test);
207*9fe41efaSTom Zanussi 		goto delete;
208*9fe41efaSTom Zanussi 	}
209*9fe41efaSTom Zanussi 
210*9fe41efaSTom Zanussi 	/* Enable the event or you won't see anything */
211*9fe41efaSTom Zanussi 	ret = trace_array_set_clr_event(empty_synth_test->tr,
212*9fe41efaSTom Zanussi 					"synthetic", "empty_synth_test", true);
213*9fe41efaSTom Zanussi 	if (ret) {
214*9fe41efaSTom Zanussi 		trace_put_event_file(empty_synth_test);
215*9fe41efaSTom Zanussi 		goto delete;
216*9fe41efaSTom Zanussi 	}
217*9fe41efaSTom Zanussi 
218*9fe41efaSTom Zanussi 	/* Create some bogus values just for testing */
219*9fe41efaSTom Zanussi 
220*9fe41efaSTom Zanussi 	vals[0] = 777;			/* next_pid_field */
221*9fe41efaSTom Zanussi 	vals[1] = (u64)"tiddlywinks";	/* next_comm_field */
222*9fe41efaSTom Zanussi 	vals[2] = 1000000;		/* ts_ns */
223*9fe41efaSTom Zanussi 	vals[3] = 1000;			/* ts_ms */
224*9fe41efaSTom Zanussi 	vals[4] = smp_processor_id();	/* cpu */
225*9fe41efaSTom Zanussi 	vals[5] = (u64)"thneed_2.0";	/* my_string_field */
226*9fe41efaSTom Zanussi 	vals[6] = 399;			/* my_int_field */
227*9fe41efaSTom Zanussi 
228*9fe41efaSTom Zanussi 	/* Now trace an empty_synth_test event */
229*9fe41efaSTom Zanussi 	ret = synth_event_trace_array(empty_synth_test, vals, ARRAY_SIZE(vals));
230*9fe41efaSTom Zanussi  out:
231*9fe41efaSTom Zanussi 	return ret;
232*9fe41efaSTom Zanussi  delete:
233*9fe41efaSTom Zanussi 	/* We got an error after creating the event, delete it */
234*9fe41efaSTom Zanussi 	synth_event_delete("empty_synth_test");
235*9fe41efaSTom Zanussi  free:
236*9fe41efaSTom Zanussi 	kfree(buf);
237*9fe41efaSTom Zanussi 
238*9fe41efaSTom Zanussi 	goto out;
239*9fe41efaSTom Zanussi }
240*9fe41efaSTom Zanussi 
241*9fe41efaSTom Zanussi static struct synth_field_desc create_synth_test_fields[] = {
242*9fe41efaSTom Zanussi 	{ .type = "pid_t",		.name = "next_pid_field" },
243*9fe41efaSTom Zanussi 	{ .type = "char[16]",		.name = "next_comm_field" },
244*9fe41efaSTom Zanussi 	{ .type = "u64",		.name = "ts_ns" },
245*9fe41efaSTom Zanussi 	{ .type = "u64",		.name = "ts_ms" },
246*9fe41efaSTom Zanussi 	{ .type = "unsigned int",	.name = "cpu" },
247*9fe41efaSTom Zanussi 	{ .type = "char[64]",		.name = "my_string_field" },
248*9fe41efaSTom Zanussi 	{ .type = "int",		.name = "my_int_field" },
249*9fe41efaSTom Zanussi };
250*9fe41efaSTom Zanussi 
251*9fe41efaSTom Zanussi /*
252*9fe41efaSTom Zanussi  * Test synthetic event creation all at once from array of field
253*9fe41efaSTom Zanussi  * descriptors.
254*9fe41efaSTom Zanussi  */
255*9fe41efaSTom Zanussi static int __init test_create_synth_event(void)
256*9fe41efaSTom Zanussi {
257*9fe41efaSTom Zanussi 	u64 vals[7];
258*9fe41efaSTom Zanussi 	int ret;
259*9fe41efaSTom Zanussi 
260*9fe41efaSTom Zanussi 	/* Create the create_synth_test event with the fields above */
261*9fe41efaSTom Zanussi 	ret = synth_event_create("create_synth_test",
262*9fe41efaSTom Zanussi 				 create_synth_test_fields,
263*9fe41efaSTom Zanussi 				 ARRAY_SIZE(create_synth_test_fields),
264*9fe41efaSTom Zanussi 				 THIS_MODULE);
265*9fe41efaSTom Zanussi 	if (ret)
266*9fe41efaSTom Zanussi 		goto out;
267*9fe41efaSTom Zanussi 
268*9fe41efaSTom Zanussi 	/*
269*9fe41efaSTom Zanussi 	 * Now get the create_synth_test event file.  We need to
270*9fe41efaSTom Zanussi 	 * prevent the instance and event from disappearing from
271*9fe41efaSTom Zanussi 	 * underneath us, which trace_get_event_file() does (though in
272*9fe41efaSTom Zanussi 	 * this case we're using the top-level instance which never
273*9fe41efaSTom Zanussi 	 * goes away).
274*9fe41efaSTom Zanussi 	 */
275*9fe41efaSTom Zanussi 	create_synth_test = trace_get_event_file(NULL, "synthetic",
276*9fe41efaSTom Zanussi 						 "create_synth_test");
277*9fe41efaSTom Zanussi 	if (IS_ERR(create_synth_test)) {
278*9fe41efaSTom Zanussi 		ret = PTR_ERR(create_synth_test);
279*9fe41efaSTom Zanussi 		goto delete;
280*9fe41efaSTom Zanussi 	}
281*9fe41efaSTom Zanussi 
282*9fe41efaSTom Zanussi 	/* Enable the event or you won't see anything */
283*9fe41efaSTom Zanussi 	ret = trace_array_set_clr_event(create_synth_test->tr,
284*9fe41efaSTom Zanussi 					"synthetic", "create_synth_test", true);
285*9fe41efaSTom Zanussi 	if (ret) {
286*9fe41efaSTom Zanussi 		trace_put_event_file(create_synth_test);
287*9fe41efaSTom Zanussi 		goto delete;
288*9fe41efaSTom Zanussi 	}
289*9fe41efaSTom Zanussi 
290*9fe41efaSTom Zanussi 	/* Create some bogus values just for testing */
291*9fe41efaSTom Zanussi 
292*9fe41efaSTom Zanussi 	vals[0] = 777;			/* next_pid_field */
293*9fe41efaSTom Zanussi 	vals[1] = (u64)"tiddlywinks";	/* next_comm_field */
294*9fe41efaSTom Zanussi 	vals[2] = 1000000;		/* ts_ns */
295*9fe41efaSTom Zanussi 	vals[3] = 1000;			/* ts_ms */
296*9fe41efaSTom Zanussi 	vals[4] = smp_processor_id();	/* cpu */
297*9fe41efaSTom Zanussi 	vals[5] = (u64)"thneed";	/* my_string_field */
298*9fe41efaSTom Zanussi 	vals[6] = 398;			/* my_int_field */
299*9fe41efaSTom Zanussi 
300*9fe41efaSTom Zanussi 	/* Now generate a create_synth_test event */
301*9fe41efaSTom Zanussi 	ret = synth_event_trace_array(create_synth_test, vals, ARRAY_SIZE(vals));
302*9fe41efaSTom Zanussi  out:
303*9fe41efaSTom Zanussi 	return ret;
304*9fe41efaSTom Zanussi  delete:
305*9fe41efaSTom Zanussi 	/* We got an error after creating the event, delete it */
306*9fe41efaSTom Zanussi 	ret = synth_event_delete("create_synth_test");
307*9fe41efaSTom Zanussi 
308*9fe41efaSTom Zanussi 	goto out;
309*9fe41efaSTom Zanussi }
310*9fe41efaSTom Zanussi 
311*9fe41efaSTom Zanussi /*
312*9fe41efaSTom Zanussi  * Test tracing a synthetic event by reserving trace buffer space,
313*9fe41efaSTom Zanussi  * then filling in fields one after another.
314*9fe41efaSTom Zanussi  */
315*9fe41efaSTom Zanussi static int __init test_add_next_synth_val(void)
316*9fe41efaSTom Zanussi {
317*9fe41efaSTom Zanussi 	struct synth_event_trace_state trace_state;
318*9fe41efaSTom Zanussi 	int ret;
319*9fe41efaSTom Zanussi 
320*9fe41efaSTom Zanussi 	/* Start by reserving space in the trace buffer */
321*9fe41efaSTom Zanussi 	ret = synth_event_trace_start(gen_synth_test, &trace_state);
322*9fe41efaSTom Zanussi 	if (ret)
323*9fe41efaSTom Zanussi 		return ret;
324*9fe41efaSTom Zanussi 
325*9fe41efaSTom Zanussi 	/* Write some bogus values into the trace buffer, one after another */
326*9fe41efaSTom Zanussi 
327*9fe41efaSTom Zanussi 	/* next_pid_field */
328*9fe41efaSTom Zanussi 	ret = synth_event_add_next_val(777, &trace_state);
329*9fe41efaSTom Zanussi 	if (ret)
330*9fe41efaSTom Zanussi 		goto out;
331*9fe41efaSTom Zanussi 
332*9fe41efaSTom Zanussi 	/* next_comm_field */
333*9fe41efaSTom Zanussi 	ret = synth_event_add_next_val((u64)"slinky", &trace_state);
334*9fe41efaSTom Zanussi 	if (ret)
335*9fe41efaSTom Zanussi 		goto out;
336*9fe41efaSTom Zanussi 
337*9fe41efaSTom Zanussi 	/* ts_ns */
338*9fe41efaSTom Zanussi 	ret = synth_event_add_next_val(1000000, &trace_state);
339*9fe41efaSTom Zanussi 	if (ret)
340*9fe41efaSTom Zanussi 		goto out;
341*9fe41efaSTom Zanussi 
342*9fe41efaSTom Zanussi 	/* ts_ms */
343*9fe41efaSTom Zanussi 	ret = synth_event_add_next_val(1000, &trace_state);
344*9fe41efaSTom Zanussi 	if (ret)
345*9fe41efaSTom Zanussi 		goto out;
346*9fe41efaSTom Zanussi 
347*9fe41efaSTom Zanussi 	/* cpu */
348*9fe41efaSTom Zanussi 	ret = synth_event_add_next_val(smp_processor_id(), &trace_state);
349*9fe41efaSTom Zanussi 	if (ret)
350*9fe41efaSTom Zanussi 		goto out;
351*9fe41efaSTom Zanussi 
352*9fe41efaSTom Zanussi 	/* my_string_field */
353*9fe41efaSTom Zanussi 	ret = synth_event_add_next_val((u64)"thneed_2.01", &trace_state);
354*9fe41efaSTom Zanussi 	if (ret)
355*9fe41efaSTom Zanussi 		goto out;
356*9fe41efaSTom Zanussi 
357*9fe41efaSTom Zanussi 	/* my_int_field */
358*9fe41efaSTom Zanussi 	ret = synth_event_add_next_val(395, &trace_state);
359*9fe41efaSTom Zanussi  out:
360*9fe41efaSTom Zanussi 	/* Finally, commit the event */
361*9fe41efaSTom Zanussi 	ret = synth_event_trace_end(&trace_state);
362*9fe41efaSTom Zanussi 
363*9fe41efaSTom Zanussi 	return ret;
364*9fe41efaSTom Zanussi }
365*9fe41efaSTom Zanussi 
366*9fe41efaSTom Zanussi /*
367*9fe41efaSTom Zanussi  * Test tracing a synthetic event by reserving trace buffer space,
368*9fe41efaSTom Zanussi  * then filling in fields using field names, which can be done in any
369*9fe41efaSTom Zanussi  * order.
370*9fe41efaSTom Zanussi  */
371*9fe41efaSTom Zanussi static int __init test_add_synth_val(void)
372*9fe41efaSTom Zanussi {
373*9fe41efaSTom Zanussi 	struct synth_event_trace_state trace_state;
374*9fe41efaSTom Zanussi 	int ret;
375*9fe41efaSTom Zanussi 
376*9fe41efaSTom Zanussi 	/* Start by reserving space in the trace buffer */
377*9fe41efaSTom Zanussi 	ret = synth_event_trace_start(gen_synth_test, &trace_state);
378*9fe41efaSTom Zanussi 	if (ret)
379*9fe41efaSTom Zanussi 		return ret;
380*9fe41efaSTom Zanussi 
381*9fe41efaSTom Zanussi 	/* Write some bogus values into the trace buffer, using field names */
382*9fe41efaSTom Zanussi 
383*9fe41efaSTom Zanussi 	ret = synth_event_add_val("ts_ns", 1000000, &trace_state);
384*9fe41efaSTom Zanussi 	if (ret)
385*9fe41efaSTom Zanussi 		goto out;
386*9fe41efaSTom Zanussi 
387*9fe41efaSTom Zanussi 	ret = synth_event_add_val("ts_ms", 1000, &trace_state);
388*9fe41efaSTom Zanussi 	if (ret)
389*9fe41efaSTom Zanussi 		goto out;
390*9fe41efaSTom Zanussi 
391*9fe41efaSTom Zanussi 	ret = synth_event_add_val("cpu", smp_processor_id(), &trace_state);
392*9fe41efaSTom Zanussi 	if (ret)
393*9fe41efaSTom Zanussi 		goto out;
394*9fe41efaSTom Zanussi 
395*9fe41efaSTom Zanussi 	ret = synth_event_add_val("next_pid_field", 777, &trace_state);
396*9fe41efaSTom Zanussi 	if (ret)
397*9fe41efaSTom Zanussi 		goto out;
398*9fe41efaSTom Zanussi 
399*9fe41efaSTom Zanussi 	ret = synth_event_add_val("next_comm_field", (u64)"silly putty",
400*9fe41efaSTom Zanussi 				  &trace_state);
401*9fe41efaSTom Zanussi 	if (ret)
402*9fe41efaSTom Zanussi 		goto out;
403*9fe41efaSTom Zanussi 
404*9fe41efaSTom Zanussi 	ret = synth_event_add_val("my_string_field", (u64)"thneed_9",
405*9fe41efaSTom Zanussi 				  &trace_state);
406*9fe41efaSTom Zanussi 	if (ret)
407*9fe41efaSTom Zanussi 		goto out;
408*9fe41efaSTom Zanussi 
409*9fe41efaSTom Zanussi 	ret = synth_event_add_val("my_int_field", 3999, &trace_state);
410*9fe41efaSTom Zanussi  out:
411*9fe41efaSTom Zanussi 	/* Finally, commit the event */
412*9fe41efaSTom Zanussi 	ret = synth_event_trace_end(&trace_state);
413*9fe41efaSTom Zanussi 
414*9fe41efaSTom Zanussi 	return ret;
415*9fe41efaSTom Zanussi }
416*9fe41efaSTom Zanussi 
417*9fe41efaSTom Zanussi /*
418*9fe41efaSTom Zanussi  * Test tracing a synthetic event all at once from array of values.
419*9fe41efaSTom Zanussi  */
420*9fe41efaSTom Zanussi static int __init test_trace_synth_event(void)
421*9fe41efaSTom Zanussi {
422*9fe41efaSTom Zanussi 	int ret;
423*9fe41efaSTom Zanussi 
424*9fe41efaSTom Zanussi 	/* Trace some bogus values just for testing */
425*9fe41efaSTom Zanussi 	ret = synth_event_trace(create_synth_test, 7,	/* number of values */
426*9fe41efaSTom Zanussi 				444,			/* next_pid_field */
427*9fe41efaSTom Zanussi 				(u64)"clackers",	/* next_comm_field */
428*9fe41efaSTom Zanussi 				1000000,		/* ts_ns */
429*9fe41efaSTom Zanussi 				1000,			/* ts_ms */
430*9fe41efaSTom Zanussi 				smp_processor_id(),	/* cpu */
431*9fe41efaSTom Zanussi 				(u64)"Thneed",		/* my_string_field */
432*9fe41efaSTom Zanussi 				999);			/* my_int_field */
433*9fe41efaSTom Zanussi 	return ret;
434*9fe41efaSTom Zanussi }
435*9fe41efaSTom Zanussi 
436*9fe41efaSTom Zanussi static int __init synth_event_gen_test_init(void)
437*9fe41efaSTom Zanussi {
438*9fe41efaSTom Zanussi 	int ret;
439*9fe41efaSTom Zanussi 
440*9fe41efaSTom Zanussi 	ret = test_gen_synth_cmd();
441*9fe41efaSTom Zanussi 	if (ret)
442*9fe41efaSTom Zanussi 		return ret;
443*9fe41efaSTom Zanussi 
444*9fe41efaSTom Zanussi 	ret = test_empty_synth_event();
445*9fe41efaSTom Zanussi 	if (ret) {
446*9fe41efaSTom Zanussi 		WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
447*9fe41efaSTom Zanussi 						  "synthetic",
448*9fe41efaSTom Zanussi 						  "gen_synth_test", false));
449*9fe41efaSTom Zanussi 		trace_put_event_file(gen_synth_test);
450*9fe41efaSTom Zanussi 		WARN_ON(synth_event_delete("gen_synth_test"));
451*9fe41efaSTom Zanussi 		goto out;
452*9fe41efaSTom Zanussi 	}
453*9fe41efaSTom Zanussi 
454*9fe41efaSTom Zanussi 	ret = test_create_synth_event();
455*9fe41efaSTom Zanussi 	if (ret) {
456*9fe41efaSTom Zanussi 		WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
457*9fe41efaSTom Zanussi 						  "synthetic",
458*9fe41efaSTom Zanussi 						  "gen_synth_test", false));
459*9fe41efaSTom Zanussi 		trace_put_event_file(gen_synth_test);
460*9fe41efaSTom Zanussi 		WARN_ON(synth_event_delete("gen_synth_test"));
461*9fe41efaSTom Zanussi 
462*9fe41efaSTom Zanussi 		WARN_ON(trace_array_set_clr_event(empty_synth_test->tr,
463*9fe41efaSTom Zanussi 						  "synthetic",
464*9fe41efaSTom Zanussi 						  "empty_synth_test", false));
465*9fe41efaSTom Zanussi 		trace_put_event_file(empty_synth_test);
466*9fe41efaSTom Zanussi 		WARN_ON(synth_event_delete("empty_synth_test"));
467*9fe41efaSTom Zanussi 		goto out;
468*9fe41efaSTom Zanussi 	}
469*9fe41efaSTom Zanussi 
470*9fe41efaSTom Zanussi 	ret = test_add_next_synth_val();
471*9fe41efaSTom Zanussi 	WARN_ON(ret);
472*9fe41efaSTom Zanussi 
473*9fe41efaSTom Zanussi 	ret = test_add_synth_val();
474*9fe41efaSTom Zanussi 	WARN_ON(ret);
475*9fe41efaSTom Zanussi 
476*9fe41efaSTom Zanussi 	ret = test_trace_synth_event();
477*9fe41efaSTom Zanussi 	WARN_ON(ret);
478*9fe41efaSTom Zanussi  out:
479*9fe41efaSTom Zanussi 	return ret;
480*9fe41efaSTom Zanussi }
481*9fe41efaSTom Zanussi 
482*9fe41efaSTom Zanussi static void __exit synth_event_gen_test_exit(void)
483*9fe41efaSTom Zanussi {
484*9fe41efaSTom Zanussi 	/* Disable the event or you can't remove it */
485*9fe41efaSTom Zanussi 	WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
486*9fe41efaSTom Zanussi 					  "synthetic",
487*9fe41efaSTom Zanussi 					  "gen_synth_test", false));
488*9fe41efaSTom Zanussi 
489*9fe41efaSTom Zanussi 	/* Now give the file and instance back */
490*9fe41efaSTom Zanussi 	trace_put_event_file(gen_synth_test);
491*9fe41efaSTom Zanussi 
492*9fe41efaSTom Zanussi 	/* Now unregister and free the synthetic event */
493*9fe41efaSTom Zanussi 	WARN_ON(synth_event_delete("gen_synth_test"));
494*9fe41efaSTom Zanussi 
495*9fe41efaSTom Zanussi 	/* Disable the event or you can't remove it */
496*9fe41efaSTom Zanussi 	WARN_ON(trace_array_set_clr_event(empty_synth_test->tr,
497*9fe41efaSTom Zanussi 					  "synthetic",
498*9fe41efaSTom Zanussi 					  "empty_synth_test", false));
499*9fe41efaSTom Zanussi 
500*9fe41efaSTom Zanussi 	/* Now give the file and instance back */
501*9fe41efaSTom Zanussi 	trace_put_event_file(empty_synth_test);
502*9fe41efaSTom Zanussi 
503*9fe41efaSTom Zanussi 	/* Now unregister and free the synthetic event */
504*9fe41efaSTom Zanussi 	WARN_ON(synth_event_delete("empty_synth_test"));
505*9fe41efaSTom Zanussi 
506*9fe41efaSTom Zanussi 	/* Disable the event or you can't remove it */
507*9fe41efaSTom Zanussi 	WARN_ON(trace_array_set_clr_event(create_synth_test->tr,
508*9fe41efaSTom Zanussi 					  "synthetic",
509*9fe41efaSTom Zanussi 					  "create_synth_test", false));
510*9fe41efaSTom Zanussi 
511*9fe41efaSTom Zanussi 	/* Now give the file and instance back */
512*9fe41efaSTom Zanussi 	trace_put_event_file(create_synth_test);
513*9fe41efaSTom Zanussi 
514*9fe41efaSTom Zanussi 	/* Now unregister and free the synthetic event */
515*9fe41efaSTom Zanussi 	WARN_ON(synth_event_delete("create_synth_test"));
516*9fe41efaSTom Zanussi }
517*9fe41efaSTom Zanussi 
518*9fe41efaSTom Zanussi module_init(synth_event_gen_test_init)
519*9fe41efaSTom Zanussi module_exit(synth_event_gen_test_exit)
520*9fe41efaSTom Zanussi 
521*9fe41efaSTom Zanussi MODULE_AUTHOR("Tom Zanussi");
522*9fe41efaSTom Zanussi MODULE_DESCRIPTION("synthetic event generation test");
523*9fe41efaSTom Zanussi MODULE_LICENSE("GPL v2");
524