darwin-kperf
Apple Silicon performance-counter access for Rust
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
cargo add darwin-kperfSee 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:
| Crate | Purpose |
|---|---|
| darwin-kperf-criterion | Use hardware counters as measurements in Criterion benchmarks. |
| darwin-kperf-events | Select Apple Silicon events and inspect their chip-specific metadata. |
| darwin-kperf-sys | Access 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
- API docs: docs.rs/darwin-kperf
- License: MIT or Apache-2.0