Linux Perf
db-export.c
Go to the documentation of this file.
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"
24 #include "util.h"
25 #include "thread-stack.h"
26 #include "callchain.h"
27 #include "call-path.h"
28 #include "db-export.h"
29 
31  struct list_head node;
32  struct comm *comm;
33 };
34 
35 static 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 
53 static 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 
65 static 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 
79 int db_export__init(struct db_export *dbe)
80 {
81  memset(dbe, 0, sizeof(struct db_export));
82  INIT_LIST_HEAD(&dbe->deferred);
83  return 0;
84 }
85 
86 int db_export__flush(struct db_export *dbe)
87 {
88  return db_export__deferred(dbe);
89 }
90 
91 void db_export__exit(struct db_export *dbe)
92 {
95  dbe->crp = NULL;
96 }
97 
98 int 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 
111 int 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 
124 int db_export__thread(struct db_export *dbe, struct thread *thread,
125  struct machine *machine, struct comm *comm)
126 {
127  struct thread *main_thread;
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) {
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)
148  goto out_put;
149  if (comm) {
150  err = db_export__comm_thread(dbe, comm, thread);
151  if (err)
152  goto out_put;
153  }
154  }
155  main_thread_db_id = main_thread->db_id;
156  if (main_thread != thread)
157  thread__put(main_thread);
158  }
159 
160  if (dbe->export_thread)
161  return dbe->export_thread(dbe, thread, main_thread_db_id,
162  machine);
163 
164  return 0;
165 
166 out_put:
167  thread__put(main_thread);
168  return err;
169 }
170 
171 int 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) {
182  if (main_thread->comm_set)
183  err = dbe->export_comm(dbe, comm);
184  else
185  err = db_export__defer_comm(dbe, comm);
186  if (err)
187  return err;
188  }
189 
190  return db_export__comm_thread(dbe, comm, main_thread);
191 }
192 
193 int 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 
206 int 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 
220 int 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 
236 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
237  u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
238 {
239  int err;
240 
241  if (al->map) {
242  struct dso *dso = al->map->dso;
243 
244  err = db_export__dso(dbe, dso, al->machine);
245  if (err)
246  return err;
247  *dso_db_id = dso->db_id;
248 
249  if (!al->sym) {
250  al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
251  if (al->sym)
252  dso__insert_symbol(dso, al->sym);
253  }
254 
255  if (al->sym) {
256  u64 *db_id = symbol__priv(al->sym);
257 
258  err = db_export__symbol(dbe, al->sym, dso);
259  if (err)
260  return err;
261  *sym_db_id = *db_id;
262  *offset = al->addr - al->sym->start;
263  }
264  }
265 
266  return 0;
267 }
268 
269 static struct call_path *call_path_from_sample(struct db_export *dbe,
270  struct machine *machine,
271  struct thread *thread,
272  struct perf_sample *sample,
273  struct perf_evsel *evsel)
274 {
275  u64 kernel_start = machine__kernel_start(machine);
276  struct call_path *current = &dbe->cpr->call_path;
277  enum chain_order saved_order = callchain_param.order;
278  int err;
279 
280  if (!symbol_conf.use_callchain || !sample->callchain)
281  return NULL;
282 
283  /*
284  * Since the call path tree must be built starting with the root, we
285  * must use ORDER_CALL for call chain resolution, in order to process
286  * the callchain starting with the root node and ending with the leaf.
287  */
289  err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
290  sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
291  if (err) {
292  callchain_param.order = saved_order;
293  return NULL;
294  }
296 
297  while (1) {
298  struct callchain_cursor_node *node;
299  struct addr_location al;
300  u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
301 
302  memset(&al, 0, sizeof(al));
303 
305  if (!node)
306  break;
307  /*
308  * Handle export of symbol and dso for this node by
309  * constructing an addr_location struct and then passing it to
310  * db_ids_from_al() to perform the export.
311  */
312  al.sym = node->sym;
313  al.map = node->map;
314  al.machine = machine;
315  al.addr = node->ip;
316 
317  if (al.map && !al.sym)
318  al.sym = dso__find_symbol(al.map->dso, al.addr);
319 
320  db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
321 
322  /* add node to the call path tree if it doesn't exist */
323  current = call_path__findnew(dbe->cpr, current,
324  al.sym, node->ip,
325  kernel_start);
326 
328  }
329 
330  /* Reset the callchain order to its prior value. */
331  callchain_param.order = saved_order;
332 
333  if (current == &dbe->cpr->call_path) {
334  /* Bail because the callchain was empty. */
335  return NULL;
336  }
337 
338  return current;
339 }
340 
342  const char *name)
343 {
344  if (dbe->export_branch_type)
345  return dbe->export_branch_type(dbe, branch_type, name);
346 
347  return 0;
348 }
349 
350 int db_export__sample(struct db_export *dbe, union perf_event *event,
351  struct perf_sample *sample, struct perf_evsel *evsel,
352  struct addr_location *al)
353 {
354  struct thread* thread = al->thread;
355  struct export_sample es = {
356  .event = event,
357  .sample = sample,
358  .evsel = evsel,
359  .al = al,
360  };
361  struct thread *main_thread;
362  struct comm *comm = NULL;
363  int err;
364 
365  err = db_export__evsel(dbe, evsel);
366  if (err)
367  return err;
368 
369  err = db_export__machine(dbe, al->machine);
370  if (err)
371  return err;
372 
373  main_thread = thread__main_thread(al->machine, thread);
374  if (main_thread)
375  comm = machine__thread_exec_comm(al->machine, main_thread);
376 
377  err = db_export__thread(dbe, thread, al->machine, comm);
378  if (err)
379  goto out_put;
380 
381  if (comm) {
382  err = db_export__comm(dbe, comm, main_thread);
383  if (err)
384  goto out_put;
385  es.comm_db_id = comm->db_id;
386  }
387 
388  es.db_id = ++dbe->sample_last_db_id;
389 
390  err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
391  if (err)
392  goto out_put;
393 
394  if (dbe->cpr) {
395  struct call_path *cp = call_path_from_sample(dbe, al->machine,
396  thread, sample,
397  evsel);
398  if (cp) {
399  db_export__call_path(dbe, cp);
400  es.call_path_id = cp->db_id;
401  }
402  }
403 
404  if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
405  sample_addr_correlates_sym(&evsel->attr)) {
406  struct addr_location addr_al;
407 
408  thread__resolve(thread, &addr_al, sample);
409  err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
410  &es.addr_sym_db_id, &es.addr_offset);
411  if (err)
412  goto out_put;
413  if (dbe->crp) {
414  err = thread_stack__process(thread, comm, sample, al,
415  &addr_al, es.db_id,
416  dbe->crp);
417  if (err)
418  goto out_put;
419  }
420  }
421 
422  if (dbe->export_sample)
423  err = dbe->export_sample(dbe, &es);
424 
425 out_put:
426  thread__put(main_thread);
427  return err;
428 }
429 
430 static struct {
432  const char *name;
433 } branch_types[] = {
434  {0, "no branch"},
437  {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
438  {PERF_IP_FLAG_BRANCH, "unconditional jump"},
440  "software interrupt"},
442  "return from interrupt"},
444  "system call"},
446  "return from system call"},
447  {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
449  PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
450  {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
453  {0, NULL}
454 };
455 
457 {
458  int i, err = 0;
459 
460  for (i = 0; branch_types[i].name ; i++) {
462  branch_types[i].name);
463  if (err)
464  break;
465  }
466  return err;
467 }
468 
469 int db_export__call_path(struct db_export *dbe, struct call_path *cp)
470 {
471  int err;
472 
473  if (cp->db_id)
474  return 0;
475 
476  if (cp->parent) {
477  err = db_export__call_path(dbe, cp->parent);
478  if (err)
479  return err;
480  }
481 
482  cp->db_id = ++dbe->call_path_last_db_id;
483 
484  if (dbe->export_call_path)
485  return dbe->export_call_path(dbe, cp);
486 
487  return 0;
488 }
489 
490 int db_export__call_return(struct db_export *dbe, struct call_return *cr)
491 {
492  int err;
493 
494  if (cr->db_id)
495  return 0;
496 
497  err = db_export__call_path(dbe, cr->cp);
498  if (err)
499  return err;
500 
501  cr->db_id = ++dbe->call_return_last_db_id;
502 
503  if (dbe->export_call_return)
504  return dbe->export_call_return(dbe, cr);
505 
506  return 0;
507 }
void thread__resolve(struct thread *thread, struct addr_location *al, struct perf_sample *sample)
Definition: event.c:1687
struct list_head node
Definition: db-export.c:31
u64 evsel_last_db_id
Definition: db-export.h:70
int(* export_branch_type)(struct db_export *dbe, u32 branch_type, const char *name)
Definition: db-export.h:62
struct list_head deferred
Definition: db-export.h:80
Definition: mem2node.c:7
int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
Definition: db-export.c:98
u64 dso_last_db_id
Definition: db-export.h:75
void dso__insert_symbol(struct dso *dso, struct symbol *sym)
Definition: symbol.c:484
u64 addr_dso_db_id
Definition: db-export.h:44
struct comm * comm
Definition: db-export.c:32
struct addr_location * al
Definition: db-export.h:38
pid_t pid_
Definition: thread.h:24
u32 branch_type
Definition: db-export.c:431
u64 call_return_last_db_id
Definition: db-export.h:79
u64 comm_thread_last_db_id
Definition: db-export.h:74
int db_export__sample(struct db_export *dbe, union perf_event *event, struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al)
Definition: db-export.c:350
struct ip_callchain * callchain
Definition: event.h:211
int int err
Definition: 5sec.c:44
struct symbol * sym
Definition: callchain.h:140
int(* export_symbol)(struct db_export *dbe, struct symbol *sym, struct dso *dso)
Definition: db-export.h:60
bool comm_set
Definition: thread.h:29
u64 addr_offset
Definition: db-export.h:46
u64 sample_last_db_id
Definition: db-export.h:77
u64 call_path_last_db_id
Definition: db-export.h:78
static void callchain_cursor_commit(struct callchain_cursor *cursor)
Definition: callchain.h:212
u64 machine_last_db_id
Definition: db-export.h:71
int db_export__init(struct db_export *dbe)
Definition: db-export.c:79
int(* export_call_path)(struct db_export *dbe, struct call_path *cp)
Definition: db-export.h:65
u64 comm_last_db_id
Definition: db-export.h:73
u64 db_id
Definition: call-path.h:41
x86 movsq based memset() in arch/x86/lib/memset_64.S") MEMSET_FN(memset_erms
u64 db_id
Definition: evsel.h:109
struct machine * machine
Definition: symbol.h:208
int db_export__branch_types(struct db_export *dbe)
Definition: db-export.c:456
struct perf_sample * sample
Definition: db-export.h:36
int(* export_evsel)(struct db_export *dbe, struct perf_evsel *evsel)
Definition: db-export.h:51
Definition: comm.h:11
struct call_path_root * cpr
Definition: db-export.h:69
void call_return_processor__free(struct call_return_processor *crp)
Definition: thread-stack.c:325
int db_export__comm_thread(struct db_export *dbe, struct comm *comm, struct thread *thread)
Definition: db-export.c:193
const char * name
Definition: db-export.c:432
struct call_path * parent
Definition: call-path.h:38
struct symbol * symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
Definition: symbol.c:251
struct map * map
Definition: symbol.h:210
Definition: thread.h:18
struct call_path call_path
Definition: call-path.h:64
struct call_path * call_path__findnew(struct call_path_root *cpr, struct call_path *parent, struct symbol *sym, u64 ip, u64 ks)
Definition: call-path.c:85
struct call_return_processor * crp
Definition: db-export.h:68
int db_export__symbol(struct db_export *dbe, struct symbol *sym, struct dso *dso)
Definition: db-export.c:220
u64 start
Definition: symbol.h:57
int thread_stack__process(struct thread *thread, struct comm *comm, struct perf_sample *sample, struct addr_location *from_al, struct addr_location *to_al, u64 ref, struct call_return_processor *crp)
Definition: thread-stack.c:535
static void callchain_cursor_advance(struct callchain_cursor *cursor)
Definition: callchain.h:228
u64 thread_last_db_id
Definition: db-export.h:72
void db_export__exit(struct db_export *dbe)
Definition: db-export.c:91
struct dso * dso
Definition: map.h:45
static struct @66 branch_types[]
u64 comm_db_id
Definition: db-export.h:40
u64 addr_sym_db_id
Definition: db-export.h:45
u64 symbol_last_db_id
Definition: db-export.h:76
pid_t tid
Definition: thread.h:25
int db_export__branch_type(struct db_export *dbe, u32 branch_type, const char *name)
Definition: db-export.c:341
u64 db_id
Definition: comm.h:18
struct symbol * sym
Definition: symbol.h:211
int(* export_sample)(struct db_export *dbe, struct export_sample *es)
Definition: db-export.h:64
Definition: dso.h:138
#define event
u64 db_id
Definition: dso.h:193
struct map * map
Definition: callchain.h:139
struct thread * thread
Definition: symbol.h:209
int db_export__call_return(struct db_export *dbe, struct call_return *cr)
Definition: db-export.c:490
u64 db_id
Definition: machine.h:57
struct symbol * dso__find_symbol(struct dso *dso, u64 addr)
Definition: symbol.c:496
int(* export_call_return)(struct db_export *dbe, struct call_return *cr)
Definition: db-export.h:66
struct thread * thread__main_thread(struct machine *machine, struct thread *thread)
Definition: thread.c:386
int db_export__thread(struct db_export *dbe, struct thread *thread, struct machine *machine, struct comm *comm)
Definition: db-export.c:124
u64 db_id
Definition: thread.h:36
union perf_event * event
Definition: db-export.h:35
u64 call_path_id
Definition: db-export.h:47
void thread__put(struct thread *thread)
Definition: thread.c:119
bool use_callchain
Definition: symbol.h:93
enum chain_order order
Definition: callchain.h:103
static int sym(yyscan_t scanner, int type, int config)
struct comm * machine__thread_exec_comm(struct machine *machine, struct thread *thread)
Definition: machine.c:516
struct perf_evsel * evsel
Definition: db-export.h:37
struct call_path * cp
Definition: thread-stack.h:60
int(* export_comm_thread)(struct db_export *dbe, u64 db_id, struct comm *comm, struct thread *thread)
Definition: db-export.h:56
static int db_ids_from_al(struct db_export *dbe, struct addr_location *al, u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
Definition: db-export.c:236
void free(void *)
int(* export_comm)(struct db_export *dbe, struct comm *comm)
Definition: db-export.h:55
static struct call_path * call_path_from_sample(struct db_export *dbe, struct machine *machine, struct thread *thread, struct perf_sample *sample, struct perf_evsel *evsel)
Definition: db-export.c:269
static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
Definition: db-export.c:65
static int db_export__deferred(struct db_export *dbe)
Definition: db-export.c:35
int(* export_dso)(struct db_export *dbe, struct dso *dso, struct machine *machine)
Definition: db-export.h:58
Definition: symbol.h:55
int thread__resolve_callchain(struct thread *thread, struct callchain_cursor *cursor, struct perf_evsel *evsel, struct perf_sample *sample, struct symbol **parent, struct addr_location *root_al, int max_stack)
Definition: machine.c:2308
int(* export_thread)(struct db_export *dbe, struct thread *thread, u64 main_thread_db_id, struct machine *machine)
Definition: db-export.h:53
bool sample_addr_correlates_sym(struct perf_event_attr *attr)
Definition: event.c:1673
int db_export__machine(struct db_export *dbe, struct machine *machine)
Definition: db-export.c:111
int db_export__flush(struct db_export *dbe)
Definition: db-export.c:86
int db_export__dso(struct db_export *dbe, struct dso *dso, struct machine *machine)
Definition: db-export.c:206
static void * symbol__priv(struct symbol *sym)
Definition: symbol.h:168
static void db_export__free_deferred(struct db_export *dbe)
Definition: db-export.c:53
static u64 machine__kernel_start(struct machine *machine)
Definition: machine.h:88
chain_order
Definition: callchain.h:50
int(* export_machine)(struct db_export *dbe, struct machine *machine)
Definition: db-export.h:52
struct thread * machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
Definition: machine.c:492
struct perf_event_attr attr
Definition: evsel.h:93
int db_export__comm(struct db_export *dbe, struct comm *comm, struct thread *main_thread)
Definition: db-export.c:171
int db_export__call_path(struct db_export *dbe, struct call_path *cp)
Definition: db-export.c:469
static struct callchain_cursor_node * callchain_cursor_current(struct callchain_cursor *cursor)
Definition: callchain.h:220
void static void * zalloc(size_t size)
Definition: util.h:20