projects/linux·blob master · refs

615 lines · 14.7 KiB

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 "dso.h"
11#include "evsel.h"
12#include "machine.h"
13#include "thread.h"
14#include "comm.h"
15#include "symbol.h"
16#include "map.h"
17#include "event.h"
18#include "thread-stack.h"
19#include "callchain.h"
20#include "call-path.h"
21#include "db-export.h"
22
23int db_export__init(struct db_export *dbe)
24{
25 memset(dbe, 0, sizeof(struct db_export));
26 return 0;
27}
28
29void db_export__exit(struct db_export *dbe)
30{
31 call_return_processor__free(dbe->crp);
32 dbe->crp = NULL;
33}
34
35int db_export__evsel(struct db_export *dbe, struct evsel *evsel)
36{
37 if (evsel->db_id)
38 return 0;
39
40 evsel->db_id = ++dbe->evsel_last_db_id;
41
42 if (dbe->export_evsel)
43 return dbe->export_evsel(dbe, evsel);
44
45 return 0;
46}
47
48int db_export__machine(struct db_export *dbe, struct machine *machine)
49{
50 if (machine->db_id)
51 return 0;
52
53 machine->db_id = ++dbe->machine_last_db_id;
54
55 if (dbe->export_machine)
56 return dbe->export_machine(dbe, machine);
57
58 return 0;
59}
60
61int db_export__thread(struct db_export *dbe, struct thread *thread,
62 struct machine *machine, struct thread *main_thread)
63{
64 u64 main_thread_db_id = 0;
65
66 if (thread__db_id(thread))
67 return 0;
68
69 thread__set_db_id(thread, ++dbe->thread_last_db_id);
70
71 if (main_thread)
72 main_thread_db_id = thread__db_id(main_thread);
73
74 if (dbe->export_thread)
75 return dbe->export_thread(dbe, thread, main_thread_db_id,
76 machine);
77
78 return 0;
79}
80
81static int __db_export__comm(struct db_export *dbe, struct comm *comm,
82 struct thread *thread)
83{
84 comm->db_id = ++dbe->comm_last_db_id;
85
86 if (dbe->export_comm)
87 return dbe->export_comm(dbe, comm, thread);
88
89 return 0;
90}
91
92int db_export__comm(struct db_export *dbe, struct comm *comm,
93 struct thread *thread)
94{
95 if (comm->db_id)
96 return 0;
97
98 return __db_export__comm(dbe, comm, thread);
99}
100
101/*
102 * Export the "exec" comm. The "exec" comm is the program / application command
103 * name at the time it first executes. It is used to group threads for the same
104 * program. Note that the main thread pid (or thread group id tgid) cannot be
105 * used because it does not change when a new program is exec'ed.
106 */
107int db_export__exec_comm(struct db_export *dbe, struct comm *comm,
108 struct thread *main_thread)
109{
110 int err;
111
112 if (comm->db_id)
113 return 0;
114
115 err = __db_export__comm(dbe, comm, main_thread);
116 if (err)
117 return err;
118
119 /*
120 * Record the main thread for this comm. Note that the main thread can
121 * have many "exec" comms because there will be a new one every time it
122 * exec's. An "exec" comm however will only ever have 1 main thread.
123 * That is different to any other threads for that same program because
124 * exec() will effectively kill them, so the relationship between the
125 * "exec" comm and non-main threads is 1-to-1. That is why
126 * db_export__comm_thread() is called here for the main thread, but it
127 * is called for non-main threads when they are exported.
128 */
129 return db_export__comm_thread(dbe, comm, main_thread);
130}
131
132int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
133 struct thread *thread)
134{
135 u64 db_id;
136
137 db_id = ++dbe->comm_thread_last_db_id;
138
139 if (dbe->export_comm_thread)
140 return dbe->export_comm_thread(dbe, db_id, comm, thread);
141
142 return 0;
143}
144
145int db_export__dso(struct db_export *dbe, struct dso *dso,
146 struct machine *machine)
147{
148 if (dso__db_id(dso))
149 return 0;
150
151 dso__set_db_id(dso, ++dbe->dso_last_db_id);
152
153 if (dbe->export_dso)
154 return dbe->export_dso(dbe, dso, machine);
155
156 return 0;
157}
158
159int db_export__symbol(struct db_export *dbe, struct symbol *sym,
160 struct dso *dso)
161{
162 u64 *sym_db_id = symbol__priv(sym);
163
164 if (*sym_db_id)
165 return 0;
166
167 *sym_db_id = ++dbe->symbol_last_db_id;
168
169 if (dbe->export_symbol)
170 return dbe->export_symbol(dbe, sym, dso);
171
172 return 0;
173}
174
175static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
176 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
177{
178 int err;
179
180 if (al->map) {
181 struct dso *dso = map__dso(al->map);
182
183 err = db_export__dso(dbe, dso, maps__machine(thread__maps(al->thread)));
184 if (err)
185 return err;
186 *dso_db_id = dso__db_id(dso);
187
188 if (!al->sym) {
189 al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
190 if (al->sym)
191 dso__insert_symbol(dso, al->sym);
192 }
193
194 if (al->sym) {
195 u64 *db_id = symbol__priv(al->sym);
196
197 err = db_export__symbol(dbe, al->sym, dso);
198 if (err)
199 return err;
200 *sym_db_id = *db_id;
201 *offset = al->addr - al->sym->start;
202 }
203 }
204
205 return 0;
206}
207
208static struct call_path *call_path_from_sample(struct db_export *dbe,
209 struct machine *machine,
210 struct thread *thread,
211 struct perf_sample *sample)
212{
213 u64 kernel_start = machine__kernel_start(machine);
214 struct call_path *current = &dbe->cpr->call_path;
215 enum chain_order saved_order = callchain_param.order;
216 struct callchain_cursor *cursor;
217 int err;
218
219 if (!symbol_conf.use_callchain || !sample->callchain)
220 return NULL;
221
222 /*
223 * Since the call path tree must be built starting with the root, we
224 * must use ORDER_CALL for call chain resolution, in order to process
225 * the callchain starting with the root node and ending with the leaf.
226 */
227 callchain_param.order = ORDER_CALLER;
228 cursor = get_tls_callchain_cursor();
229 err = thread__resolve_callchain(thread, cursor,
230 sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
231 if (err) {
232 callchain_param.order = saved_order;
233 return NULL;
234 }
235 callchain_cursor_commit(cursor);
236
237 while (1) {
238 struct callchain_cursor_node *node;
239 struct addr_location al;
240 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
241
242
243 node = callchain_cursor_current(cursor);
244 if (!node)
245 break;
246
247 /*
248 * Handle export of symbol and dso for this node by
249 * constructing an addr_location struct and then passing it to
250 * db_ids_from_al() to perform the export.
251 */
252 addr_location__init(&al);
253 al.sym = node->ms.sym;
254 al.map = map__get(node->ms.map);
255 al.addr = node->ip;
256 al.thread = thread__get(thread);
257
258 if (al.map && !al.sym)
259 al.sym = dso__find_symbol(map__dso(al.map), al.addr);
260
261 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
262
263 /* add node to the call path tree if it doesn't exist */
264 current = call_path__findnew(dbe->cpr, current,
265 al.sym, node->ip,
266 kernel_start);
267
268 callchain_cursor_advance(cursor);
269 addr_location__exit(&al);
270 }
271
272 /* Reset the callchain order to its prior value. */
273 callchain_param.order = saved_order;
274
275 if (current == &dbe->cpr->call_path) {
276 /* Bail because the callchain was empty. */
277 return NULL;
278 }
279
280 return current;
281}
282
283int db_export__branch_type(struct db_export *dbe, u32 branch_type,
284 const char *name)
285{
286 if (dbe->export_branch_type)
287 return dbe->export_branch_type(dbe, branch_type, name);
288
289 return 0;
290}
291
292static int db_export__threads(struct db_export *dbe, struct thread *thread,
293 struct thread *main_thread,
294 struct machine *machine, struct comm **comm_ptr)
295{
296 struct comm *comm = NULL;
297 struct comm *curr_comm;
298 int err;
299
300 if (main_thread) {
301 /*
302 * A thread has a reference to the main thread, so export the
303 * main thread first.
304 */
305 err = db_export__thread(dbe, main_thread, machine, main_thread);
306 if (err)
307 return err;
308 /*
309 * Export comm before exporting the non-main thread because
310 * db_export__comm_thread() can be called further below.
311 */
312 comm = machine__thread_exec_comm(machine, main_thread);
313 if (comm) {
314 err = db_export__exec_comm(dbe, comm, main_thread);
315 if (err)
316 return err;
317 *comm_ptr = comm;
318 }
319 }
320
321 if (thread != main_thread) {
322 /*
323 * For a non-main thread, db_export__comm_thread() must be
324 * called only if thread has not previously been exported.
325 */
326 bool export_comm_thread = comm && !thread__db_id(thread);
327
328 err = db_export__thread(dbe, thread, machine, main_thread);
329 if (err)
330 return err;
331
332 if (export_comm_thread) {
333 err = db_export__comm_thread(dbe, comm, thread);
334 if (err)
335 return err;
336 }
337 }
338
339 curr_comm = thread__comm(thread);
340 if (curr_comm)
341 return db_export__comm(dbe, curr_comm, thread);
342
343 return 0;
344}
345
346int db_export__sample(struct db_export *dbe, union perf_event *event,
347 struct perf_sample *sample,
348 struct addr_location *al, struct addr_location *addr_al)
349{
350 struct thread *thread = al->thread;
351 struct export_sample es = {
352 .event = event,
353 .sample = sample,
354 .al = al,
355 };
356 struct thread *main_thread;
357 struct comm *comm = NULL;
358 struct machine *machine = NULL;
359 int err;
360
361 if (thread__maps(thread))
362 machine = maps__machine(thread__maps(thread));
363 if (!machine)
364 return -1;
365
366 err = db_export__evsel(dbe, sample->evsel);
367 if (err)
368 return err;
369
370 err = db_export__machine(dbe, machine);
371 if (err)
372 return err;
373
374 main_thread = thread__main_thread(machine, thread);
375
376 err = db_export__threads(dbe, thread, main_thread, machine, &comm);
377 if (err)
378 goto out_put;
379
380 if (comm)
381 es.comm_db_id = comm->db_id;
382
383 es.db_id = ++dbe->sample_last_db_id;
384
385 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
386 if (err)
387 goto out_put;
388
389 if (dbe->cpr) {
390 struct call_path *cp = call_path_from_sample(dbe, machine,
391 thread, sample);
392 if (cp) {
393 db_export__call_path(dbe, cp);
394 es.call_path_id = cp->db_id;
395 }
396 }
397
398 if (addr_al) {
399 err = db_ids_from_al(dbe, addr_al, &es.addr_dso_db_id,
400 &es.addr_sym_db_id, &es.addr_offset);
401 if (err)
402 goto out_put;
403 if (dbe->crp) {
404 err = thread_stack__process(thread, comm, sample, al,
405 addr_al, es.db_id,
406 dbe->crp);
407 if (err)
408 goto out_put;
409 }
410 }
411
412 if (dbe->export_sample)
413 err = dbe->export_sample(dbe, &es);
414
415out_put:
416 thread__put(main_thread);
417 return err;
418}
419
420static struct {
421 u32 branch_type;
422 const char *name;
423} branch_types[] = {
424 {0, "no branch"},
425 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
426 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
427 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
428 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
429 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
430 "software interrupt"},
431 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
432 "return from interrupt"},
433 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
434 "system call"},
435 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
436 "return from system call"},
437 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
438 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
439 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
440 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
441 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
442 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
443 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vm entry"},
444 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vm exit"},
445 {0, NULL}
446};
447
448int db_export__branch_types(struct db_export *dbe)
449{
450 int i, err = 0;
451
452 for (i = 0; branch_types[i].name ; i++) {
453 err = db_export__branch_type(dbe, branch_types[i].branch_type,
454 branch_types[i].name);
455 if (err)
456 break;
457 }
458
459 /* Add trace begin / end variants */
460 for (i = 0; branch_types[i].name ; i++) {
461 const char *name = branch_types[i].name;
462 u32 type = branch_types[i].branch_type;
463 char buf[64];
464
465 if (type == PERF_IP_FLAG_BRANCH ||
466 (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
467 continue;
468
469 snprintf(buf, sizeof(buf), "trace begin / %s", name);
470 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
471 if (err)
472 break;
473
474 snprintf(buf, sizeof(buf), "%s / trace end", name);
475 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
476 if (err)
477 break;
478 }
479
480 return err;
481}
482
483int db_export__call_path(struct db_export *dbe, struct call_path *cp)
484{
485 int err;
486
487 if (cp->db_id)
488 return 0;
489
490 if (cp->parent) {
491 err = db_export__call_path(dbe, cp->parent);
492 if (err)
493 return err;
494 }
495
496 cp->db_id = ++dbe->call_path_last_db_id;
497
498 if (dbe->export_call_path)
499 return dbe->export_call_path(dbe, cp);
500
501 return 0;
502}
503
504int db_export__call_return(struct db_export *dbe, struct call_return *cr,
505 u64 *parent_db_id)
506{
507 int err;
508
509 err = db_export__call_path(dbe, cr->cp);
510 if (err)
511 return err;
512
513 if (!cr->db_id)
514 cr->db_id = ++dbe->call_return_last_db_id;
515
516 if (parent_db_id) {
517 if (!*parent_db_id)
518 *parent_db_id = ++dbe->call_return_last_db_id;
519 cr->parent_db_id = *parent_db_id;
520 }
521
522 if (dbe->export_call_return)
523 return dbe->export_call_return(dbe, cr);
524
525 return 0;
526}
527
528static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,
529 pid_t pid, pid_t tid, u64 *db_id,
530 struct comm **comm_ptr, bool *is_idle)
531{
532 struct thread *thread = machine__find_thread(machine, pid, tid);
533 struct thread *main_thread;
534 int err = 0;
535
536 if (!thread || !thread__comm_set(thread))
537 goto out_put;
538
539 *is_idle = !thread__pid(thread) && !thread__tid(thread);
540
541 main_thread = thread__main_thread(machine, thread);
542
543 err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr);
544
545 *db_id = thread__db_id(thread);
546
547 thread__put(main_thread);
548out_put:
549 thread__put(thread);
550
551 return err;
552}
553
554int db_export__switch(struct db_export *dbe, union perf_event *event,
555 struct perf_sample *sample, struct machine *machine)
556{
557 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
558 bool out_preempt = out &&
559 (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
560 int flags = out | (out_preempt << 1);
561 bool is_idle_a = false, is_idle_b = false;
562 u64 th_a_id = 0, th_b_id = 0;
563 u64 comm_out_id, comm_in_id;
564 struct comm *comm_a = NULL;
565 struct comm *comm_b = NULL;
566 u64 th_out_id, th_in_id;
567 u64 db_id;
568 int err;
569
570 err = db_export__machine(dbe, machine);
571 if (err)
572 return err;
573
574 err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid,
575 &th_a_id, &comm_a, &is_idle_a);
576 if (err)
577 return err;
578
579 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
580 pid_t pid = event->context_switch.next_prev_pid;
581 pid_t tid = event->context_switch.next_prev_tid;
582
583 err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id,
584 &comm_b, &is_idle_b);
585 if (err)
586 return err;
587 }
588
589 /*
590 * Do not export if both threads are unknown (i.e. not being traced),
591 * or one is unknown and the other is the idle task.
592 */
593 if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b))
594 return 0;
595
596 db_id = ++dbe->context_switch_last_db_id;
597
598 if (out) {
599 th_out_id = th_a_id;
600 th_in_id = th_b_id;
601 comm_out_id = comm_a ? comm_a->db_id : 0;
602 comm_in_id = comm_b ? comm_b->db_id : 0;
603 } else {
604 th_out_id = th_b_id;
605 th_in_id = th_a_id;
606 comm_out_id = comm_b ? comm_b->db_id : 0;
607 comm_in_id = comm_a ? comm_a->db_id : 0;
608 }
609
610 if (dbe->export_context_switch)
611 return dbe->export_context_switch(dbe, db_id, machine, sample,
612 th_out_id, comm_out_id,
613 th_in_id, comm_in_id, flags);
614 return 0;
615}