perf_event/events/
raw.rs

1use perf_event_open_sys::bindings;
2
3use crate::events::Event;
4
5/// A raw perf counter for the current CPU.
6///
7/// Most CPUs have additional counters beyond those provided by the kernel.
8/// `Raw` events allow you to access those events. Note that the values needed
9/// to configure raw events a liable to change between CPU vendors and even
10/// different hardware revisions of the same platform.
11///
12/// The event can be chosen by setting the `config` field. Most events will
13/// only need that, but others may require setting the `config1` or `config2`
14/// fields as well.
15///
16/// To find the config values required for counters consult your CPU manual.
17/// - For Intel CPUs, see the Intel Software Developer Manual, volume 3B.
18/// - For AMD, see the AMD BIOS and Kernel Developer Guide.
19/// - Other vendors should have equivalent documentation.
20///
21/// Example:
22///
23/// ```no_run
24/// use perf_event::events::Raw;
25/// use perf_event::{Builder, Group};
26///
27/// // Raw config values for an ARMv8 PMU.
28/// let INSNS_RETIRED: Raw = Raw::new(0x08);
29/// let CPU_CYCLES: Raw = Raw::new(0x11);
30///
31/// let mut group = Group::new()?;
32/// let raw_insns_retired = group.add(&Builder::new(INSNS_RETIRED).include_kernel())?;
33/// let raw_cpu_cycles = group.add(&Builder::new(CPU_CYCLES).include_kernel())?;
34/// # std::io::Result::Ok(())
35/// ```
36#[non_exhaustive]
37#[derive(Copy, Clone, Debug, Eq, PartialEq)]
38pub struct Raw {
39    /// Raw config of the event
40    pub config: u64,
41
42    /// Raw config1 of the event
43    pub config1: u64,
44
45    /// Raw config2 of the event
46    pub config2: u64,
47}
48
49impl Raw {
50    /// Create a new raw event value with the given config value.
51    ///
52    /// This sets all other config fields to zero. For most events this should
53    /// be sufficient but in other cases methods are provided to set those
54    /// fields as well.
55    pub const fn new(config: u64) -> Self {
56        Raw {
57            config,
58            config1: 0,
59            config2: 0,
60        }
61    }
62
63    /// Set config
64    pub const fn config(mut self, config: u64) -> Self {
65        self.config = config;
66        self
67    }
68
69    /// Set config1
70    pub const fn config1(mut self, config1: u64) -> Self {
71        self.config1 = config1;
72        self
73    }
74
75    /// Set config2
76    pub const fn config2(mut self, config2: u64) -> Self {
77        self.config2 = config2;
78        self
79    }
80}
81
82impl Event for Raw {
83    fn update_attrs(self, attr: &mut bindings::perf_event_attr) {
84        attr.type_ = bindings::PERF_TYPE_RAW;
85        attr.config = self.config;
86        attr.config1 = self.config1;
87        attr.config2 = self.config2;
88    }
89}