summaryrefslogtreecommitdiff
path: root/samples/trace_events/trace_custom_sched.c
blob: 70a12c32ff99106ce22696fcbd3ad254f3f0dc25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// SPDX-License-Identifier: GPL-2.0
/*
 * event tracer
 *
 * Copyright (C) 2022 Google Inc, Steven Rostedt <rostedt@goodmis.org>
 */

#define pr_fmt(fmt) fmt

#include <linux/trace_events.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <trace/events/sched.h>

#define THIS_SYSTEM "custom_sched"

#define SCHED_PRINT_FMT							\
	C("prev_prio=%d next_pid=%d next_prio=%d", REC->prev_prio, REC->next_pid, \
	  REC->next_prio)

#define SCHED_WAKING_FMT				\
	C("pid=%d prio=%d", REC->pid, REC->prio)

#undef C
#define C(a, b...) a, b

static struct trace_event_fields sched_switch_fields[] = {
	{
		.type = "unsigned short",
		.name = "prev_prio",
		.size = sizeof(short),
		.align = __alignof__(short),
		.is_signed = 0,
		.filter_type = FILTER_OTHER,
	},
	{
		.type = "unsigned short",
		.name = "next_prio",
		.size = sizeof(short),
		.align = __alignof__(short),
		.is_signed = 0,
		.filter_type = FILTER_OTHER,
	},
	{
		.type = "unsigned int",
		.name = "next_prio",
		.size = sizeof(int),
		.align = __alignof__(int),
		.is_signed = 0,
		.filter_type = FILTER_OTHER,
	},
	{}
};

struct sched_event {
	struct trace_entry	ent;
	unsigned short		prev_prio;
	unsigned short		next_prio;
	unsigned int		next_pid;
};

static struct trace_event_fields sched_waking_fields[] = {
	{
		.type = "unsigned int",
		.name = "pid",
		.size = sizeof(int),
		.align = __alignof__(int),
		.is_signed = 0,
		.filter_type = FILTER_OTHER,
	},
	{
		.type = "unsigned short",
		.name = "prio",
		.size = sizeof(short),
		.align = __alignof__(short),
		.is_signed = 0,
		.filter_type = FILTER_OTHER,
	},
	{}
};

struct wake_event {
	struct trace_entry	ent;
	unsigned int		pid;
	unsigned short		prio;
};

static void sched_switch_probe(void *data, bool preempt, struct task_struct *prev,
			       struct task_struct *next)
{
	struct trace_event_file *trace_file = data;
	struct trace_event_buffer fbuffer;
	struct sched_event *entry;

	if (trace_trigger_soft_disabled(trace_file))
		return;

	entry = trace_event_buffer_reserve(&fbuffer, trace_file,
					   sizeof(*entry));

	if (!entry)
		return;

	entry->prev_prio = prev->prio;
	entry->next_prio = next->prio;
	entry->next_pid = next->pid;

	trace_event_buffer_commit(&fbuffer);
}

static struct trace_event_class sched_switch_class = {
	.system			= THIS_SYSTEM,
	.reg			= trace_event_reg,
	.fields_array		= sched_switch_fields,
	.fields			= LIST_HEAD_INIT(sched_switch_class.fields),
	.probe			= sched_switch_probe,
};

static void sched_waking_probe(void *data, struct task_struct *t)
{
	struct trace_event_file *trace_file = data;
	struct trace_event_buffer fbuffer;
	struct wake_event *entry;

	if (trace_trigger_soft_disabled(trace_file))
		return;

	entry = trace_event_buffer_reserve(&fbuffer, trace_file,
					   sizeof(*entry));

	if (!entry)
		return;

	entry->prio = t->prio;
	entry->pid = t->pid;

	trace_event_buffer_commit(&fbuffer);
}

static struct trace_event_class sched_waking_class = {
	.system			= THIS_SYSTEM,
	.reg			= trace_event_reg,
	.fields_array		= sched_waking_fields,
	.fields			= LIST_HEAD_INIT(sched_waking_class.fields),
	.probe			= sched_waking_probe,
};

static enum print_line_t sched_switch_output(struct trace_iterator *iter, int flags,
					     struct trace_event *trace_event)
{
	struct trace_seq *s = &iter->seq;
	struct sched_event *REC = (struct sched_event *)iter->ent;
	int ret;

	ret = trace_raw_output_prep(iter, trace_event);
	if (ret != TRACE_TYPE_HANDLED)
		return ret;

	trace_seq_printf(s, SCHED_PRINT_FMT);
	trace_seq_putc(s, '\n');

	return trace_handle_return(s);
}

static struct trace_event_functions sched_switch_funcs = {
	.trace			= sched_switch_output,
};

static enum print_line_t sched_waking_output(struct trace_iterator *iter, int flags,
					     struct trace_event *trace_event)
{
	struct trace_seq *s = &iter->seq;
	struct wake_event *REC = (struct wake_event *)iter->ent;
	int ret;

	ret = trace_raw_output_prep(iter, trace_event);
	if (ret != TRACE_TYPE_HANDLED)
		return ret;

	trace_seq_printf(s, SCHED_WAKING_FMT);
	trace_seq_putc(s, '\n');

	return trace_handle_return(s);
}

static struct trace_event_functions sched_waking_funcs = {
	.trace			= sched_waking_output,
};

#undef C
#define C(a, b...) #a "," __stringify(b)

static struct trace_event_call sched_switch_call = {
	.class			= &sched_switch_class,
	.event			= {
		.funcs			= &sched_switch_funcs,
	},
	.print_fmt		= SCHED_PRINT_FMT,
	.module			= THIS_MODULE,
	.flags			= TRACE_EVENT_FL_TRACEPOINT,
};

static struct trace_event_call sched_waking_call = {
	.class			= &sched_waking_class,
	.event			= {
		.funcs			= &sched_waking_funcs,
	},
	.print_fmt		= SCHED_WAKING_FMT,
	.module			= THIS_MODULE,
	.flags			= TRACE_EVENT_FL_TRACEPOINT,
};

static void fct(struct tracepoint *tp, void *priv)
{
	if (tp->name && strcmp(tp->name, "sched_switch") == 0)
		sched_switch_call.tp = tp;
	else if (tp->name && strcmp(tp->name, "sched_waking") == 0)
		sched_waking_call.tp = tp;
}

static int add_event(struct trace_event_call *call)
{
	int ret;

	ret = register_trace_event(&call->event);
	if (WARN_ON(!ret))
		return -ENODEV;

	ret = trace_add_event_call(call);
	if (WARN_ON(ret))
		unregister_trace_event(&call->event);

	return ret;
}

static int __init trace_sched_init(void)
{
	int ret;

	check_trace_callback_type_sched_switch(sched_switch_probe);
	check_trace_callback_type_sched_waking(sched_waking_probe);

	for_each_kernel_tracepoint(fct, NULL);

	ret = add_event(&sched_switch_call);
	if (ret)
		return ret;

	ret = add_event(&sched_waking_call);
	if (ret)
		trace_remove_event_call(&sched_switch_call);

	return ret;
}

static void __exit trace_sched_exit(void)
{
	trace_set_clr_event(THIS_SYSTEM, "sched_switch", 0);
	trace_set_clr_event(THIS_SYSTEM, "sched_waking", 0);

	trace_remove_event_call(&sched_switch_call);
	trace_remove_event_call(&sched_waking_call);
}

module_init(trace_sched_init);
module_exit(trace_sched_exit);

MODULE_AUTHOR("Steven Rostedt");
MODULE_DESCRIPTION("Custom scheduling events");
MODULE_LICENSE("GPL");