2 * db-export.c: Support for exporting data suitable for import to a database
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 #include "thread-stack.h"
26 #include "callchain.h"
27 #include "call-path.h"
28 #include "db-export.h"
30 struct deferred_export {
31 struct list_head node;
35 static int db_export__deferred(struct db_export *dbe)
37 struct deferred_export *de;
40 while (!list_empty(&dbe->deferred)) {
41 de = list_entry(dbe->deferred.next, struct deferred_export,
43 err = dbe->export_comm(dbe, de->comm);
53 static void db_export__free_deferred(struct db_export *dbe)
55 struct deferred_export *de;
57 while (!list_empty(&dbe->deferred)) {
58 de = list_entry(dbe->deferred.next, struct deferred_export,
65 static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
67 struct deferred_export *de;
69 de = zalloc(sizeof(struct deferred_export));
74 list_add_tail(&de->node, &dbe->deferred);
79 int db_export__init(struct db_export *dbe)
81 memset(dbe, 0, sizeof(struct db_export));
82 INIT_LIST_HEAD(&dbe->deferred);
86 int db_export__flush(struct db_export *dbe)
88 return db_export__deferred(dbe);
91 void db_export__exit(struct db_export *dbe)
93 db_export__free_deferred(dbe);
94 call_return_processor__free(dbe->crp);
98 int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
103 evsel->db_id = ++dbe->evsel_last_db_id;
105 if (dbe->export_evsel)
106 return dbe->export_evsel(dbe, evsel);
111 int db_export__machine(struct db_export *dbe, struct machine *machine)
116 machine->db_id = ++dbe->machine_last_db_id;
118 if (dbe->export_machine)
119 return dbe->export_machine(dbe, machine);
124 int db_export__thread(struct db_export *dbe, struct thread *thread,
125 struct machine *machine, struct comm *comm)
127 struct thread *main_thread;
128 u64 main_thread_db_id = 0;
134 thread->db_id = ++dbe->thread_last_db_id;
136 if (thread->pid_ != -1) {
137 if (thread->pid_ == thread->tid) {
138 main_thread = thread;
140 main_thread = machine__findnew_thread(machine,
145 err = db_export__thread(dbe, main_thread, machine,
150 err = db_export__comm_thread(dbe, comm, thread);
155 main_thread_db_id = main_thread->db_id;
156 if (main_thread != thread)
157 thread__put(main_thread);
160 if (dbe->export_thread)
161 return dbe->export_thread(dbe, thread, main_thread_db_id,
167 thread__put(main_thread);
171 int db_export__comm(struct db_export *dbe, struct comm *comm,
172 struct thread *main_thread)
179 comm->db_id = ++dbe->comm_last_db_id;
181 if (dbe->export_comm) {
182 if (main_thread->comm_set)
183 err = dbe->export_comm(dbe, comm);
185 err = db_export__defer_comm(dbe, comm);
190 return db_export__comm_thread(dbe, comm, main_thread);
193 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
194 struct thread *thread)
198 db_id = ++dbe->comm_thread_last_db_id;
200 if (dbe->export_comm_thread)
201 return dbe->export_comm_thread(dbe, db_id, comm, thread);
206 int db_export__dso(struct db_export *dbe, struct dso *dso,
207 struct machine *machine)
212 dso->db_id = ++dbe->dso_last_db_id;
215 return dbe->export_dso(dbe, dso, machine);
220 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
223 u64 *sym_db_id = symbol__priv(sym);
228 *sym_db_id = ++dbe->symbol_last_db_id;
230 if (dbe->export_symbol)
231 return dbe->export_symbol(dbe, sym, dso);
236 static struct thread *get_main_thread(struct machine *machine, struct thread *thread)
238 if (thread->pid_ == thread->tid)
239 return thread__get(thread);
241 if (thread->pid_ == -1)
244 return machine__find_thread(machine, thread->pid_, thread->pid_);
247 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
248 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
253 struct dso *dso = al->map->dso;
255 err = db_export__dso(dbe, dso, al->machine);
258 *dso_db_id = dso->db_id;
261 al->sym = symbol__new(al->addr, 0, 0, "unknown");
263 dso__insert_symbol(dso, al->map->type, al->sym);
267 u64 *db_id = symbol__priv(al->sym);
269 err = db_export__symbol(dbe, al->sym, dso);
273 *offset = al->addr - al->sym->start;
280 static struct call_path *call_path_from_sample(struct db_export *dbe,
281 struct machine *machine,
282 struct thread *thread,
283 struct perf_sample *sample,
284 struct perf_evsel *evsel)
286 u64 kernel_start = machine__kernel_start(machine);
287 struct call_path *current = &dbe->cpr->call_path;
288 enum chain_order saved_order = callchain_param.order;
291 if (!symbol_conf.use_callchain || !sample->callchain)
295 * Since the call path tree must be built starting with the root, we
296 * must use ORDER_CALL for call chain resolution, in order to process
297 * the callchain starting with the root node and ending with the leaf.
299 callchain_param.order = ORDER_CALLER;
300 err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
301 sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
303 callchain_param.order = saved_order;
306 callchain_cursor_commit(&callchain_cursor);
309 struct callchain_cursor_node *node;
310 struct addr_location al;
311 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
313 memset(&al, 0, sizeof(al));
315 node = callchain_cursor_current(&callchain_cursor);
319 * Handle export of symbol and dso for this node by
320 * constructing an addr_location struct and then passing it to
321 * db_ids_from_al() to perform the export.
325 al.machine = machine;
328 if (al.map && !al.sym)
329 al.sym = dso__find_symbol(al.map->dso, MAP__FUNCTION,
332 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
334 /* add node to the call path tree if it doesn't exist */
335 current = call_path__findnew(dbe->cpr, current,
339 callchain_cursor_advance(&callchain_cursor);
342 /* Reset the callchain order to its prior value. */
343 callchain_param.order = saved_order;
345 if (current == &dbe->cpr->call_path) {
346 /* Bail because the callchain was empty. */
353 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
356 if (dbe->export_branch_type)
357 return dbe->export_branch_type(dbe, branch_type, name);
362 int db_export__sample(struct db_export *dbe, union perf_event *event,
363 struct perf_sample *sample, struct perf_evsel *evsel,
364 struct addr_location *al)
366 struct thread* thread = al->thread;
367 struct export_sample es = {
373 struct thread *main_thread;
374 struct comm *comm = NULL;
377 err = db_export__evsel(dbe, evsel);
381 err = db_export__machine(dbe, al->machine);
385 main_thread = get_main_thread(al->machine, thread);
387 comm = machine__thread_exec_comm(al->machine, main_thread);
389 err = db_export__thread(dbe, thread, al->machine, comm);
394 err = db_export__comm(dbe, comm, main_thread);
397 es.comm_db_id = comm->db_id;
400 es.db_id = ++dbe->sample_last_db_id;
402 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
407 struct call_path *cp = call_path_from_sample(dbe, al->machine,
411 db_export__call_path(dbe, cp);
412 es.call_path_id = cp->db_id;
416 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
417 sample_addr_correlates_sym(&evsel->attr)) {
418 struct addr_location addr_al;
420 thread__resolve(thread, &addr_al, sample);
421 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
422 &es.addr_sym_db_id, &es.addr_offset);
426 err = thread_stack__process(thread, comm, sample, al,
434 if (dbe->export_sample)
435 err = dbe->export_sample(dbe, &es);
438 thread__put(main_thread);
447 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
448 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
449 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
450 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
451 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
452 "software interrupt"},
453 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
454 "return from interrupt"},
455 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
457 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
458 "return from system call"},
459 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
460 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
461 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
462 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
463 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
464 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
468 int db_export__branch_types(struct db_export *dbe)
472 for (i = 0; branch_types[i].name ; i++) {
473 err = db_export__branch_type(dbe, branch_types[i].branch_type,
474 branch_types[i].name);
481 int db_export__call_path(struct db_export *dbe, struct call_path *cp)
489 err = db_export__call_path(dbe, cp->parent);
494 cp->db_id = ++dbe->call_path_last_db_id;
496 if (dbe->export_call_path)
497 return dbe->export_call_path(dbe, cp);
502 int db_export__call_return(struct db_export *dbe, struct call_return *cr)
509 err = db_export__call_path(dbe, cr->cp);
513 cr->db_id = ++dbe->call_return_last_db_id;
515 if (dbe->export_call_return)
516 return dbe->export_call_return(dbe, cr);