-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing documentation for attach_perf_event and open_perf_event #1756
Comments
Since you are looking at this, could you help with a pull request to add these documentation? The file is |
I'm leaving in a couple of days and I will be AFK for 2/3 weeks. I don't think I will be able to work on it before I leave, but I can do it once I come back if still needed. |
Thanks @cippaciong for pointing out missed documentation. I think they are an important part of usability for BCC. I added a lot of the Perf Event related logic and was just a bit lazy on writing reference guide. I will try to work on adding them as well |
hello, attach_perf_event() is still missed in reference_guide.md. Will someone add it? Thanks! |
Not sure whether @palmtenor got cycles to do this or not. But since you are looking at this, maybe you can submit a pull request to add the missing info. to the reference_guide? |
sure. I can do it, but I can't promise it. |
Sorry for the delay:( I will try spend some time working on the documentations if possible. Meanwhile if you writing can write them up I would be happy to proof read them:) |
Hi, I'm interested in using perf events but also found that there was no documentation. An update would be greatly appreciated. :) |
Still looking for documentation for usage for bpf_attach_perf_event and bpf_attach_perf_event_raw. I want to be able to use raw perf events besides the 9 in perf_event.h. Would be great if we can filter PEBS data records in a bcc function. Any example of using precise events within bcc? |
|
Thanks @yonghong-song. Is there an example of using the raw event. I wish to use the "mem_load_retired.l3_miss:pp" event from perf. I am interested in the virtual data addresses that it dumps as a precise event (using -d flag). How do I capture this through the "perf_event_attr " you mentioned? |
@palmtenor Could you help? Maybe some documentation to translate metrics to attr numbers? I guess you could look at kernel perf code tools/perf/pmu-events/arch/x86/..., might find some information. |
Here is an example I am trying to get working with the "attach_perf_event_raw" api: So, I try to use attach_perf_event_raw in this manner by defining the attribute: try: Questions: Thanks in advance. |
Current bcc does not have |
Tried instrumenting but it does not reach perf_event_open syscall since no such interface is present, as you correctly pointed out. It would be great to have a python interface to "attach_perf_event_raw". Since the C interface "bpf_attach_perf_event()" from libbpf.c anyway calls "bpf_attach_perf_event_raw()" after populating the perf_event_attr structure. But without direct interface to the raw function, we are limited by attributes in "attach_perf_event". |
This is the C++ API. Maybe you can take a look at
This should give you an example how to add |
Thanks @yonghong-song. I was able to create a python interface for "attach_perf_event_raw" which calls the C api "bpf_attach_perf_event_raw" and tested this for raw values of CPU_CYCLES. |
Currently, the uapi
is not enough for you to read other perf event values. There is a trick for this though.
You could use |
Thanks @yonghong-song , please let me know if you have any examples with this trick? I am getting errors compiling bpf code accessing any data structure within bpf_perf_event_data_kern. Nothing on the web using this. |
Something like below (uncompiled, untested)
|
Thanks Yonghong-song. I am reading the "perf_sample_data" structure since it has the addr fields I need (basically enabling perf in ebpf beyond just counting). Seems like my code is extracting fields but many come back as static values which make no sense, maybe my method to read ring buffer is wrong? - Here is my code snippet:
} --> Based on PERF_SAMPLE_DATA structure in perf_event.h def print_event(cpu, data, size): b["events"].open_perf_buffer(print_event) The output come out as: |
The below
is not correct. |
Thanks this has been working. Essentially enabled perf record like behavior with ability to use the attach_perf_event_raw interface. Would need additions to make it usable, like mapping raw events to the perf list etc. before it can be usable in general. |
@hsane2001 Hi do you have an example of your code that you would be willing to share - I am about to begin to debug some code from one of our MSc students, we are attempting to access unc_cbo_cache_lookup.read_i and unc_arb_trk_requests.writes events using open_perf_event from python that fails with an invalid argument and attempts to read the counter value return an error number. The counters succeed with perf stat. |
@drandynisbet - I am still hitting issues of related to sampling where I get much more data from the same perf record command as compared to "perf through epbf". Can you share your code and where it seems to be failing? Also, does not seem like you are using bcc, rather programming perf_event_open dirctly? |
@yonghong-song - Although I am able to read the perf buffer through bcc now, the number of samples get limited to ~10K. I believe this is because BCC is not giving any way to set the MMAP_PAGE_CNT to the mapped buffer which may have a samll default, after perf_event_open is called. After attaching the perf event, how can I set the size of the MMAP buffer? I also ran into limitations on the BPF_HASH which I increased by providing a higher 'size' value than default. |
See tools memleak.py for an example to have bigger default hash table size. |
@hsane2001 text=""" int get_memory_read(void *ctx) { b.attach_uretprobe(name="/home/drandynisbet/MSCS/Jvm_performance/hello", sym="simple_program", fn_name="get_memory_read",pid=pid) # attach probe to function simple_program in hello executable Comment/uncomment to try different counters ... |
sudo strace perf stat -vv -e unc_cbo_cache_lookup.read_i /bin/ls |
@drandynisbet In case of "cnt1.open_perf_event(0xd,0x2081) # FAILS" --> Why is the 1st argument given as 0xd? Since its a raw perf event, it should still be 0x4 (PERF_TYPE_RAW). I am able to program other counters in raw mode this way. |
We have also been trying to follow this example for our own tool. We would like to use precise events to record LLC misses to virtual address regions of a configurable size. We are able to enable perf collection of the appropriate events using attach_perf_event_raw with the C++ API, but we are still having trouble reading the events from the BPF side as described in this thread. Here is our BPF code:
On the user side, we initialize perf as follows:
Our code for initializing the perf_event_attr struct is below:
The trouble we are having is that ctx->addr actually appears to be the correct virtual address. However, if we use the technique described in this post, we are not able to collect any of the other information associated with the sample. Actually, if we cast the ctx pointer to a (bpf_perf_event_data_kern*), the data we read with bpf_probe_read seems to be garbage. Could you provide any additional guidance? @hsane2001 would you mind posting the BPF code you used for your tool? Thanks in advance. |
Just FYI for anyone interested -- I am now able to read the bpf_perf_event_data_kern structure using the following code:
The virtual address this code reads matches exactly with the virtual address in ctx->addr. I thought I would be able to read other parts of the perf_sample_data with this code. However, it looks like most of the perf_sample_data structure is not ready by the time this code is reached. (specifically, in Linux kernel v. 5.7.2, it looks like perf_sample_data_init() has been called, but perf_prepare_sample() is not reached before the BPF callback is reached). |
Hi @drjantz, |
For now, we just want to use BPF to profile the LLC misses to different virtual regions. I think we can get the PID from bpf_get_current_pid_tgid() (and we've tested that call -- the PIDs do look reasonable), so the virtual address is really all we need right now. I just wanted to be able to read the other fields in perf_sample_data in case we found a use for any of that info later. However, it really looks like reading those other fields just gives us garbage right now. For instance, the code below just prints garbage for the PID:
|
Looks like this thread got rather side-tracked, and there still is no documentation for In the meantime, I've found this comment for the C function Lines 115 to 119 in 101304b
Specifically, I was interested in the meaning of the Lines 1459 to 1464 in 949a4e5
|
And then the exact meaning of the various arguments is documented in the man page of the |
Hello, in these days I've been playing around with perf events for a project I've been working on. While I successfully managed to get things working, mainly looking at code examples like
tools/profile.py
, it occurred to me that there is very little documentation aboutattach_perf_event
andopen_perf_event
.The
attach_perf_event
function seems to be completely undocumented, I can't find any mention of it in the reference guide, whileopen_perf_event
is mentioned just once in a more general example aboutBPF_PERF_ARRAY
.It would be nice to add a few lines to explain how to use those helpers, maybe providing some examples showing different ways to interact with them, e.g.:
The text was updated successfully, but these errors were encountered: