Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
6319790
Documentation
LICENSES
arch
block
certs
crypto
drivers
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
accounting
arch
bpf
build
cgroup
crypto
debugging
firewire
firmware
gpio
hv
iio
include
io_uring
kvm
laptop
leds
lib
memory-model
nfsd
objtool
pci
pcmcia
perf
Documentation
arch
bench
examples
include
jvmti
pmu-events
python
scripts
tests
trace
ui
util
c++
cs-etm-decoder
include
intel-pt-decoder
libunwind
scripting-engines
Build
PERF-VERSION-GEN
annotate.c
annotate.h
archinsn.h
arm-spe-pkt-decoder.c
arm-spe-pkt-decoder.h
arm-spe.c
arm-spe.h
auxtrace.c
auxtrace.h
block-range.c
block-range.h
bpf-event.c
bpf-event.h
bpf-loader.c
bpf-loader.h
bpf-prologue.c
bpf-prologue.h
bpf_map.c
bpf_map.h
branch.c
branch.h
build-id.c
build-id.h
cache.h
call-path.c
call-path.h
callchain.c
callchain.h
cgroup.c
cgroup.h
cloexec.c
cloexec.h
color.c
color.h
color_config.c
comm.c
comm.h
compress.h
config.c
config.h
counts.c
counts.h
cpu-set-sched.h
cpumap.c
cpumap.h
cputopo.c
cputopo.h
cs-etm.c
cs-etm.h
data-convert-bt.c
data-convert-bt.h
data-convert.h
data.c
data.h
db-export.c
db-export.h
debug.c
debug.h
demangle-java.c
demangle-java.h
demangle-rust.c
demangle-rust.h
dso.c
dso.h
dump-insn.c
dump-insn.h
dwarf-aux.c
dwarf-aux.h
dwarf-regs.c
env.c
env.h
event.c
event.h
evlist.c
evlist.h
evsel.c
evsel.h
evsel_fprintf.c
expr.h
expr.y
find-map.c
genelf.c
genelf.h
genelf_debug.c
generate-cmdlist.sh
get_current_dir_name.c
get_current_dir_name.h
group.h
header.c
header.h
help-unknown-cmd.c
help-unknown-cmd.h
hist.c
hist.h
intel-bts.c
intel-bts.h
intel-pt.c
intel-pt.h
intlist.c
intlist.h
jit.h
jitdump.c
jitdump.h
kvm-stat.h
levenshtein.c
levenshtein.h
llvm-utils.c
llvm-utils.h
lzma.c
machine.c
machine.h
map.c
map.h
map_groups.h
map_symbol.h
mem-events.c
mem-events.h
mem2node.c
mem2node.h
memswap.c
memswap.h
metricgroup.c
metricgroup.h
mmap.c
mmap.h
namespaces.c
namespaces.h
ordered-events.c
ordered-events.h
parse-branch-options.c
parse-branch-options.h
parse-events.c
parse-events.h
parse-events.l
parse-events.y
parse-regs-options.c
parse-regs-options.h
path.c
path.h
perf-hooks-list.h
perf-hooks.c
perf-hooks.h
perf_regs.c
perf_regs.h
pmu.c
pmu.h
pmu.l
pmu.y
print_binary.c
print_binary.h
probe-event.c
probe-event.h
probe-file.c
probe-file.h
probe-finder.c
probe-finder.h
pstack.c
pstack.h
python-ext-sources
python.c
rb_resort.h
rblist.c
rblist.h
record.c
rlimit.c
rlimit.h
rwsem.c
rwsem.h
s390-cpumcf-kernel.h
s390-cpumsf-kernel.h
s390-cpumsf.c
s390-cpumsf.h
s390-sample-raw.c
sample-raw.c
sample-raw.h
session.c
session.h
setns.c
setup.py
smt.c
smt.h
sort.c
sort.h
srccode.c
srccode.h
srcline.c
srcline.h
stat-display.c
stat-shadow.c
stat.c
stat.h
strbuf.c
strbuf.h
strfilter.c
strfilter.h
string.c
string2.h
strlist.c
strlist.h
svghelper.c
svghelper.h
symbol-elf.c
symbol-minimal.c
symbol.c
symbol.h
symbol_conf.h
symbol_fprintf.c
syscalltbl.c
syscalltbl.h
target.c
target.h
term.c
term.h
thread-stack.c
thread-stack.h
thread.c
thread.h
thread_map.c
thread_map.h
time-utils.c
time-utils.h
tool.h
top.c
top.h
trace-event-info.c
trace-event-parse.c
trace-event-read.c
trace-event-scripting.c
trace-event.c
trace-event.h
trigger.h
tsc.c
tsc.h
units.c
units.h
unwind-libdw.c
unwind-libdw.h
unwind-libunwind-local.c
unwind-libunwind.c
unwind.h
usage.c
util-cxx.h
util.c
util.h
values.c
values.h
vdso.c
vdso.h
xyarray.c
xyarray.h
zlib.c
zstd.c
.gitignore
Build
CREDITS
MANIFEST
Makefile
Makefile.config
Makefile.perf
builtin-annotate.c
builtin-bench.c
builtin-buildid-cache.c
builtin-buildid-list.c
builtin-c2c.c
builtin-config.c
builtin-data.c
builtin-diff.c
builtin-evlist.c
builtin-ftrace.c
builtin-help.c
builtin-inject.c
builtin-kallsyms.c
builtin-kmem.c
builtin-kvm.c
builtin-list.c
builtin-lock.c
builtin-mem.c
builtin-probe.c
builtin-record.c
builtin-report.c
builtin-sched.c
builtin-script.c
builtin-stat.c
builtin-timechart.c
builtin-top.c
builtin-trace.c
builtin-version.c
builtin.h
check-headers.sh
command-list.txt
design.txt
perf-archive.sh
perf-completion.sh
perf-read-vdso.c
perf-sys.h
perf-with-kcore.sh
perf.c
perf.h
power
scripts
spi
testing
thermal
time
usb
virtio
vm
wmi
Makefile
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
tools
/
perf
/
util
/
db-export.c
Blame
Blame
Latest commit
History
History
476 lines (386 loc) · 11.1 KB
Breadcrumbs
linux
/
tools
/
perf
/
util
/
db-export.c
Top
File metadata and controls
Code
Blame
476 lines (386 loc) · 11.1 KB
Raw
// SPDX-License-Identifier: GPL-2.0-only /* * db-export.c: Support for exporting data suitable for import to a database * Copyright (c) 2014, Intel Corporation. */ #include <errno.h> #include <stdlib.h> #include "evsel.h" #include "machine.h" #include "thread.h" #include "comm.h" #include "symbol.h" #include "map.h" #include "event.h" #include "thread-stack.h" #include "callchain.h" #include "call-path.h" #include "db-export.h" #include <linux/zalloc.h> int db_export__init(struct db_export *dbe) { memset(dbe, 0, sizeof(struct db_export)); return 0; } void db_export__exit(struct db_export *dbe) { call_return_processor__free(dbe->crp); dbe->crp = NULL; } 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, struct thread *main_thread) { u64 main_thread_db_id = 0; int err; if (thread->db_id) return 0; thread->db_id = ++dbe->thread_last_db_id; if (main_thread) { if (main_thread != thread && comm) { err = db_export__comm_thread(dbe, comm, thread); if (err) return err; } main_thread_db_id = main_thread->db_id; } if (dbe->export_thread) return dbe->export_thread(dbe, thread, main_thread_db_id, machine); return 0; } /* * Export the "exec" comm. The "exec" comm is the program / application command * name at the time it first executes. It is used to group threads for the same * program. Note that the main thread pid (or thread group id tgid) cannot be * used because it does not change when a new program is exec'ed. */ int db_export__exec_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) { err = dbe->export_comm(dbe, comm); if (err) return err; } /* * Record the main thread for this comm. Note that the main thread can * have many "exec" comms because there will be a new one every time it * exec's. An "exec" comm however will only ever have 1 main thread. * That is different to any other threads for that same program because * exec() will effectively kill them, so the relationship between the * "exec" comm and non-main threads is 1-to-1. That is why * db_export__comm_thread() is called here for the main thread, but it * is called for non-main threads when they are exported. */ 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; } 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, 0, "unknown"); if (al->sym) dso__insert_symbol(dso, al->sym); } 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; } 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, sample, NULL, NULL, PERF_MAX_STACK_DEPTH); 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; al.addr = node->ip; if (al.map && !al.sym) al.sym = dso__find_symbol(al.map->dso, al.addr); 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; } 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; } int db_export__sample(struct db_export *dbe, union perf_event *event, struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al) { struct thread* thread = al->thread; struct export_sample es = { .event = event, .sample = sample, .evsel = evsel, .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; main_thread = thread__main_thread(al->machine, thread); if (main_thread) { comm = machine__thread_exec_comm(al->machine, main_thread); /* * A thread has a reference to the main thread, so export the * main thread first. */ err = db_export__thread(dbe, main_thread, al->machine, comm, main_thread); if (err) goto out_put; if (comm) { err = db_export__exec_comm(dbe, comm, main_thread); if (err) goto out_put; es.comm_db_id = comm->db_id; } } if (thread != main_thread) { err = db_export__thread(dbe, thread, al->machine, comm, main_thread); if (err) goto out_put; } 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) goto out_put; if (dbe->cpr) { struct call_path *cp = call_path_from_sample(dbe, al->machine, thread, sample, evsel); if (cp) { db_export__call_path(dbe, cp); es.call_path_id = cp->db_id; } } if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && sample_addr_correlates_sym(&evsel->attr)) { struct addr_location addr_al; thread__resolve(thread, &addr_al, sample); err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id, &es.addr_sym_db_id, &es.addr_offset); if (err) goto out_put; if (dbe->crp) { err = thread_stack__process(thread, comm, sample, al, &addr_al, es.db_id, dbe->crp); if (err) goto out_put; } } if (dbe->export_sample) err = dbe->export_sample(dbe, &es); out_put: thread__put(main_thread); return err; } 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; } /* Add trace begin / end variants */ for (i = 0; branch_types[i].name ; i++) { const char *name = branch_types[i].name; u32 type = branch_types[i].branch_type; char buf[64]; if (type == PERF_IP_FLAG_BRANCH || (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END))) continue; snprintf(buf, sizeof(buf), "trace begin / %s", name); err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf); if (err) break; snprintf(buf, sizeof(buf), "%s / trace end", name); err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf); if (err) break; } return err; } 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, u64 *parent_db_id) { int err; err = db_export__call_path(dbe, cr->cp); if (err) return err; if (!cr->db_id) cr->db_id = ++dbe->call_return_last_db_id; if (parent_db_id) { if (!*parent_db_id) *parent_db_id = ++dbe->call_return_last_db_id; cr->parent_db_id = *parent_db_id; } if (dbe->export_call_return) return dbe->export_call_return(dbe, cr); return 0; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
You can’t perform that action at this time.