Blame view

tools/perf/util/db-export.c 11.2 KB
0db15b1e8   Adrian Hunter   perf tools: Add f...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*
   * db-export.c: Support for exporting data suitable for import to a database
   * Copyright (c) 2014, Intel Corporation.
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms and conditions of the GNU General Public License,
   * version 2, as published by the Free Software Foundation.
   *
   * This program is distributed in the hope it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   *
   */
  
  #include <errno.h>
  
  #include "evsel.h"
  #include "machine.h"
  #include "thread.h"
  #include "comm.h"
  #include "symbol.h"
  #include "event.h"
758008b26   Adrian Hunter   perf tools: Defer...
24
  #include "util.h"
88f50d602   Adrian Hunter   perf tools: Add c...
25
  #include "thread-stack.h"
0a3eba3ad   Chris Phlipot   perf script: Enab...
26
  #include "callchain.h"
451db1261   Chris Phlipot   perf tools: Refac...
27
  #include "call-path.h"
0db15b1e8   Adrian Hunter   perf tools: Add f...
28
  #include "db-export.h"
758008b26   Adrian Hunter   perf tools: Defer...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  struct deferred_export {
  	struct list_head node;
  	struct comm *comm;
  };
  
  static int db_export__deferred(struct db_export *dbe)
  {
  	struct deferred_export *de;
  	int err;
  
  	while (!list_empty(&dbe->deferred)) {
  		de = list_entry(dbe->deferred.next, struct deferred_export,
  				node);
  		err = dbe->export_comm(dbe, de->comm);
  		list_del(&de->node);
  		free(de);
  		if (err)
  			return err;
  	}
  
  	return 0;
  }
  
  static void db_export__free_deferred(struct db_export *dbe)
  {
  	struct deferred_export *de;
  
  	while (!list_empty(&dbe->deferred)) {
  		de = list_entry(dbe->deferred.next, struct deferred_export,
  				node);
  		list_del(&de->node);
  		free(de);
  	}
  }
  
  static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
  {
  	struct deferred_export *de;
  
  	de = zalloc(sizeof(struct deferred_export));
  	if (!de)
  		return -ENOMEM;
  
  	de->comm = comm;
  	list_add_tail(&de->node, &dbe->deferred);
  
  	return 0;
  }
0db15b1e8   Adrian Hunter   perf tools: Add f...
77
78
79
  int db_export__init(struct db_export *dbe)
  {
  	memset(dbe, 0, sizeof(struct db_export));
758008b26   Adrian Hunter   perf tools: Defer...
80
  	INIT_LIST_HEAD(&dbe->deferred);
0db15b1e8   Adrian Hunter   perf tools: Add f...
81
82
  	return 0;
  }
758008b26   Adrian Hunter   perf tools: Defer...
83
84
85
86
  int db_export__flush(struct db_export *dbe)
  {
  	return db_export__deferred(dbe);
  }
88f50d602   Adrian Hunter   perf tools: Add c...
87
  void db_export__exit(struct db_export *dbe)
0db15b1e8   Adrian Hunter   perf tools: Add f...
88
  {
758008b26   Adrian Hunter   perf tools: Defer...
89
  	db_export__free_deferred(dbe);
88f50d602   Adrian Hunter   perf tools: Add c...
90
91
  	call_return_processor__free(dbe->crp);
  	dbe->crp = NULL;
0db15b1e8   Adrian Hunter   perf tools: Add f...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  }
  
  int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
  {
  	if (evsel->db_id)
  		return 0;
  
  	evsel->db_id = ++dbe->evsel_last_db_id;
  
  	if (dbe->export_evsel)
  		return dbe->export_evsel(dbe, evsel);
  
  	return 0;
  }
  
  int db_export__machine(struct db_export *dbe, struct machine *machine)
  {
  	if (machine->db_id)
  		return 0;
  
  	machine->db_id = ++dbe->machine_last_db_id;
  
  	if (dbe->export_machine)
  		return dbe->export_machine(dbe, machine);
  
  	return 0;
  }
  
  int db_export__thread(struct db_export *dbe, struct thread *thread,
  		      struct machine *machine, struct comm *comm)
  {
b91fc39f4   Arnaldo Carvalho de Melo   perf machine: Pro...
123
  	struct thread *main_thread;
0db15b1e8   Adrian Hunter   perf tools: Add f...
124
125
126
127
128
129
130
131
132
  	u64 main_thread_db_id = 0;
  	int err;
  
  	if (thread->db_id)
  		return 0;
  
  	thread->db_id = ++dbe->thread_last_db_id;
  
  	if (thread->pid_ != -1) {
0db15b1e8   Adrian Hunter   perf tools: Add f...
133
134
135
136
137
138
139
140
141
142
143
  		if (thread->pid_ == thread->tid) {
  			main_thread = thread;
  		} else {
  			main_thread = machine__findnew_thread(machine,
  							      thread->pid_,
  							      thread->pid_);
  			if (!main_thread)
  				return -ENOMEM;
  			err = db_export__thread(dbe, main_thread, machine,
  						comm);
  			if (err)
b91fc39f4   Arnaldo Carvalho de Melo   perf machine: Pro...
144
  				goto out_put;
0db15b1e8   Adrian Hunter   perf tools: Add f...
145
146
147
  			if (comm) {
  				err = db_export__comm_thread(dbe, comm, thread);
  				if (err)
b91fc39f4   Arnaldo Carvalho de Melo   perf machine: Pro...
148
  					goto out_put;
0db15b1e8   Adrian Hunter   perf tools: Add f...
149
150
151
  			}
  		}
  		main_thread_db_id = main_thread->db_id;
b91fc39f4   Arnaldo Carvalho de Melo   perf machine: Pro...
152
153
  		if (main_thread != thread)
  			thread__put(main_thread);
0db15b1e8   Adrian Hunter   perf tools: Add f...
154
155
156
157
158
159
160
  	}
  
  	if (dbe->export_thread)
  		return dbe->export_thread(dbe, thread, main_thread_db_id,
  					  machine);
  
  	return 0;
b91fc39f4   Arnaldo Carvalho de Melo   perf machine: Pro...
161
162
163
164
  
  out_put:
  	thread__put(main_thread);
  	return err;
0db15b1e8   Adrian Hunter   perf tools: Add f...
165
166
167
168
169
170
171
172
173
174
175
176
177
  }
  
  int db_export__comm(struct db_export *dbe, struct comm *comm,
  		    struct thread *main_thread)
  {
  	int err;
  
  	if (comm->db_id)
  		return 0;
  
  	comm->db_id = ++dbe->comm_last_db_id;
  
  	if (dbe->export_comm) {
758008b26   Adrian Hunter   perf tools: Defer...
178
179
180
181
  		if (main_thread->comm_set)
  			err = dbe->export_comm(dbe, comm);
  		else
  			err = db_export__defer_comm(dbe, comm);
0db15b1e8   Adrian Hunter   perf tools: Add f...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  		if (err)
  			return err;
  	}
  
  	return db_export__comm_thread(dbe, comm, main_thread);
  }
  
  int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
  			   struct thread *thread)
  {
  	u64 db_id;
  
  	db_id = ++dbe->comm_thread_last_db_id;
  
  	if (dbe->export_comm_thread)
  		return dbe->export_comm_thread(dbe, db_id, comm, thread);
  
  	return 0;
  }
  
  int db_export__dso(struct db_export *dbe, struct dso *dso,
  		   struct machine *machine)
  {
  	if (dso->db_id)
  		return 0;
  
  	dso->db_id = ++dbe->dso_last_db_id;
  
  	if (dbe->export_dso)
  		return dbe->export_dso(dbe, dso, machine);
  
  	return 0;
  }
  
  int db_export__symbol(struct db_export *dbe, struct symbol *sym,
  		      struct dso *dso)
  {
  	u64 *sym_db_id = symbol__priv(sym);
  
  	if (*sym_db_id)
  		return 0;
  
  	*sym_db_id = ++dbe->symbol_last_db_id;
  
  	if (dbe->export_symbol)
  		return dbe->export_symbol(dbe, sym, dso);
  
  	return 0;
  }
0db15b1e8   Adrian Hunter   perf tools: Add f...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
  			  u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
  {
  	int err;
  
  	if (al->map) {
  		struct dso *dso = al->map->dso;
  
  		err = db_export__dso(dbe, dso, al->machine);
  		if (err)
  			return err;
  		*dso_db_id = dso->db_id;
  
  		if (!al->sym) {
  			al->sym = symbol__new(al->addr, 0, 0, "unknown");
  			if (al->sym)
bd0a51dd2   Chris Phlipot   perf script: Fix ...
247
  				dso__insert_symbol(dso, al->map->type, al->sym);
0db15b1e8   Adrian Hunter   perf tools: Add f...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  		}
  
  		if (al->sym) {
  			u64 *db_id = symbol__priv(al->sym);
  
  			err = db_export__symbol(dbe, al->sym, dso);
  			if (err)
  				return err;
  			*sym_db_id = *db_id;
  			*offset = al->addr - al->sym->start;
  		}
  	}
  
  	return 0;
  }
0a3eba3ad   Chris Phlipot   perf script: Enab...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  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)
  {
  	u64 kernel_start = machine__kernel_start(machine);
  	struct call_path *current = &dbe->cpr->call_path;
  	enum chain_order saved_order = callchain_param.order;
  	int err;
  
  	if (!symbol_conf.use_callchain || !sample->callchain)
  		return NULL;
  
  	/*
  	 * Since the call path tree must be built starting with the root, we
  	 * must use ORDER_CALL for call chain resolution, in order to process
  	 * the callchain starting with the root node and ending with the leaf.
  	 */
  	callchain_param.order = ORDER_CALLER;
  	err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
fe176085a   Arnaldo Carvalho de Melo   perf tools: Fix u...
284
  					sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
0a3eba3ad   Chris Phlipot   perf script: Enab...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  	if (err) {
  		callchain_param.order = saved_order;
  		return NULL;
  	}
  	callchain_cursor_commit(&callchain_cursor);
  
  	while (1) {
  		struct callchain_cursor_node *node;
  		struct addr_location al;
  		u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
  
  		memset(&al, 0, sizeof(al));
  
  		node = callchain_cursor_current(&callchain_cursor);
  		if (!node)
  			break;
  		/*
  		 * Handle export of symbol and dso for this node by
  		 * constructing an addr_location struct and then passing it to
  		 * db_ids_from_al() to perform the export.
  		 */
  		al.sym = node->sym;
  		al.map = node->map;
  		al.machine = machine;
7a2544c00   Chris Phlipot   perf script: Fix ...
309
  		al.addr = node->ip;
0a3eba3ad   Chris Phlipot   perf script: Enab...
310

83302e79b   Chris Phlipot   perf script: Fix ...
311
312
313
  		if (al.map && !al.sym)
  			al.sym = dso__find_symbol(al.map->dso, MAP__FUNCTION,
  						  al.addr);
0a3eba3ad   Chris Phlipot   perf script: Enab...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  		db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
  
  		/* add node to the call path tree if it doesn't exist */
  		current = call_path__findnew(dbe->cpr, current,
  					     al.sym, node->ip,
  					     kernel_start);
  
  		callchain_cursor_advance(&callchain_cursor);
  	}
  
  	/* Reset the callchain order to its prior value. */
  	callchain_param.order = saved_order;
  
  	if (current == &dbe->cpr->call_path) {
  		/* Bail because the callchain was empty. */
  		return NULL;
  	}
  
  	return current;
  }
f2bff0076   Adrian Hunter   perf tools: Add b...
334
335
336
337
338
339
340
341
  int db_export__branch_type(struct db_export *dbe, u32 branch_type,
  			   const char *name)
  {
  	if (dbe->export_branch_type)
  		return dbe->export_branch_type(dbe, branch_type, name);
  
  	return 0;
  }
0db15b1e8   Adrian Hunter   perf tools: Add f...
342
343
  int db_export__sample(struct db_export *dbe, union perf_event *event,
  		      struct perf_sample *sample, struct perf_evsel *evsel,
7327259d7   Arnaldo Carvalho de Melo   perf db-export: N...
344
  		      struct addr_location *al)
0db15b1e8   Adrian Hunter   perf tools: Add f...
345
  {
7327259d7   Arnaldo Carvalho de Melo   perf db-export: N...
346
  	struct thread* thread = al->thread;
0db15b1e8   Adrian Hunter   perf tools: Add f...
347
348
349
350
  	struct export_sample es = {
  		.event = event,
  		.sample = sample,
  		.evsel = evsel,
0db15b1e8   Adrian Hunter   perf tools: Add f...
351
352
353
354
355
356
357
358
359
360
361
362
363
  		.al = al,
  	};
  	struct thread *main_thread;
  	struct comm *comm = NULL;
  	int err;
  
  	err = db_export__evsel(dbe, evsel);
  	if (err)
  		return err;
  
  	err = db_export__machine(dbe, al->machine);
  	if (err)
  		return err;
480ca357f   Andi Kleen   perf thread: Adop...
364
  	main_thread = thread__main_thread(al->machine, thread);
0db15b1e8   Adrian Hunter   perf tools: Add f...
365
366
367
368
369
  	if (main_thread)
  		comm = machine__thread_exec_comm(al->machine, main_thread);
  
  	err = db_export__thread(dbe, thread, al->machine, comm);
  	if (err)
427cde328   Adrian Hunter   perf db-export: F...
370
  		goto out_put;
0db15b1e8   Adrian Hunter   perf tools: Add f...
371
372
373
374
  
  	if (comm) {
  		err = db_export__comm(dbe, comm, main_thread);
  		if (err)
427cde328   Adrian Hunter   perf db-export: F...
375
  			goto out_put;
0db15b1e8   Adrian Hunter   perf tools: Add f...
376
377
378
379
380
381
382
  		es.comm_db_id = comm->db_id;
  	}
  
  	es.db_id = ++dbe->sample_last_db_id;
  
  	err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
  	if (err)
427cde328   Adrian Hunter   perf db-export: F...
383
  		goto out_put;
0db15b1e8   Adrian Hunter   perf tools: Add f...
384

0a3eba3ad   Chris Phlipot   perf script: Enab...
385
386
387
388
  	if (dbe->cpr) {
  		struct call_path *cp = call_path_from_sample(dbe, al->machine,
  							     thread, sample,
  							     evsel);
568850eaa   Chris Phlipot   perf script: Add ...
389
  		if (cp) {
0a3eba3ad   Chris Phlipot   perf script: Enab...
390
  			db_export__call_path(dbe, cp);
568850eaa   Chris Phlipot   perf script: Add ...
391
392
  			es.call_path_id = cp->db_id;
  		}
0a3eba3ad   Chris Phlipot   perf script: Enab...
393
  	}
0db15b1e8   Adrian Hunter   perf tools: Add f...
394
395
396
  	if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  	    sample_addr_correlates_sym(&evsel->attr)) {
  		struct addr_location addr_al;
c2740a87c   Arnaldo Carvalho de Melo   perf thread: Rena...
397
  		thread__resolve(thread, &addr_al, sample);
0db15b1e8   Adrian Hunter   perf tools: Add f...
398
399
400
  		err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
  				     &es.addr_sym_db_id, &es.addr_offset);
  		if (err)
427cde328   Adrian Hunter   perf db-export: F...
401
  			goto out_put;
88f50d602   Adrian Hunter   perf tools: Add c...
402
403
404
405
406
  		if (dbe->crp) {
  			err = thread_stack__process(thread, comm, sample, al,
  						    &addr_al, es.db_id,
  						    dbe->crp);
  			if (err)
427cde328   Adrian Hunter   perf db-export: F...
407
  				goto out_put;
88f50d602   Adrian Hunter   perf tools: Add c...
408
  		}
0db15b1e8   Adrian Hunter   perf tools: Add f...
409
410
411
  	}
  
  	if (dbe->export_sample)
427cde328   Adrian Hunter   perf db-export: F...
412
  		err = dbe->export_sample(dbe, &es);
0db15b1e8   Adrian Hunter   perf tools: Add f...
413

427cde328   Adrian Hunter   perf db-export: F...
414
415
416
  out_put:
  	thread__put(main_thread);
  	return err;
0db15b1e8   Adrian Hunter   perf tools: Add f...
417
  }
f2bff0076   Adrian Hunter   perf tools: Add b...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  
  static struct {
  	u32 branch_type;
  	const char *name;
  } branch_types[] = {
  	{0, "no branch"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
  	{PERF_IP_FLAG_BRANCH, "unconditional jump"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
  	 "software interrupt"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
  	 "return from interrupt"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
  	 "system call"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
  	 "return from system call"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  	 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
  	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
  	{0, NULL}
  };
  
  int db_export__branch_types(struct db_export *dbe)
  {
  	int i, err = 0;
  
  	for (i = 0; branch_types[i].name ; i++) {
  		err = db_export__branch_type(dbe, branch_types[i].branch_type,
  					     branch_types[i].name);
  		if (err)
  			break;
  	}
  	return err;
  }
88f50d602   Adrian Hunter   perf tools: Add c...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  
  int db_export__call_path(struct db_export *dbe, struct call_path *cp)
  {
  	int err;
  
  	if (cp->db_id)
  		return 0;
  
  	if (cp->parent) {
  		err = db_export__call_path(dbe, cp->parent);
  		if (err)
  			return err;
  	}
  
  	cp->db_id = ++dbe->call_path_last_db_id;
  
  	if (dbe->export_call_path)
  		return dbe->export_call_path(dbe, cp);
  
  	return 0;
  }
  
  int db_export__call_return(struct db_export *dbe, struct call_return *cr)
  {
  	int err;
  
  	if (cr->db_id)
  		return 0;
  
  	err = db_export__call_path(dbe, cr->cp);
  	if (err)
  		return err;
  
  	cr->db_id = ++dbe->call_return_last_db_id;
  
  	if (dbe->export_call_return)
  		return dbe->export_call_return(dbe, cr);
  
  	return 0;
  }