Skip to content

Added perf_event_header definition #24264

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

GiuseppeCesarano
Copy link

While using the Linux perf subsystem in sampling mode, I noticed that the perf_event_header struct and its associated constants were missing from the bindings. This struct is essential as a building block for user-defined structs that can be bitcast to the events in the mmaped output region.

The struct definition and constants are based on the uapi source.

I tried to keep the style and naming consistent with the rest of the codebase. Since this is my first PR to the project, please let me know if anything needs adjustment.

@alexrp alexrp self-assigned this Jun 25, 2025
@GiuseppeCesarano
Copy link
Author

I've also noticed that perf_event_mmap_page is missing. Here's a possible translation:

pub const perf_event_mmap_page = extern struct {
    version: u32,
    compt_version: u32,
    lock: u32,
    index: u32,
    offset: i64,
    time_enabled: u64,
    time_running: u64,
    capabilities: extern union {
        capabilities: u64,
        cap: packed struct {
            bit0: u1,
            bit0_is_deprecated: u1,
            user_rdpmc: u1,
            user_time: u1,
            user_time_zero: u1,
            user_time_short: u1,
            ____res: u58,
        },
    },
    pcm_width: u16,
    time_shift: u16,
    time_mult: u32,
    time_offset: u64,
    time_zero: u64,
    size: u32,
    __reserved_1: u32,
    time_cycles: u64,
    time_mask: u64,
    __reserved: [116 * 8]u8,
    data_head: u64,
    data_tail: u64,
    data_offset: u64,
    data_size: u64,
    aux_head: u64,
    aux_tail: u64,
    aux_offset: u64,
    aux_size: u64,
};

I'm not including it yet in this PR because the capabilities field adds a small naming/layout decision — the Zig version would need to be accessed like:

_ = m.capabilities.capabilities;
// or
_ = m.capabilities.cap.bit0;

while in c would be

m.capabilities;
// or
m.cap_bit0;

Since this is my first PR to Zig, I’d rather not make that call on my own. If there’s a preferred naming or layout approach here, I’d be glad to follow up with a separate PR or add it to this one as a second commit.

@alexrp
Copy link
Member

alexrp commented Jul 3, 2025

Here's a possible translation

I think capabilities should just be a packed struct - no need for the union.

@GiuseppeCesarano
Copy link
Author

I added the perf_event_mmap_page definition and removed the default values for perf_event_header because this struct will be bitcasted to a memory location, making the default initializations unnecessary.

I also noticed that the current perf_event_attr definition is outdated; the struct now has more flags and additional fields. See the Linux current UAPI and Zig std definition for reference. If needed, I’m happy to update that too—just let me know if you prefer it in this PR or a separate one.

@alexrp
Copy link
Member

alexrp commented Jul 3, 2025

If needed, I’m happy to update that too—just let me know if you prefer it in this PR or a separate one.

Sure, might as well include that if you feel like it.

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

Successfully merging this pull request may close these issues.

2 participants