Kernel tracepoint support (ugly)
I understand that adding kernel tracepoint support in BPF is going to take a while. In the meantime, would it be possible to put together a tool that relies on kprobes? What I had in mind was to enable the tracepoint using ftrace and then place a kprobe in the trace_event_raw_event_* function or in trace_event_buffer_commit, or some similar location. The kprobe would have access to the entry structure defined by TRACE_EVENT.
Is that a direction worth pursuing? Would it be possible (for efficiency) to instruct the ftrace infrastructure to discard the event and not even put it in the ftrace buffer?
:) I was hoping it won't get to it, but that's a reasonable 'workaround'. Probably enabling tracepoints via perf and adding kprobe to perf_trace_* will be a tiny bit better, since the code that enables tracepoint via perf_event_open will be reused when tracepoint+bpf is ready. but going via ftrace is also ok-ish. Both are ugly hacks, of course, and will be slower than normal kprobe at the beginning of the function, since trace_event_raw* and perf_trace* are 'notrace', so kprobe will do full 'int 3'. Also note that they will not have access to struct of trace_event, but rather 'proto' part of it only.
OK, suppose I go through perf - would it be possible to have perf discard the events and not write them out to perf.data? I'm looking to eliminate the inefficiency in doing analysis and filtering in user space, otherwise it isn't really worth pursuing...
if perf_event_open is done for the task, then perf_trace* will be called, but it will not produce any output, since pid != pid_of_our_python_process. Another approach is to add a filter to it that always returns false. It's a bit slower, since alloc + copy of arguments and registers will be happening before discarding event. The main overhead is int3 anyway.
OK, so just to make sure I understand the recommended approach. Turn on the tracepoints using perf record with a filter that always returns false so that no data actually goes into a file. Place a kprobe on one of the methods that process the perf event, such as perf_tp_event and try to capture data out of the provided record. Makes sense?
Also adding @brendangregg if you have any thoughts.
Turn tracepoint on with perf_event_open() syscall, add a filter via PERF_EVENT_IOC_SET_FILTER ioctl and add kprobe to corresponding perf_trace_* function. There are ~400 of them for each tracepoint. Like perf_trace_block_bio_complete. There will be no trace record. Only functions args in ctx->di, si ... Adding kprobe to perf_tp_event not really usable, since all events will go through it and record will be different and hard to parse.
I was kind of hoping to use perf and not having to replicate the whole "turn on the tracepoint and set a filter" functionality :-) That might be a bit out of scope for something like this, which is by definition temporary until BPF supports tracepoints... What do you think?
you mean to execute perf binary?! that's more complex and slower and fragile than doing a syscall.
Well I'm willing to try. While I have your attention, do you know if there's a simple example of turning on a tracepoint and adding a filter?
bcc/src/src/cc/libbpf.c for opening an event. Doing ioctl is trivial. Just pass a string to it: ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
All right, I think I have something reasonable at this point -- https://github.com/goldshtn/bcc/tree/tpoint. There are a few small issues to figure out, but the main one is this: some tracepoints don't have a corresponding perf_trace_ function, so I don't know where to place the kprobe.
Here's an example that works nicely:
# ./argdist.py -H 't:block:block_rq_complete(void *dummy, void *dummy2, void *dummy3, int nr_bytes):int:nr_bytes'
[05:36:59]
nr_bytes : count distribution
0 -> 1 : 0 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 1 |****************************************|
But there are many examples of tracepoints for which I can't find the corresponding perf_trace_ function. For example:
power:cpu_idle-- the tracepoint exists, but there's noperf_trace_cpu_idlefunction on my system or in fact anything in /proc/kallsyms matchingperf_trace_.*idle- All
syscalls:*tracepoints -- there's justperf_trace_sys_enterandperf_trace_sys_exitbut not specific functions for each syscall
Lastly, the perf_trace_ functions' first argument is not declared in the TP_PROTO macro. Do you know what it is? I can try exploring the trace macro gore but if you happen to know...
I'd appreciate any pointers that would help make this even more useful.
/cc @brendangregg @4ast
I've looked through the patches in your tree, but it was too hard for me to see what's going on. My only recommendation is to do it in generic bcc python module instead of in argdist. Other than that clean it up and send a pull req. Don't bother parsing first arg. It's a pointer to raw 'data' buffer. tracepoint code stores its args in there and then perf side copies this buffer into userspace.
@4ast Thanks! I'll add it to the bcc module. About my other question, do you know why some tracepoints don't have corresponding perf_trace_ functions? Is there any workaround?
syscall* tracepoints are slow and not recommended. It's faster to kprobe into sys_foo. The perf_trace* for power:cpu_idle is actually done by perf_trace_cpu. $ sudo cat /sys/kernel/debug/tracing/events/power/cpu_frequency print fmt: "state=%lu cpu_id=%lu" which is the same format and perf_trace_cpu() helper function used by power:cpu_idle and power:cpu_frequency. I don't know how to easily tell which perf_trace* corresponds to tracepoint name.
Great work! And I didn't realize perf_trace_* was such a strong convention, but I see it now. I should have seen it sooner -- it's from the macros (TRACE_EVENT etc).
So I don't get how we get to nr_bytes.
# cat /sys/kernel/debug/tracing/events/block/block_rq_complete/format
name: block_rq_complete
ID: 777
format:
field:unsigned short common_type; offset:0; size:2; signed:0;
field:unsigned char common_flags; offset:2; size:1; signed:0;
field:unsigned char common_preempt_count; offset:3; size:1; signed:0;
field:int common_pid; offset:4; size:4; signed:1;
field:dev_t dev; offset:8; size:4; signed:0;
field:sector_t sector; offset:16; size:8; signed:0;
field:unsigned int nr_sector; offset:24; size:4; signed:0;
field:int errors; offset:28; size:4; signed:1;
field:char rwbs[8]; offset:32; size:8; signed:1;
field:__data_loc char[] cmd; offset:40; size:4; signed:1;
So the args should be: dev, sector, nr_sector, ... right? So eliding arg0 (as @4ast said), the one-liner should be:
./argdist.py -H 't:block:block_rq_complete(void *dummy2, void *dummy3, int nr_sector)
I guess it's clear from your output anyway, since we aren't doing block device I/O of "8 -> 15" bytes :)
As for @4ast's last Q, how we make tracepoint names to perf_trace_* functions, then I think it can all come from the events pathnames:
/sys/kernel/debug/tracing/events/block/block_rq_complete
I think we can parse that to give us the tracepoint name (last two components -> block:block_rq_complete), and also the function name (last component prefixed with perf_trace_). Although they don't all exist as those functions (eg, one kernel I'm on is missing perf_event_block_bio_backmerge), so we'd have to check /sys/kernel/debug/tracing/available_filter_functions.
RE syscall tracing: we probably should have a docs directory, and a simple syscalls.md document to explain how to trace syscalls, and the caveats of kprobes (stability).
I used to know the difference between sys_read and SyS_read, but I forget right now, and it's not easy to duckduckgo for (aka google for).
I suppose bcc could map syscall tracepoints to the sys kprobes, and have a simple lookup table for that. This would be optional -- you could trace the kprobe directly, or try a syscall tracepoint mapping. The problem is maintenance of the lookup table, especially if we need one bcc repo to support different kernel versions. I'm reminded of SystemTap's tapsets, that get mired in kernel version tests. It might not be so bad if we just do it for syscalls and nothing else.
... block:block_bio_backmerge got inlined, hence no perf_trace_...
@brendangregg re: available_filter_functions is for ftrace only. It's not applicable to kprobe or tracepoints. Most of the time the last two dirs of /sys/kernel/debug/tracing/events/block/block_bio_backmerge will match to perf_trace_aa_bb, but not always. Like in this case the function is perf_trace_block_bio_merge. Inside include/trace/events/block.h there is line "DEFINE_EVENT(block_bio_merge, block_bio_backmerge" which means that tracepoint block:block_bio_backmerge (and block:block_bio_frontmerge) are served by the same perf_trace_block_bio_merge() handler. I don't know how to derive it from things other than source code. I'm not worried about mapping of syscalls. I don't think anyone cares about collecting stats for all syscalls, since new ones are constantly added. There is a standard set of syscalls like sys_open and their prototype are quite stable. Most of the time adding sys_ will do the trick.
Anyway I'm working on proper support for tracepoints. The goal is to let bpf programs define 2nd argument into bpf prog as a special struct. Like for block/block_rq_complete the 2nd arg will be struct S { dev_t dev; sector_t sector; unsigned int nr_sector; int errors; char rwbs[8]; }; and bpf_prog(struct pt_regs_ctx, struct S_tp) will be able to access it directly as 'tp->nr_sector'.
Technically it's possible to hack it already (without kernel changes). The first argument (that I said not to use) will be filled with such struct, but kprobe should be not on the first instruction, but close to the last and ctx->di will be pointing to filled in struct like above. We'd need to bpf_probe_read() it first. Why it would work? because this is how perf_trace_* are defined. See include/trace/perf.h:70
@brendangregg Re nr_bytes vs. nr_sectors, it's weird but the TP_PROTO macro mentions nr_bytes. See here: http://lxr.free-electrons.com/source/include/trace/events/block.h#L145
TRACE_EVENT(block_rq_complete,
TP_PROTO(struct request_queue *q, struct request *rq,
unsigned int nr_bytes),
...
I don't think it's feasible to come up with the perf_trace_ function name by parsing the source code, so I'll just settle for what I'm currently doing and warn that it might fail for some tracepoints. Indeed, savvy users can simply figure out the perf_trace_ function they need and place a kprobe there directly :-)
@4ast I will experiment with a kretprobe on the perf_trace_ functions and see if I have access to the formatted entry struct. That would be best because that's what the tracepoint consumers are going to expect. (As an aside, is BPF tracepoint support expected in 4.6, 4.7, something later?)
Hello
I did a test looking at nr_bytes vs sector as it didn’t make sense to me how we could get nr_bytes when it is used to work out the sector value. So maybe it didn’t matter what was passed in to the script.
root@henky:~# dd if=/dev/zero of=./aaaa oflag=direct bs=8192 count=10 10+0 records in 10+0 records out 81920 bytes (82 kB) copied, 0.00516186 s, 15.9 MB/s
root@henky:/var/tmp/march# ./argdist.py -H 't:block:block_rq_complete(void dummy, void *dummy2, void *dummy3, int allan):int:allan' [19:51:10] allan : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 1 |_| [19:51:11] allan : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 1 |_| [19:51:12] allan : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 2 |**_| [19:51:13] allan : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 2 |_* | 16 -> 31 : 0 | | 32 -> 63 : 0 | | 64 -> 127 : 0 | | 128 -> 255 : 0 | | 256 -> 511 : 0 | | 512 -> 1023 : 0 | | 1024 -> 2047 : 0 | | 2048 -> 4095 : 0 | | 4096 -> 8191 : 0 | | 8192 -> 16383 : 10 |**********|
with nr_sector
root@henky:/var/tmp/march# # ./argdist.py -H 't:block:block_rq_complete(void dummy, void *dummy2, void *dummy3, int nr_sector):int:nr_sector' [20:05:32] [20:05:33] nr_sector : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 1 |_| [20:05:35] nr_sector : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 1 |_* | 16 -> 31 : 0 | | 32 -> 63 : 0 | | 64 -> 127 : 0 | | 128 -> 255 : 0 | | 256 -> 511 : 0 | | 512 -> 1023 : 0 | | 1024 -> 2047 : 0 | | 2048 -> 4095 : 0 | | 4096 -> 8191 : 0 | | 8192 -> 16383 : 10 |_| [20:05:36] nr_sector : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 2 |_* | 16 -> 31 : 0 | | 32 -> 63 : 0 | | 64 -> 127 : 0 | | 128 -> 255 : 0 | | 256 -> 511 : 0 | | 512 -> 1023 : 0 | | 1024 -> 2047 : 0 | | 2048 -> 4095 : 0 | | 4096 -> 8191 : 0 | | 8192 -> 16383 : 10 |*************************************|
On 5 Mar 2016, at 13:45, Sasha Goldshtein [email protected] wrote:
All right, I think I have something reasonable at this point -- https://github.com/goldshtn/bcc/tree/tpoint https://github.com/goldshtn/bcc/tree/tpoint. There are a few small issues to figure out, but the main one is this: some tracepoints don't have a corresponding perf_trace_ function, so I don't know where to place the kprobe.
Here's an example that works nicely:
./argdist.py -H 't:block:block_rq_complete(void *dummy, void *dummy2, void *dummy3, int nr_bytes):int:nr_bytes'
[05:36:59] nr_bytes : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 1 |****************************************| But there are many examples of tracepoints for which I can't find the corresponding perf_trace_ function. For example:
power:cpu_idle -- the tracepoint exists, but there's no perf_trace_cpu_idle function on my system or in fact anything in /proc/kallsyms matching perf_trace_.idle All syscalls: tracepoints -- there's just perf_trace_sys_enter and perf_trace_sys_exit but not specific functions for each syscall Lastly, the perf_trace_ functions' first argument is not declared in the TP_PROTO macro. Do you know what it is? I can try exploring the trace macro gore but if you happen to know...
I'd appreciate any pointers that would help make this even more useful.
/cc @brendangregg https://github.com/brendangregg @4ast https://github.com/4ast — Reply to this email directly or view it on GitHub https://github.com/iovisor/bcc/issues/419#issuecomment-192646451.
I might be missing something, but did you simply run the same command with 'allan' and 'nr_sector' as the parameter names? If so, there will be no difference because the parameter names you give the traced method don't matter at all, and have nothing to do with the names used in the trace structure.