Skip to content
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

How to ensure perf buffer memory alignment? #2432

Closed
yzhao1012 opened this issue Jun 29, 2019 · 4 comments
Closed

How to ensure perf buffer memory alignment? #2432

yzhao1012 opened this issue Jun 29, 2019 · 4 comments

Comments

@yzhao1012
Copy link
Contributor

Hi All,

We have a struct used in C code, some thing like:
struct event {
u64 data;
} attribute((packed, aligned(8));

BPF_PERF_OUTPUT(events);
events.perf_submit(..., sizeof(event));

In the user space C++ code, we open the perf buffer with 8 pages:
BPF bpf;
bpf.open_perf_buffer(events, output_fn, loss_fn, cookie, 8);

When we read the data from perf buffer, we sometimes get unaligned pointers.

Because the submitted memory is 8 bytes, that means that the perf buffer's own memory is not aligned on 8 bytes.

Is there anyway we can ensure perf buffer's own memory alignment?

@yzhao1012
Copy link
Contributor Author

Note:
IIRC, open_perf_buffer() eventually mmap() the pages, and that memory region is always aligned:
https://stackoverflow.com/questions/42259495/does-mmap-return-aligned-pointer-values

If that's the case, then submitting aligned memory chunk should always result into read at aligned address, right?

But we do saw unaligned pointer. :(

@palmtenor
Copy link
Member

A little bit more details: actual struct got written to the Perf Buffer is a PERF_RECORD_SAMPLE (see perf_event_open.2 man page). In BCC case we don't set any of the PERF_SAMPLE_* bits, so it would be just the 8-byte struct perf_event_header followed by size and the raw data. The man page says:

    size, data[size]
        If PERF_SAMPLE_RAW is enabled, then a 32-bit value
        indicating size is included followed by an array of
        8-bit values of length size.  The values are padded
        with 0 to have 64-bit alignment.

@yonghong-song
Copy link
Collaborator

From @palmtenor explanation in the above, the data alignment in the memory will be 4, but not 8. Maybe you want to design your struct event to be aligned at 4 (not 8) and your interested fields aligned at 8.

@yzhao1012
Copy link
Contributor Author

Thanks All!

1 quick clarification:
"size, data[size]" "The values are padded with 0 to have 64-bit alignment"
This sounds like #1 perf buffer's memory region is 64bit/8bytes aligned, but when read, the data pointer points 'data', which is 4 bytes after size, therefore it becomes 4 bytes aligned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants