Skip to Content

darwin-kperf

Apple Silicon performance-counter access for Rust

RustStable↓ 116.3k

IntroductionCopy link to this section

darwin-kperf is a Rust crate for reading hardware performance counters on macOS running on Apple Silicon. It provides access to Apple's private kperf and kpc interfaces — the same facilities that power Instruments — so you can configure and sample the CPU's performance-monitoring counters (cycles, retired instructions, cache misses, branch mispredictions, and more) directly from Rust.

It's intended for low-level performance work on Apple Silicon Macs: micro-benchmarking, profiling, and collecting precise hardware metrics that aren't otherwise exposed to user-space code.

The crate is published on crates.io at darwin-kperf.

InstallationCopy link to this section

Bash
cargo add darwin-kperf

See the crates.io page for the latest released version.

UsageCopy link to this section

darwin-kperf targets macOS on Apple Silicon. You select the counters you care about, start sampling, and read the per-counter values back out. For the full API surface — counter configuration, the available counter classes, and the sampling helpers — see the API documentation on docs.rs.

Reading these counters relies on private macOS frameworks and typically requires elevated privileges (for example, running as root). The crate is macOS- and Apple-Silicon-only.

Companion cratesCopy link to this section

Three published crates support the performance-counter APIs:

CratePurpose
darwin-kperf-criterionUse hardware counters as measurements in Criterion benchmarks.
darwin-kperf-eventsSelect Apple Silicon events and inspect their chip-specific metadata.
darwin-kperf-sysAccess the raw bindings to Apple's private performance-counter frameworks.

The safe darwin-kperf API builds on the events and sys crates. Use the Criterion integration when adding counter measurements to a benchmark suite.

Source and licenseCopy link to this section