diff options
| author | Dennis Kobert <dennis@kobert.dev> | 2025-03-08 21:06:20 +0100 |
|---|---|---|
| committer | Dennis Kobert <dennis@kobert.dev> | 2025-03-08 21:07:27 +0100 |
| commit | b4eae244826a0fb8907ef50c8ef9a38da40b719d (patch) | |
| tree | b57014575b3dfc38c5f25115357253d037b3189c /perf-event/examples/group.rs | |
| parent | 06dd6966ad27f0c52c56092dd2f45a06541e5c6c (diff) | |
Vendor perf event
Diffstat (limited to 'perf-event/examples/group.rs')
| -rw-r--r-- | perf-event/examples/group.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/perf-event/examples/group.rs b/perf-event/examples/group.rs new file mode 100644 index 0000000..1aa2629 --- /dev/null +++ b/perf-event/examples/group.rs @@ -0,0 +1,58 @@ +use perf_event::events::{Cache, CacheOp, CacheResult, Hardware, WhichCache}; +use perf_event::{Builder, Group}; + +fn main() -> std::io::Result<()> { + const ACCESS: Cache = Cache { + which: WhichCache::L1D, + operation: CacheOp::READ, + result: CacheResult::ACCESS, + }; + const MISS: Cache = Cache { + result: CacheResult::MISS, + ..ACCESS + }; + + let mut group = Group::new()?; + let access_counter = Builder::new().group(&mut group).kind(ACCESS).build()?; + let miss_counter = Builder::new().group(&mut group).kind(MISS).build()?; + let branches = Builder::new() + .group(&mut group) + .kind(Hardware::BRANCH_INSTRUCTIONS) + .build()?; + let missed_branches = Builder::new() + .group(&mut group) + .kind(Hardware::BRANCH_MISSES) + .build()?; + + // Note that if you add more counters than you actually have hardware for, + // the kernel will time-slice them, which means you may get no coverage for + // short measurements. See the documentation. + + let vec = (0..=51).collect::<Vec<_>>(); + + group.enable()?; + println!("{:?}", vec); + group.disable()?; + + let counts = group.read()?; + println!( + "L1D cache misses/references: {} / {} ({:.0}%)", + counts[&miss_counter], + counts[&access_counter], + (counts[&miss_counter] as f64 / counts[&access_counter] as f64) * 100.0 + ); + + println!( + "branch prediction misses/total: {} / {} ({:.0}%)", + counts[&missed_branches], + counts[&branches], + (counts[&missed_branches] as f64 / counts[&branches] as f64) * 100.0 + ); + + // You can iterate over a `Counts` value: + for (id, value) in &counts { + println!("Counter id {} has value {}", id, value); + } + + Ok(()) +} |
