linux/tools/perf/util
Wang Nan 6122d57e9f perf data: Support converting data from bpf_perf_event_output()
bpf_perf_event_output() outputs data through sample->raw_data. This
patch adds support to convert those data into CTF. A python script then
can be used to process output data from BPF programs.

Test result:

  # cat ./test_bpf_output_2.c
  /************************ BEGIN **************************/
  #include <uapi/linux/bpf.h>
  struct bpf_map_def {
 	unsigned int type;
 	unsigned int key_size;
 	unsigned int value_size;
 	unsigned int max_entries;
  };
  #define SEC(NAME) __attribute__((section(NAME), used))
  static u64 (*ktime_get_ns)(void) =
 	(void *)BPF_FUNC_ktime_get_ns;
  static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
 	(void *)BPF_FUNC_trace_printk;
  static int (*get_smp_processor_id)(void) =
 	(void *)BPF_FUNC_get_smp_processor_id;
  static int (*perf_event_output)(void *, struct bpf_map_def *, int, void *, unsigned long) =
 	(void *)BPF_FUNC_perf_event_output;

  struct bpf_map_def SEC("maps") channel = {
 	.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
 	.key_size = sizeof(int),
 	.value_size = sizeof(u32),
 	.max_entries = __NR_CPUS__,
  };

  static inline int __attribute__((always_inline))
  func(void *ctx, int type)
  {
 	struct {
 		u64 ktime;
 		int type;
 	} __attribute__((packed)) output_data;
 	char error_data[] = "Error: failed to output\n";
 	int err;

 	output_data.type = type;
 	output_data.ktime = ktime_get_ns();
 	err = perf_event_output(ctx, &channel, get_smp_processor_id(),
 				&output_data, sizeof(output_data));
 	if (err)
 		trace_printk(error_data, sizeof(error_data));
 	return 0;
  }
  SEC("func_begin=sys_nanosleep")
  int func_begin(void *ctx) {return func(ctx, 1);}
  SEC("func_end=sys_nanosleep%return")
  int func_end(void *ctx) { return func(ctx, 2);}
  char _license[] SEC("license") = "GPL";
  int _version SEC("version") = LINUX_VERSION_CODE;
  /************************* END ***************************/

  # ./perf record -e bpf-output/no-inherit,name=evt/ \
                 -e ./test_bpf_output_2.c/map:channel.event=evt/ \
                 usleep 100000
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 0.012 MB perf.data (2 samples) ]

  # ./perf script
          usleep 14942 92503.198504: evt:  ffffffff810e0ba1 sys_nanosleep (/lib/modules/4.3.0....
          usleep 14942 92503.298562: evt:  ffffffff810585e9 kretprobe_trampoline_holder (/lib....

  # ./perf data convert --to-ctf ./out.ctf
  [ perf data convert: Converted 'perf.data' into CTF data './out.ctf' ]
  [ perf data convert: Converted and wrote 0.000 MB (2 samples) ]

  # babeltrace ./out.ctf
  [01:41:43.198504134] (+?.?????????) evt: { cpu_id = 0 }, { perf_ip = 0xFFFFFFFF810E0BA1, perf_tid = 14942, perf_pid = 14942, perf_id = 1044, raw_len = 3, raw_data = [ [0] = 0x32C0C07B, [1] = 0x5421, [2] = 0x1 ] }
  [01:41:43.298562257] (+0.100058123) evt: { cpu_id = 0 }, { perf_ip = 0xFFFFFFFF810585E9, perf_tid = 14942, perf_pid = 14942, perf_id = 1044, raw_len = 3, raw_data = [ [0] = 0x38B77FAA, [1] = 0x5421, [2] = 0x2 ] }

  # cat ./test_bpf_output_2.py
  from babeltrace import TraceCollection
  tc = TraceCollection()
  tc.add_trace('./out.ctf', 'ctf')
  d = {1:[], 2:[]}
  for event in tc.events:
     if not event.name.startswith('evt'):
         continue
     raw_data = event['raw_data']
     (time, type) = ((raw_data[0] + (raw_data[1] << 32)), raw_data[2])
     d[type].append(time)
  print(list(map(lambda i: d[2][i] - d[1][i], range(len(d[1])))));

  # python3 ./test_bpf_output_2.py
  [100056879]

Committer note:

Make sure you have python3-devel installed, not python-devel, which may
be for python2, which will lead to some "PyInstance_Type" errors. Also
make sure that you use the right libbabeltrace, because it is shipped
in Fedora, for instance, but an older version.

To build libbabeltrace's python binding one also needs to use:

 ./configure --enable-python-bindings

And then set PYTHONPATH=/usr/local/lib64/python3.4/site-packages/.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Li Zefan <lizefan@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1456479154-136027-9-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-03-03 11:10:34 -03:00
..
include tools lib: Move bitmap.[ch] from tools/perf/ to tools/{lib,include}/ 2016-01-08 12:35:46 -03:00
intel-pt-decoder perf intel-pt: Make logging slightly more efficient 2015-09-28 16:45:26 -03:00
scripting-engines perf script: Exception handling when the print fmt is empty 2016-02-25 12:54:20 -03:00
abspath.c
alias.c perf tools: Introduce zfree 2013-12-27 15:17:00 -03:00
annotate.c perf annotate: ARM support 2015-12-07 18:13:00 -03:00
annotate.h perf annotate: Fix sizeof_sym_hist overflow issue 2015-10-05 16:15:38 -03:00
auxtrace.c perf auxtrace: Add perf_evlist pointer to *info_priv_size() 2016-01-29 17:14:30 -03:00
auxtrace.h perf auxtrace: Add perf_evlist pointer to *info_priv_size() 2016-01-29 17:14:30 -03:00
bpf-loader.c perf tools: Introduce bpf-output event 2016-02-22 14:37:21 -03:00
bpf-loader.h perf tools: Support setting different slots in a BPF map separately 2016-02-22 12:48:50 -03:00
bpf-prologue.c perf bpf: Add prologue for BPF programs for fetching arguments 2015-11-18 17:51:04 -03:00
bpf-prologue.h perf bpf: Add prologue for BPF programs for fetching arguments 2015-11-18 17:51:04 -03:00
Build perf tools: Add monitored events array 2016-02-23 12:11:06 -03:00
build-id.c perf symbols: Fix symbols searching for module in buildid-cache 2016-02-12 10:54:47 -03:00
build-id.h perf symbols: Fix symbols searching for module in buildid-cache 2016-02-12 10:54:47 -03:00
cache.h perf config: Add '--system' and '--user' options to select which config file is used 2016-02-12 10:54:46 -03:00
callchain.c perf callchain: Check return value of append_chain_children() 2016-02-19 19:15:01 -03:00
callchain.h perf top: Decay periods in callchains 2016-01-08 12:37:51 -03:00
cgroup.c perf subcmd: Create subcmd library 2015-12-17 14:27:14 -03:00
cgroup.h perf cgroup: Use atomic.h for refcounting 2015-05-15 16:12:20 -03:00
cloexec.c perf bench numa: Fix to show proper convergence stats 2015-06-25 12:28:35 -03:00
cloexec.h perf tools: Fix build errors with mipsel-linux-uclibc compiler 2015-08-05 16:56:16 -03:00
color.c perf config: Bring perf_default_config to the very beginning at main() 2016-02-26 19:49:16 -03:00
color.h perf tools: Remove trail argument to color vsprintf 2015-08-05 16:44:02 -03:00
comm.c perf comm: Use atomic.h for refcounting 2015-05-27 12:21:43 -03:00
comm.h perf tools: Add facility to export data in database-friendly way 2014-10-29 10:32:49 -02:00
config.c perf config: Add '--system' and '--user' options to select which config file is used 2016-02-12 10:54:46 -03:00
counts.c perf stat: Move perf_counts struct and functions into separate object 2015-08-08 14:16:49 -03:00
counts.h perf stat: Move perf_counts struct and functions into separate object 2015-08-08 14:16:49 -03:00
cpumap.c perf cpumap: Auto initialize cpu__max_{node,cpu} 2016-01-26 16:08:36 -03:00
cpumap.h perf cpumap: Auto initialize cpu__max_{node,cpu} 2016-01-26 16:08:36 -03:00
ctype.c perf ui/stdio: Align column header for hierarchy output 2016-02-24 20:21:12 -03:00
data-convert-bt.c perf data: Support converting data from bpf_perf_event_output() 2016-03-03 11:10:34 -03:00
data-convert-bt.h perf data: Support using -f to override perf.data file ownership for 'convert' 2015-04-02 13:18:52 -03:00
data.c perf util: Replace strerror with strerror_r for thread-safety 2014-08-15 10:58:35 -03:00
data.h perf tools: Add perf_data_file__write interface 2013-12-02 09:22:46 -03:00
db-export.c perf db-export: Fix thread ref-counting 2015-05-29 12:43:39 -03:00
db-export.h perf db-export: No need to have ->thread twice in struct export_sample 2015-04-02 13:18:43 -03:00
debug.c perf tools: Make binary data printer code in trace_event public available 2016-02-24 11:38:01 -03:00
debug.h perf tools: Initialize libapi debug output 2016-02-16 17:12:59 -03:00
demangle-java.c perf symbols: add Java demangling support 2016-02-05 09:46:45 -03:00
demangle-java.h perf symbols: add Java demangling support 2016-02-05 09:46:45 -03:00
dso.c perf symbols: Do not read symbols/data from device files 2016-01-26 11:52:43 -03:00
dso.h perf symbols: Fix dso lookup by long name and missing buildids 2015-11-13 11:14:36 -03:00
dwarf-aux.c perf probe: Fix to add missed brace around if block 2015-08-13 14:51:26 -03:00
dwarf-aux.h perf probe: Ignore tail calls to probed functions 2015-05-14 10:05:09 -03:00
env.c perf tools: Add perf data cache feature 2016-02-16 17:13:00 -03:00
env.h perf tools: Add perf data cache feature 2016-02-16 17:13:00 -03:00
event.c perf tools: handle spaces in file names obtained from /proc/pid/maps 2016-02-05 09:39:56 -03:00
event.h perf tools: Add event_update event cpus type 2015-12-17 15:13:38 -03:00
evlist.c perf tools: Only set filter for tracepoints events 2016-02-26 19:50:01 -03:00
evlist.h perf tools: Enable passing event to BPF object 2016-02-22 12:30:50 -03:00
evsel.c perf tools: Introduce bpf-output event 2016-02-22 14:37:21 -03:00
evsel.h perf tools: Introduce bpf-output event 2016-02-22 14:37:21 -03:00
find-vdso-map.c perf tools: Build programs to copy 32-bit compatibility 2014-10-29 10:32:48 -02:00
genelf_debug.c perf jit: add source line info support 2016-02-05 12:33:09 -03:00
genelf.c perf jit: add source line info support 2016-02-05 12:33:09 -03:00
genelf.h perf jit: add source line info support 2016-02-05 12:33:09 -03:00
generate-cmdlist.sh perf tools: Do not show trace command if it's not compiled in 2016-01-08 12:46:17 -03:00
header.c perf tools: Add perf data cache feature 2016-02-16 17:13:00 -03:00
header.h perf tools: Add perf data cache feature 2016-02-16 17:13:00 -03:00
help-unknown-cmd.c perf config: Bring perf_default_config to the very beginning at main() 2016-02-26 19:49:16 -03:00
help-unknown-cmd.h perf tools: Move help_unknown_cmd() to its own file 2015-12-14 12:30:37 -03:00
hist.c perf report: Update column width of dynamic entries 2016-02-26 19:38:48 -03:00
hist.h perf report: Fix indentation of dynamic entries in hierarchy 2016-02-26 18:36:11 -03:00
intel-bts.c perf tools: Fix use of wrong event when processing exit events 2015-09-02 17:46:26 -03:00
intel-bts.h perf tools: Add Intel BTS support 2015-08-21 11:34:10 -03:00
intel-pt.c perf tools: Fix thread lifetime related segfaut in intel_pt 2016-02-02 12:51:11 -03:00
intel-pt.h perf tools: Pass Intel PT information for decoding MTC and CYC 2015-08-24 17:46:43 -03:00
intlist.c
intlist.h
jit.h perf inject: Add jitdump mmap injection support 2016-02-05 09:46:45 -03:00
jitdump.c perf jit: add source line info support 2016-02-05 12:33:09 -03:00
jitdump.h perf inject: Add jitdump mmap injection support 2016-02-05 09:46:45 -03:00
kvm-stat.h perf kvm/powerpc: Port perf kvm stat to powerpc 2016-01-29 17:49:54 -03:00
levenshtein.c
levenshtein.h
llvm-utils.c perf tools: Make fetch_kernel_version() publicly available 2015-11-06 15:57:18 -03:00
llvm-utils.h perf tests: Add LLVM test for eBPF on-the-fly compiling 2015-08-07 10:57:24 -03:00
lzma.c perf tools: Add lzma decompression support for kernel module 2015-03-21 14:53:40 -03:00
machine.c perf thread: Fix reference count initial state 2015-12-14 12:08:55 -03:00
machine.h perf machine: Introduce machine__find_kernel_symbol_by_name() 2016-01-26 11:52:51 -03:00
map.c perf tools: Fix maps__fixup_overlappings to put used maps 2015-12-09 13:42:00 -03:00
map.h perf maps: Introduce maps__find_symbol_by_name() 2015-09-30 18:34:25 -03:00
mem-events.c perf script: Display data_src values 2016-02-24 10:32:11 -03:00
mem-events.h perf script: Display data_src values 2016-02-24 10:32:11 -03:00
ordered-events.c perf ordered_events: Clear the progress bar at the end of a flush 2015-08-24 17:16:22 -03:00
ordered-events.h perf ordered_samples: Remove references to perf_{evlist,tool} and machines 2015-03-31 17:52:32 -03:00
parse-branch-options.c perf subcmd: Create subcmd library 2015-12-17 14:27:14 -03:00
parse-branch-options.h perf tools: Move branch option parsing to own file 2015-05-27 21:02:17 -03:00
parse-events.c perf tools: Apply tracepoint event definition options to BPF script 2016-02-22 13:02:44 -03:00
parse-events.h perf tools: Apply tracepoint event definition options to BPF script 2016-02-22 13:02:44 -03:00
parse-events.l perf tools: Introduce bpf-output event 2016-02-22 14:37:21 -03:00
parse-events.y perf tools: Fix parsing of pmu events with empty list of modifiers 2016-02-25 10:56:21 -03:00
parse-regs-options.c perf subcmd: Create subcmd library 2015-12-17 14:27:14 -03:00
parse-regs-options.h perf record: Add ability to name registers to record 2015-08-31 18:01:33 -03:00
path.c perf tools: Move strlcpy() from perf to tools/lib/string.c 2015-12-16 16:09:39 -03:00
perf_regs.c perf tools: Fix build break on powerpc due to sample_reg_masks 2015-09-30 18:34:27 -03:00
perf_regs.h perf tools: Fix build break on powerpc due to sample_reg_masks 2015-09-30 18:34:27 -03:00
PERF-VERSION-GEN perf tools: Fix version when building out of tree 2013-11-07 10:40:47 -03:00
pmu.c perf tools: Fix locale handling in pmu parsing 2016-03-03 11:04:54 -03:00
pmu.h perf tools: Add perf_pmu__format_bits() 2015-08-06 16:49:01 -03:00
pmu.l
pmu.y
probe-event.c perf bpf: Allow BPF program config probing options 2015-11-18 17:51:04 -03:00
probe-event.h perf probe: Export init/exit_probe_symbol_maps() 2015-09-15 09:48:32 -03:00
probe-file.c perf probe: Verify parameters in two functions 2015-11-11 18:41:32 -03:00
probe-file.h perf probe: Print deleted events in cmd_probe() 2015-09-04 12:43:44 -03:00
probe-finder.c perf probe: Search both .eh_frame and .debug_frame sections for probe location 2016-02-02 13:30:16 -03:00
probe-finder.h perf probe: Search both .eh_frame and .debug_frame sections for probe location 2016-02-02 13:30:16 -03:00
pstack.c perf tools: Introduce pstack_peek() 2015-05-05 18:13:22 -03:00
pstack.h perf tools: Introduce pstack_peek() 2015-05-05 18:13:22 -03:00
python-ext-sources tools lib: Move bitmap.[ch] from tools/perf/ to tools/{lib,include}/ 2016-01-08 12:35:46 -03:00
python.c perf python: Support the PERF_RECORD_SWITCH event 2015-10-07 19:41:50 -03:00
quote.c
quote.h
rblist.c
rblist.h
record.c perf tools: Add a helper function to probe whether cpu-wide tracing is possible 2015-08-17 11:08:37 -03:00
session.c perf kvm record/report: 'unprocessable sample' error while recording/reporting guest data 2016-01-15 16:31:58 -03:00
session.h perf tools: Make perf_session__register_idle_thread drop the refcount 2015-12-10 16:28:58 -03:00
setup.py perf tools: Fix python extension build 2016-02-29 11:18:25 -03:00
sort.c perf report: Update column width of dynamic entries 2016-02-26 19:38:48 -03:00
sort.h perf hists browser: Show message for percent limit 2016-02-26 11:20:36 -03:00
srcline.c perf tools: Always use non inlined file name for 'srcfile' sort key 2015-09-02 16:30:46 -03:00
stat-shadow.c perf stat: Abstract stat metrics printing 2016-02-16 17:13:00 -03:00
stat.c Merge branch 'perf/urgent' into perf/core, to pick up fixes 2016-02-04 08:57:44 +01:00
stat.h perf stat: Abstract stat metrics printing 2016-02-16 17:13:00 -03:00
strbuf.c perf tools: Remove strbuf_{remove,splice}() 2016-02-23 16:21:04 -03:00
strbuf.h perf tools: Remove strbuf_{remove,splice}() 2016-02-23 16:21:04 -03:00
strfilter.c perf tools: Add strfilter__string to recover rules string 2015-05-04 12:43:54 -03:00
strfilter.h perf tools: Add strfilter__string to recover rules string 2015-05-04 12:43:54 -03:00
string.c tools: Adopt memdup() from tools/perf, moving it to tools/lib/string.c 2015-11-18 17:51:02 -03:00
strlist.c perf tools: Add file_only config option to strlist 2016-01-12 12:42:07 -03:00
strlist.h perf tools: Add file_only config option to strlist 2016-01-12 12:42:07 -03:00
svghelper.c perf tools: Add reference counting for cpu_map object 2015-06-25 15:15:50 -03:00
svghelper.h perf timechart: Implement IO mode 2014-07-10 00:22:54 +02:00
symbol-elf.c perf symbols: add Java demangling support 2016-02-05 09:46:45 -03:00
symbol-minimal.c perf symbols: Fix type error when reading a build-id 2015-10-28 10:02:00 -03:00
symbol.c perf symbols: Fix symbols searching for module in buildid-cache 2016-02-12 10:54:47 -03:00
symbol.h perf hists: Basic support of hierarchical report view 2016-02-24 13:35:44 -03:00
target.c perf target: Simplify handling of strerror_r return 2015-03-24 12:08:30 -03:00
target.h perf target: Move the checking of which map function to call into function. 2013-12-04 13:46:37 -03:00
term.c perf tools: Move term functions out of util.c 2015-12-09 13:42:02 -03:00
term.h perf tools: Move term functions out of util.c 2015-12-09 13:42:02 -03:00
thread_map.c perf thread_map: Add thread_map__new_event function 2015-12-17 14:38:16 -03:00
thread_map.h perf thread_map: Add thread_map__new_event function 2015-12-17 14:38:16 -03:00
thread-stack.c perf tools: Ensure thread-stack is flushed 2015-06-19 16:03:33 -03:00
thread-stack.h perf tools: Ensure thread-stack is flushed 2015-06-19 16:03:33 -03:00
thread.c perf thread: Fix reference count initial state 2015-12-14 12:08:55 -03:00
thread.h perf tools: Elliminate alignment holes 2015-05-18 10:17:33 -03:00
tool.h perf tools: Add event_update user level event 2015-12-17 15:10:16 -03:00
top.c perf tools: Rename 'perf_record_opts' to 'record_opts 2013-12-19 14:43:45 -03:00
top.h tools: Consolidate types.h 2014-05-01 21:22:39 +02:00
trace-event-info.c tools lib api fs: Move tracing_path interface into api/fs/tracing_path.c 2015-09-04 12:00:45 -03:00
trace-event-parse.c irq_poll: make blk-iopoll available outside the block layer 2015-12-11 11:52:24 -08:00
trace-event-read.c perf tools: Stop reading the kallsyms data from perf.data 2015-07-23 22:51:11 -03:00
trace-event-scripting.c perf scripting: No need to pass thread twice to the scripting callbacks 2015-04-02 13:18:41 -03:00
trace-event.c tools lib api fs: Adopt filename__read_str from perf 2016-02-16 17:12:56 -03:00
trace-event.h perf script: Add process_stat/process_stat_interval scripting interface 2016-01-06 20:11:15 -03:00
tsc.c perf tools: Move rdtsc() function 2014-07-23 11:48:11 -03:00
tsc.h perf tools: Move rdtsc() function 2014-07-23 11:48:11 -03:00
unwind-libdw.c perf libdw: Check for mmaps also in MAP__VARIABLE tree 2016-01-08 14:16:57 -03:00
unwind-libdw.h perf callchain: Add order support for libdw DWARF unwinder 2015-11-23 18:31:13 -03:00
unwind-libunwind.c perf unwind: Check for mmaps also in MAP__VARIABLE tree 2016-01-08 14:16:34 -03:00
unwind.h perf callchains: Use thread->mg->machine 2014-10-29 10:32:46 -02:00
usage.c perf test: Suppress libtraceevent warnings 2015-10-19 14:58:10 -03:00
util.c perf tools: Make binary data printer code in trace_event public available 2016-02-24 11:38:01 -03:00
util.h perf ui/stdio: Align column header for hierarchy output 2016-02-24 20:21:12 -03:00
values.c perf tools: Use zfree to help detect use after free bugs 2013-12-27 17:08:19 -03:00
values.h tools: Consolidate types.h 2014-05-01 21:22:39 +02:00
vdso.c perf tools: Fix lockup using 32-bit compat vdso 2015-07-07 11:05:08 -03:00
vdso.h perf machine: Fix up vdso methods names 2015-05-29 12:43:44 -03:00
wrapper.c
xyarray.c perf tools: Introduce xyarray__reset function 2015-06-16 10:34:39 -03:00
xyarray.h perf tools: Introduce xyarray__reset function 2015-06-16 10:34:39 -03:00
zlib.c perf tools: Add gzip decompression support for kernel module 2014-11-05 10:11:26 -03:00