e310x_hal/core/
counters.rs

1//! Performance counters
2
3use riscv::register::{mcycle, mhpmcounter3, mhpmcounter4, minstret};
4
5/// Opaque mcycle register
6pub struct MCYCLE;
7
8impl MCYCLE {
9    /// Read mcycle and mcycleh registers.
10    #[inline]
11    pub fn value(&self) -> u64 {
12        mcycle::read64()
13    }
14}
15
16/// Opaque minstret register.
17pub struct MINSTRET;
18
19impl MINSTRET {
20    /// Read minstret and minstreth registers.
21    #[inline]
22    pub fn value(&self) -> u64 {
23        minstret::read64()
24    }
25}
26
27/// Opaque mhpmcounter3 register.
28pub struct MHPMCOUNTER3;
29
30impl MHPMCOUNTER3 {
31    /// Read mhpmcounter3 and mhpmcounter3h registers.
32    #[inline]
33    pub fn value(&self) -> u64 {
34        mhpmcounter3::read64()
35    }
36}
37
38/// Opaque mhpmcounter4 register.
39pub struct MHPMCOUNTER4;
40
41impl MHPMCOUNTER4 {
42    /// Read mhpmcounter4 and mhpmcounter4h registers.
43    #[inline]
44    pub fn value(&self) -> u64 {
45        mhpmcounter4::read64()
46    }
47}
48
49/// Performance counters
50pub struct PerformanceCounters {
51    /// 64-bit mcycle counter
52    pub mcycle: MCYCLE,
53    /// 64-bit minstret counter
54    pub minstret: MINSTRET,
55    /// 40-bit mhpmcounter3 counter
56    pub mhpmcounter3: MHPMCOUNTER3,
57    /// 40-bit mhpmcounter4 counter
58    pub mhpmcounter4: MHPMCOUNTER4,
59    // TODO: mhpmevent3, mhpmevent4
60}
61
62impl PerformanceCounters {
63    pub(crate) fn new() -> Self {
64        Self {
65            mcycle: MCYCLE,
66            minstret: MINSTRET,
67            mhpmcounter3: MHPMCOUNTER3,
68            mhpmcounter4: MHPMCOUNTER4,
69        }
70    }
71}