| 47 | #[test] |
| 48 | #[ignore = "requires root privileges"] |
| 49 | fn sample_fixed_counters() { |
| 50 | let sampler = Sampler::new().expect("Sampler::new() failed - are you running as root?"); |
| 51 | |
| 52 | let mut thread = sampler |
| 53 | .thread([Event::FixedInstructions, Event::FixedCycles]) |
| 54 | .expect("failed to create ThreadSampler"); |
| 55 | |
| 56 | thread.start().expect("failed to start counting"); |
| 57 | |
| 58 | let before = thread.sample().expect("failed to read counters (before)"); |
| 59 | do_work(); |
| 60 | let after = thread.sample().expect("failed to read counters (after)"); |
| 61 | |
| 62 | thread.stop().expect("failed to stop counting"); |
| 63 | |
| 64 | let instructions = after[0].wrapping_sub(before[0]); |
| 65 | let cycles = after[1].wrapping_sub(before[1]); |
| 66 | |
| 67 | eprintln!("instructions: {instructions}"); |
| 68 | eprintln!("cycles: {cycles}"); |
| 69 | #[expect(clippy::cast_precision_loss)] |
| 70 | let ipc = instructions as f64 / cycles as f64; |
| 71 | eprintln!("IPC: {ipc:.2}"); |
| 72 | |
| 73 | // Sanity: 100k iterations should retire at least tens of thousands of |
| 74 | // instructions and consume a non-trivial number of cycles. |
| 75 | // assert!( |
| 76 | // instructions > 10_000, |
| 77 | // "instruction count suspiciously low: {instructions}" |
| 78 | // ); |
| 79 | // assert!(cycles > 10_000, "cycle count suspiciously low: {cycles}"); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | #[ignore = "requires root privileges"] |