1- use crate :: buildin:: Matrix ;
1+ use crate :: buildin:: * ;
22
3- use serde_derive :: Serialize ;
3+ use serde :: { Deserialize , Serialize } ;
44
55use core:: ops:: RangeInclusive ;
66
77#[ repr( transparent) ]
8- #[ derive( Serialize ) ]
8+ #[ derive( Deserialize , Serialize ) ]
99#[ serde( transparent) ]
10- pub struct EventToMhpmevent < ' de > ( Matrix < ' de , 3 > ) ;
10+ pub struct EventToMhpmevent < ' a > ( Matrix < ' a , 3 > ) ;
1111
1212#[ repr( transparent) ]
13- #[ derive( Serialize ) ]
13+ #[ derive( Deserialize , Serialize ) ]
1414#[ serde( transparent) ]
15- pub struct EventToMhpmcounters < ' de > ( Matrix < ' de , 3 > ) ;
15+ pub struct EventToMhpmcounters < ' a > ( Matrix < ' a , 3 > ) ;
1616
1717#[ repr( transparent) ]
18- #[ derive( Serialize ) ]
18+ #[ derive( Deserialize , Serialize ) ]
1919#[ serde( transparent) ]
20- pub struct RawEventToMhpcounters < ' de > ( Matrix < ' de , 5 > ) ;
20+ pub struct RawEventToMhpcounters < ' a > ( Matrix < ' a , 5 > ) ;
2121
2222impl EventToMhpmevent < ' _ > {
23- pub fn get_len ( & self ) -> usize {
23+ pub fn len ( & self ) -> usize {
2424 self . 0 . len ( )
2525 }
2626
27+ pub fn is_empty ( & self ) -> bool {
28+ self . 0 . is_empty ( )
29+ }
30+
2731 pub fn get_event_id ( & self , i : usize ) -> u32 {
2832 u32:: from_be ( self . 0 . get ( i) [ 0 ] )
2933 }
@@ -35,10 +39,14 @@ impl EventToMhpmevent<'_> {
3539}
3640
3741impl EventToMhpmcounters < ' _ > {
38- pub fn get_len ( & self ) -> usize {
42+ pub fn len ( & self ) -> usize {
3943 self . 0 . len ( )
4044 }
4145
46+ pub fn is_empty ( & self ) -> bool {
47+ self . 0 . is_empty ( )
48+ }
49+
4250 pub fn get_event_idx_range ( & self , i : usize ) -> RangeInclusive < u32 > {
4351 let current = self . 0 . get ( i) ;
4452 u32:: from_be ( current[ 0 ] ) ..=u32:: from_be ( current[ 1 ] )
@@ -51,10 +59,14 @@ impl EventToMhpmcounters<'_> {
5159}
5260
5361impl RawEventToMhpcounters < ' _ > {
54- pub fn get_len ( & self ) -> usize {
62+ pub fn len ( & self ) -> usize {
5563 self . 0 . len ( )
5664 }
5765
66+ pub fn is_empty ( & self ) -> bool {
67+ self . 0 . is_empty ( )
68+ }
69+
5870 pub fn get_event_idx_base ( & self , i : usize ) -> u64 {
5971 let current = self . 0 . get ( i) ;
6072 ( ( u32:: from_be ( current[ 0 ] ) as u64 ) << 32 ) | ( u32:: from_be ( current[ 1 ] ) as u64 )
@@ -70,3 +82,41 @@ impl RawEventToMhpcounters<'_> {
7082 u32:: from_be ( current[ 4 ] )
7183 }
7284}
85+
86+ #[ cfg( test) ]
87+ mod tests {
88+ use super :: EventToMhpmcounters ;
89+ use crate :: { Dtb , DtbPtr , buildin:: Node , from_raw_mut} ;
90+
91+ const RAW_DEVICE_TREE : & [ u8 ] = include_bytes ! ( "../../examples/qemu-virt.dtb" ) ;
92+ const BUFFER_SIZE : usize = RAW_DEVICE_TREE . len ( ) ;
93+ #[ test]
94+ fn test_chosen_stdout ( ) {
95+ #[ repr( align( 8 ) ) ]
96+ struct AlignedBuffer {
97+ pub data : [ u8 ; RAW_DEVICE_TREE . len ( ) ] ,
98+ }
99+ let mut aligned_data: Box < AlignedBuffer > = Box :: new ( AlignedBuffer {
100+ data : [ 0 ; BUFFER_SIZE ] ,
101+ } ) ;
102+ aligned_data. data [ ..BUFFER_SIZE ] . clone_from_slice ( RAW_DEVICE_TREE ) ;
103+ let mut slice = aligned_data. data . to_vec ( ) ;
104+ let ptr = DtbPtr :: from_raw ( slice. as_mut_ptr ( ) ) . unwrap ( ) ;
105+ let dtb = Dtb :: from ( ptr) . share ( ) ;
106+
107+ let node: Node = from_raw_mut ( & dtb) . unwrap ( ) ;
108+ let result = node
109+ . find ( "/pmu" )
110+ . unwrap ( )
111+ . get_prop ( "riscv,event-to-mhpmcounters" )
112+ . unwrap ( )
113+ . deserialize :: < EventToMhpmcounters > ( ) ;
114+ assert_eq ! ( result. len( ) , 5 ) ;
115+ assert_eq ! ( result. get_event_idx_range( 0 ) , 1 ..=1 ) ;
116+ assert_eq ! ( result. get_counter_bitmap( 0 ) , 0x7fff9 ) ;
117+ assert_eq ! ( result. get_event_idx_range( 1 ) , 2 ..=2 ) ;
118+ assert_eq ! ( result. get_counter_bitmap( 1 ) , 0x7fffc ) ;
119+ assert_eq ! ( result. get_event_idx_range( 2 ) , 0x10019 ..=0x10019 ) ;
120+ assert_eq ! ( result. get_counter_bitmap( 2 ) , 0x7fff8 ) ;
121+ }
122+ }
0 commit comments