perf script: Enable db export to output sampled callchains
[linux-2.6-block.git] / tools / perf / util / db-export.c
CommitLineData
0db15b1e
AH
1/*
2 * db-export.c: Support for exporting data suitable for import to a database
3 * Copyright (c) 2014, Intel Corporation.
4 *
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.
8 *
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
12 * more details.
13 *
14 */
15
16#include <errno.h>
17
18#include "evsel.h"
19#include "machine.h"
20#include "thread.h"
21#include "comm.h"
22#include "symbol.h"
23#include "event.h"
758008b2 24#include "util.h"
88f50d60 25#include "thread-stack.h"
0a3eba3a 26#include "callchain.h"
451db126 27#include "call-path.h"
0db15b1e
AH
28#include "db-export.h"
29
758008b2
AH
30struct deferred_export {
31 struct list_head node;
32 struct comm *comm;
33};
34
35static int db_export__deferred(struct db_export *dbe)
36{
37 struct deferred_export *de;
38 int err;
39
40 while (!list_empty(&dbe->deferred)) {
41 de = list_entry(dbe->deferred.next, struct deferred_export,
42 node);
43 err = dbe->export_comm(dbe, de->comm);
44 list_del(&de->node);
45 free(de);
46 if (err)
47 return err;
48 }
49
50 return 0;
51}
52
53static void db_export__free_deferred(struct db_export *dbe)
54{
55 struct deferred_export *de;
56
57 while (!list_empty(&dbe->deferred)) {
58 de = list_entry(dbe->deferred.next, struct deferred_export,
59 node);
60 list_del(&de->node);
61 free(de);
62 }
63}
64
65static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
66{
67 struct deferred_export *de;
68
69 de = zalloc(sizeof(struct deferred_export));
70 if (!de)
71 return -ENOMEM;
72
73 de->comm = comm;
74 list_add_tail(&de->node, &dbe->deferred);
75
76 return 0;
77}
78
0db15b1e
AH
79int db_export__init(struct db_export *dbe)
80{
81 memset(dbe, 0, sizeof(struct db_export));
758008b2 82 INIT_LIST_HEAD(&dbe->deferred);
0db15b1e
AH
83 return 0;
84}
85
758008b2
AH
86int db_export__flush(struct db_export *dbe)
87{
88 return db_export__deferred(dbe);
89}
90
88f50d60 91void db_export__exit(struct db_export *dbe)
0db15b1e 92{
758008b2 93 db_export__free_deferred(dbe);
88f50d60
AH
94 call_return_processor__free(dbe->crp);
95 dbe->crp = NULL;
0db15b1e
AH
96}
97
98int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
99{
100 if (evsel->db_id)
101 return 0;
102
103 evsel->db_id = ++dbe->evsel_last_db_id;
104
105 if (dbe->export_evsel)
106 return dbe->export_evsel(dbe, evsel);
107
108 return 0;
109}
110
111int db_export__machine(struct db_export *dbe, struct machine *machine)
112{
113 if (machine->db_id)
114 return 0;
115
116 machine->db_id = ++dbe->machine_last_db_id;
117
118 if (dbe->export_machine)
119 return dbe->export_machine(dbe, machine);
120
121 return 0;
122}
123
124int db_export__thread(struct db_export *dbe, struct thread *thread,
125 struct machine *machine, struct comm *comm)
126{
b91fc39f 127 struct thread *main_thread;
0db15b1e
AH
128 u64 main_thread_db_id = 0;
129 int err;
130
131 if (thread->db_id)
132 return 0;
133
134 thread->db_id = ++dbe->thread_last_db_id;
135
136 if (thread->pid_ != -1) {
0db15b1e
AH
137 if (thread->pid_ == thread->tid) {
138 main_thread = thread;
139 } else {
140 main_thread = machine__findnew_thread(machine,
141 thread->pid_,
142 thread->pid_);
143 if (!main_thread)
144 return -ENOMEM;
145 err = db_export__thread(dbe, main_thread, machine,
146 comm);
147 if (err)
b91fc39f 148 goto out_put;
0db15b1e
AH
149 if (comm) {
150 err = db_export__comm_thread(dbe, comm, thread);
151 if (err)
b91fc39f 152 goto out_put;
0db15b1e
AH
153 }
154 }
155 main_thread_db_id = main_thread->db_id;
b91fc39f
ACM
156 if (main_thread != thread)
157 thread__put(main_thread);
0db15b1e
AH
158 }
159
160 if (dbe->export_thread)
161 return dbe->export_thread(dbe, thread, main_thread_db_id,
162 machine);
163
164 return 0;
b91fc39f
ACM
165
166out_put:
167 thread__put(main_thread);
168 return err;
0db15b1e
AH
169}
170
171int db_export__comm(struct db_export *dbe, struct comm *comm,
172 struct thread *main_thread)
173{
174 int err;
175
176 if (comm->db_id)
177 return 0;
178
179 comm->db_id = ++dbe->comm_last_db_id;
180
181 if (dbe->export_comm) {
758008b2
AH
182 if (main_thread->comm_set)
183 err = dbe->export_comm(dbe, comm);
184 else
185 err = db_export__defer_comm(dbe, comm);
0db15b1e
AH
186 if (err)
187 return err;
188 }
189
190 return db_export__comm_thread(dbe, comm, main_thread);
191}
192
193int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
194 struct thread *thread)
195{
196 u64 db_id;
197
198 db_id = ++dbe->comm_thread_last_db_id;
199
200 if (dbe->export_comm_thread)
201 return dbe->export_comm_thread(dbe, db_id, comm, thread);
202
203 return 0;
204}
205
206int db_export__dso(struct db_export *dbe, struct dso *dso,
207 struct machine *machine)
208{
209 if (dso->db_id)
210 return 0;
211
212 dso->db_id = ++dbe->dso_last_db_id;
213
214 if (dbe->export_dso)
215 return dbe->export_dso(dbe, dso, machine);
216
217 return 0;
218}
219
220int db_export__symbol(struct db_export *dbe, struct symbol *sym,
221 struct dso *dso)
222{
223 u64 *sym_db_id = symbol__priv(sym);
224
225 if (*sym_db_id)
226 return 0;
227
228 *sym_db_id = ++dbe->symbol_last_db_id;
229
230 if (dbe->export_symbol)
231 return dbe->export_symbol(dbe, sym, dso);
232
233 return 0;
234}
235
236static struct thread *get_main_thread(struct machine *machine, struct thread *thread)
237{
238 if (thread->pid_ == thread->tid)
427cde32 239 return thread__get(thread);
0db15b1e
AH
240
241 if (thread->pid_ == -1)
242 return NULL;
243
244 return machine__find_thread(machine, thread->pid_, thread->pid_);
245}
246
247static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
248 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
249{
250 int err;
251
252 if (al->map) {
253 struct dso *dso = al->map->dso;
254
255 err = db_export__dso(dbe, dso, al->machine);
256 if (err)
257 return err;
258 *dso_db_id = dso->db_id;
259
260 if (!al->sym) {
261 al->sym = symbol__new(al->addr, 0, 0, "unknown");
262 if (al->sym)
263 symbols__insert(&dso->symbols[al->map->type],
264 al->sym);
265 }
266
267 if (al->sym) {
268 u64 *db_id = symbol__priv(al->sym);
269
270 err = db_export__symbol(dbe, al->sym, dso);
271 if (err)
272 return err;
273 *sym_db_id = *db_id;
274 *offset = al->addr - al->sym->start;
275 }
276 }
277
278 return 0;
279}
280
0a3eba3a
CP
281static struct call_path *call_path_from_sample(struct db_export *dbe,
282 struct machine *machine,
283 struct thread *thread,
284 struct perf_sample *sample,
285 struct perf_evsel *evsel)
286{
287 u64 kernel_start = machine__kernel_start(machine);
288 struct call_path *current = &dbe->cpr->call_path;
289 enum chain_order saved_order = callchain_param.order;
290 int err;
291
292 if (!symbol_conf.use_callchain || !sample->callchain)
293 return NULL;
294
295 /*
296 * Since the call path tree must be built starting with the root, we
297 * must use ORDER_CALL for call chain resolution, in order to process
298 * the callchain starting with the root node and ending with the leaf.
299 */
300 callchain_param.order = ORDER_CALLER;
301 err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
302 sample, NULL, NULL,
303 sysctl_perf_event_max_stack);
304 if (err) {
305 callchain_param.order = saved_order;
306 return NULL;
307 }
308 callchain_cursor_commit(&callchain_cursor);
309
310 while (1) {
311 struct callchain_cursor_node *node;
312 struct addr_location al;
313 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
314
315 memset(&al, 0, sizeof(al));
316
317 node = callchain_cursor_current(&callchain_cursor);
318 if (!node)
319 break;
320 /*
321 * Handle export of symbol and dso for this node by
322 * constructing an addr_location struct and then passing it to
323 * db_ids_from_al() to perform the export.
324 */
325 al.sym = node->sym;
326 al.map = node->map;
327 al.machine = machine;
328 if (al.map)
329 al.addr = al.map->map_ip(al.map, node->ip);
330 else
331 al.addr = node->ip;
332
333 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
334
335 /* add node to the call path tree if it doesn't exist */
336 current = call_path__findnew(dbe->cpr, current,
337 al.sym, node->ip,
338 kernel_start);
339
340 callchain_cursor_advance(&callchain_cursor);
341 }
342
343 /* Reset the callchain order to its prior value. */
344 callchain_param.order = saved_order;
345
346 if (current == &dbe->cpr->call_path) {
347 /* Bail because the callchain was empty. */
348 return NULL;
349 }
350
351 return current;
352}
353
f2bff007
AH
354int db_export__branch_type(struct db_export *dbe, u32 branch_type,
355 const char *name)
356{
357 if (dbe->export_branch_type)
358 return dbe->export_branch_type(dbe, branch_type, name);
359
360 return 0;
361}
362
0db15b1e
AH
363int db_export__sample(struct db_export *dbe, union perf_event *event,
364 struct perf_sample *sample, struct perf_evsel *evsel,
7327259d 365 struct addr_location *al)
0db15b1e 366{
7327259d 367 struct thread* thread = al->thread;
0db15b1e
AH
368 struct export_sample es = {
369 .event = event,
370 .sample = sample,
371 .evsel = evsel,
0db15b1e
AH
372 .al = al,
373 };
374 struct thread *main_thread;
375 struct comm *comm = NULL;
376 int err;
377
378 err = db_export__evsel(dbe, evsel);
379 if (err)
380 return err;
381
382 err = db_export__machine(dbe, al->machine);
383 if (err)
384 return err;
385
386 main_thread = get_main_thread(al->machine, thread);
387 if (main_thread)
388 comm = machine__thread_exec_comm(al->machine, main_thread);
389
390 err = db_export__thread(dbe, thread, al->machine, comm);
391 if (err)
427cde32 392 goto out_put;
0db15b1e
AH
393
394 if (comm) {
395 err = db_export__comm(dbe, comm, main_thread);
396 if (err)
427cde32 397 goto out_put;
0db15b1e
AH
398 es.comm_db_id = comm->db_id;
399 }
400
401 es.db_id = ++dbe->sample_last_db_id;
402
403 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
404 if (err)
427cde32 405 goto out_put;
0db15b1e 406
0a3eba3a
CP
407 if (dbe->cpr) {
408 struct call_path *cp = call_path_from_sample(dbe, al->machine,
409 thread, sample,
410 evsel);
411 if (cp)
412 db_export__call_path(dbe, cp);
413 }
414
0db15b1e
AH
415 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
416 sample_addr_correlates_sym(&evsel->attr)) {
417 struct addr_location addr_al;
418
c2740a87 419 thread__resolve(thread, &addr_al, sample);
0db15b1e
AH
420 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
421 &es.addr_sym_db_id, &es.addr_offset);
422 if (err)
427cde32 423 goto out_put;
88f50d60
AH
424 if (dbe->crp) {
425 err = thread_stack__process(thread, comm, sample, al,
426 &addr_al, es.db_id,
427 dbe->crp);
428 if (err)
427cde32 429 goto out_put;
88f50d60 430 }
0db15b1e
AH
431 }
432
433 if (dbe->export_sample)
427cde32 434 err = dbe->export_sample(dbe, &es);
0db15b1e 435
427cde32
AH
436out_put:
437 thread__put(main_thread);
438 return err;
0db15b1e 439}
f2bff007
AH
440
441static struct {
442 u32 branch_type;
443 const char *name;
444} branch_types[] = {
445 {0, "no branch"},
446 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
447 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
448 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
449 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
450 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
451 "software interrupt"},
452 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
453 "return from interrupt"},
454 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
455 "system call"},
456 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
457 "return from system call"},
458 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
459 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
460 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
461 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
462 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
463 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
464 {0, NULL}
465};
466
467int db_export__branch_types(struct db_export *dbe)
468{
469 int i, err = 0;
470
471 for (i = 0; branch_types[i].name ; i++) {
472 err = db_export__branch_type(dbe, branch_types[i].branch_type,
473 branch_types[i].name);
474 if (err)
475 break;
476 }
477 return err;
478}
88f50d60
AH
479
480int db_export__call_path(struct db_export *dbe, struct call_path *cp)
481{
482 int err;
483
484 if (cp->db_id)
485 return 0;
486
487 if (cp->parent) {
488 err = db_export__call_path(dbe, cp->parent);
489 if (err)
490 return err;
491 }
492
493 cp->db_id = ++dbe->call_path_last_db_id;
494
495 if (dbe->export_call_path)
496 return dbe->export_call_path(dbe, cp);
497
498 return 0;
499}
500
501int db_export__call_return(struct db_export *dbe, struct call_return *cr)
502{
503 int err;
504
505 if (cr->db_id)
506 return 0;
507
508 err = db_export__call_path(dbe, cr->cp);
509 if (err)
510 return err;
511
512 cr->db_id = ++dbe->call_return_last_db_id;
513
514 if (dbe->export_call_return)
515 return dbe->export_call_return(dbe, cr);
516
517 return 0;
518}