xref: /openbmc/linux/tools/perf/util/db-export.c (revision 113094f7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * db-export.c: Support for exporting data suitable for import to a database
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 
7 #include <errno.h>
8 #include <stdlib.h>
9 
10 #include "evsel.h"
11 #include "machine.h"
12 #include "thread.h"
13 #include "comm.h"
14 #include "symbol.h"
15 #include "map.h"
16 #include "event.h"
17 #include "thread-stack.h"
18 #include "callchain.h"
19 #include "call-path.h"
20 #include "db-export.h"
21 #include <linux/zalloc.h>
22 
23 struct deferred_export {
24 	struct list_head node;
25 	struct comm *comm;
26 };
27 
28 static int db_export__deferred(struct db_export *dbe)
29 {
30 	struct deferred_export *de;
31 	int err;
32 
33 	while (!list_empty(&dbe->deferred)) {
34 		de = list_entry(dbe->deferred.next, struct deferred_export,
35 				node);
36 		err = dbe->export_comm(dbe, de->comm);
37 		list_del_init(&de->node);
38 		free(de);
39 		if (err)
40 			return err;
41 	}
42 
43 	return 0;
44 }
45 
46 static void db_export__free_deferred(struct db_export *dbe)
47 {
48 	struct deferred_export *de;
49 
50 	while (!list_empty(&dbe->deferred)) {
51 		de = list_entry(dbe->deferred.next, struct deferred_export,
52 				node);
53 		list_del_init(&de->node);
54 		free(de);
55 	}
56 }
57 
58 static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
59 {
60 	struct deferred_export *de;
61 
62 	de = zalloc(sizeof(struct deferred_export));
63 	if (!de)
64 		return -ENOMEM;
65 
66 	de->comm = comm;
67 	list_add_tail(&de->node, &dbe->deferred);
68 
69 	return 0;
70 }
71 
72 int db_export__init(struct db_export *dbe)
73 {
74 	memset(dbe, 0, sizeof(struct db_export));
75 	INIT_LIST_HEAD(&dbe->deferred);
76 	return 0;
77 }
78 
79 int db_export__flush(struct db_export *dbe)
80 {
81 	return db_export__deferred(dbe);
82 }
83 
84 void db_export__exit(struct db_export *dbe)
85 {
86 	db_export__free_deferred(dbe);
87 	call_return_processor__free(dbe->crp);
88 	dbe->crp = NULL;
89 }
90 
91 int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
92 {
93 	if (evsel->db_id)
94 		return 0;
95 
96 	evsel->db_id = ++dbe->evsel_last_db_id;
97 
98 	if (dbe->export_evsel)
99 		return dbe->export_evsel(dbe, evsel);
100 
101 	return 0;
102 }
103 
104 int db_export__machine(struct db_export *dbe, struct machine *machine)
105 {
106 	if (machine->db_id)
107 		return 0;
108 
109 	machine->db_id = ++dbe->machine_last_db_id;
110 
111 	if (dbe->export_machine)
112 		return dbe->export_machine(dbe, machine);
113 
114 	return 0;
115 }
116 
117 int db_export__thread(struct db_export *dbe, struct thread *thread,
118 		      struct machine *machine, struct comm *comm)
119 {
120 	struct thread *main_thread;
121 	u64 main_thread_db_id = 0;
122 	int err;
123 
124 	if (thread->db_id)
125 		return 0;
126 
127 	thread->db_id = ++dbe->thread_last_db_id;
128 
129 	if (thread->pid_ != -1) {
130 		if (thread->pid_ == thread->tid) {
131 			main_thread = thread;
132 		} else {
133 			main_thread = machine__findnew_thread(machine,
134 							      thread->pid_,
135 							      thread->pid_);
136 			if (!main_thread)
137 				return -ENOMEM;
138 			err = db_export__thread(dbe, main_thread, machine,
139 						comm);
140 			if (err)
141 				goto out_put;
142 			if (comm) {
143 				err = db_export__comm_thread(dbe, comm, thread);
144 				if (err)
145 					goto out_put;
146 			}
147 		}
148 		main_thread_db_id = main_thread->db_id;
149 		if (main_thread != thread)
150 			thread__put(main_thread);
151 	}
152 
153 	if (dbe->export_thread)
154 		return dbe->export_thread(dbe, thread, main_thread_db_id,
155 					  machine);
156 
157 	return 0;
158 
159 out_put:
160 	thread__put(main_thread);
161 	return err;
162 }
163 
164 int db_export__comm(struct db_export *dbe, struct comm *comm,
165 		    struct thread *main_thread)
166 {
167 	int err;
168 
169 	if (comm->db_id)
170 		return 0;
171 
172 	comm->db_id = ++dbe->comm_last_db_id;
173 
174 	if (dbe->export_comm) {
175 		if (main_thread->comm_set)
176 			err = dbe->export_comm(dbe, comm);
177 		else
178 			err = db_export__defer_comm(dbe, comm);
179 		if (err)
180 			return err;
181 	}
182 
183 	return db_export__comm_thread(dbe, comm, main_thread);
184 }
185 
186 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
187 			   struct thread *thread)
188 {
189 	u64 db_id;
190 
191 	db_id = ++dbe->comm_thread_last_db_id;
192 
193 	if (dbe->export_comm_thread)
194 		return dbe->export_comm_thread(dbe, db_id, comm, thread);
195 
196 	return 0;
197 }
198 
199 int db_export__dso(struct db_export *dbe, struct dso *dso,
200 		   struct machine *machine)
201 {
202 	if (dso->db_id)
203 		return 0;
204 
205 	dso->db_id = ++dbe->dso_last_db_id;
206 
207 	if (dbe->export_dso)
208 		return dbe->export_dso(dbe, dso, machine);
209 
210 	return 0;
211 }
212 
213 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
214 		      struct dso *dso)
215 {
216 	u64 *sym_db_id = symbol__priv(sym);
217 
218 	if (*sym_db_id)
219 		return 0;
220 
221 	*sym_db_id = ++dbe->symbol_last_db_id;
222 
223 	if (dbe->export_symbol)
224 		return dbe->export_symbol(dbe, sym, dso);
225 
226 	return 0;
227 }
228 
229 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
230 			  u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
231 {
232 	int err;
233 
234 	if (al->map) {
235 		struct dso *dso = al->map->dso;
236 
237 		err = db_export__dso(dbe, dso, al->machine);
238 		if (err)
239 			return err;
240 		*dso_db_id = dso->db_id;
241 
242 		if (!al->sym) {
243 			al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
244 			if (al->sym)
245 				dso__insert_symbol(dso, al->sym);
246 		}
247 
248 		if (al->sym) {
249 			u64 *db_id = symbol__priv(al->sym);
250 
251 			err = db_export__symbol(dbe, al->sym, dso);
252 			if (err)
253 				return err;
254 			*sym_db_id = *db_id;
255 			*offset = al->addr - al->sym->start;
256 		}
257 	}
258 
259 	return 0;
260 }
261 
262 static struct call_path *call_path_from_sample(struct db_export *dbe,
263 					       struct machine *machine,
264 					       struct thread *thread,
265 					       struct perf_sample *sample,
266 					       struct perf_evsel *evsel)
267 {
268 	u64 kernel_start = machine__kernel_start(machine);
269 	struct call_path *current = &dbe->cpr->call_path;
270 	enum chain_order saved_order = callchain_param.order;
271 	int err;
272 
273 	if (!symbol_conf.use_callchain || !sample->callchain)
274 		return NULL;
275 
276 	/*
277 	 * Since the call path tree must be built starting with the root, we
278 	 * must use ORDER_CALL for call chain resolution, in order to process
279 	 * the callchain starting with the root node and ending with the leaf.
280 	 */
281 	callchain_param.order = ORDER_CALLER;
282 	err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
283 					sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
284 	if (err) {
285 		callchain_param.order = saved_order;
286 		return NULL;
287 	}
288 	callchain_cursor_commit(&callchain_cursor);
289 
290 	while (1) {
291 		struct callchain_cursor_node *node;
292 		struct addr_location al;
293 		u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
294 
295 		memset(&al, 0, sizeof(al));
296 
297 		node = callchain_cursor_current(&callchain_cursor);
298 		if (!node)
299 			break;
300 		/*
301 		 * Handle export of symbol and dso for this node by
302 		 * constructing an addr_location struct and then passing it to
303 		 * db_ids_from_al() to perform the export.
304 		 */
305 		al.sym = node->sym;
306 		al.map = node->map;
307 		al.machine = machine;
308 		al.addr = node->ip;
309 
310 		if (al.map && !al.sym)
311 			al.sym = dso__find_symbol(al.map->dso, al.addr);
312 
313 		db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
314 
315 		/* add node to the call path tree if it doesn't exist */
316 		current = call_path__findnew(dbe->cpr, current,
317 					     al.sym, node->ip,
318 					     kernel_start);
319 
320 		callchain_cursor_advance(&callchain_cursor);
321 	}
322 
323 	/* Reset the callchain order to its prior value. */
324 	callchain_param.order = saved_order;
325 
326 	if (current == &dbe->cpr->call_path) {
327 		/* Bail because the callchain was empty. */
328 		return NULL;
329 	}
330 
331 	return current;
332 }
333 
334 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
335 			   const char *name)
336 {
337 	if (dbe->export_branch_type)
338 		return dbe->export_branch_type(dbe, branch_type, name);
339 
340 	return 0;
341 }
342 
343 int db_export__sample(struct db_export *dbe, union perf_event *event,
344 		      struct perf_sample *sample, struct perf_evsel *evsel,
345 		      struct addr_location *al)
346 {
347 	struct thread* thread = al->thread;
348 	struct export_sample es = {
349 		.event = event,
350 		.sample = sample,
351 		.evsel = evsel,
352 		.al = al,
353 	};
354 	struct thread *main_thread;
355 	struct comm *comm = NULL;
356 	int err;
357 
358 	err = db_export__evsel(dbe, evsel);
359 	if (err)
360 		return err;
361 
362 	err = db_export__machine(dbe, al->machine);
363 	if (err)
364 		return err;
365 
366 	main_thread = thread__main_thread(al->machine, thread);
367 	if (main_thread)
368 		comm = machine__thread_exec_comm(al->machine, main_thread);
369 
370 	err = db_export__thread(dbe, thread, al->machine, comm);
371 	if (err)
372 		goto out_put;
373 
374 	if (comm) {
375 		err = db_export__comm(dbe, comm, main_thread);
376 		if (err)
377 			goto out_put;
378 		es.comm_db_id = comm->db_id;
379 	}
380 
381 	es.db_id = ++dbe->sample_last_db_id;
382 
383 	err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
384 	if (err)
385 		goto out_put;
386 
387 	if (dbe->cpr) {
388 		struct call_path *cp = call_path_from_sample(dbe, al->machine,
389 							     thread, sample,
390 							     evsel);
391 		if (cp) {
392 			db_export__call_path(dbe, cp);
393 			es.call_path_id = cp->db_id;
394 		}
395 	}
396 
397 	if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
398 	    sample_addr_correlates_sym(&evsel->attr)) {
399 		struct addr_location addr_al;
400 
401 		thread__resolve(thread, &addr_al, sample);
402 		err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
403 				     &es.addr_sym_db_id, &es.addr_offset);
404 		if (err)
405 			goto out_put;
406 		if (dbe->crp) {
407 			err = thread_stack__process(thread, comm, sample, al,
408 						    &addr_al, es.db_id,
409 						    dbe->crp);
410 			if (err)
411 				goto out_put;
412 		}
413 	}
414 
415 	if (dbe->export_sample)
416 		err = dbe->export_sample(dbe, &es);
417 
418 out_put:
419 	thread__put(main_thread);
420 	return err;
421 }
422 
423 static struct {
424 	u32 branch_type;
425 	const char *name;
426 } branch_types[] = {
427 	{0, "no branch"},
428 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
429 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
430 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
431 	{PERF_IP_FLAG_BRANCH, "unconditional jump"},
432 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
433 	 "software interrupt"},
434 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
435 	 "return from interrupt"},
436 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
437 	 "system call"},
438 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
439 	 "return from system call"},
440 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
441 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
442 	 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
443 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
444 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
445 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
446 	{0, NULL}
447 };
448 
449 int db_export__branch_types(struct db_export *dbe)
450 {
451 	int i, err = 0;
452 
453 	for (i = 0; branch_types[i].name ; i++) {
454 		err = db_export__branch_type(dbe, branch_types[i].branch_type,
455 					     branch_types[i].name);
456 		if (err)
457 			break;
458 	}
459 
460 	/* Add trace begin / end variants */
461 	for (i = 0; branch_types[i].name ; i++) {
462 		const char *name = branch_types[i].name;
463 		u32 type = branch_types[i].branch_type;
464 		char buf[64];
465 
466 		if (type == PERF_IP_FLAG_BRANCH ||
467 		    (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
468 			continue;
469 
470 		snprintf(buf, sizeof(buf), "trace begin / %s", name);
471 		err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
472 		if (err)
473 			break;
474 
475 		snprintf(buf, sizeof(buf), "%s / trace end", name);
476 		err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
477 		if (err)
478 			break;
479 	}
480 
481 	return err;
482 }
483 
484 int db_export__call_path(struct db_export *dbe, struct call_path *cp)
485 {
486 	int err;
487 
488 	if (cp->db_id)
489 		return 0;
490 
491 	if (cp->parent) {
492 		err = db_export__call_path(dbe, cp->parent);
493 		if (err)
494 			return err;
495 	}
496 
497 	cp->db_id = ++dbe->call_path_last_db_id;
498 
499 	if (dbe->export_call_path)
500 		return dbe->export_call_path(dbe, cp);
501 
502 	return 0;
503 }
504 
505 int db_export__call_return(struct db_export *dbe, struct call_return *cr,
506 			   u64 *parent_db_id)
507 {
508 	int err;
509 
510 	err = db_export__call_path(dbe, cr->cp);
511 	if (err)
512 		return err;
513 
514 	if (!cr->db_id)
515 		cr->db_id = ++dbe->call_return_last_db_id;
516 
517 	if (parent_db_id) {
518 		if (!*parent_db_id)
519 			*parent_db_id = ++dbe->call_return_last_db_id;
520 		cr->parent_db_id = *parent_db_id;
521 	}
522 
523 	if (dbe->export_call_return)
524 		return dbe->export_call_return(dbe, cr);
525 
526 	return 0;
527 }
528