1 // SPDX-License-Identifier: GPL-2.0-only
3 * db-export.c: Support for exporting data suitable for import to a database
4 * Copyright (c) 2014, Intel Corporation.
18 #include "thread-stack.h"
19 #include "callchain.h"
20 #include "call-path.h"
21 #include "db-export.h"
22 #include <linux/zalloc.h>
24 int db_export__init(struct db_export
*dbe
)
26 memset(dbe
, 0, sizeof(struct db_export
));
30 void db_export__exit(struct db_export
*dbe
)
32 call_return_processor__free(dbe
->crp
);
36 int db_export__evsel(struct db_export
*dbe
, struct evsel
*evsel
)
41 evsel
->db_id
= ++dbe
->evsel_last_db_id
;
43 if (dbe
->export_evsel
)
44 return dbe
->export_evsel(dbe
, evsel
);
49 int db_export__machine(struct db_export
*dbe
, struct machine
*machine
)
54 machine
->db_id
= ++dbe
->machine_last_db_id
;
56 if (dbe
->export_machine
)
57 return dbe
->export_machine(dbe
, machine
);
62 int db_export__thread(struct db_export
*dbe
, struct thread
*thread
,
63 struct machine
*machine
, struct thread
*main_thread
)
65 u64 main_thread_db_id
= 0;
67 if (thread__db_id(thread
))
70 thread__set_db_id(thread
, ++dbe
->thread_last_db_id
);
73 main_thread_db_id
= thread__db_id(main_thread
);
75 if (dbe
->export_thread
)
76 return dbe
->export_thread(dbe
, thread
, main_thread_db_id
,
82 static int __db_export__comm(struct db_export
*dbe
, struct comm
*comm
,
83 struct thread
*thread
)
85 comm
->db_id
= ++dbe
->comm_last_db_id
;
88 return dbe
->export_comm(dbe
, comm
, thread
);
93 int db_export__comm(struct db_export
*dbe
, struct comm
*comm
,
94 struct thread
*thread
)
99 return __db_export__comm(dbe
, comm
, thread
);
103 * Export the "exec" comm. The "exec" comm is the program / application command
104 * name at the time it first executes. It is used to group threads for the same
105 * program. Note that the main thread pid (or thread group id tgid) cannot be
106 * used because it does not change when a new program is exec'ed.
108 int db_export__exec_comm(struct db_export
*dbe
, struct comm
*comm
,
109 struct thread
*main_thread
)
116 err
= __db_export__comm(dbe
, comm
, main_thread
);
121 * Record the main thread for this comm. Note that the main thread can
122 * have many "exec" comms because there will be a new one every time it
123 * exec's. An "exec" comm however will only ever have 1 main thread.
124 * That is different to any other threads for that same program because
125 * exec() will effectively kill them, so the relationship between the
126 * "exec" comm and non-main threads is 1-to-1. That is why
127 * db_export__comm_thread() is called here for the main thread, but it
128 * is called for non-main threads when they are exported.
130 return db_export__comm_thread(dbe
, comm
, main_thread
);
133 int db_export__comm_thread(struct db_export
*dbe
, struct comm
*comm
,
134 struct thread
*thread
)
138 db_id
= ++dbe
->comm_thread_last_db_id
;
140 if (dbe
->export_comm_thread
)
141 return dbe
->export_comm_thread(dbe
, db_id
, comm
, thread
);
146 int db_export__dso(struct db_export
*dbe
, struct dso
*dso
,
147 struct machine
*machine
)
152 dso__set_db_id(dso
, ++dbe
->dso_last_db_id
);
155 return dbe
->export_dso(dbe
, dso
, machine
);
160 int db_export__symbol(struct db_export
*dbe
, struct symbol
*sym
,
163 u64
*sym_db_id
= symbol__priv(sym
);
168 *sym_db_id
= ++dbe
->symbol_last_db_id
;
170 if (dbe
->export_symbol
)
171 return dbe
->export_symbol(dbe
, sym
, dso
);
176 static int db_ids_from_al(struct db_export
*dbe
, struct addr_location
*al
,
177 u64
*dso_db_id
, u64
*sym_db_id
, u64
*offset
)
182 struct dso
*dso
= map__dso(al
->map
);
184 err
= db_export__dso(dbe
, dso
, maps__machine(al
->maps
));
187 *dso_db_id
= dso__db_id(dso
);
190 al
->sym
= symbol__new(al
->addr
, 0, 0, 0, "unknown");
192 dso__insert_symbol(dso
, al
->sym
);
196 u64
*db_id
= symbol__priv(al
->sym
);
198 err
= db_export__symbol(dbe
, al
->sym
, dso
);
202 *offset
= al
->addr
- al
->sym
->start
;
209 static struct call_path
*call_path_from_sample(struct db_export
*dbe
,
210 struct machine
*machine
,
211 struct thread
*thread
,
212 struct perf_sample
*sample
,
215 u64 kernel_start
= machine__kernel_start(machine
);
216 struct call_path
*current
= &dbe
->cpr
->call_path
;
217 enum chain_order saved_order
= callchain_param
.order
;
218 struct callchain_cursor
*cursor
;
221 if (!symbol_conf
.use_callchain
|| !sample
->callchain
)
225 * Since the call path tree must be built starting with the root, we
226 * must use ORDER_CALL for call chain resolution, in order to process
227 * the callchain starting with the root node and ending with the leaf.
229 callchain_param
.order
= ORDER_CALLER
;
230 cursor
= get_tls_callchain_cursor();
231 err
= thread__resolve_callchain(thread
, cursor
, evsel
,
232 sample
, NULL
, NULL
, PERF_MAX_STACK_DEPTH
);
234 callchain_param
.order
= saved_order
;
237 callchain_cursor_commit(cursor
);
240 struct callchain_cursor_node
*node
;
241 struct addr_location al
;
242 u64 dso_db_id
= 0, sym_db_id
= 0, offset
= 0;
245 node
= callchain_cursor_current(cursor
);
250 * Handle export of symbol and dso for this node by
251 * constructing an addr_location struct and then passing it to
252 * db_ids_from_al() to perform the export.
254 addr_location__init(&al
);
255 al
.sym
= node
->ms
.sym
;
256 al
.map
= map__get(node
->ms
.map
);
257 al
.maps
= maps__get(thread__maps(thread
));
260 if (al
.map
&& !al
.sym
)
261 al
.sym
= dso__find_symbol(map__dso(al
.map
), al
.addr
);
263 db_ids_from_al(dbe
, &al
, &dso_db_id
, &sym_db_id
, &offset
);
265 /* add node to the call path tree if it doesn't exist */
266 current
= call_path__findnew(dbe
->cpr
, current
,
270 callchain_cursor_advance(cursor
);
271 addr_location__exit(&al
);
274 /* Reset the callchain order to its prior value. */
275 callchain_param
.order
= saved_order
;
277 if (current
== &dbe
->cpr
->call_path
) {
278 /* Bail because the callchain was empty. */
285 int db_export__branch_type(struct db_export
*dbe
, u32 branch_type
,
288 if (dbe
->export_branch_type
)
289 return dbe
->export_branch_type(dbe
, branch_type
, name
);
294 static int db_export__threads(struct db_export
*dbe
, struct thread
*thread
,
295 struct thread
*main_thread
,
296 struct machine
*machine
, struct comm
**comm_ptr
)
298 struct comm
*comm
= NULL
;
299 struct comm
*curr_comm
;
304 * A thread has a reference to the main thread, so export the
307 err
= db_export__thread(dbe
, main_thread
, machine
, main_thread
);
311 * Export comm before exporting the non-main thread because
312 * db_export__comm_thread() can be called further below.
314 comm
= machine__thread_exec_comm(machine
, main_thread
);
316 err
= db_export__exec_comm(dbe
, comm
, main_thread
);
323 if (thread
!= main_thread
) {
325 * For a non-main thread, db_export__comm_thread() must be
326 * called only if thread has not previously been exported.
328 bool export_comm_thread
= comm
&& !thread__db_id(thread
);
330 err
= db_export__thread(dbe
, thread
, machine
, main_thread
);
334 if (export_comm_thread
) {
335 err
= db_export__comm_thread(dbe
, comm
, thread
);
341 curr_comm
= thread__comm(thread
);
343 return db_export__comm(dbe
, curr_comm
, thread
);
348 int db_export__sample(struct db_export
*dbe
, union perf_event
*event
,
349 struct perf_sample
*sample
, struct evsel
*evsel
,
350 struct addr_location
*al
, struct addr_location
*addr_al
)
352 struct thread
*thread
= al
->thread
;
353 struct export_sample es
= {
359 struct thread
*main_thread
;
360 struct comm
*comm
= NULL
;
361 struct machine
*machine
;
364 err
= db_export__evsel(dbe
, evsel
);
368 machine
= maps__machine(al
->maps
);
369 err
= db_export__machine(dbe
, machine
);
373 main_thread
= thread__main_thread(machine
, thread
);
375 err
= db_export__threads(dbe
, thread
, main_thread
, machine
, &comm
);
380 es
.comm_db_id
= comm
->db_id
;
382 es
.db_id
= ++dbe
->sample_last_db_id
;
384 err
= db_ids_from_al(dbe
, al
, &es
.dso_db_id
, &es
.sym_db_id
, &es
.offset
);
389 struct call_path
*cp
= call_path_from_sample(dbe
, machine
,
393 db_export__call_path(dbe
, cp
);
394 es
.call_path_id
= cp
->db_id
;
399 err
= db_ids_from_al(dbe
, addr_al
, &es
.addr_dso_db_id
,
400 &es
.addr_sym_db_id
, &es
.addr_offset
);
404 err
= thread_stack__process(thread
, comm
, sample
, al
,
412 if (dbe
->export_sample
)
413 err
= dbe
->export_sample(dbe
, &es
);
416 thread__put(main_thread
);
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
,
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"},
448 int db_export__branch_types(struct db_export
*dbe
)
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
);
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
;
465 if (type
== PERF_IP_FLAG_BRANCH
||
466 (type
& (PERF_IP_FLAG_TRACE_BEGIN
| PERF_IP_FLAG_TRACE_END
)))
469 snprintf(buf
, sizeof(buf
), "trace begin / %s", name
);
470 err
= db_export__branch_type(dbe
, type
| PERF_IP_FLAG_TRACE_BEGIN
, buf
);
474 snprintf(buf
, sizeof(buf
), "%s / trace end", name
);
475 err
= db_export__branch_type(dbe
, type
| PERF_IP_FLAG_TRACE_END
, buf
);
483 int db_export__call_path(struct db_export
*dbe
, struct call_path
*cp
)
491 err
= db_export__call_path(dbe
, cp
->parent
);
496 cp
->db_id
= ++dbe
->call_path_last_db_id
;
498 if (dbe
->export_call_path
)
499 return dbe
->export_call_path(dbe
, cp
);
504 int db_export__call_return(struct db_export
*dbe
, struct call_return
*cr
,
509 err
= db_export__call_path(dbe
, cr
->cp
);
514 cr
->db_id
= ++dbe
->call_return_last_db_id
;
518 *parent_db_id
= ++dbe
->call_return_last_db_id
;
519 cr
->parent_db_id
= *parent_db_id
;
522 if (dbe
->export_call_return
)
523 return dbe
->export_call_return(dbe
, cr
);
528 static 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
)
532 struct thread
*thread
= machine__find_thread(machine
, pid
, tid
);
533 struct thread
*main_thread
;
536 if (!thread
|| !thread__comm_set(thread
))
539 *is_idle
= !thread__pid(thread
) && !thread__tid(thread
);
541 main_thread
= thread__main_thread(machine
, thread
);
543 err
= db_export__threads(dbe
, thread
, main_thread
, machine
, comm_ptr
);
545 *db_id
= thread__db_id(thread
);
547 thread__put(main_thread
);
554 int db_export__switch(struct db_export
*dbe
, union perf_event
*event
,
555 struct perf_sample
*sample
, struct machine
*machine
)
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
;
570 err
= db_export__machine(dbe
, machine
);
574 err
= db_export__pid_tid(dbe
, machine
, sample
->pid
, sample
->tid
,
575 &th_a_id
, &comm_a
, &is_idle_a
);
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
;
583 err
= db_export__pid_tid(dbe
, machine
, pid
, tid
, &th_b_id
,
584 &comm_b
, &is_idle_b
);
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.
593 if ((!th_a_id
|| is_idle_a
) && (!th_b_id
|| is_idle_b
))
596 db_id
= ++dbe
->context_switch_last_db_id
;
601 comm_out_id
= comm_a
? comm_a
->db_id
: 0;
602 comm_in_id
= comm_b
? comm_b
->db_id
: 0;
606 comm_out_id
= comm_b
? comm_b
->db_id
: 0;
607 comm_in_id
= comm_a
? comm_a
->db_id
: 0;
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
);