Add a tracepoint for BPF perf sampling
Subsystems (3)
PATCH v2 3/3 perf script: add bpf-output field to usage message
In reply to Brendan GreggThis adds the bpf-output field to the perf script usage message, and docs.
--- tools/perf/Documentation/perf-script.txt | 4 ++-- tools/perf/builtin-script.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-script.txt +2 -2
@@ -116,8 +116,8 @@ OPTIONS --fields:: Comma separated list of fields to print. Options are: comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff, - srcline, period, iregs, brstack, brstacksym, flags. - Field list can be prepended with the type, trace, sw or hw, + srcline, period, iregs, brstack, brstacksym, flags, bpf-output, + callindent. Field list can be prepended with the type, trace, sw or hw, to indicate to which event type the field list applies. e.g., -F sw:comm,tid,time,ip,sym and -F trace:time,cpu,trace
tools/perf/builtin-script.c +1 -1
@@ -2116,7 +2116,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) "Valid types: hw,sw,trace,raw. " "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," "addr,symoff,period,iregs,brstack,brstacksym,flags," - "callindent", parse_output_fields), + "bpf-output,callindent", parse_output_fields),OPT_BOOLEAN ('a' ,"all-cpus" , &system_wide,"system-wide collection from all CPUs" ), OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
Signature · 1 line
2.7.4
v2 cover Add a tracepoint for BPF perf sampling
This patchset adds a tracepoint for perf sampling, perf:perf_hrtimer, and includes a complete example in samples/bpf for using it to frequency count sampled instruction pointers in a BPF map.
---
Changes in v2:
- added samples/bpf/sampleip* example for perf:perf_hrtimer
- added bpf-output to perf script usage message
Brendan Gregg (3):
perf/core: Add a tracepoint for perf sampling samples/bpf: Add a sampling BPF example perf script: add bpf-output field to usage message
include/trace/events/perf.h | 29 +++++ kernel/events/core.c | 5 + samples/bpf/Makefile | 4 + samples/bpf/sampleip_kern.c | 48 ++++++++ samples/bpf/sampleip_user.c | 189 +++++++++++++++++++++++++++++++ tools/perf/Documentation/perf-script.txt | 4 +- tools/perf/builtin-script.c | 2 +- 7 files changed, 278 insertions(+), 3 deletions(-) create mode 100644 include/trace/events/perf.h create mode 100644 samples/bpf/sampleip_kern.c create mode 100644 samples/bpf/sampleip_user.c
Signature · 1 line
2.7.4
PATCH v2 1/3 perf/core: Add a tracepoint for perf sampling
When perf is performing hrtimer-based sampling, this tracepoint can be used by BPF to run additional logic on each sample. For example, BPF can fetch stack traces and frequency count them in kernel context, for an efficient profiler.
--- include/trace/events/perf.h | 29 +++++++++++++++++++++++++++++ kernel/events/core.c | 5 +++++ 2 files changed, 34 insertions(+) create mode 100644 include/trace/events/perf.h
include/trace/events/perf.h new file +29 -0
@@ -0,0 +1,29 @@ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM perf + +#if !defined(_TRACE_PERF_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_PERF_H + +#include <linux/tracepoint.h> + +TRACE_EVENT (perf_hrtimer, + TP_PROTO(struct pt_regs *regs, struct perf_event *event), + +TP_ARGS (regs, event), + +TP_STRUCT__entry ( + __field(struct pt_regs *, regs) + __field(struct perf_event *, event) + ), + +TP_fast_assign ( + __entry->regs = regs; + __entry->event = event; + ), + +TP_printk ("regs=%p evt=%p" , __entry->regs, __entry->event) +); +#endif /* _TRACE_PERF_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h>
kernel/events/core.c +5 -0
@@ -51,6 +51,9 @@#include <asm/irq_regs.h> +#define CREATE_TRACE_POINTS +#include <trace/events/perf.h> +typedef int (*remote_function_f )(void *);struct remote_function_call {
@@ -8062,6 +8065,8 @@ static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)perf_sample_data_init (&data,0 , event->hw.last_period); regs =get_irq_regs (); +trace_perf_hrtimer (regs, event); +if (regs && !perf_exclude_event (event, regs)) {if (!(event->attr.exclude_idle &&is_idle_task (current)))if (__perf_event_overflow (event,1 , &data, regs))
Signature · 1 line
2.7.4
PATCH v2 2/3 samples/bpf: Add a sampling BPF example
In reply to Brendan GreggThis example samples the instruction pointer at a timed interval, and frequency counts it in a BPF map. It is an example of summarizing sampled data in-kernel for passing to user space. It uses the perf:perf_hrtimer tracepoint with perf_events sampling.
Example output:
Sampling at 99 Hertz for 5 seconds. Ctrl-C also ends.
ADDR KSYM COUNT 0xffffffff81257088 __fsnotify_parent 1 0x7f20b792d9b0 (user) 1 0xffffffff8121469e __vfs_read 1 0xffffffff81214afd rw_verify_area 1 0xffffffff8123327e __fget_light 1 0x7fc0965a6e2c (user) 1 0xffffffff81233c04 __fdget_pos 1 0xffffffff81378528 common_file_perm 1 0x404d90 (user) 1 0xffffffff81214c13 vfs_read 1 [...] 0xffffffff813d9e97 copy_user_enhanced_fast_string 3 0xffffffff817e310c _raw_spin_lock_irqsave 4 0xffffffff817e31a0 entry_SYSCALL_64_fastpath 4 0xffffffff814fb96c extract_crng 6 0xffffffff813d9e95 copy_user_enhanced_fast_string 7 0xffffffff814fb8a3 _extract_crng 7 0xffffffff817e2d55 _raw_spin_unlock_irqrestore 1399 0xffffffff8105fb46 native_safe_halt 2190
It also has basic options:
USAGE: sampleip [-F freq] [duration]
-F freq # sample frequency (Hertz), default 99
duration # sampling duration (seconds), default 5
--- samples/bpf/Makefile | 4 + samples/bpf/sampleip_kern.c | 48 +++++++++++ samples/bpf/sampleip_user.c | 189 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 samples/bpf/sampleip_kern.c create mode 100644 samples/bpf/sampleip_user.c
samples/bpf/Makefile +4 -0
@@ -24,6 +24,7 @@ hostprogs-y += test_overhead hostprogs-y += test_cgrp2_array_pin hostprogs-y += xdp1 hostprogs-y += xdp2 +hostprogs-y += sampleip test_verifier-objs := test_verifier.o libbpf.o test_maps-objs := test_maps.o libbpf.o
@@ -49,6 +50,7 @@ test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o xdp1-objs := bpf_load.o libbpf.o xdp1_user.o # reuse xdp1 source intentionally xdp2-objs := bpf_load.o libbpf.o xdp1_user.o +sampleip-objs := bpf_load.o libbpf.o sampleip_user.o # Tell kbuild to always build the programs always := $(hostprogs-y)
@@ -74,6 +76,7 @@ always += parse_varlen.o parse_simple.o parse_ldabs.o always += test_cgrp2_tc_kern.o always += xdp1_kern.o always += xdp2_kern.o +always += sampleip_kern.o HOSTCFLAGS += -I$(objtree)/usr/include
@@ -97,6 +100,7 @@ HOSTLOADLIBES_map_perf_test += -lelf -lrt HOSTLOADLIBES_test_overhead += -lelf -lrt HOSTLOADLIBES_xdp1 += -lelf HOSTLOADLIBES_xdp2 += -lelf +HOSTLOADLIBES_sampleip += -lelf # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: # make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
samples/bpf/sampleip_kern.c new file +48 -0
48 lines not on this page · raw message
samples/bpf/sampleip_user.c new file +189 -0
189 lines not on this page · raw message
Signature · 1 line
2.7.4
Peter Zijlstra
maintainerRe: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
In reply to Brendan GreggQuoted 4 lines · Brendan Gregg
When perf is performing hrtimer-based sampling, this tracepoint can be used by BPF to run additional logic on each sample. For example, BPF can fetch stack traces and frequency count them in kernel context, for an efficient profiler.
Urgh, no like.
And then next week we'll add a tracepoint to some other random pmu or whatnot.
Re: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Brendan Gregg
When perf is performing hrtimer-based sampling, this tracepoint can be used by BPF to run additional logic on each sample. For example, BPF can fetch stack traces and frequency count them in kernel context, for an efficient profiler.
Urgh, no like.
And then next week we'll add a tracepoint to some other random pmu or whatnot.
I wouldn't want random pmu tracepoints either, but I can see how it looks with a new tracepoint category of "perf:", and maybe I shouldn't have called it that. Would it be better if the tracepoint was called "timer:perf_hrtimer"?
As for pmu tracepoints: if I were to instrument it (although I wasn't planning to), I'd put a tracepoint in perf_event_overflow() called "perf:perf_overflow", with the same arguments. That could then be used for all PMU overflow events, without needing to add specific tracepoints. The "perf:" category would then have two tracepoints, and would not need to grow. Does that option sound better than "timer:perf_hrtimer"? (As in, keeping the category "perf" so that we have a later option of adding a perf:perf_overflow tracepoint if need be?)
It's really perf_hrtimer that we'll use daily, for CPU profiling with in-kernel aggregations of instruction pointers and stack traces, and other sampling needs. It's one tracepoint that will add much value, and will mean that BPF programs can then be attached to kprobes, uprobes, tracepoints, and timed samples.
Thanks for your consideration,
Brendan
Peter Zijlstra
maintainerRe: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
Quoted 5 lines · Brendan Gregg
As for pmu tracepoints: if I were to instrument it (although I wasn't planning to), I'd put a tracepoint in
perf_event_overflow()called "perf:perf_overflow", with the same arguments. That could then be used for all PMU overflow events, without needing to add specific tracepoints.
Could we not teach BPF to replace event->overflow_handler and inject itself there?
We don't currently have nice interfaces for doing that, but it should be possible to do I think. We already have the indirect function call, so injecting ourself there has 0 overhead.
Re: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Brendan Gregg
As for pmu tracepoints: if I were to instrument it (although I wasn't planning to), I'd put a tracepoint in
perf_event_overflow()called "perf:perf_overflow", with the same arguments. That could then be used for all PMU overflow events, without needing to add specific tracepoints.
Quoted 6 lines
Could we not teach BPF to replace
event->overflow_handlerand inject itself there?We don't currently have nice interfaces for doing that, but it should be possible to do I think. We already have the indirect function call, so injecting ourself there has 0 overhead.
you're right. All makes sense. I guess I was too lazy to look into how to do it properly. Adding a tracepoint looked like quick and easy way to achieve the same.
As far as api goes probably existing IOC_SET_BPF ioctl will do too. Currently overflow_handler is set at event alloc time. If we start changing it on the fly with atomic xchg(), afaik things shouldn't break, since each overflow_handler is run to completion and doesn't change global state, right?
Re: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
Quoted 9 lines · Alexei Starovoitov
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Brendan Gregg
As for pmu tracepoints: if I were to instrument it (although I wasn't planning to), I'd put a tracepoint in
perf_event_overflow()called "perf:perf_overflow", with the same arguments. That could then be used for all PMU overflow events, without needing to add specific tracepoints.
Quoted 6 lines
Quoted 6 lines
Could we not teach BPF to replace
event->overflow_handlerand inject itself there?We don't currently have nice interfaces for doing that, but it should be possible to do I think. We already have the indirect function call, so injecting ourself there has 0 overhead.
Sounds like a good idea, especially for things like struct file_operations so that we can statically instrument file system read/writes with zero non-enabled overhead, and not worry about high frequency workloads (>10M events/sec).
These perf probes aren't high frequency, though, and the code is not normally in use, so overhead should be much less of a concern. Sampling at 999 Hertz * CPUs is as frequent as I'd go. And if the tracepoint code is still adding a mem read, conditional, and branch, then that's not many instructions, especially considering the normal use case of these perf functions: creating records and writing to a perf ring buffer, then picking that up in user space by perf, then either processing it live or writing to perf.data, back to the file system, etc. It would be hard to benchmark the effect of adding a few instructions to that path (and any results may be more sensitive to cache line placement than the instructions).
The perf:perf_hrtimer probe point is also reading state mid-way through a function, so it's not quite as simple as wrapping the function pointer. I do like that idea, though, but for things like struct file_operations.
Quoted 10 lines
you're right. All makes sense. I guess I was too lazy to look into how to do it properly. Adding a tracepoint looked like quick and easy way to achieve the same.
As far as api goes probably existingIOC_SET_BPFioctl will do too. Currentlyoverflow_handleris set at event alloc time. If we start changing it on the fly with atomicxchg(), afaik things shouldn't break, since eachoverflow_handleris run to completion and doesn't change global state, right?
How would it be implemented? I was thinking of adding explicit wrappers, eg:
static ssize_t__ext4_file_write_iter_tracepoint(struct kiocb *iocb, struct iov_iter *from)
{
trace_ext4_write_iter(iocb, from);
ext4_file_write_iter(iocb, from);
}Then switching in __ext4_file_write_iter_tracepoint() as needed.
I was wondering about doing this dynamically, but wouldn't that then create another problem of maintenance -- we'd need to decorate such code with a comment, at least, to say "this function is exposed as a tracepoint".
If a dynamic approach is still desired, and the goal is zero non-enabled overhead, then I'd also wonder if we could leverage kprobes to do this. Imagine walking a file_operations to find the addresses, and then kprobe-ing the addresses. Not the same (or probably as efficient) as setting the function pointer, but, using existing kprobes.
Brendan
Re: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
Quoted 23 lines · Brendan Gregg
Quoted 9 lines · Alexei Starovoitov
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Brendan Gregg
As for pmu tracepoints: if I were to instrument it (although I wasn't planning to), I'd put a tracepoint in
perf_event_overflow()called "perf:perf_overflow", with the same arguments. That could then be used for all PMU overflow events, without needing to add specific tracepoints.Quoted 6 lines
Quoted 6 lines
Could we not teach BPF to replace
event->overflow_handlerand inject itself there?We don't currently have nice interfaces for doing that, but it should be possible to do I think. We already have the indirect function call, so injecting ourself there has 0 overhead.
Sounds like a good idea, especially for things like struct
file_operationsso that we can statically instrument file system read/writes with zero non-enabled overhead, and not worry about high frequency workloads (>10M events/sec).
Quoted 11 lines
These perf probes aren't high frequency, though, and the code is not normally in use, so overhead should be much less of a concern. Sampling at 999 Hertz * CPUs is as frequent as I'd go. And if the tracepoint code is still adding a mem read, conditional, and branch, then that's not many instructions, especially considering the normal use case of these perf functions: creating records and writing to a perf ring buffer, then picking that up in user space by perf, then either processing it live or writing to perf.data, back to the file system, etc. It would be hard to benchmark the effect of adding a few instructions to that path (and any results may be more sensitive to cache line placement than the instructions).
tracepoints are actually zero overhead already via static-key mechanism. I don't think Peter's objection for the tracepoint was due to overhead.
Quoted 16 lines
The perf:perf_hrtimer probe point is also reading state mid-way through a function, so it's not quite as simple as wrapping the function pointer. I do like that idea, though, but for things like struct
file_operations.Quoted 10 lines
you're right. All makes sense. I guess I was too lazy to look into how to do it properly. Adding a tracepoint looked like quick and easy way to achieve the same.
As far as api goes probably existingIOC_SET_BPFioctl will do too. Currentlyoverflow_handleris set at event alloc time. If we start changing it on the fly with atomicxchg(), afaik things shouldn't break, since eachoverflow_handleris run to completion and doesn't change global state, right?
Quoted 1 line
How would it be implemented? I was thinking of adding explicit wrappers, eg:
instead of adding a tracepoint to perf_swevent_hrtimer we can replace overflow_handler for that particular event with some form of bpf wrapper. (probably new bpf program type). Then not only periodic events will be triggering bpf prog, but pmu events as well.
So instead of normal __perf_event_output() writing into ringbuffer, a bpf prog will be called that can optionally write into different rb via bpf_perf_event_output. The question is what to pass into the program to make the most use out of it. 'struct pt_regs' is done deal. but perf_sample_data we cannot pass as-is, since it's kernel internal. Probably something similar to __sk_buff mirror would be needed. Another nice benefit of doing via overflow_handler instead of tracepoint is that exclude_idle, exclude_user, exclude_kernel flags of the perf event will all magically work and program will be event specific.
So two parallel 'perf record'-like sampling won't conflict.
Peter Zijlstra
maintainerRe: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
Quoted 2 lines · Alexei Starovoitov
tracepoints are actually zero overhead already via static-key mechanism. I don't think Peter's objection for the tracepoint was due to overhead.
Almost 0, they still have some I$ footprint, but yes. My main worry is that we can feed tracepoints into perf, so having tracepoints in perf is tricky.
I also don't much like this tracepoint being specific to the hrtimer bits, I can well imagine people wanting to do the same thing for hardware based samples or whatnot.
Quoted 4 lines
Quoted 4 lines
The perf:perf_hrtimer probe point is also reading state mid-way through a function, so it's not quite as simple as wrapping the function pointer. I do like that idea, though, but for things like struct
file_operations.
So what additional state to you need?
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Currently
overflow_handleris set at event alloc time. If we start changing it on the fly with atomicxchg(), afaik things shouldn't break, since eachoverflow_handleris run to completion and doesn't change global state, right?
Yes, or even a simple WRITE_ONCE() to replace it, as long as we make sure to use a READ_ONCE() to load the pointer.
As long as we're sure to limit this poking to a single user its fairly simple to get right. The moment there can be concurrency a lot of fail can happen.
Quoted 4 lines
instead of adding a tracepoint to
perf_swevent_hrtimerwe can replaceoverflow_handlerfor that particular event with some form of bpf wrapper. (probably new bpf program type). Then not only periodic events will be triggering bpf prog, but pmu events as well.
Exactly.
Quoted 3 lines
So instead of normal
__perf_event_output()writing into ringbuffer, a bpf prog will be called that can optionally write into different rb viabpf_perf_event_output.
It could even chain and call into the original function once its done and have both outputs.
Quoted 3 lines
The question is what to pass into the
program to make the most use out of it. 'structpt_regs' is done deal. butperf_sample_datawe cannot pass as-is, since it's kernel internal.
Urgh, does it have to be stable API? Can't we simply rely on the kernel headers to provide the right structure definition?
Re: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
Quoted 7 lines · Peter Zijlstra
Quoted 2 lines · Alexei Starovoitov
tracepoints are actually zero overhead already via static-key mechanism. I don't think Peter's objection for the tracepoint was due to overhead.
Almost 0, they still have some I$ footprint, but yes. My main worry is that we can feed tracepoints into perf, so having tracepoints in perf is tricky.
Coincidentally I$ footprint was my most recent use case for needing
this: I have an I$ busting workload, and wanting to profile instructions at a very high rate to get a breakdown of I$ population. (Normally I'd use I$ miss overflow, but none of our Linux systems have
PMCs: cloud.)
Quoted 3 lines
I also don't much like this tracepoint being specific to the hrtimer bits, I can well imagine people wanting to do the same thing for hardware based samples or whatnot.
Sure, which is why I thought we'd have two in a perf category. I'm all for PMCs events, even though we can't currently use them!
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
The perf:perf_hrtimer probe point is also reading state mid-way through a function, so it's not quite as simple as wrapping the function pointer. I do like that idea, though, but for things like struct
file_operations.So what additional state to you need?
I was pulling in regs after get_irq_regs(), struct perf_event *event after it's populated. Not that hard to duplicate. Just noting it didn't map directly to the function entry.
I wanted perf_event just for event->ctx->task->pid, so that a BPF program can differentiate between it's samples and other concurrent sessions.
(I was thinking of changing my patch to expose pid_t instead of perf_event, since I was noticing it didn't add many instructions.)
[...]
Quoted 6 lines
Quoted 4 lines
instead of adding a tracepoint to
perf_swevent_hrtimerwe can replaceoverflow_handlerfor that particular event with some form of bpf wrapper. (probably new bpf program type). Then not only periodic events will be triggering bpf prog, but pmu events as well.Exactly.
Although the timer use case is a bit different, and is via hwc->hrtimer.function = perf_swevent_hrtimer.
[...]
Quoted 6 lines
Quoted 3 lines
The question is what to pass into the
program to make the most use out of it. 'structpt_regs' is done deal. butperf_sample_datawe cannot pass as-is, since it's kernel internal.Urgh, does it have to be stable API? Can't we simply rely on the kernel headers to provide the right structure definition?
For timer it can be: struct pt_regs, pid_t.
So that would restrict your BPF program to one timer, since if you had two (from one pid) you couldn't tell them apart. But I'm not sure of a use case for two in-kernel timers. If there were, we could also add struct perf_event_attr, which has enough info to tell things apart, and is already exposed to user space.
I haven't looked into the PMU arguments, but perhaps that could be:
struct pt_regs, pid_t, struct perf_event_attr.
Thanks,
Brendan
Re: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
In reply to Peter ZijlstraQuoted 11 lines · Peter Zijlstra
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Currently
overflow_handleris set at event alloc time. If we start changing it on the fly with atomicxchg(), afaik things shouldn't break, since eachoverflow_handleris run to completion and doesn't change global state, right?Yes, or even a simple
WRITE_ONCE()to replace it, as long as we make sure to use aREAD_ONCE()to load the pointer.As long as we're sure to limit this poking to a single user its fairly simple to get right. The moment there can be concurrency a lot of fail can happen.
agreed.
Quoted 6 lines
Quoted 3 lines
So instead of normal
__perf_event_output()writing into ringbuffer, a bpf prog will be called that can optionally write into different rb viabpf_perf_event_output.It could even chain and call into the original function once its done and have both outputs.
interesting idea. makes sense.
Also thinking about concurrency and the need to remember the original handler somewhere, would it be cleaner api to add a bit to perf_event_attr and use attr.config1 as bpf_fd ?
Then perf_event_open at event creation time will use bpf prog as overflow_handler. That solves concurrency concerns and potential semantical issues if we go with ioctl() approach.
Like if we perf_event_open() an event for a task, then bpf attach to it, what children task and corresponding inherited events suppose to do? Inherit overflow_handler, right? but then deatch of bpf in the parent suppose to clear it in inherited events as well. A bit complicated. I guess we can define it that way.
Just seems easier to do bpf attach at perf_event_open time only.
Quoted 6 lines
Quoted 3 lines
The question is what to pass into the
program to make the most use out of it. 'structpt_regs' is done deal. butperf_sample_datawe cannot pass as-is, since it's kernel internal.Urgh, does it have to be stable API? Can't we simply rely on the kernel headers to provide the right structure definition?
yes we can. The concern is about assumptions people will make about perf_sample_data and the speed of access to it. From bpf program point of view the pointer to perf_sample_data will be opaque unsafe pointer, so any access to fields would have to be done via bpf_probe_read which has non-trivial overhead.
If we go with the uapi mirror of perf_sample_data approach, it will be fast, since mirror is not an actual struct. Like the 'struct __sk_buff' we have in uapi/linux/bpf.h is a meta structure. It's not allocated anywhere and no fields are copied. When bpf program does 'skb->vlan_present' the verifier rewrites it at load time into corresponding access to kernel internal 'struct sk_buff' fields with bitmask, shifts and such. For this case we can define something like
struct bpf_perf_sample_data {
__u64 period;
};
then bpf prog will only be able to access that signle field which verifier will translate into 'data->period' where data is 'struct perf_sample_data *' Later we can add other fields if necessary. The kernel is free to mess around with perf_sample_data whichever way without impacting bpf progs.
Peter Zijlstra
maintainerRe: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
In reply to Brendan GreggQuoted 2 lines · Brendan Gregg
(Normally I'd use I$ miss overflow, but none of our Linux systems have
PMCs: cloud.)
I think I had better not comment on that ;-)
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
The perf:perf_hrtimer probe point is also reading state mid-way through a function, so it's not quite as simple as wrapping the function pointer. I do like that idea, though, but for things like struct
file_operations.So what additional state to you need?
I was pulling in regs after
get_irq_regs(), structperf_event*event after it's populated. Not that hard to duplicate. Just noting it didn't map directly to the function entry.
Right, both of which are available to the overflow handler.
Quoted 6 lines
I wanted
perf_eventjust forevent->ctx->task->pid, so that a BPF program can differentiate between it's samples and other concurrent sessions.(I was thinking of changing my patch to expose
pid_tinstead ofperf_event, since I was noticing it didn't add many instructions.)
Slightly confused, event->ctx->task == current, no? We flip that pointer when we flip the contexts.
At which point, it should be the same as SAMPLE_TID.
!?
Quoted 10 lines
[...]
Quoted 6 lines
Quoted 4 lines
instead of adding a tracepoint to
perf_swevent_hrtimerwe can replaceoverflow_handlerfor that particular event with some form of bpf wrapper. (probably new bpf program type). Then not only periodic events will be triggering bpf prog, but pmu events as well.Exactly.
Although the timer use case is a bit different, and is via
hwc->hrtimer.function=perf_swevent_hrtimer.
Still not entirely sure why you could not hook into event->overflow_handler.
Peter Zijlstra
maintainerRe: [PATCH v2 1/3] perf/core: Add a tracepoint for perf sampling
In reply to Alexei StarovoitovQuoted 3 lines · Alexei Starovoitov
Also thinking about concurrency and the need to remember the original handler somewhere, would it be cleaner api to add a bit to
perf_event_attrand use attr.config1 asbpf_fd?
attr.config[12] are in use already, typically uncore events need them. We cannot rely on those bits being unused.
Quoted 10 lines
Then
perf_event_openat event creation time will use bpf prog asoverflow_handler. That solves concurrency concerns and potential semantical issues if we go withioctl()approach.Like if we
perf_event_open()an event for a task, then bpf attach to it, what children task and corresponding inherited events suppose to do? Inheritoverflow_handler, right? but then deatch of bpf in the parent suppose to clear it in inherited events as well. A bit complicated. I guess we can define it that way.
Just seems easier to do bpf attach atperf_event_opentime only.
Which is why I would've liked BPF to create its own events, instead of userspace passing them in through this array.
Because now you have a chicken'n'egg issue with that you want BPF to use the overflow handler but need BPF running before you have an actual handler to link to.
Quoted 14 lines
Quoted 2 lines
Urgh, does it have to be stable API? Can't we simply rely on the kernel headers to provide the right structure definition?
yes we can. The concern is about assumptions people will make about
perf_sample_dataand the speed of access to it. From bpf program point of view the pointer toperf_sample_datawill be opaque unsafe pointer, so any access to fields would have to be done viabpf_probe_readwhich has non-trivial overhead.
If we go with the uapi mirror ofperf_sample_dataapproach, it will be fast, since mirror is not an actual struct. Like the 'struct__sk_buff' we have inuapi/linux/bpf.his a meta structure. It's not allocated anywhere and no fields are copied. When bpf program does 'skb->vlan_present' the verifier rewrites it at load time into corresponding access to kernel internal 'structsk_buff' fields with bitmask, shifts and such.
Quoted 8 lines
For this case we can define something like
structbpf_perf_sample_data{
__u64period;
};
then bpf prog will only be able to access that signle field which verifier will translate into 'data->period' where data is 'structperf_sample_data*' Later we can add other fields if necessary. The kernel is free to mess around withperf_sample_datawhichever way without impacting bpf progs.
Hmm, I was not aware of that. Should be doable indeed.
tip-bot for Brendan Gregg
merge bot[tip:perf/urgent] perf script: Add 'bpf-output' field to usage message
In reply to Brendan Greggperf script: Add 'bpf-output' field to usage message
Commit-ID: bcdc09af3ef30ef071677544ce23a1c8873a2dda Gitweb: http://git.kernel.org/tip/bcdc09af3ef30ef071677544ce23a1c8873a2dda Author: Brendan Gregg <[email protected]> AuthorDate: Wed, 3 Aug 2016 02:47:49 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Tue, 9 Aug 2016 10:46:43 -0300
perf script: Add 'bpf-output' field to usage message
This adds the 'bpf-output' field to the perf script usage message, and docs.
--- tools/perf/Documentation/perf-script.txt | 4 ++-- tools/perf/builtin-script.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-script.txt +2 -2
@@ -116,8 +116,8 @@ OPTIONS --fields:: Comma separated list of fields to print. Options are: comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff, - srcline, period, iregs, brstack, brstacksym, flags. - Field list can be prepended with the type, trace, sw or hw, + srcline, period, iregs, brstack, brstacksym, flags, bpf-output, + callindent. Field list can be prepended with the type, trace, sw or hw, to indicate to which event type the field list applies. e.g., -F sw:comm,tid,time,ip,sym and -F trace:time,cpu,trace
tools/perf/builtin-script.c +1 -1
@@ -2116,7 +2116,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused) "Valid types: hw,sw,trace,raw. " "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso," "addr,symoff,period,iregs,brstack,brstacksym,flags," - "callindent", parse_output_fields), + "bpf-output,callindent", parse_output_fields),OPT_BOOLEAN ('a' ,"all-cpus" , &system_wide,"system-wide collection from all CPUs" ), OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",