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