Part of the accepted-design index. See the perf guide for usage and permissions.
perf uses the existing x/sys/unix open, attribute, read, and ioctl bindings.
The counting fields fit the original attribute prefix. For a size rejection,
retry once only if Linux reports a smaller valid size that retains every
requested field. Other errors, including a PMU-specific refusal, retain their
errno. The public API owns event specifications and resources. The package has
no dependency on the workload, scheduling, or mapping facilities.
A group owns its leader and every sibling. Fixed membership provides a bounded read size and keeps IDs in configuration order. It also avoids group mutation racing reads or changing an existing inherited layout. A target can still fork while the group opens, so the workload owner coordinates startup when using inheritance. The group keeps its descriptors private. Siblings open enabled so toggling the leader alone schedules them together, including when counting resumes. Reset uses the group ioctl flag to clear every member’s count.
Request counts, IDs, and both timing fields in each native record. Returning raw values preserves the kernel observation when an event never schedules or the caller needs to compute deltas before scaling. Counter reads produce typed snapshots without a stream or polling contract.
Use x/sys’s perf event attributes and metadata page directly. Each sampler owns one standalone event and one writable forward mapping. The fixed configuration determines each sample’s layout, so decoding can proceed from the record and the selected fields. Overwrite rings and AUX mappings have different ownership and loss protocols.
Read the published head with a Go atomic load before copying record bytes. After the copy, advance the tail with a Go atomic store so the producer may reuse that space. Serializing consumers preserves tail order across reads. Owned copies allow readers to retain records across consumption and close. This follows the Linux 7.0 UAPI and the kernel’s acquire/release protocol.
A one-byte wakeup watermark covers every appended record type. Go’s runtime poller waits on the perf descriptor while the ring head determines whether a record can be read. Registration is checked before opening succeeds. Closing the file interrupts runtime waits, and the sampler lock excludes access to the mapping during teardown. An explicit CLOCK_MONOTONIC request gives sample and throttle timestamps one clock domain. Attribute-size negotiation retains the clock ID along with the base sampling fields.
The descriptor and mapping have independent lifetimes. A surviving mapping keeps an enabled event sampling. Close therefore attempts to unmap first. If that fails, it attempts to disable the event while it still owns the descriptor. Close consumes the descriptor once. A retry releases the remaining mapping without acting on a reused descriptor number.