xref: /openbmc/linux/kernel/trace/synth_event_gen_test.c (revision f2cc020d7876de7583feb52ec939a32419cf9468)
19fe41efaSTom Zanussi // SPDX-License-Identifier: GPL-2.0
29fe41efaSTom Zanussi /*
3*f2cc020dSIngo Molnar  * Test module for in-kernel synthetic event creation and generation.
49fe41efaSTom Zanussi  *
59fe41efaSTom Zanussi  * Copyright (C) 2019 Tom Zanussi <zanussi@kernel.org>
69fe41efaSTom Zanussi  */
79fe41efaSTom Zanussi 
89fe41efaSTom Zanussi #include <linux/module.h>
99fe41efaSTom Zanussi #include <linux/trace_events.h>
109fe41efaSTom Zanussi 
119fe41efaSTom Zanussi /*
129fe41efaSTom Zanussi  * This module is a simple test of basic functionality for in-kernel
139fe41efaSTom Zanussi  * synthetic event creation and generation, the first and second tests
149fe41efaSTom Zanussi  * using synth_event_gen_cmd_start() and synth_event_add_field(), the
159fe41efaSTom Zanussi  * third uses synth_event_create() to do it all at once with a static
169fe41efaSTom Zanussi  * field array.
179fe41efaSTom Zanussi  *
189fe41efaSTom Zanussi  * Following that are a few examples using the created events to test
199fe41efaSTom Zanussi  * various ways of tracing a synthetic event.
209fe41efaSTom Zanussi  *
219fe41efaSTom Zanussi  * To test, select CONFIG_SYNTH_EVENT_GEN_TEST and build the module.
229fe41efaSTom Zanussi  * Then:
239fe41efaSTom Zanussi  *
249fe41efaSTom Zanussi  * # insmod kernel/trace/synth_event_gen_test.ko
259fe41efaSTom Zanussi  * # cat /sys/kernel/debug/tracing/trace
269fe41efaSTom Zanussi  *
279fe41efaSTom Zanussi  * You should see several events in the trace buffer -
289fe41efaSTom Zanussi  * "create_synth_test", "empty_synth_test", and several instances of
299fe41efaSTom Zanussi  * "gen_synth_test".
309fe41efaSTom Zanussi  *
319fe41efaSTom Zanussi  * To remove the events, remove the module:
329fe41efaSTom Zanussi  *
339fe41efaSTom Zanussi  * # rmmod synth_event_gen_test
349fe41efaSTom Zanussi  *
359fe41efaSTom Zanussi  */
369fe41efaSTom Zanussi 
379fe41efaSTom Zanussi static struct trace_event_file *create_synth_test;
389fe41efaSTom Zanussi static struct trace_event_file *empty_synth_test;
399fe41efaSTom Zanussi static struct trace_event_file *gen_synth_test;
409fe41efaSTom Zanussi 
419fe41efaSTom Zanussi /*
429fe41efaSTom Zanussi  * Test to make sure we can create a synthetic event, then add more
439fe41efaSTom Zanussi  * fields.
449fe41efaSTom Zanussi  */
459fe41efaSTom Zanussi static int __init test_gen_synth_cmd(void)
469fe41efaSTom Zanussi {
479fe41efaSTom Zanussi 	struct dynevent_cmd cmd;
489fe41efaSTom Zanussi 	u64 vals[7];
499fe41efaSTom Zanussi 	char *buf;
509fe41efaSTom Zanussi 	int ret;
519fe41efaSTom Zanussi 
529fe41efaSTom Zanussi 	/* Create a buffer to hold the generated command */
539fe41efaSTom Zanussi 	buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
549fe41efaSTom Zanussi 	if (!buf)
559fe41efaSTom Zanussi 		return -ENOMEM;
569fe41efaSTom Zanussi 
579fe41efaSTom Zanussi 	/* Before generating the command, initialize the cmd object */
589fe41efaSTom Zanussi 	synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
599fe41efaSTom Zanussi 
609fe41efaSTom Zanussi 	/*
619fe41efaSTom Zanussi 	 * Create the empty gen_synth_test synthetic event with the
629fe41efaSTom Zanussi 	 * first 4 fields.
639fe41efaSTom Zanussi 	 */
649fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_start(&cmd, "gen_synth_test", THIS_MODULE,
659fe41efaSTom Zanussi 					"pid_t", "next_pid_field",
669fe41efaSTom Zanussi 					"char[16]", "next_comm_field",
679fe41efaSTom Zanussi 					"u64", "ts_ns",
689fe41efaSTom Zanussi 					"u64", "ts_ms");
699fe41efaSTom Zanussi 	if (ret)
709fe41efaSTom Zanussi 		goto free;
719fe41efaSTom Zanussi 
729fe41efaSTom Zanussi 	/* Use synth_event_add_field to add the rest of the fields */
739fe41efaSTom Zanussi 
749fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "unsigned int", "cpu");
759fe41efaSTom Zanussi 	if (ret)
769fe41efaSTom Zanussi 		goto free;
779fe41efaSTom Zanussi 
789fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "char[64]", "my_string_field");
799fe41efaSTom Zanussi 	if (ret)
809fe41efaSTom Zanussi 		goto free;
819fe41efaSTom Zanussi 
829fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "int", "my_int_field");
839fe41efaSTom Zanussi 	if (ret)
849fe41efaSTom Zanussi 		goto free;
859fe41efaSTom Zanussi 
869fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_end(&cmd);
879fe41efaSTom Zanussi 	if (ret)
889fe41efaSTom Zanussi 		goto free;
899fe41efaSTom Zanussi 
909fe41efaSTom Zanussi 	/*
919fe41efaSTom Zanussi 	 * Now get the gen_synth_test event file.  We need to prevent
929fe41efaSTom Zanussi 	 * the instance and event from disappearing from underneath
939fe41efaSTom Zanussi 	 * us, which trace_get_event_file() does (though in this case
949fe41efaSTom Zanussi 	 * we're using the top-level instance which never goes away).
959fe41efaSTom Zanussi 	 */
969fe41efaSTom Zanussi 	gen_synth_test = trace_get_event_file(NULL, "synthetic",
979fe41efaSTom Zanussi 					      "gen_synth_test");
989fe41efaSTom Zanussi 	if (IS_ERR(gen_synth_test)) {
999fe41efaSTom Zanussi 		ret = PTR_ERR(gen_synth_test);
1009fe41efaSTom Zanussi 		goto delete;
1019fe41efaSTom Zanussi 	}
1029fe41efaSTom Zanussi 
1039fe41efaSTom Zanussi 	/* Enable the event or you won't see anything */
1049fe41efaSTom Zanussi 	ret = trace_array_set_clr_event(gen_synth_test->tr,
1059fe41efaSTom Zanussi 					"synthetic", "gen_synth_test", true);
1069fe41efaSTom Zanussi 	if (ret) {
1079fe41efaSTom Zanussi 		trace_put_event_file(gen_synth_test);
1089fe41efaSTom Zanussi 		goto delete;
1099fe41efaSTom Zanussi 	}
1109fe41efaSTom Zanussi 
1119fe41efaSTom Zanussi 	/* Create some bogus values just for testing */
1129fe41efaSTom Zanussi 
1139fe41efaSTom Zanussi 	vals[0] = 777;			/* next_pid_field */
114279eef05STom Zanussi 	vals[1] = (u64)(long)"hula hoops";	/* next_comm_field */
1159fe41efaSTom Zanussi 	vals[2] = 1000000;		/* ts_ns */
1169fe41efaSTom Zanussi 	vals[3] = 1000;			/* ts_ms */
1173c18a9beSSteven Rostedt (VMware) 	vals[4] = raw_smp_processor_id(); /* cpu */
118279eef05STom Zanussi 	vals[5] = (u64)(long)"thneed";	/* my_string_field */
1199fe41efaSTom Zanussi 	vals[6] = 598;			/* my_int_field */
1209fe41efaSTom Zanussi 
1219fe41efaSTom Zanussi 	/* Now generate a gen_synth_test event */
1229fe41efaSTom Zanussi 	ret = synth_event_trace_array(gen_synth_test, vals, ARRAY_SIZE(vals));
1239fe41efaSTom Zanussi  out:
1249fe41efaSTom Zanussi 	return ret;
1259fe41efaSTom Zanussi  delete:
1269fe41efaSTom Zanussi 	/* We got an error after creating the event, delete it */
1279fe41efaSTom Zanussi 	synth_event_delete("gen_synth_test");
1289fe41efaSTom Zanussi  free:
1299fe41efaSTom Zanussi 	kfree(buf);
1309fe41efaSTom Zanussi 
1319fe41efaSTom Zanussi 	goto out;
1329fe41efaSTom Zanussi }
1339fe41efaSTom Zanussi 
1349fe41efaSTom Zanussi /*
1359fe41efaSTom Zanussi  * Test to make sure we can create an initially empty synthetic event,
1369fe41efaSTom Zanussi  * then add all the fields.
1379fe41efaSTom Zanussi  */
1389fe41efaSTom Zanussi static int __init test_empty_synth_event(void)
1399fe41efaSTom Zanussi {
1409fe41efaSTom Zanussi 	struct dynevent_cmd cmd;
1419fe41efaSTom Zanussi 	u64 vals[7];
1429fe41efaSTom Zanussi 	char *buf;
1439fe41efaSTom Zanussi 	int ret;
1449fe41efaSTom Zanussi 
1459fe41efaSTom Zanussi 	/* Create a buffer to hold the generated command */
1469fe41efaSTom Zanussi 	buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1479fe41efaSTom Zanussi 	if (!buf)
1489fe41efaSTom Zanussi 		return -ENOMEM;
1499fe41efaSTom Zanussi 
1509fe41efaSTom Zanussi 	/* Before generating the command, initialize the cmd object */
1519fe41efaSTom Zanussi 	synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1529fe41efaSTom Zanussi 
1539fe41efaSTom Zanussi 	/*
1549fe41efaSTom Zanussi 	 * Create the empty_synth_test synthetic event with no fields.
1559fe41efaSTom Zanussi 	 */
1569fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_start(&cmd, "empty_synth_test", THIS_MODULE);
1579fe41efaSTom Zanussi 	if (ret)
1589fe41efaSTom Zanussi 		goto free;
1599fe41efaSTom Zanussi 
1609fe41efaSTom Zanussi 	/* Use synth_event_add_field to add all of the fields */
1619fe41efaSTom Zanussi 
1629fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "pid_t", "next_pid_field");
1639fe41efaSTom Zanussi 	if (ret)
1649fe41efaSTom Zanussi 		goto free;
1659fe41efaSTom Zanussi 
1669fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "char[16]", "next_comm_field");
1679fe41efaSTom Zanussi 	if (ret)
1689fe41efaSTom Zanussi 		goto free;
1699fe41efaSTom Zanussi 
1709fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "u64", "ts_ns");
1719fe41efaSTom Zanussi 	if (ret)
1729fe41efaSTom Zanussi 		goto free;
1739fe41efaSTom Zanussi 
1749fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "u64", "ts_ms");
1759fe41efaSTom Zanussi 	if (ret)
1769fe41efaSTom Zanussi 		goto free;
1779fe41efaSTom Zanussi 
1789fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "unsigned int", "cpu");
1799fe41efaSTom Zanussi 	if (ret)
1809fe41efaSTom Zanussi 		goto free;
1819fe41efaSTom Zanussi 
1829fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "char[64]", "my_string_field");
1839fe41efaSTom Zanussi 	if (ret)
1849fe41efaSTom Zanussi 		goto free;
1859fe41efaSTom Zanussi 
1869fe41efaSTom Zanussi 	ret = synth_event_add_field(&cmd, "int", "my_int_field");
1879fe41efaSTom Zanussi 	if (ret)
1889fe41efaSTom Zanussi 		goto free;
1899fe41efaSTom Zanussi 
1909fe41efaSTom Zanussi 	/* All fields have been added, close and register the synth event */
1919fe41efaSTom Zanussi 
1929fe41efaSTom Zanussi 	ret = synth_event_gen_cmd_end(&cmd);
1939fe41efaSTom Zanussi 	if (ret)
1949fe41efaSTom Zanussi 		goto free;
1959fe41efaSTom Zanussi 
1969fe41efaSTom Zanussi 	/*
1979fe41efaSTom Zanussi 	 * Now get the empty_synth_test event file.  We need to
1989fe41efaSTom Zanussi 	 * prevent the instance and event from disappearing from
1999fe41efaSTom Zanussi 	 * underneath us, which trace_get_event_file() does (though in
2009fe41efaSTom Zanussi 	 * this case we're using the top-level instance which never
2019fe41efaSTom Zanussi 	 * goes away).
2029fe41efaSTom Zanussi 	 */
2039fe41efaSTom Zanussi 	empty_synth_test = trace_get_event_file(NULL, "synthetic",
2049fe41efaSTom Zanussi 						"empty_synth_test");
2059fe41efaSTom Zanussi 	if (IS_ERR(empty_synth_test)) {
2069fe41efaSTom Zanussi 		ret = PTR_ERR(empty_synth_test);
2079fe41efaSTom Zanussi 		goto delete;
2089fe41efaSTom Zanussi 	}
2099fe41efaSTom Zanussi 
2109fe41efaSTom Zanussi 	/* Enable the event or you won't see anything */
2119fe41efaSTom Zanussi 	ret = trace_array_set_clr_event(empty_synth_test->tr,
2129fe41efaSTom Zanussi 					"synthetic", "empty_synth_test", true);
2139fe41efaSTom Zanussi 	if (ret) {
2149fe41efaSTom Zanussi 		trace_put_event_file(empty_synth_test);
2159fe41efaSTom Zanussi 		goto delete;
2169fe41efaSTom Zanussi 	}
2179fe41efaSTom Zanussi 
2189fe41efaSTom Zanussi 	/* Create some bogus values just for testing */
2199fe41efaSTom Zanussi 
2209fe41efaSTom Zanussi 	vals[0] = 777;			/* next_pid_field */
221279eef05STom Zanussi 	vals[1] = (u64)(long)"tiddlywinks";	/* next_comm_field */
2229fe41efaSTom Zanussi 	vals[2] = 1000000;		/* ts_ns */
2239fe41efaSTom Zanussi 	vals[3] = 1000;			/* ts_ms */
2243c18a9beSSteven Rostedt (VMware) 	vals[4] = raw_smp_processor_id(); /* cpu */
225279eef05STom Zanussi 	vals[5] = (u64)(long)"thneed_2.0";	/* my_string_field */
2269fe41efaSTom Zanussi 	vals[6] = 399;			/* my_int_field */
2279fe41efaSTom Zanussi 
2289fe41efaSTom Zanussi 	/* Now trace an empty_synth_test event */
2299fe41efaSTom Zanussi 	ret = synth_event_trace_array(empty_synth_test, vals, ARRAY_SIZE(vals));
2309fe41efaSTom Zanussi  out:
2319fe41efaSTom Zanussi 	return ret;
2329fe41efaSTom Zanussi  delete:
2339fe41efaSTom Zanussi 	/* We got an error after creating the event, delete it */
2349fe41efaSTom Zanussi 	synth_event_delete("empty_synth_test");
2359fe41efaSTom Zanussi  free:
2369fe41efaSTom Zanussi 	kfree(buf);
2379fe41efaSTom Zanussi 
2389fe41efaSTom Zanussi 	goto out;
2399fe41efaSTom Zanussi }
2409fe41efaSTom Zanussi 
2419fe41efaSTom Zanussi static struct synth_field_desc create_synth_test_fields[] = {
2429fe41efaSTom Zanussi 	{ .type = "pid_t",		.name = "next_pid_field" },
2439fe41efaSTom Zanussi 	{ .type = "char[16]",		.name = "next_comm_field" },
2449fe41efaSTom Zanussi 	{ .type = "u64",		.name = "ts_ns" },
245bd82631dSTom Zanussi 	{ .type = "char[]",		.name = "dynstring_field_1" },
2469fe41efaSTom Zanussi 	{ .type = "u64",		.name = "ts_ms" },
2479fe41efaSTom Zanussi 	{ .type = "unsigned int",	.name = "cpu" },
2489fe41efaSTom Zanussi 	{ .type = "char[64]",		.name = "my_string_field" },
249bd82631dSTom Zanussi 	{ .type = "char[]",		.name = "dynstring_field_2" },
2509fe41efaSTom Zanussi 	{ .type = "int",		.name = "my_int_field" },
2519fe41efaSTom Zanussi };
2529fe41efaSTom Zanussi 
2539fe41efaSTom Zanussi /*
2549fe41efaSTom Zanussi  * Test synthetic event creation all at once from array of field
2559fe41efaSTom Zanussi  * descriptors.
2569fe41efaSTom Zanussi  */
2579fe41efaSTom Zanussi static int __init test_create_synth_event(void)
2589fe41efaSTom Zanussi {
259bd82631dSTom Zanussi 	u64 vals[9];
2609fe41efaSTom Zanussi 	int ret;
2619fe41efaSTom Zanussi 
2629fe41efaSTom Zanussi 	/* Create the create_synth_test event with the fields above */
2639fe41efaSTom Zanussi 	ret = synth_event_create("create_synth_test",
2649fe41efaSTom Zanussi 				 create_synth_test_fields,
2659fe41efaSTom Zanussi 				 ARRAY_SIZE(create_synth_test_fields),
2669fe41efaSTom Zanussi 				 THIS_MODULE);
2679fe41efaSTom Zanussi 	if (ret)
2689fe41efaSTom Zanussi 		goto out;
2699fe41efaSTom Zanussi 
2709fe41efaSTom Zanussi 	/*
2719fe41efaSTom Zanussi 	 * Now get the create_synth_test event file.  We need to
2729fe41efaSTom Zanussi 	 * prevent the instance and event from disappearing from
2739fe41efaSTom Zanussi 	 * underneath us, which trace_get_event_file() does (though in
2749fe41efaSTom Zanussi 	 * this case we're using the top-level instance which never
2759fe41efaSTom Zanussi 	 * goes away).
2769fe41efaSTom Zanussi 	 */
2779fe41efaSTom Zanussi 	create_synth_test = trace_get_event_file(NULL, "synthetic",
2789fe41efaSTom Zanussi 						 "create_synth_test");
2799fe41efaSTom Zanussi 	if (IS_ERR(create_synth_test)) {
2809fe41efaSTom Zanussi 		ret = PTR_ERR(create_synth_test);
2819fe41efaSTom Zanussi 		goto delete;
2829fe41efaSTom Zanussi 	}
2839fe41efaSTom Zanussi 
2849fe41efaSTom Zanussi 	/* Enable the event or you won't see anything */
2859fe41efaSTom Zanussi 	ret = trace_array_set_clr_event(create_synth_test->tr,
2869fe41efaSTom Zanussi 					"synthetic", "create_synth_test", true);
2879fe41efaSTom Zanussi 	if (ret) {
2889fe41efaSTom Zanussi 		trace_put_event_file(create_synth_test);
2899fe41efaSTom Zanussi 		goto delete;
2909fe41efaSTom Zanussi 	}
2919fe41efaSTom Zanussi 
2929fe41efaSTom Zanussi 	/* Create some bogus values just for testing */
2939fe41efaSTom Zanussi 
2949fe41efaSTom Zanussi 	vals[0] = 777;			/* next_pid_field */
295279eef05STom Zanussi 	vals[1] = (u64)(long)"tiddlywinks";	/* next_comm_field */
2969fe41efaSTom Zanussi 	vals[2] = 1000000;		/* ts_ns */
297bd82631dSTom Zanussi 	vals[3] = (u64)(long)"xrayspecs";	/* dynstring_field_1 */
298bd82631dSTom Zanussi 	vals[4] = 1000;			/* ts_ms */
299bd82631dSTom Zanussi 	vals[5] = raw_smp_processor_id(); /* cpu */
300bd82631dSTom Zanussi 	vals[6] = (u64)(long)"thneed";	/* my_string_field */
301bd82631dSTom Zanussi 	vals[7] = (u64)(long)"kerplunk";	/* dynstring_field_2 */
302bd82631dSTom Zanussi 	vals[8] = 398;			/* my_int_field */
3039fe41efaSTom Zanussi 
3049fe41efaSTom Zanussi 	/* Now generate a create_synth_test event */
3059fe41efaSTom Zanussi 	ret = synth_event_trace_array(create_synth_test, vals, ARRAY_SIZE(vals));
3069fe41efaSTom Zanussi  out:
3079fe41efaSTom Zanussi 	return ret;
3089fe41efaSTom Zanussi  delete:
3099fe41efaSTom Zanussi 	/* We got an error after creating the event, delete it */
310b111545dSKaixu Xia 	synth_event_delete("create_synth_test");
3119fe41efaSTom Zanussi 
3129fe41efaSTom Zanussi 	goto out;
3139fe41efaSTom Zanussi }
3149fe41efaSTom Zanussi 
3159fe41efaSTom Zanussi /*
3169fe41efaSTom Zanussi  * Test tracing a synthetic event by reserving trace buffer space,
3179fe41efaSTom Zanussi  * then filling in fields one after another.
3189fe41efaSTom Zanussi  */
3199fe41efaSTom Zanussi static int __init test_add_next_synth_val(void)
3209fe41efaSTom Zanussi {
3219fe41efaSTom Zanussi 	struct synth_event_trace_state trace_state;
3229fe41efaSTom Zanussi 	int ret;
3239fe41efaSTom Zanussi 
3249fe41efaSTom Zanussi 	/* Start by reserving space in the trace buffer */
3259fe41efaSTom Zanussi 	ret = synth_event_trace_start(gen_synth_test, &trace_state);
3269fe41efaSTom Zanussi 	if (ret)
3279fe41efaSTom Zanussi 		return ret;
3289fe41efaSTom Zanussi 
3299fe41efaSTom Zanussi 	/* Write some bogus values into the trace buffer, one after another */
3309fe41efaSTom Zanussi 
3319fe41efaSTom Zanussi 	/* next_pid_field */
3329fe41efaSTom Zanussi 	ret = synth_event_add_next_val(777, &trace_state);
3339fe41efaSTom Zanussi 	if (ret)
3349fe41efaSTom Zanussi 		goto out;
3359fe41efaSTom Zanussi 
3369fe41efaSTom Zanussi 	/* next_comm_field */
337279eef05STom Zanussi 	ret = synth_event_add_next_val((u64)(long)"slinky", &trace_state);
3389fe41efaSTom Zanussi 	if (ret)
3399fe41efaSTom Zanussi 		goto out;
3409fe41efaSTom Zanussi 
3419fe41efaSTom Zanussi 	/* ts_ns */
3429fe41efaSTom Zanussi 	ret = synth_event_add_next_val(1000000, &trace_state);
3439fe41efaSTom Zanussi 	if (ret)
3449fe41efaSTom Zanussi 		goto out;
3459fe41efaSTom Zanussi 
3469fe41efaSTom Zanussi 	/* ts_ms */
3479fe41efaSTom Zanussi 	ret = synth_event_add_next_val(1000, &trace_state);
3489fe41efaSTom Zanussi 	if (ret)
3499fe41efaSTom Zanussi 		goto out;
3509fe41efaSTom Zanussi 
3519fe41efaSTom Zanussi 	/* cpu */
3523c18a9beSSteven Rostedt (VMware) 	ret = synth_event_add_next_val(raw_smp_processor_id(), &trace_state);
3539fe41efaSTom Zanussi 	if (ret)
3549fe41efaSTom Zanussi 		goto out;
3559fe41efaSTom Zanussi 
3569fe41efaSTom Zanussi 	/* my_string_field */
357279eef05STom Zanussi 	ret = synth_event_add_next_val((u64)(long)"thneed_2.01", &trace_state);
3589fe41efaSTom Zanussi 	if (ret)
3599fe41efaSTom Zanussi 		goto out;
3609fe41efaSTom Zanussi 
3619fe41efaSTom Zanussi 	/* my_int_field */
3629fe41efaSTom Zanussi 	ret = synth_event_add_next_val(395, &trace_state);
3639fe41efaSTom Zanussi  out:
3649fe41efaSTom Zanussi 	/* Finally, commit the event */
3659fe41efaSTom Zanussi 	ret = synth_event_trace_end(&trace_state);
3669fe41efaSTom Zanussi 
3679fe41efaSTom Zanussi 	return ret;
3689fe41efaSTom Zanussi }
3699fe41efaSTom Zanussi 
3709fe41efaSTom Zanussi /*
3719fe41efaSTom Zanussi  * Test tracing a synthetic event by reserving trace buffer space,
3729fe41efaSTom Zanussi  * then filling in fields using field names, which can be done in any
3739fe41efaSTom Zanussi  * order.
3749fe41efaSTom Zanussi  */
3759fe41efaSTom Zanussi static int __init test_add_synth_val(void)
3769fe41efaSTom Zanussi {
3779fe41efaSTom Zanussi 	struct synth_event_trace_state trace_state;
3789fe41efaSTom Zanussi 	int ret;
3799fe41efaSTom Zanussi 
3809fe41efaSTom Zanussi 	/* Start by reserving space in the trace buffer */
3819fe41efaSTom Zanussi 	ret = synth_event_trace_start(gen_synth_test, &trace_state);
3829fe41efaSTom Zanussi 	if (ret)
3839fe41efaSTom Zanussi 		return ret;
3849fe41efaSTom Zanussi 
3859fe41efaSTom Zanussi 	/* Write some bogus values into the trace buffer, using field names */
3869fe41efaSTom Zanussi 
3879fe41efaSTom Zanussi 	ret = synth_event_add_val("ts_ns", 1000000, &trace_state);
3889fe41efaSTom Zanussi 	if (ret)
3899fe41efaSTom Zanussi 		goto out;
3909fe41efaSTom Zanussi 
3919fe41efaSTom Zanussi 	ret = synth_event_add_val("ts_ms", 1000, &trace_state);
3929fe41efaSTom Zanussi 	if (ret)
3939fe41efaSTom Zanussi 		goto out;
3949fe41efaSTom Zanussi 
3953c18a9beSSteven Rostedt (VMware) 	ret = synth_event_add_val("cpu", raw_smp_processor_id(), &trace_state);
3969fe41efaSTom Zanussi 	if (ret)
3979fe41efaSTom Zanussi 		goto out;
3989fe41efaSTom Zanussi 
3999fe41efaSTom Zanussi 	ret = synth_event_add_val("next_pid_field", 777, &trace_state);
4009fe41efaSTom Zanussi 	if (ret)
4019fe41efaSTom Zanussi 		goto out;
4029fe41efaSTom Zanussi 
403279eef05STom Zanussi 	ret = synth_event_add_val("next_comm_field", (u64)(long)"silly putty",
4049fe41efaSTom Zanussi 				  &trace_state);
4059fe41efaSTom Zanussi 	if (ret)
4069fe41efaSTom Zanussi 		goto out;
4079fe41efaSTom Zanussi 
408279eef05STom Zanussi 	ret = synth_event_add_val("my_string_field", (u64)(long)"thneed_9",
4099fe41efaSTom Zanussi 				  &trace_state);
4109fe41efaSTom Zanussi 	if (ret)
4119fe41efaSTom Zanussi 		goto out;
4129fe41efaSTom Zanussi 
4139fe41efaSTom Zanussi 	ret = synth_event_add_val("my_int_field", 3999, &trace_state);
4149fe41efaSTom Zanussi  out:
4159fe41efaSTom Zanussi 	/* Finally, commit the event */
4169fe41efaSTom Zanussi 	ret = synth_event_trace_end(&trace_state);
4179fe41efaSTom Zanussi 
4189fe41efaSTom Zanussi 	return ret;
4199fe41efaSTom Zanussi }
4209fe41efaSTom Zanussi 
4219fe41efaSTom Zanussi /*
4229fe41efaSTom Zanussi  * Test tracing a synthetic event all at once from array of values.
4239fe41efaSTom Zanussi  */
4249fe41efaSTom Zanussi static int __init test_trace_synth_event(void)
4259fe41efaSTom Zanussi {
4269fe41efaSTom Zanussi 	int ret;
4279fe41efaSTom Zanussi 
4289fe41efaSTom Zanussi 	/* Trace some bogus values just for testing */
429bd82631dSTom Zanussi 	ret = synth_event_trace(create_synth_test, 9,	/* number of values */
430279eef05STom Zanussi 				(u64)444,		/* next_pid_field */
431279eef05STom Zanussi 				(u64)(long)"clackers",	/* next_comm_field */
432279eef05STom Zanussi 				(u64)1000000,		/* ts_ns */
433bd82631dSTom Zanussi 				(u64)(long)"viewmaster",/* dynstring_field_1 */
434279eef05STom Zanussi 				(u64)1000,		/* ts_ms */
4353c18a9beSSteven Rostedt (VMware) 				(u64)raw_smp_processor_id(), /* cpu */
436279eef05STom Zanussi 				(u64)(long)"Thneed",	/* my_string_field */
437bd82631dSTom Zanussi 				(u64)(long)"yoyos",	/* dynstring_field_2 */
438279eef05STom Zanussi 				(u64)999);		/* my_int_field */
4399fe41efaSTom Zanussi 	return ret;
4409fe41efaSTom Zanussi }
4419fe41efaSTom Zanussi 
4429fe41efaSTom Zanussi static int __init synth_event_gen_test_init(void)
4439fe41efaSTom Zanussi {
4449fe41efaSTom Zanussi 	int ret;
4459fe41efaSTom Zanussi 
4469fe41efaSTom Zanussi 	ret = test_gen_synth_cmd();
4479fe41efaSTom Zanussi 	if (ret)
4489fe41efaSTom Zanussi 		return ret;
4499fe41efaSTom Zanussi 
4509fe41efaSTom Zanussi 	ret = test_empty_synth_event();
4519fe41efaSTom Zanussi 	if (ret) {
4529fe41efaSTom Zanussi 		WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
4539fe41efaSTom Zanussi 						  "synthetic",
4549fe41efaSTom Zanussi 						  "gen_synth_test", false));
4559fe41efaSTom Zanussi 		trace_put_event_file(gen_synth_test);
4569fe41efaSTom Zanussi 		WARN_ON(synth_event_delete("gen_synth_test"));
4579fe41efaSTom Zanussi 		goto out;
4589fe41efaSTom Zanussi 	}
4599fe41efaSTom Zanussi 
4609fe41efaSTom Zanussi 	ret = test_create_synth_event();
4619fe41efaSTom Zanussi 	if (ret) {
4629fe41efaSTom Zanussi 		WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
4639fe41efaSTom Zanussi 						  "synthetic",
4649fe41efaSTom Zanussi 						  "gen_synth_test", false));
4659fe41efaSTom Zanussi 		trace_put_event_file(gen_synth_test);
4669fe41efaSTom Zanussi 		WARN_ON(synth_event_delete("gen_synth_test"));
4679fe41efaSTom Zanussi 
4689fe41efaSTom Zanussi 		WARN_ON(trace_array_set_clr_event(empty_synth_test->tr,
4699fe41efaSTom Zanussi 						  "synthetic",
4709fe41efaSTom Zanussi 						  "empty_synth_test", false));
4719fe41efaSTom Zanussi 		trace_put_event_file(empty_synth_test);
4729fe41efaSTom Zanussi 		WARN_ON(synth_event_delete("empty_synth_test"));
4739fe41efaSTom Zanussi 		goto out;
4749fe41efaSTom Zanussi 	}
4759fe41efaSTom Zanussi 
4769fe41efaSTom Zanussi 	ret = test_add_next_synth_val();
4779fe41efaSTom Zanussi 	WARN_ON(ret);
4789fe41efaSTom Zanussi 
4799fe41efaSTom Zanussi 	ret = test_add_synth_val();
4809fe41efaSTom Zanussi 	WARN_ON(ret);
4819fe41efaSTom Zanussi 
4829fe41efaSTom Zanussi 	ret = test_trace_synth_event();
4839fe41efaSTom Zanussi 	WARN_ON(ret);
4849fe41efaSTom Zanussi  out:
4859fe41efaSTom Zanussi 	return ret;
4869fe41efaSTom Zanussi }
4879fe41efaSTom Zanussi 
4889fe41efaSTom Zanussi static void __exit synth_event_gen_test_exit(void)
4899fe41efaSTom Zanussi {
4909fe41efaSTom Zanussi 	/* Disable the event or you can't remove it */
4919fe41efaSTom Zanussi 	WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
4929fe41efaSTom Zanussi 					  "synthetic",
4939fe41efaSTom Zanussi 					  "gen_synth_test", false));
4949fe41efaSTom Zanussi 
4959fe41efaSTom Zanussi 	/* Now give the file and instance back */
4969fe41efaSTom Zanussi 	trace_put_event_file(gen_synth_test);
4979fe41efaSTom Zanussi 
4989fe41efaSTom Zanussi 	/* Now unregister and free the synthetic event */
4999fe41efaSTom Zanussi 	WARN_ON(synth_event_delete("gen_synth_test"));
5009fe41efaSTom Zanussi 
5019fe41efaSTom Zanussi 	/* Disable the event or you can't remove it */
5029fe41efaSTom Zanussi 	WARN_ON(trace_array_set_clr_event(empty_synth_test->tr,
5039fe41efaSTom Zanussi 					  "synthetic",
5049fe41efaSTom Zanussi 					  "empty_synth_test", false));
5059fe41efaSTom Zanussi 
5069fe41efaSTom Zanussi 	/* Now give the file and instance back */
5079fe41efaSTom Zanussi 	trace_put_event_file(empty_synth_test);
5089fe41efaSTom Zanussi 
5099fe41efaSTom Zanussi 	/* Now unregister and free the synthetic event */
5109fe41efaSTom Zanussi 	WARN_ON(synth_event_delete("empty_synth_test"));
5119fe41efaSTom Zanussi 
5129fe41efaSTom Zanussi 	/* Disable the event or you can't remove it */
5139fe41efaSTom Zanussi 	WARN_ON(trace_array_set_clr_event(create_synth_test->tr,
5149fe41efaSTom Zanussi 					  "synthetic",
5159fe41efaSTom Zanussi 					  "create_synth_test", false));
5169fe41efaSTom Zanussi 
5179fe41efaSTom Zanussi 	/* Now give the file and instance back */
5189fe41efaSTom Zanussi 	trace_put_event_file(create_synth_test);
5199fe41efaSTom Zanussi 
5209fe41efaSTom Zanussi 	/* Now unregister and free the synthetic event */
5219fe41efaSTom Zanussi 	WARN_ON(synth_event_delete("create_synth_test"));
5229fe41efaSTom Zanussi }
5239fe41efaSTom Zanussi 
5249fe41efaSTom Zanussi module_init(synth_event_gen_test_init)
5259fe41efaSTom Zanussi module_exit(synth_event_gen_test_exit)
5269fe41efaSTom Zanussi 
5279fe41efaSTom Zanussi MODULE_AUTHOR("Tom Zanussi");
5289fe41efaSTom Zanussi MODULE_DESCRIPTION("synthetic event generation test");
5299fe41efaSTom Zanussi MODULE_LICENSE("GPL v2");
530