aboutsummaryrefslogtreecommitdiffstats
path: root/kernel-shark/src/plugins/sched_events.c
blob: 14f8edb2836b61c44e26fdbdbe575abb0b08d135 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// SPDX-License-Identifier: LGPL-2.1

/*
 * Copyright (C) 2018 VMware Inc, Yordan Karadzhov <y.karadz@gmail.com>
 */

/**
 *  @file    sched_events.c
 *  @brief   Defines a callback function for Sched events used to registers the
 *	     "next" task (if not registered already) and to changes the value
 *	     of the "pid" field of the "sched_switch" entries such that, it
 *	     will be ploted as part of the "next" task.
 */

// C
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

// KernelShark
#include "plugins/sched_events.h"

/** Plugin context instance. */
struct plugin_sched_context *plugin_sched_context_handler = NULL;

static bool define_wakeup_event(struct tep_handle *tep, const char *wakeup_name,
				struct tep_event **wakeup_event,
				struct tep_format_field **pid_field)
{
	struct tep_event *event;

	event = tep_find_event_by_name(tep, "sched", wakeup_name);
	if (!event)
		return false;

	*wakeup_event = event;
	*pid_field = tep_find_any_field(event, "pid");

	return true;
}

static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
{
	struct plugin_sched_context *plugin_ctx;
	struct tep_event *event;
	bool wakeup_found;

	/* No context should exist when we initialize the plugin. */
	assert(plugin_sched_context_handler == NULL);

	plugin_sched_context_handler =
		malloc(sizeof(*plugin_sched_context_handler));
	if (!plugin_sched_context_handler) {
		fprintf(stderr,
			"Failed to allocate memory for plugin_sched_context.\n");
		return false;
	}

	plugin_ctx = plugin_sched_context_handler;
	plugin_ctx->handle = kshark_ctx->handle;
	plugin_ctx->pevent = kshark_ctx->pevent;
	plugin_ctx->collections = NULL;

	event = tep_find_event_by_name(plugin_ctx->pevent,
				       "sched", "sched_switch");
	if (!event)
		return false;

	plugin_ctx->sched_switch_event = event;
	plugin_ctx->sched_switch_next_field =
		tep_find_any_field(event, "next_pid");

	plugin_ctx->sched_switch_comm_field =
		tep_find_field(event, "next_comm");

	plugin_ctx->sched_switch_prev_state_field =
		tep_find_field(event, "prev_state");


	wakeup_found = define_wakeup_event(kshark_ctx->pevent, "sched_wakeup",
					   &plugin_ctx->sched_wakeup_event,
					   &plugin_ctx->sched_wakeup_pid_field);

	wakeup_found |= define_wakeup_event(kshark_ctx->pevent, "sched_wakeup_new",
					   &plugin_ctx->sched_wakeup_new_event,
					   &plugin_ctx->sched_wakeup_new_pid_field);

	wakeup_found |= define_wakeup_event(kshark_ctx->pevent, "sched_waking",
					   &plugin_ctx->sched_waking_event,
					   &plugin_ctx->sched_waking_pid_field);

	if (!wakeup_found)
		return false;

	plugin_ctx->second_pass_hash = tracecmd_filter_id_hash_alloc();

	return true;
}

/**
 * @brief Get the Process Id of the next scheduled task.
 *
 * @param record: Input location for a sched_switch record.
 */
int plugin_get_next_pid(struct tep_record *record)
{
	struct plugin_sched_context *plugin_ctx =
		plugin_sched_context_handler;
	unsigned long long val;
	int ret;

	ret = tep_read_number_field(plugin_ctx->sched_switch_next_field,
				    record->data, &val);

	return ret ? : val;
}

static void plugin_register_command(struct kshark_context *kshark_ctx,
				    struct tep_record *record,
				    int pid)
{
	struct plugin_sched_context *plugin_ctx =
		plugin_sched_context_handler;
	const char *comm;

	if (!plugin_ctx->sched_switch_comm_field)
		return;

	comm = record->data + plugin_ctx->sched_switch_comm_field->offset;
	/*
	 * TODO: The retrieve of the name of the command above needs to be
	 * implemented as a wrapper function in libtracevent.
	 */

	if (!tep_pid_is_registered(kshark_ctx->pevent, pid))
			tep_register_comm(kshark_ctx->pevent, comm, pid);
}

int find_wakeup_pid(struct kshark_context *kshark_ctx, struct kshark_entry *e,
		    struct tep_event *wakeup_event, struct tep_format_field *pid_field)
{
	struct tep_record *record;
	unsigned long long val;
	int ret;

	if (!wakeup_event || e->event_id != wakeup_event->id)
		return -1;

	record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
	ret = tep_read_number_field(pid_field, record->data, &val);
	free_record(record);

	if (ret)
		return -1;

	return val;
}

static bool wakeup_match_rec_pid(struct plugin_sched_context *plugin_ctx,
				 struct kshark_context *kshark_ctx,
				 struct kshark_entry *e,
				 int pid)
{
	struct tep_event *wakeup_events[] = {
		plugin_ctx->sched_waking_event,
		plugin_ctx->sched_wakeup_event,
		plugin_ctx->sched_wakeup_new_event,
	};
	struct tep_format_field *wakeup_fields[] = {
		plugin_ctx->sched_waking_pid_field,
		plugin_ctx->sched_wakeup_pid_field,
		plugin_ctx->sched_wakeup_new_pid_field,
	};
	int i, wakeup_pid = -1;

	for (i = 0; i < sizeof(wakeup_events) / sizeof(wakeup_events[0]); i++) {
		wakeup_pid = find_wakeup_pid(kshark_ctx, e, wakeup_events[i], wakeup_fields[i]);
		if (wakeup_pid >= 0)
			break;
	}

	if (wakeup_pid >= 0 && wakeup_pid == pid)
		return true;

	return false;
}

/**
 * @brief Process Id matching function adapted for sched_wakeup and
 *	  sched_wakeup_new events.
 *
 * @param kshark_ctx: Input location for the session context pointer.
 * @param e: kshark_entry to be checked.
 * @param pid: Matching condition value.
 *
 * @returns True if the Pid of the record matches the value of "pid".
 *	    Otherwise false.
 */
bool plugin_wakeup_match_rec_pid(struct kshark_context *kshark_ctx,
				 struct kshark_entry *e,
				 int pid)
{
	struct plugin_sched_context *plugin_ctx;

	plugin_ctx = plugin_sched_context_handler;
	if (!plugin_ctx)
		return false;

	return wakeup_match_rec_pid(plugin_ctx, kshark_ctx, e, pid);
}

/**
 * @brief Process Id matching function adapted for sched_switch events.
 *
 * @param kshark_ctx: Input location for the session context pointer.
 * @param e: kshark_entry to be checked.
 * @param pid: Matching condition value.
 *
 * @returns True if the Pid of the record matches the value of "pid".
 *	    Otherwise false.
 */
bool plugin_switch_match_rec_pid(struct kshark_context *kshark_ctx,
				 struct kshark_entry *e,
				 int pid)
{
	struct plugin_sched_context *plugin_ctx;
	unsigned long long val;
	int ret, switch_pid = -1;

	plugin_ctx = plugin_sched_context_handler;

	if (plugin_ctx->sched_switch_event &&
	    e->event_id == plugin_ctx->sched_switch_event->id) {
		struct tep_record *record;

		record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
		ret = tep_read_number_field(plugin_ctx->sched_switch_prev_state_field,
					    record->data, &val);

		if (ret == 0 && !(val & 0x7f))
			switch_pid = tep_data_pid(plugin_ctx->pevent, record);

		free_record(record);
	}

	if (switch_pid >= 0 && switch_pid == pid)
		return true;

	return false;
}

/**
 * @brief Process Id matching function adapted for sched_switch events.
 *
 * @param kshark_ctx: Input location for the session context pointer.
 * @param e: kshark_entry to be checked.
 * @param pid: Matching condition value.
 *
 * @returns True if the Pid of the entry matches the value of "pid".
 *	    Otherwise false.
 */
bool plugin_switch_match_entry_pid(struct kshark_context *kshark_ctx,
				   struct kshark_entry *e,
				   int pid)
{
	struct plugin_sched_context *plugin_ctx;

	plugin_ctx = plugin_sched_context_handler;

	if (plugin_ctx->sched_switch_event &&
	    e->event_id == plugin_ctx->sched_switch_event->id &&
	    e->pid == pid)
		return true;

	return false;
}

/**
 * @brief A match function to be used to process a data collections for
 *	  the Sched events plugin.
 *
 * @param kshark_ctx: Input location for the session context pointer.
 * @param e: kshark_entry to be checked.
 * @param pid: Matching condition value.
 *
 * @returns True if the entry is relevant for the Sched events plugin.
 *	    Otherwise false.
 */
bool plugin_match_pid(struct kshark_context *kshark_ctx,
		      struct kshark_entry *e, int pid)
{
	return plugin_switch_match_entry_pid(kshark_ctx, e, pid) ||
	       plugin_switch_match_rec_pid(kshark_ctx, e, pid) ||
	       plugin_wakeup_match_rec_pid(kshark_ctx, e, pid);
}

static void plugin_sched_action(struct kshark_context *kshark_ctx,
				struct tep_record *rec,
				struct kshark_entry *entry)
{
	int pid = plugin_get_next_pid(rec);
	if (pid >= 0) {
		entry->pid = pid;
		plugin_register_command(kshark_ctx, rec, entry->pid);
	}
}

static int plugin_sched_init(struct kshark_context *kshark_ctx)
{
	struct plugin_sched_context *plugin_ctx;

	if (!plugin_sched_init_context(kshark_ctx)) {
		free(plugin_sched_context_handler);
		plugin_sched_context_handler = NULL;
		return 0;
	}

	plugin_ctx = plugin_sched_context_handler;

	kshark_register_event_handler(&kshark_ctx->event_handlers,
				      plugin_ctx->sched_switch_event->id,
				      plugin_sched_action,
				      plugin_draw);

	return 1;
}

static int plugin_sched_close(struct kshark_context *kshark_ctx)
{
	struct plugin_sched_context *plugin_ctx;

	if (!plugin_sched_context_handler)
		return 0;

	plugin_ctx = plugin_sched_context_handler;

	kshark_unregister_event_handler(&kshark_ctx->event_handlers,
					plugin_ctx->sched_switch_event->id,
					plugin_sched_action,
					plugin_draw);

	tracecmd_filter_id_hash_free(plugin_ctx->second_pass_hash);

	kshark_free_collection_list(plugin_ctx->collections);
	free(plugin_ctx);
	plugin_sched_context_handler = NULL;

	return 1;
}

/** Load this plugin. */
int KSHARK_PLUGIN_INITIALIZER(struct kshark_context *kshark_ctx)
{
	return plugin_sched_init(kshark_ctx);
}

/** Unload this plugin. */
int KSHARK_PLUGIN_DEINITIALIZER(struct kshark_context *kshark_ctx)
{
	return plugin_sched_close(kshark_ctx);
}