summaryrefslogtreecommitdiffstats
path: root/samples/bpf/trace_output_kern.c
diff options
context:
space:
mode:
authorAlexei Starovoitov <ast@plumgrid.com>2015-10-21 05:02:35 +0200
committerDavid S. Miller <davem@davemloft.net>2015-10-22 15:42:15 +0200
commit39111695b1b88a21e81983a38786d877e770da81 (patch)
tree33c57207f5ed18a5408ed9c25ec0ab169ec0fb5d /samples/bpf/trace_output_kern.c
parentbpf: introduce bpf_perf_event_output() helper (diff)
downloadlinux-39111695b1b88a21e81983a38786d877e770da81.tar.xz
linux-39111695b1b88a21e81983a38786d877e770da81.zip
samples: bpf: add bpf_perf_event_output example
Performance test and example of bpf_perf_event_output(). kprobe is attached to sys_write() and trivial bpf program streams pid+cookie into userspace via PERF_COUNT_SW_BPF_OUTPUT event. Usage: $ sudo ./bld_x64/samples/bpf/trace_output recv 2968913 events per sec Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples/bpf/trace_output_kern.c')
-rw-r--r--samples/bpf/trace_output_kern.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/samples/bpf/trace_output_kern.c b/samples/bpf/trace_output_kern.c
new file mode 100644
index 000000000000..8d8d1ec429eb
--- /dev/null
+++ b/samples/bpf/trace_output_kern.c
@@ -0,0 +1,31 @@
+#include <linux/ptrace.h>
+#include <linux/version.h>
+#include <uapi/linux/bpf.h>
+#include "bpf_helpers.h"
+
+struct bpf_map_def SEC("maps") my_map = {
+ .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
+ .key_size = sizeof(int),
+ .value_size = sizeof(u32),
+ .max_entries = 2,
+};
+
+SEC("kprobe/sys_write")
+int bpf_prog1(struct pt_regs *ctx)
+{
+ struct S {
+ u64 pid;
+ u64 cookie;
+ } data;
+
+ memset(&data, 0, sizeof(data));
+ data.pid = bpf_get_current_pid_tgid();
+ data.cookie = 0x12345678;
+
+ bpf_perf_event_output(ctx, &my_map, 0, &data, sizeof(data));
+
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
+u32 _version SEC("version") = LINUX_VERSION_CODE;