Skip to content

Commit 254f637

Browse files
committed
feat: add riscv_pmu value
Signed-off-by: Woshiluo Luo <woshiluo.luo@outlook.com>
1 parent af86aa2 commit 254f637

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ edition = "2024"
1616
[dependencies]
1717
serde = { version = "1.0", default-features = false }
1818
erased-serde = "0.4"
19-
20-
[dev-dependencies]
2119
serde_derive = "1.0"
2220

2321
[features]

src/de_mut/matrix.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,40 @@ pub struct MatrixItem<'de, const T: usize> {
1111
}
1212

1313
impl<'de, const T: usize> Matrix<'de, T> {
14-
// Block size in bytes.
14+
#[inline(always)]
1515
pub fn get_block_size() -> usize {
16+
// Block size in bytes.
1617
T * 4
1718
}
1819

20+
#[inline(always)]
1921
pub fn iter(&self) -> MatrixItem<'de, T> {
2022
MatrixItem {
2123
offset: 0,
2224
data: self.data,
2325
}
2426
}
27+
28+
#[inline(always)]
29+
pub fn len(&self) -> usize {
30+
self.data.len() / T
31+
}
32+
33+
#[inline(always)]
34+
pub fn is_empty(&self) -> bool {
35+
self.data.len() != 0
36+
}
37+
38+
#[inline(always)]
39+
pub fn get(&self, i: usize) -> &'de [u32] {
40+
&self.data[i * T..(i + 1) * T]
41+
}
2542
}
2643

2744
impl<'de, const T: usize> Iterator for MatrixItem<'de, T> {
2845
type Item = &'de [u32];
2946

47+
#[inline(always)]
3048
fn next(&mut self) -> Option<Self::Item> {
3149
if self.data.len() <= self.offset {
3250
return None;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ pub mod de;
2222
pub mod error;
2323
pub mod ser;
2424
pub mod utils;
25+
pub mod value;
2526

2627
mod common;
2728
mod de_mut;
2829
mod tag;
29-
mod value;
3030

3131
pub use value::compatible::Compatible;
3232

src/value/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod compatible;
22
pub mod cpu;
3+
pub mod riscv_pmu;
34
mod tree;

src/value/riscv_pmu.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use crate::buildin::Matrix;
2+
3+
use serde_derive::Serialize;
4+
5+
use core::ops::RangeInclusive;
6+
7+
#[repr(transparent)]
8+
#[derive(Serialize)]
9+
#[serde(transparent)]
10+
pub struct EventToMhpmevent<'de>(Matrix<'de, 3>);
11+
12+
#[repr(transparent)]
13+
#[derive(Serialize)]
14+
#[serde(transparent)]
15+
pub struct EventToMhpmcounters<'de>(Matrix<'de, 3>);
16+
17+
#[repr(transparent)]
18+
#[derive(Serialize)]
19+
#[serde(transparent)]
20+
pub struct RawEventToMhpcounters<'de>(Matrix<'de, 5>);
21+
22+
impl EventToMhpmevent<'_> {
23+
pub fn get_len(&self) -> usize {
24+
self.0.len()
25+
}
26+
27+
pub fn get_event_id(&self, i: usize) -> u32 {
28+
u32::from_be(self.0.get(i)[0])
29+
}
30+
31+
pub fn get_selector_value(&self, i: usize) -> u64 {
32+
let current = self.0.get(i);
33+
((u32::from_be(current[1]) as u64) << 32) | (u32::from_be(current[0]) as u64)
34+
}
35+
}
36+
37+
impl EventToMhpmcounters<'_> {
38+
pub fn get_len(&self) -> usize {
39+
self.0.len()
40+
}
41+
42+
pub fn get_event_idx_range(&self, i: usize) -> RangeInclusive<u32> {
43+
let current = self.0.get(i);
44+
u32::from_be(current[0])..=u32::from_be(current[1])
45+
}
46+
47+
pub fn get_counter_bitmap(&self, i: usize) -> u32 {
48+
let current = self.0.get(i);
49+
u32::from_be(current[2])
50+
}
51+
}
52+
53+
impl RawEventToMhpcounters<'_> {
54+
pub fn get_len(&self) -> usize {
55+
self.0.len()
56+
}
57+
58+
pub fn get_event_idx_base(&self, i: usize) -> u64 {
59+
let current = self.0.get(i);
60+
((u32::from_be(current[0]) as u64) << 32) | (u32::from_be(current[1]) as u64)
61+
}
62+
63+
pub fn get_event_idx_mask(&self, i: usize) -> u64 {
64+
let current = self.0.get(i);
65+
((u32::from_be(current[2]) as u64) << 32) | (u32::from_be(current[3]) as u64)
66+
}
67+
68+
pub fn get_counter_bitmap(&self, i: usize) -> u32 {
69+
let current = self.0.get(i);
70+
u32::from_be(current[4])
71+
}
72+
}

0 commit comments

Comments
 (0)