Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
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 perf_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)
67 return 0;
68
69 thread->db_id = ++dbe->thread_last_db_id;
70
71 if (main_thread)
72 main_thread_db_id = main_thread->db_id;
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)
149 return 0;
150
151 dso->db_id = ++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 = al->map->dso;
182
183 err = db_export__dso(dbe, dso, al->machine);
184 if (err)
185 return err;
186 *dso_db_id = dso->db_id;
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 struct perf_evsel *evsel)
213{
214 u64 kernel_start = machine__kernel_start(machine);
215 struct call_path *current = &dbe->cpr->call_path;
216 enum chain_order saved_order = callchain_param.order;
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 err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
229 sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
230 if (err) {
231 callchain_param.order = saved_order;
232 return NULL;
233 }
234 callchain_cursor_commit(&callchain_cursor);
235
236 while (1) {
237 struct callchain_cursor_node *node;
238 struct addr_location al;
239 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
240
241 memset(&al, 0, sizeof(al));
242
243 node = callchain_cursor_current(&callchain_cursor);
244 if (!node)
245 break;
246 /*
247 * Handle export of symbol and dso for this node by
248 * constructing an addr_location struct and then passing it to
249 * db_ids_from_al() to perform the export.
250 */
251 al.sym = node->sym;
252 al.map = node->map;
253 al.machine = machine;
254 al.addr = node->ip;
255
256 if (al.map && !al.sym)
257 al.sym = dso__find_symbol(al.map->dso, al.addr);
258
259 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
260
261 /* add node to the call path tree if it doesn't exist */
262 current = call_path__findnew(dbe->cpr, current,
263 al.sym, node->ip,
264 kernel_start);
265
266 callchain_cursor_advance(&callchain_cursor);
267 }
268
269 /* Reset the callchain order to its prior value. */
270 callchain_param.order = saved_order;
271
272 if (current == &dbe->cpr->call_path) {
273 /* Bail because the callchain was empty. */
274 return NULL;
275 }
276
277 return current;
278}
279
280int db_export__branch_type(struct db_export *dbe, u32 branch_type,
281 const char *name)
282{
283 if (dbe->export_branch_type)
284 return dbe->export_branch_type(dbe, branch_type, name);
285
286 return 0;
287}
288
289static int db_export__threads(struct db_export *dbe, struct thread *thread,
290 struct thread *main_thread,
291 struct machine *machine, struct comm **comm_ptr)
292{
293 struct comm *comm = NULL;
294 struct comm *curr_comm;
295 int err;
296
297 if (main_thread) {
298 /*
299 * A thread has a reference to the main thread, so export the
300 * main thread first.
301 */
302 err = db_export__thread(dbe, main_thread, machine, main_thread);
303 if (err)
304 return err;
305 /*
306 * Export comm before exporting the non-main thread because
307 * db_export__comm_thread() can be called further below.
308 */
309 comm = machine__thread_exec_comm(machine, main_thread);
310 if (comm) {
311 err = db_export__exec_comm(dbe, comm, main_thread);
312 if (err)
313 return err;
314 *comm_ptr = comm;
315 }
316 }
317
318 if (thread != main_thread) {
319 /*
320 * For a non-main thread, db_export__comm_thread() must be
321 * called only if thread has not previously been exported.
322 */
323 bool export_comm_thread = comm && !thread->db_id;
324
325 err = db_export__thread(dbe, thread, machine, main_thread);
326 if (err)
327 return err;
328
329 if (export_comm_thread) {
330 err = db_export__comm_thread(dbe, comm, thread);
331 if (err)
332 return err;
333 }
334 }
335
336 curr_comm = thread__comm(thread);
337 if (curr_comm)
338 return db_export__comm(dbe, curr_comm, thread);
339
340 return 0;
341}
342
343int 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
368 err = db_export__threads(dbe, thread, main_thread, al->machine, &comm);
369 if (err)
370 goto out_put;
371
372 if (comm)
373 es.comm_db_id = comm->db_id;
374
375 es.db_id = ++dbe->sample_last_db_id;
376
377 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
378 if (err)
379 goto out_put;
380
381 if (dbe->cpr) {
382 struct call_path *cp = call_path_from_sample(dbe, al->machine,
383 thread, sample,
384 evsel);
385 if (cp) {
386 db_export__call_path(dbe, cp);
387 es.call_path_id = cp->db_id;
388 }
389 }
390
391 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
392 sample_addr_correlates_sym(&evsel->attr)) {
393 struct addr_location addr_al;
394
395 thread__resolve(thread, &addr_al, sample);
396 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
397 &es.addr_sym_db_id, &es.addr_offset);
398 if (err)
399 goto out_put;
400 if (dbe->crp) {
401 err = thread_stack__process(thread, comm, sample, al,
402 &addr_al, es.db_id,
403 dbe->crp);
404 if (err)
405 goto out_put;
406 }
407 }
408
409 if (dbe->export_sample)
410 err = dbe->export_sample(dbe, &es);
411
412out_put:
413 thread__put(main_thread);
414 return err;
415}
416
417static struct {
418 u32 branch_type;
419 const char *name;
420} branch_types[] = {
421 {0, "no branch"},
422 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
423 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
424 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
425 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
426 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
427 "software interrupt"},
428 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
429 "return from interrupt"},
430 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
431 "system call"},
432 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
433 "return from system call"},
434 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
435 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
436 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
437 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
438 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
439 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
440 {0, NULL}
441};
442
443int db_export__branch_types(struct db_export *dbe)
444{
445 int i, err = 0;
446
447 for (i = 0; branch_types[i].name ; i++) {
448 err = db_export__branch_type(dbe, branch_types[i].branch_type,
449 branch_types[i].name);
450 if (err)
451 break;
452 }
453
454 /* Add trace begin / end variants */
455 for (i = 0; branch_types[i].name ; i++) {
456 const char *name = branch_types[i].name;
457 u32 type = branch_types[i].branch_type;
458 char buf[64];
459
460 if (type == PERF_IP_FLAG_BRANCH ||
461 (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
462 continue;
463
464 snprintf(buf, sizeof(buf), "trace begin / %s", name);
465 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
466 if (err)
467 break;
468
469 snprintf(buf, sizeof(buf), "%s / trace end", name);
470 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
471 if (err)
472 break;
473 }
474
475 return err;
476}
477
478int db_export__call_path(struct db_export *dbe, struct call_path *cp)
479{
480 int err;
481
482 if (cp->db_id)
483 return 0;
484
485 if (cp->parent) {
486 err = db_export__call_path(dbe, cp->parent);
487 if (err)
488 return err;
489 }
490
491 cp->db_id = ++dbe->call_path_last_db_id;
492
493 if (dbe->export_call_path)
494 return dbe->export_call_path(dbe, cp);
495
496 return 0;
497}
498
499int db_export__call_return(struct db_export *dbe, struct call_return *cr,
500 u64 *parent_db_id)
501{
502 int err;
503
504 err = db_export__call_path(dbe, cr->cp);
505 if (err)
506 return err;
507
508 if (!cr->db_id)
509 cr->db_id = ++dbe->call_return_last_db_id;
510
511 if (parent_db_id) {
512 if (!*parent_db_id)
513 *parent_db_id = ++dbe->call_return_last_db_id;
514 cr->parent_db_id = *parent_db_id;
515 }
516
517 if (dbe->export_call_return)
518 return dbe->export_call_return(dbe, cr);
519
520 return 0;
521}
522
523static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,
524 pid_t pid, pid_t tid, u64 *db_id,
525 struct comm **comm_ptr, bool *is_idle)
526{
527 struct thread *thread = machine__find_thread(machine, pid, tid);
528 struct thread *main_thread;
529 int err = 0;
530
531 if (!thread || !thread->comm_set)
532 goto out_put;
533
534 *is_idle = !thread->pid_ && !thread->tid;
535
536 main_thread = thread__main_thread(machine, thread);
537
538 err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr);
539
540 *db_id = thread->db_id;
541
542 thread__put(main_thread);
543out_put:
544 thread__put(thread);
545
546 return err;
547}
548
549int db_export__switch(struct db_export *dbe, union perf_event *event,
550 struct perf_sample *sample, struct machine *machine)
551{
552 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
553 bool out_preempt = out &&
554 (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
555 int flags = out | (out_preempt << 1);
556 bool is_idle_a = false, is_idle_b = false;
557 u64 th_a_id = 0, th_b_id = 0;
558 u64 comm_out_id, comm_in_id;
559 struct comm *comm_a = NULL;
560 struct comm *comm_b = NULL;
561 u64 th_out_id, th_in_id;
562 u64 db_id;
563 int err;
564
565 err = db_export__machine(dbe, machine);
566 if (err)
567 return err;
568
569 err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid,
570 &th_a_id, &comm_a, &is_idle_a);
571 if (err)
572 return err;
573
574 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
575 pid_t pid = event->context_switch.next_prev_pid;
576 pid_t tid = event->context_switch.next_prev_tid;
577
578 err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id,
579 &comm_b, &is_idle_b);
580 if (err)
581 return err;
582 }
583
584 /*
585 * Do not export if both threads are unknown (i.e. not being traced),
586 * or one is unknown and the other is the idle task.
587 */
588 if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b))
589 return 0;
590
591 db_id = ++dbe->context_switch_last_db_id;
592
593 if (out) {
594 th_out_id = th_a_id;
595 th_in_id = th_b_id;
596 comm_out_id = comm_a ? comm_a->db_id : 0;
597 comm_in_id = comm_b ? comm_b->db_id : 0;
598 } else {
599 th_out_id = th_b_id;
600 th_in_id = th_a_id;
601 comm_out_id = comm_b ? comm_b->db_id : 0;
602 comm_in_id = comm_a ? comm_a->db_id : 0;
603 }
604
605 if (dbe->export_context_switch)
606 return dbe->export_context_switch(dbe, db_id, machine, sample,
607 th_out_id, comm_out_id,
608 th_in_id, comm_in_id, flags);
609 return 0;
610}