summaryrefslogtreecommitdiffstats
path: root/vendor/perf-event/examples/big-group.rs
blob: 73b5578274e8d666fd9c9f87b61b8993550d1044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use perf_event::{Builder, Group};
use perf_event::events::{Cache, CacheOp, CacheResult, Hardware, WhichCache};

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()?;
    let insns = Builder::new().group(&mut group).kind(Hardware::INSTRUCTIONS).build()?;
    let cycles = Builder::new().group(&mut group).kind(Hardware::CPU_CYCLES).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.
    //
    // On my machine, this program won't collect any data unless I disable the
    // NMI watchdog, as described in the documentation for `Group`. My machine
    // has four counters, and this program tries to use all of them, but the NMI
    // watchdog uses one up.

    let mut vec = (0..=100000).collect::<Vec<_>>();

    group.enable()?;
    vec.sort();
    println!("{:?}", &vec[0..10]);
    group.disable()?;

    let counts = group.read()?;

    println!("enabled for {}ns, actually running for {}ns",
             counts.time_enabled(),
             counts.time_running());

    if counts.time_running() == 0 {
        println!("Group was never running; no results available.");
        return Ok(());
    }

    if counts.time_running() < counts.time_enabled() {
        println!("Counts cover only a portion of the execution.");
    }

    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);

    println!("{} instructions, {} cycles ({:.2} cpi)",
             counts[&insns],
             counts[&cycles],
             counts[&cycles] as f64 / counts[&insns] as f64);

    // You can iterate over a `Counts` value:
    for (id, value) in &counts {
        println!("Counter id {} has value {}", id, value);
    }

    Ok(())
}