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.
17 #include "thread-stack.h"
18 #include "callchain.h"
19 #include "call-path.h"
20 #include "db-export.h"
22 struct deferred_export {
23 struct list_head node;
27 static int db_export__deferred(struct db_export *dbe)
29 struct deferred_export *de;
32 while (!list_empty(&dbe->deferred)) {
33 de = list_entry(dbe->deferred.next, struct deferred_export,
35 err = dbe->export_comm(dbe, de->comm);
45 static void db_export__free_deferred(struct db_export *dbe)
47 struct deferred_export *de;
49 while (!list_empty(&dbe->deferred)) {
50 de = list_entry(dbe->deferred.next, struct deferred_export,
57 static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
59 struct deferred_export *de;
61 de = zalloc(sizeof(struct deferred_export));
66 list_add_tail(&de->node, &dbe->deferred);
71 int db_export__init(struct db_export *dbe)
73 memset(dbe, 0, sizeof(struct db_export));
74 INIT_LIST_HEAD(&dbe->deferred);
78 int db_export__flush(struct db_export *dbe)
80 return db_export__deferred(dbe);
83 void db_export__exit(struct db_export *dbe)
85 db_export__free_deferred(dbe);
86 call_return_processor__free(dbe->crp);
90 int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
95 evsel->db_id = ++dbe->evsel_last_db_id;
97 if (dbe->export_evsel)
98 return dbe->export_evsel(dbe, evsel);
103 int db_export__machine(struct db_export *dbe, struct machine *machine)
108 machine->db_id = ++dbe->machine_last_db_id;
110 if (dbe->export_machine)
111 return dbe->export_machine(dbe, machine);
116 int db_export__thread(struct db_export *dbe, struct thread *thread,
117 struct machine *machine, struct comm *comm)
119 struct thread *main_thread;
120 u64 main_thread_db_id = 0;
126 thread->db_id = ++dbe->thread_last_db_id;
128 if (thread->pid_ != -1) {
129 if (thread->pid_ == thread->tid) {
130 main_thread = thread;
132 main_thread = machine__findnew_thread(machine,
137 err = db_export__thread(dbe, main_thread, machine,
142 err = db_export__comm_thread(dbe, comm, thread);
147 main_thread_db_id = main_thread->db_id;
148 if (main_thread != thread)
149 thread__put(main_thread);
152 if (dbe->export_thread)
153 return dbe->export_thread(dbe, thread, main_thread_db_id,
159 thread__put(main_thread);
163 int db_export__comm(struct db_export *dbe, struct comm *comm,
164 struct thread *main_thread)
171 comm->db_id = ++dbe->comm_last_db_id;
173 if (dbe->export_comm) {
174 if (main_thread->comm_set)
175 err = dbe->export_comm(dbe, comm);
177 err = db_export__defer_comm(dbe, comm);
182 return db_export__comm_thread(dbe, comm, main_thread);
185 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
186 struct thread *thread)
190 db_id = ++dbe->comm_thread_last_db_id;
192 if (dbe->export_comm_thread)
193 return dbe->export_comm_thread(dbe, db_id, comm, thread);
198 int db_export__dso(struct db_export *dbe, struct dso *dso,
199 struct machine *machine)
204 dso->db_id = ++dbe->dso_last_db_id;
207 return dbe->export_dso(dbe, dso, machine);
212 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
215 u64 *sym_db_id = symbol__priv(sym);
220 *sym_db_id = ++dbe->symbol_last_db_id;
222 if (dbe->export_symbol)
223 return dbe->export_symbol(dbe, sym, dso);
228 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
229 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
234 struct dso *dso = al->map->dso;
236 err = db_export__dso(dbe, dso, al->machine);
239 *dso_db_id = dso->db_id;
242 al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
244 dso__insert_symbol(dso, al->sym);
248 u64 *db_id = symbol__priv(al->sym);
250 err = db_export__symbol(dbe, al->sym, dso);
254 *offset = al->addr - al->sym->start;
261 static struct call_path *call_path_from_sample(struct db_export *dbe,
262 struct machine *machine,
263 struct thread *thread,
264 struct perf_sample *sample,
265 struct perf_evsel *evsel)
267 u64 kernel_start = machine__kernel_start(machine);
268 struct call_path *current = &dbe->cpr->call_path;
269 enum chain_order saved_order = callchain_param.order;
272 if (!symbol_conf.use_callchain || !sample->callchain)
276 * Since the call path tree must be built starting with the root, we
277 * must use ORDER_CALL for call chain resolution, in order to process
278 * the callchain starting with the root node and ending with the leaf.
280 callchain_param.order = ORDER_CALLER;
281 err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
282 sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
284 callchain_param.order = saved_order;
287 callchain_cursor_commit(&callchain_cursor);
290 struct callchain_cursor_node *node;
291 struct addr_location al;
292 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
294 memset(&al, 0, sizeof(al));
296 node = callchain_cursor_current(&callchain_cursor);
300 * Handle export of symbol and dso for this node by
301 * constructing an addr_location struct and then passing it to
302 * db_ids_from_al() to perform the export.
306 al.machine = machine;
309 if (al.map && !al.sym)
310 al.sym = dso__find_symbol(al.map->dso, al.addr);
312 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
314 /* add node to the call path tree if it doesn't exist */
315 current = call_path__findnew(dbe->cpr, current,
319 callchain_cursor_advance(&callchain_cursor);
322 /* Reset the callchain order to its prior value. */
323 callchain_param.order = saved_order;
325 if (current == &dbe->cpr->call_path) {
326 /* Bail because the callchain was empty. */
333 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
336 if (dbe->export_branch_type)
337 return dbe->export_branch_type(dbe, branch_type, name);
342 int db_export__sample(struct db_export *dbe, union perf_event *event,
343 struct perf_sample *sample, struct perf_evsel *evsel,
344 struct addr_location *al)
346 struct thread* thread = al->thread;
347 struct export_sample es = {
353 struct thread *main_thread;
354 struct comm *comm = NULL;
357 err = db_export__evsel(dbe, evsel);
361 err = db_export__machine(dbe, al->machine);
365 main_thread = thread__main_thread(al->machine, thread);
367 comm = machine__thread_exec_comm(al->machine, main_thread);
369 err = db_export__thread(dbe, thread, al->machine, comm);
374 err = db_export__comm(dbe, comm, main_thread);
377 es.comm_db_id = comm->db_id;
380 es.db_id = ++dbe->sample_last_db_id;
382 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
387 struct call_path *cp = call_path_from_sample(dbe, al->machine,
391 db_export__call_path(dbe, cp);
392 es.call_path_id = cp->db_id;
396 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
397 sample_addr_correlates_sym(&evsel->attr)) {
398 struct addr_location addr_al;
400 thread__resolve(thread, &addr_al, sample);
401 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
402 &es.addr_sym_db_id, &es.addr_offset);
406 err = thread_stack__process(thread, comm, sample, al,
414 if (dbe->export_sample)
415 err = dbe->export_sample(dbe, &es);
418 thread__put(main_thread);
427 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
428 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
429 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
430 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
431 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
432 "software interrupt"},
433 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
434 "return from interrupt"},
435 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
437 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
438 "return from system call"},
439 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
440 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
441 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
442 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
443 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
444 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
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);