19fe41efaSTom Zanussi // SPDX-License-Identifier: GPL-2.0
29fe41efaSTom Zanussi /*
3f2cc020dSIngo 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
252455f0e1SRoss Zwisler * # cat /sys/kernel/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 */
test_gen_synth_cmd(void)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));
123a4527fefSShang XiaoJing free:
124a4527fefSShang XiaoJing kfree(buf);
1259fe41efaSTom Zanussi return ret;
1269fe41efaSTom Zanussi delete:
1279fe41efaSTom Zanussi /* We got an error after creating the event, delete it */
1289fe41efaSTom Zanussi synth_event_delete("gen_synth_test");
129a4527fefSShang XiaoJing goto free;
1309fe41efaSTom Zanussi }
1319fe41efaSTom Zanussi
1329fe41efaSTom Zanussi /*
1339fe41efaSTom Zanussi * Test to make sure we can create an initially empty synthetic event,
1349fe41efaSTom Zanussi * then add all the fields.
1359fe41efaSTom Zanussi */
test_empty_synth_event(void)1369fe41efaSTom Zanussi static int __init test_empty_synth_event(void)
1379fe41efaSTom Zanussi {
1389fe41efaSTom Zanussi struct dynevent_cmd cmd;
1399fe41efaSTom Zanussi u64 vals[7];
1409fe41efaSTom Zanussi char *buf;
1419fe41efaSTom Zanussi int ret;
1429fe41efaSTom Zanussi
1439fe41efaSTom Zanussi /* Create a buffer to hold the generated command */
1449fe41efaSTom Zanussi buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1459fe41efaSTom Zanussi if (!buf)
1469fe41efaSTom Zanussi return -ENOMEM;
1479fe41efaSTom Zanussi
1489fe41efaSTom Zanussi /* Before generating the command, initialize the cmd object */
1499fe41efaSTom Zanussi synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1509fe41efaSTom Zanussi
1519fe41efaSTom Zanussi /*
1529fe41efaSTom Zanussi * Create the empty_synth_test synthetic event with no fields.
1539fe41efaSTom Zanussi */
1549fe41efaSTom Zanussi ret = synth_event_gen_cmd_start(&cmd, "empty_synth_test", THIS_MODULE);
1559fe41efaSTom Zanussi if (ret)
1569fe41efaSTom Zanussi goto free;
1579fe41efaSTom Zanussi
1589fe41efaSTom Zanussi /* Use synth_event_add_field to add all of the fields */
1599fe41efaSTom Zanussi
1609fe41efaSTom Zanussi ret = synth_event_add_field(&cmd, "pid_t", "next_pid_field");
1619fe41efaSTom Zanussi if (ret)
1629fe41efaSTom Zanussi goto free;
1639fe41efaSTom Zanussi
1649fe41efaSTom Zanussi ret = synth_event_add_field(&cmd, "char[16]", "next_comm_field");
1659fe41efaSTom Zanussi if (ret)
1669fe41efaSTom Zanussi goto free;
1679fe41efaSTom Zanussi
1689fe41efaSTom Zanussi ret = synth_event_add_field(&cmd, "u64", "ts_ns");
1699fe41efaSTom Zanussi if (ret)
1709fe41efaSTom Zanussi goto free;
1719fe41efaSTom Zanussi
1729fe41efaSTom Zanussi ret = synth_event_add_field(&cmd, "u64", "ts_ms");
1739fe41efaSTom Zanussi if (ret)
1749fe41efaSTom Zanussi goto free;
1759fe41efaSTom Zanussi
1769fe41efaSTom Zanussi ret = synth_event_add_field(&cmd, "unsigned int", "cpu");
1779fe41efaSTom Zanussi if (ret)
1789fe41efaSTom Zanussi goto free;
1799fe41efaSTom Zanussi
1809fe41efaSTom Zanussi ret = synth_event_add_field(&cmd, "char[64]", "my_string_field");
1819fe41efaSTom Zanussi if (ret)
1829fe41efaSTom Zanussi goto free;
1839fe41efaSTom Zanussi
1849fe41efaSTom Zanussi ret = synth_event_add_field(&cmd, "int", "my_int_field");
1859fe41efaSTom Zanussi if (ret)
1869fe41efaSTom Zanussi goto free;
1879fe41efaSTom Zanussi
1889fe41efaSTom Zanussi /* All fields have been added, close and register the synth event */
1899fe41efaSTom Zanussi
1909fe41efaSTom Zanussi ret = synth_event_gen_cmd_end(&cmd);
1919fe41efaSTom Zanussi if (ret)
1929fe41efaSTom Zanussi goto free;
1939fe41efaSTom Zanussi
1949fe41efaSTom Zanussi /*
1959fe41efaSTom Zanussi * Now get the empty_synth_test event file. We need to
1969fe41efaSTom Zanussi * prevent the instance and event from disappearing from
1979fe41efaSTom Zanussi * underneath us, which trace_get_event_file() does (though in
1989fe41efaSTom Zanussi * this case we're using the top-level instance which never
1999fe41efaSTom Zanussi * goes away).
2009fe41efaSTom Zanussi */
2019fe41efaSTom Zanussi empty_synth_test = trace_get_event_file(NULL, "synthetic",
2029fe41efaSTom Zanussi "empty_synth_test");
2039fe41efaSTom Zanussi if (IS_ERR(empty_synth_test)) {
2049fe41efaSTom Zanussi ret = PTR_ERR(empty_synth_test);
2059fe41efaSTom Zanussi goto delete;
2069fe41efaSTom Zanussi }
2079fe41efaSTom Zanussi
2089fe41efaSTom Zanussi /* Enable the event or you won't see anything */
2099fe41efaSTom Zanussi ret = trace_array_set_clr_event(empty_synth_test->tr,
2109fe41efaSTom Zanussi "synthetic", "empty_synth_test", true);
2119fe41efaSTom Zanussi if (ret) {
2129fe41efaSTom Zanussi trace_put_event_file(empty_synth_test);
2139fe41efaSTom Zanussi goto delete;
2149fe41efaSTom Zanussi }
2159fe41efaSTom Zanussi
2169fe41efaSTom Zanussi /* Create some bogus values just for testing */
2179fe41efaSTom Zanussi
2189fe41efaSTom Zanussi vals[0] = 777; /* next_pid_field */
219279eef05STom Zanussi vals[1] = (u64)(long)"tiddlywinks"; /* next_comm_field */
2209fe41efaSTom Zanussi vals[2] = 1000000; /* ts_ns */
2219fe41efaSTom Zanussi vals[3] = 1000; /* ts_ms */
2223c18a9beSSteven Rostedt (VMware) vals[4] = raw_smp_processor_id(); /* cpu */
223279eef05STom Zanussi vals[5] = (u64)(long)"thneed_2.0"; /* my_string_field */
2249fe41efaSTom Zanussi vals[6] = 399; /* my_int_field */
2259fe41efaSTom Zanussi
2269fe41efaSTom Zanussi /* Now trace an empty_synth_test event */
2279fe41efaSTom Zanussi ret = synth_event_trace_array(empty_synth_test, vals, ARRAY_SIZE(vals));
228a4527fefSShang XiaoJing free:
229a4527fefSShang XiaoJing kfree(buf);
2309fe41efaSTom Zanussi return ret;
2319fe41efaSTom Zanussi delete:
2329fe41efaSTom Zanussi /* We got an error after creating the event, delete it */
2339fe41efaSTom Zanussi synth_event_delete("empty_synth_test");
234a4527fefSShang XiaoJing goto free;
2359fe41efaSTom Zanussi }
2369fe41efaSTom Zanussi
2379fe41efaSTom Zanussi static struct synth_field_desc create_synth_test_fields[] = {
2389fe41efaSTom Zanussi { .type = "pid_t", .name = "next_pid_field" },
2399fe41efaSTom Zanussi { .type = "char[16]", .name = "next_comm_field" },
2409fe41efaSTom Zanussi { .type = "u64", .name = "ts_ns" },
241bd82631dSTom Zanussi { .type = "char[]", .name = "dynstring_field_1" },
2429fe41efaSTom Zanussi { .type = "u64", .name = "ts_ms" },
2439fe41efaSTom Zanussi { .type = "unsigned int", .name = "cpu" },
2449fe41efaSTom Zanussi { .type = "char[64]", .name = "my_string_field" },
245bd82631dSTom Zanussi { .type = "char[]", .name = "dynstring_field_2" },
2469fe41efaSTom Zanussi { .type = "int", .name = "my_int_field" },
2479fe41efaSTom Zanussi };
2489fe41efaSTom Zanussi
2499fe41efaSTom Zanussi /*
2509fe41efaSTom Zanussi * Test synthetic event creation all at once from array of field
2519fe41efaSTom Zanussi * descriptors.
2529fe41efaSTom Zanussi */
test_create_synth_event(void)2539fe41efaSTom Zanussi static int __init test_create_synth_event(void)
2549fe41efaSTom Zanussi {
255bd82631dSTom Zanussi u64 vals[9];
2569fe41efaSTom Zanussi int ret;
2579fe41efaSTom Zanussi
2589fe41efaSTom Zanussi /* Create the create_synth_test event with the fields above */
2599fe41efaSTom Zanussi ret = synth_event_create("create_synth_test",
2609fe41efaSTom Zanussi create_synth_test_fields,
2619fe41efaSTom Zanussi ARRAY_SIZE(create_synth_test_fields),
2629fe41efaSTom Zanussi THIS_MODULE);
2639fe41efaSTom Zanussi if (ret)
2649fe41efaSTom Zanussi goto out;
2659fe41efaSTom Zanussi
2669fe41efaSTom Zanussi /*
2679fe41efaSTom Zanussi * Now get the create_synth_test event file. We need to
2689fe41efaSTom Zanussi * prevent the instance and event from disappearing from
2699fe41efaSTom Zanussi * underneath us, which trace_get_event_file() does (though in
2709fe41efaSTom Zanussi * this case we're using the top-level instance which never
2719fe41efaSTom Zanussi * goes away).
2729fe41efaSTom Zanussi */
2739fe41efaSTom Zanussi create_synth_test = trace_get_event_file(NULL, "synthetic",
2749fe41efaSTom Zanussi "create_synth_test");
2759fe41efaSTom Zanussi if (IS_ERR(create_synth_test)) {
2769fe41efaSTom Zanussi ret = PTR_ERR(create_synth_test);
2779fe41efaSTom Zanussi goto delete;
2789fe41efaSTom Zanussi }
2799fe41efaSTom Zanussi
2809fe41efaSTom Zanussi /* Enable the event or you won't see anything */
2819fe41efaSTom Zanussi ret = trace_array_set_clr_event(create_synth_test->tr,
2829fe41efaSTom Zanussi "synthetic", "create_synth_test", true);
2839fe41efaSTom Zanussi if (ret) {
2849fe41efaSTom Zanussi trace_put_event_file(create_synth_test);
2859fe41efaSTom Zanussi goto delete;
2869fe41efaSTom Zanussi }
2879fe41efaSTom Zanussi
2889fe41efaSTom Zanussi /* Create some bogus values just for testing */
2899fe41efaSTom Zanussi
2909fe41efaSTom Zanussi vals[0] = 777; /* next_pid_field */
291279eef05STom Zanussi vals[1] = (u64)(long)"tiddlywinks"; /* next_comm_field */
2929fe41efaSTom Zanussi vals[2] = 1000000; /* ts_ns */
293bd82631dSTom Zanussi vals[3] = (u64)(long)"xrayspecs"; /* dynstring_field_1 */
294bd82631dSTom Zanussi vals[4] = 1000; /* ts_ms */
295bd82631dSTom Zanussi vals[5] = raw_smp_processor_id(); /* cpu */
296bd82631dSTom Zanussi vals[6] = (u64)(long)"thneed"; /* my_string_field */
297bd82631dSTom Zanussi vals[7] = (u64)(long)"kerplunk"; /* dynstring_field_2 */
298bd82631dSTom Zanussi vals[8] = 398; /* my_int_field */
2999fe41efaSTom Zanussi
3009fe41efaSTom Zanussi /* Now generate a create_synth_test event */
3019fe41efaSTom Zanussi ret = synth_event_trace_array(create_synth_test, vals, ARRAY_SIZE(vals));
3029fe41efaSTom Zanussi out:
3039fe41efaSTom Zanussi return ret;
3049fe41efaSTom Zanussi delete:
3059fe41efaSTom Zanussi /* We got an error after creating the event, delete it */
306b111545dSKaixu Xia synth_event_delete("create_synth_test");
3079fe41efaSTom Zanussi
3089fe41efaSTom Zanussi goto out;
3099fe41efaSTom Zanussi }
3109fe41efaSTom Zanussi
3119fe41efaSTom Zanussi /*
3129fe41efaSTom Zanussi * Test tracing a synthetic event by reserving trace buffer space,
3139fe41efaSTom Zanussi * then filling in fields one after another.
3149fe41efaSTom Zanussi */
test_add_next_synth_val(void)3159fe41efaSTom Zanussi static int __init test_add_next_synth_val(void)
3169fe41efaSTom Zanussi {
3179fe41efaSTom Zanussi struct synth_event_trace_state trace_state;
3189fe41efaSTom Zanussi int ret;
3199fe41efaSTom Zanussi
3209fe41efaSTom Zanussi /* Start by reserving space in the trace buffer */
3219fe41efaSTom Zanussi ret = synth_event_trace_start(gen_synth_test, &trace_state);
3229fe41efaSTom Zanussi if (ret)
3239fe41efaSTom Zanussi return ret;
3249fe41efaSTom Zanussi
3259fe41efaSTom Zanussi /* Write some bogus values into the trace buffer, one after another */
3269fe41efaSTom Zanussi
3279fe41efaSTom Zanussi /* next_pid_field */
3289fe41efaSTom Zanussi ret = synth_event_add_next_val(777, &trace_state);
3299fe41efaSTom Zanussi if (ret)
3309fe41efaSTom Zanussi goto out;
3319fe41efaSTom Zanussi
3329fe41efaSTom Zanussi /* next_comm_field */
333279eef05STom Zanussi ret = synth_event_add_next_val((u64)(long)"slinky", &trace_state);
3349fe41efaSTom Zanussi if (ret)
3359fe41efaSTom Zanussi goto out;
3369fe41efaSTom Zanussi
3379fe41efaSTom Zanussi /* ts_ns */
3389fe41efaSTom Zanussi ret = synth_event_add_next_val(1000000, &trace_state);
3399fe41efaSTom Zanussi if (ret)
3409fe41efaSTom Zanussi goto out;
3419fe41efaSTom Zanussi
3429fe41efaSTom Zanussi /* ts_ms */
3439fe41efaSTom Zanussi ret = synth_event_add_next_val(1000, &trace_state);
3449fe41efaSTom Zanussi if (ret)
3459fe41efaSTom Zanussi goto out;
3469fe41efaSTom Zanussi
3479fe41efaSTom Zanussi /* cpu */
3483c18a9beSSteven Rostedt (VMware) ret = synth_event_add_next_val(raw_smp_processor_id(), &trace_state);
3499fe41efaSTom Zanussi if (ret)
3509fe41efaSTom Zanussi goto out;
3519fe41efaSTom Zanussi
3529fe41efaSTom Zanussi /* my_string_field */
353279eef05STom Zanussi ret = synth_event_add_next_val((u64)(long)"thneed_2.01", &trace_state);
3549fe41efaSTom Zanussi if (ret)
3559fe41efaSTom Zanussi goto out;
3569fe41efaSTom Zanussi
3579fe41efaSTom Zanussi /* my_int_field */
3589fe41efaSTom Zanussi ret = synth_event_add_next_val(395, &trace_state);
3599fe41efaSTom Zanussi out:
3609fe41efaSTom Zanussi /* Finally, commit the event */
3619fe41efaSTom Zanussi ret = synth_event_trace_end(&trace_state);
3629fe41efaSTom Zanussi
3639fe41efaSTom Zanussi return ret;
3649fe41efaSTom Zanussi }
3659fe41efaSTom Zanussi
3669fe41efaSTom Zanussi /*
3679fe41efaSTom Zanussi * Test tracing a synthetic event by reserving trace buffer space,
3689fe41efaSTom Zanussi * then filling in fields using field names, which can be done in any
3699fe41efaSTom Zanussi * order.
3709fe41efaSTom Zanussi */
test_add_synth_val(void)3719fe41efaSTom Zanussi static int __init test_add_synth_val(void)
3729fe41efaSTom Zanussi {
3739fe41efaSTom Zanussi struct synth_event_trace_state trace_state;
3749fe41efaSTom Zanussi int ret;
3759fe41efaSTom Zanussi
3769fe41efaSTom Zanussi /* Start by reserving space in the trace buffer */
3779fe41efaSTom Zanussi ret = synth_event_trace_start(gen_synth_test, &trace_state);
3789fe41efaSTom Zanussi if (ret)
3799fe41efaSTom Zanussi return ret;
3809fe41efaSTom Zanussi
3819fe41efaSTom Zanussi /* Write some bogus values into the trace buffer, using field names */
3829fe41efaSTom Zanussi
3839fe41efaSTom Zanussi ret = synth_event_add_val("ts_ns", 1000000, &trace_state);
3849fe41efaSTom Zanussi if (ret)
3859fe41efaSTom Zanussi goto out;
3869fe41efaSTom Zanussi
3879fe41efaSTom Zanussi ret = synth_event_add_val("ts_ms", 1000, &trace_state);
3889fe41efaSTom Zanussi if (ret)
3899fe41efaSTom Zanussi goto out;
3909fe41efaSTom Zanussi
3913c18a9beSSteven Rostedt (VMware) ret = synth_event_add_val("cpu", raw_smp_processor_id(), &trace_state);
3929fe41efaSTom Zanussi if (ret)
3939fe41efaSTom Zanussi goto out;
3949fe41efaSTom Zanussi
3959fe41efaSTom Zanussi ret = synth_event_add_val("next_pid_field", 777, &trace_state);
3969fe41efaSTom Zanussi if (ret)
3979fe41efaSTom Zanussi goto out;
3989fe41efaSTom Zanussi
399279eef05STom Zanussi ret = synth_event_add_val("next_comm_field", (u64)(long)"silly putty",
4009fe41efaSTom Zanussi &trace_state);
4019fe41efaSTom Zanussi if (ret)
4029fe41efaSTom Zanussi goto out;
4039fe41efaSTom Zanussi
404279eef05STom Zanussi ret = synth_event_add_val("my_string_field", (u64)(long)"thneed_9",
4059fe41efaSTom Zanussi &trace_state);
4069fe41efaSTom Zanussi if (ret)
4079fe41efaSTom Zanussi goto out;
4089fe41efaSTom Zanussi
4099fe41efaSTom Zanussi ret = synth_event_add_val("my_int_field", 3999, &trace_state);
4109fe41efaSTom Zanussi out:
4119fe41efaSTom Zanussi /* Finally, commit the event */
4129fe41efaSTom Zanussi ret = synth_event_trace_end(&trace_state);
4139fe41efaSTom Zanussi
4149fe41efaSTom Zanussi return ret;
4159fe41efaSTom Zanussi }
4169fe41efaSTom Zanussi
4179fe41efaSTom Zanussi /*
4189fe41efaSTom Zanussi * Test tracing a synthetic event all at once from array of values.
4199fe41efaSTom Zanussi */
test_trace_synth_event(void)4209fe41efaSTom Zanussi static int __init test_trace_synth_event(void)
4219fe41efaSTom Zanussi {
4229fe41efaSTom Zanussi int ret;
4239fe41efaSTom Zanussi
4249fe41efaSTom Zanussi /* Trace some bogus values just for testing */
425bd82631dSTom Zanussi ret = synth_event_trace(create_synth_test, 9, /* number of values */
426279eef05STom Zanussi (u64)444, /* next_pid_field */
427279eef05STom Zanussi (u64)(long)"clackers", /* next_comm_field */
428279eef05STom Zanussi (u64)1000000, /* ts_ns */
429bd82631dSTom Zanussi (u64)(long)"viewmaster",/* dynstring_field_1 */
430279eef05STom Zanussi (u64)1000, /* ts_ms */
4313c18a9beSSteven Rostedt (VMware) (u64)raw_smp_processor_id(), /* cpu */
432279eef05STom Zanussi (u64)(long)"Thneed", /* my_string_field */
433bd82631dSTom Zanussi (u64)(long)"yoyos", /* dynstring_field_2 */
434279eef05STom Zanussi (u64)999); /* my_int_field */
4359fe41efaSTom Zanussi return ret;
4369fe41efaSTom Zanussi }
4379fe41efaSTom Zanussi
synth_event_gen_test_init(void)4389fe41efaSTom Zanussi static int __init synth_event_gen_test_init(void)
4399fe41efaSTom Zanussi {
4409fe41efaSTom Zanussi int ret;
4419fe41efaSTom Zanussi
4429fe41efaSTom Zanussi ret = test_gen_synth_cmd();
4439fe41efaSTom Zanussi if (ret)
4449fe41efaSTom Zanussi return ret;
4459fe41efaSTom Zanussi
4469fe41efaSTom Zanussi ret = test_empty_synth_event();
4479fe41efaSTom Zanussi if (ret) {
4489fe41efaSTom Zanussi WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
4499fe41efaSTom Zanussi "synthetic",
4509fe41efaSTom Zanussi "gen_synth_test", false));
4519fe41efaSTom Zanussi trace_put_event_file(gen_synth_test);
4529fe41efaSTom Zanussi WARN_ON(synth_event_delete("gen_synth_test"));
4539fe41efaSTom Zanussi goto out;
4549fe41efaSTom Zanussi }
4559fe41efaSTom Zanussi
4569fe41efaSTom Zanussi ret = test_create_synth_event();
4579fe41efaSTom Zanussi if (ret) {
4589fe41efaSTom Zanussi WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
4599fe41efaSTom Zanussi "synthetic",
4609fe41efaSTom Zanussi "gen_synth_test", false));
4619fe41efaSTom Zanussi trace_put_event_file(gen_synth_test);
4629fe41efaSTom Zanussi WARN_ON(synth_event_delete("gen_synth_test"));
4639fe41efaSTom Zanussi
4649fe41efaSTom Zanussi WARN_ON(trace_array_set_clr_event(empty_synth_test->tr,
4659fe41efaSTom Zanussi "synthetic",
4669fe41efaSTom Zanussi "empty_synth_test", false));
4679fe41efaSTom Zanussi trace_put_event_file(empty_synth_test);
4689fe41efaSTom Zanussi WARN_ON(synth_event_delete("empty_synth_test"));
4699fe41efaSTom Zanussi goto out;
4709fe41efaSTom Zanussi }
4719fe41efaSTom Zanussi
4729fe41efaSTom Zanussi ret = test_add_next_synth_val();
4739fe41efaSTom Zanussi WARN_ON(ret);
4749fe41efaSTom Zanussi
4759fe41efaSTom Zanussi ret = test_add_synth_val();
4769fe41efaSTom Zanussi WARN_ON(ret);
4779fe41efaSTom Zanussi
4789fe41efaSTom Zanussi ret = test_trace_synth_event();
4799fe41efaSTom Zanussi WARN_ON(ret);
480*7e39c55eSSteven Rostedt (Google)
481*7e39c55eSSteven Rostedt (Google) /* Disable when done */
482*7e39c55eSSteven Rostedt (Google) trace_array_set_clr_event(gen_synth_test->tr,
483*7e39c55eSSteven Rostedt (Google) "synthetic",
484*7e39c55eSSteven Rostedt (Google) "gen_synth_test", false);
485*7e39c55eSSteven Rostedt (Google) trace_array_set_clr_event(empty_synth_test->tr,
486*7e39c55eSSteven Rostedt (Google) "synthetic",
487*7e39c55eSSteven Rostedt (Google) "empty_synth_test", false);
488*7e39c55eSSteven Rostedt (Google) trace_array_set_clr_event(create_synth_test->tr,
489*7e39c55eSSteven Rostedt (Google) "synthetic",
490*7e39c55eSSteven Rostedt (Google) "create_synth_test", false);
4919fe41efaSTom Zanussi out:
4929fe41efaSTom Zanussi return ret;
4939fe41efaSTom Zanussi }
4949fe41efaSTom Zanussi
synth_event_gen_test_exit(void)4959fe41efaSTom Zanussi static void __exit synth_event_gen_test_exit(void)
4969fe41efaSTom Zanussi {
4979fe41efaSTom Zanussi /* Disable the event or you can't remove it */
4989fe41efaSTom Zanussi WARN_ON(trace_array_set_clr_event(gen_synth_test->tr,
4999fe41efaSTom Zanussi "synthetic",
5009fe41efaSTom Zanussi "gen_synth_test", false));
5019fe41efaSTom Zanussi
5029fe41efaSTom Zanussi /* Now give the file and instance back */
5039fe41efaSTom Zanussi trace_put_event_file(gen_synth_test);
5049fe41efaSTom Zanussi
5059fe41efaSTom Zanussi /* Now unregister and free the synthetic event */
5069fe41efaSTom Zanussi WARN_ON(synth_event_delete("gen_synth_test"));
5079fe41efaSTom Zanussi
5089fe41efaSTom Zanussi /* Disable the event or you can't remove it */
5099fe41efaSTom Zanussi WARN_ON(trace_array_set_clr_event(empty_synth_test->tr,
5109fe41efaSTom Zanussi "synthetic",
5119fe41efaSTom Zanussi "empty_synth_test", false));
5129fe41efaSTom Zanussi
5139fe41efaSTom Zanussi /* Now give the file and instance back */
5149fe41efaSTom Zanussi trace_put_event_file(empty_synth_test);
5159fe41efaSTom Zanussi
5169fe41efaSTom Zanussi /* Now unregister and free the synthetic event */
5179fe41efaSTom Zanussi WARN_ON(synth_event_delete("empty_synth_test"));
5189fe41efaSTom Zanussi
5199fe41efaSTom Zanussi /* Disable the event or you can't remove it */
5209fe41efaSTom Zanussi WARN_ON(trace_array_set_clr_event(create_synth_test->tr,
5219fe41efaSTom Zanussi "synthetic",
5229fe41efaSTom Zanussi "create_synth_test", false));
5239fe41efaSTom Zanussi
5249fe41efaSTom Zanussi /* Now give the file and instance back */
5259fe41efaSTom Zanussi trace_put_event_file(create_synth_test);
5269fe41efaSTom Zanussi
5279fe41efaSTom Zanussi /* Now unregister and free the synthetic event */
5289fe41efaSTom Zanussi WARN_ON(synth_event_delete("create_synth_test"));
5299fe41efaSTom Zanussi }
5309fe41efaSTom Zanussi
5319fe41efaSTom Zanussi module_init(synth_event_gen_test_init)
5329fe41efaSTom Zanussi module_exit(synth_event_gen_test_exit)
5339fe41efaSTom Zanussi
5349fe41efaSTom Zanussi MODULE_AUTHOR("Tom Zanussi");
5359fe41efaSTom Zanussi MODULE_DESCRIPTION("synthetic event generation test");
5369fe41efaSTom Zanussi MODULE_LICENSE("GPL v2");
537