diff --git a/tools/perflib/src/lib.rs b/tools/perflib/src/lib.rs index e138d133a..9350d700e 100644 --- a/tools/perflib/src/lib.rs +++ b/tools/perflib/src/lib.rs @@ -1,10 +1,12 @@ -use utralib::generated::*; -use core::num::{NonZeroU8, NonZeroU16}; +use core::num::{NonZeroU16, NonZeroU8}; use std::cell::RefCell; +use utralib::generated::*; + // To read out performance monitoring data, use the `usb_update.py` script as follows: // ` python3 .\..\usb_update.py --dump v2p.txt --dump-file .\ring_aes_8.bin` -// where the `v2p.txt` file contains a virtual to physical mapping that is generated by the `perflib` framework and +// where the `v2p.txt` file contains a virtual to physical mapping that is generated by the `perflib` +// framework and pub const BUFLEN: usize = 1024 * 512; @@ -21,11 +23,11 @@ pub const BUFLEN: usize = 1024 * 512; #[macro_export] macro_rules! perf_entry { ($pid:expr, $fid:expr, $meta:expr, $index:expr, $line:expr) => { - (($fid as u32) & 0x7) << 29 | - (($pid as u32) & 0x1F) << 24 | - (($meta as u32) & 0x7) << 21 | - (($index as u32) & 0xFF) << 13 | - (($line as u32) & 0x1FFF) + (($fid as u32) & 0x7) << 29 + | (($pid as u32) & 0x1F) << 24 + | (($meta as u32) & 0x7) << 21 + | (($index as u32) & 0xFF) << 13 + | (($line as u32) & 0x1FFF) }; } pub const PERFMETA_NONE: u32 = 0; @@ -44,25 +46,19 @@ pub struct PerfClient { event_csr: AtomicCsr, } impl PerfClient { - pub fn new( - event_csr: AtomicCsr, - ) -> Self { - Self { - event_csr, - } - } + pub fn new(event_csr: AtomicCsr) -> Self { Self { event_csr } } + #[inline(always)] pub fn log_event_unchecked(&self, code: u32) { // event_sourceN is identical across each block, so we can take advantage of the flexibility of // UTRA to do a non-specific cross-crate register access pattern. - self.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, - code); + self.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, code); } } /// Manager interface for performance monitoring. This also includes a performance event port. pub struct PerfMgr<'a> { - data: RefCell::<&'a mut [PerfLogEntry]>, + data: RefCell<&'a mut [PerfLogEntry]>, perf_csr: AtomicCsr, event_csr: AtomicCsr, log_index: RefCell, @@ -76,19 +72,14 @@ pub struct PerfMgr<'a> { dbg_buf_count: RefCell, } -impl <'a> PerfMgr<'a> { - pub fn new( - log_ptr: *mut u8, - perf_csr: AtomicCsr, - event_csr: AtomicCsr, - ) -> Self { - let data = - unsafe{ - core::slice::from_raw_parts_mut( - log_ptr as *mut PerfLogEntry, - BUFLEN / core::mem::size_of::() - ) - }; +impl<'a> PerfMgr<'a> { + pub fn new(log_ptr: *mut u8, perf_csr: AtomicCsr, event_csr: AtomicCsr) -> Self { + let data = unsafe { + core::slice::from_raw_parts_mut( + log_ptr as *mut PerfLogEntry, + BUFLEN / core::mem::size_of::(), + ) + }; // initialize the buffer region when the manager is created for d in data.iter_mut() { d.code = 0; @@ -101,7 +92,7 @@ impl <'a> PerfMgr<'a> { log_index: RefCell::new(0), ts_cont: RefCell::new(0), saturation_limit: 0xffff_ffff, - prescaler: 0, // 1 clock per sample + prescaler: 0, // 1 clock per sample event_bit_width: 31, // 32 bits width saturate: true, dbg_buf_count: RefCell::new(0), @@ -123,12 +114,11 @@ impl <'a> PerfMgr<'a> { /// Zero is an invalid value for clocks per sample. Hence NonZeroU16 type. #[allow(dead_code)] - pub fn clocks_per_sample(&mut self, cps: NonZeroU16) { - self.prescaler = cps.get() - 1; - } + pub fn clocks_per_sample(&mut self, cps: NonZeroU16) { self.prescaler = cps.get() - 1; } - /// Sets the width of the event code. This is enforced by hardware, software is free to pass in a code that is too large. - /// Excess bits will be ignored, starting from the MSB side. A bitwdith of 0 is illegal. A bitwidth larger than 32 is set to 32. + /// Sets the width of the event code. This is enforced by hardware, software is free to pass in a code + /// that is too large. Excess bits will be ignored, starting from the MSB side. A bitwdith of 0 is + /// illegal. A bitwidth larger than 32 is set to 32. #[allow(dead_code)] pub fn code_bitwidth(&mut self, bitwidth: NonZeroU8) { let bw = bitwidth.get(); @@ -163,40 +153,41 @@ impl <'a> PerfMgr<'a> { self.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT1, (self.saturation_limit >> 32) as u32); // configure the system - self.perf_csr.wo(utra::perfcounter::CONFIG, + self.perf_csr.wo( + utra::perfcounter::CONFIG, self.perf_csr.ms(utra::perfcounter::CONFIG_PRESCALER, self.prescaler as u32) - | self.perf_csr.ms(utra::perfcounter::CONFIG_SATURATE, if self.saturate {1} else {0}) - | self.perf_csr.ms(utra::perfcounter::CONFIG_EVENT_WIDTH_MINUS_ONE, self.event_bit_width as u32) + | self.perf_csr.ms(utra::perfcounter::CONFIG_SATURATE, if self.saturate { 1 } else { 0 }) + | self + .perf_csr + .ms(utra::perfcounter::CONFIG_EVENT_WIDTH_MINUS_ONE, self.event_bit_width as u32), ); self.log_index.replace(0); self.ts_cont.replace(0); } - pub fn start(&self) { - self.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1); - } + pub fn start(&self) { self.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1); } - /// This function is convenient, but the overhead of the checking adds a lot of cache line noise to the data - /// If you are looking to get very cycle-accurate counts, use `log_event_unchecked` with manual flush calls + /// This function is convenient, but the overhead of the checking adds a lot of cache line noise to the + /// data If you are looking to get very cycle-accurate counts, use `log_event_unchecked` with manual + /// flush calls /// /// returns Ok(()) if the event could be logged /// returns an Err if the performance buffer would overflow #[inline(always)] - pub fn log_event(&self, code: u32) -> Result::<(), xous::Error> { + pub fn log_event(&self, code: u32) -> Result<(), xous::Error> { self.flush_if_full()?; - self.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, - code); + self.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, code); Ok(()) } #[allow(dead_code)] #[inline(always)] pub fn log_event_unchecked(&self, code: u32) { - self.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, - code); + self.event_csr.wfo(utra::event_source1::PERFEVENT_CODE, code); } + #[allow(dead_code)] - pub fn flush(&self) -> Result::<(), xous::Error> { + pub fn flush(&self) -> Result<(), xous::Error> { let mut oom = false; // stop the counter self.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1); @@ -206,9 +197,11 @@ impl <'a> PerfMgr<'a> { while self.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 { if *self.log_index.borrow() < self.data.borrow().len() { self.dbg_buf_count.replace(self.dbg_buf_count.take() + 1); // this tracks total entries copied into the perfbuf - (*self.data.borrow_mut())[*self.log_index.borrow()].code = self.perf_csr.r(utra::perfcounter::EVENT_RAW0); + (*self.data.borrow_mut())[*self.log_index.borrow()].code = + self.perf_csr.r(utra::perfcounter::EVENT_RAW0); ts_offset = self.perf_csr.r(utra::perfcounter::EVENT_RAW1) as u32; - (*self.data.borrow_mut())[*self.log_index.borrow()].ts = *self.ts_cont.borrow() as u32 + ts_offset; + (*self.data.borrow_mut())[*self.log_index.borrow()].ts = + *self.ts_cont.borrow() as u32 + ts_offset; let i = self.perf_csr.r(utra::perfcounter::EVENT_INDEX); // this advances the FIFO if i != expected_i & 0xfff { @@ -226,7 +219,8 @@ impl <'a> PerfMgr<'a> { self.ts_cont.replace(self.ts_cont.take() + ts_offset as u64); // restart the counter if !oom { - // duplicate this code down here because we want the reset and log to be as close as possible to the return statement + // duplicate this code down here because we want the reset and log to be as close as possible to + // the return statement self.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1); Ok(()) } else { @@ -236,19 +230,16 @@ impl <'a> PerfMgr<'a> { } #[allow(dead_code)] - pub fn flush_if_full(&self) -> Result::<(), xous::Error> { + pub fn flush_if_full(&self) -> Result<(), xous::Error> { // check to see if the FIFO is full first. If so, drain it. - if self.perf_csr.rf(utra::perfcounter::STATUS_FULL) != 0 { - self.flush() - } else { - Ok(()) - } + if self.perf_csr.rf(utra::perfcounter::STATUS_FULL) != 0 { self.flush() } else { Ok(()) } } - /// Flushes any data in the FIFO to the performance buffer. Also stops the performance counter from running. - /// Returns the total number of entries in the buffer, or an OOM if the buffer is full - /// This will update the time stamp rollover counter, so, you could in theory restart after this without losing state. - pub fn stop_and_flush(&self) -> Result:: { + /// Flushes any data in the FIFO to the performance buffer. Also stops the performance counter from + /// running. Returns the total number of entries in the buffer, or an OOM if the buffer is full + /// This will update the time stamp rollover counter, so, you could in theory restart after this without + /// losing state. + pub fn stop_and_flush(&self) -> Result { let mut oom = false; // stop the counter self.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1); @@ -258,9 +249,11 @@ impl <'a> PerfMgr<'a> { while self.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 { if *self.log_index.borrow() < self.data.borrow().len() { self.dbg_buf_count.replace(self.dbg_buf_count.take() + 1); // this tracks total entries copied into the perfbuf - self.data.borrow_mut()[*self.log_index.borrow()].code = self.perf_csr.r(utra::perfcounter::EVENT_RAW0); + self.data.borrow_mut()[*self.log_index.borrow()].code = + self.perf_csr.r(utra::perfcounter::EVENT_RAW0); ts_offset = self.perf_csr.r(utra::perfcounter::EVENT_RAW1) as u32; - self.data.borrow_mut()[*self.log_index.borrow()].ts = *self.ts_cont.borrow() as u32 + ts_offset; + self.data.borrow_mut()[*self.log_index.borrow()].ts = + *self.ts_cont.borrow() as u32 + ts_offset; let i = self.perf_csr.r(utra::perfcounter::EVENT_INDEX); // this advances the FIFO if i != expected_i { @@ -276,11 +269,7 @@ impl <'a> PerfMgr<'a> { self.ts_cont.replace(self.ts_cont.take() + ts_offset as u64); log::info!("FIFO final flush had {} entries", if expected_i == 0 { 0 } else { expected_i - 1 }); log::info!("Had {} buffer entries", *self.dbg_buf_count.borrow()); // should be the same as above, but trying to chase down some minor issues... - if !oom { - Ok(*self.dbg_buf_count.borrow()) - } else { - Err(xous::Error::OutOfMemory) - } + if !oom { Ok(*self.dbg_buf_count.borrow()) } else { Err(xous::Error::OutOfMemory) } } #[allow(dead_code)] @@ -290,11 +279,15 @@ impl <'a> PerfMgr<'a> { Ok(addr) => log::info!("got {}", addr), Err(e) => log::info!("error: {:?}", e), }; - log::info!("Buf pmem loc: {:x}", xous::syscall::virt_to_phys(self.data.borrow().as_ptr() as usize).unwrap_or(0)); + log::info!( + "Buf pmem loc: {:x}", + xous::syscall::virt_to_phys(self.data.borrow().as_ptr() as usize).unwrap_or(0) + ); log::info!("PerfLogEntry size: {}", core::mem::size_of::()); log::info!("Now printing the page table mapping for the performance buffer:"); for page in (0..BUFLEN).step_by(4096) { - log::info!("V|P {:x} {:x}", + log::info!( + "V|P {:x} {:x}", self.data.borrow().as_ptr() as usize + page, xous::syscall::virt_to_phys(self.data.borrow().as_ptr() as usize + page).unwrap_or(0), );