xref: /linux/samples/trace_events/trace_custom_sched.c (revision 3f44ae972ae5cf6a75eb40314a6a81bd694d0f53)
1953c2f05SSteven Rostedt (Google) // SPDX-License-Identifier: GPL-2.0
2953c2f05SSteven Rostedt (Google) /*
3953c2f05SSteven Rostedt (Google)  * event tracer
4953c2f05SSteven Rostedt (Google)  *
5953c2f05SSteven Rostedt (Google)  * Copyright (C) 2022 Google Inc, Steven Rostedt <rostedt@goodmis.org>
6953c2f05SSteven Rostedt (Google)  */
7953c2f05SSteven Rostedt (Google) 
8953c2f05SSteven Rostedt (Google) #define pr_fmt(fmt) fmt
9953c2f05SSteven Rostedt (Google) 
10953c2f05SSteven Rostedt (Google) #include <linux/trace_events.h>
11953c2f05SSteven Rostedt (Google) #include <linux/module.h>
12953c2f05SSteven Rostedt (Google) #include <linux/sched.h>
13*3a73333fSSteven Rostedt (Google) 
14*3a73333fSSteven Rostedt (Google) /*
15*3a73333fSSteven Rostedt (Google)  * Must include the event header that the custom event will attach to,
16*3a73333fSSteven Rostedt (Google)  * from the C file, and not in the custom header file.
17*3a73333fSSteven Rostedt (Google)  */
18953c2f05SSteven Rostedt (Google) #include <trace/events/sched.h>
19953c2f05SSteven Rostedt (Google) 
20*3a73333fSSteven Rostedt (Google) /* Declare CREATE_CUSTOM_TRACE_EVENTS before including custom header */
21*3a73333fSSteven Rostedt (Google) #define CREATE_CUSTOM_TRACE_EVENTS
22953c2f05SSteven Rostedt (Google) 
23*3a73333fSSteven Rostedt (Google) #include "trace_custom_sched.h"
24953c2f05SSteven Rostedt (Google) 
25*3a73333fSSteven Rostedt (Google) /*
26*3a73333fSSteven Rostedt (Google)  * As the trace events are not exported to modules, the use of
27*3a73333fSSteven Rostedt (Google)  * for_each_kernel_tracepoint() is needed to find the trace event
28*3a73333fSSteven Rostedt (Google)  * to attach to. The fct() function below, is a callback that
29*3a73333fSSteven Rostedt (Google)  * will be called for every event.
30*3a73333fSSteven Rostedt (Google)  *
31*3a73333fSSteven Rostedt (Google)  * Helper functions are created by the TRACE_CUSTOM_EVENT() macro
32*3a73333fSSteven Rostedt (Google)  * update the event. Those are of the form:
33*3a73333fSSteven Rostedt (Google)  *
34*3a73333fSSteven Rostedt (Google)  *    trace_custom_event_<event>_update()
35*3a73333fSSteven Rostedt (Google)  *
36*3a73333fSSteven Rostedt (Google)  * Where <event> is the event to attach.
37*3a73333fSSteven Rostedt (Google)  */
fct(struct tracepoint * tp,void * priv)38953c2f05SSteven Rostedt (Google) static void fct(struct tracepoint *tp, void *priv)
39953c2f05SSteven Rostedt (Google) {
40*3a73333fSSteven Rostedt (Google) 	trace_custom_event_sched_switch_update(tp);
41*3a73333fSSteven Rostedt (Google) 	trace_custom_event_sched_waking_update(tp);
42953c2f05SSteven Rostedt (Google) }
43953c2f05SSteven Rostedt (Google) 
trace_sched_init(void)44953c2f05SSteven Rostedt (Google) static int __init trace_sched_init(void)
45953c2f05SSteven Rostedt (Google) {
46953c2f05SSteven Rostedt (Google) 	for_each_kernel_tracepoint(fct, NULL);
47*3a73333fSSteven Rostedt (Google) 	return 0;
48953c2f05SSteven Rostedt (Google) }
49953c2f05SSteven Rostedt (Google) 
trace_sched_exit(void)50953c2f05SSteven Rostedt (Google) static void __exit trace_sched_exit(void)
51953c2f05SSteven Rostedt (Google) {
52953c2f05SSteven Rostedt (Google) }
53953c2f05SSteven Rostedt (Google) 
54953c2f05SSteven Rostedt (Google) module_init(trace_sched_init);
55953c2f05SSteven Rostedt (Google) module_exit(trace_sched_exit);
56953c2f05SSteven Rostedt (Google) 
57953c2f05SSteven Rostedt (Google) MODULE_AUTHOR("Steven Rostedt");
58953c2f05SSteven Rostedt (Google) MODULE_DESCRIPTION("Custom scheduling events");
59953c2f05SSteven Rostedt (Google) MODULE_LICENSE("GPL");
60