commit a snapshot before refactoring perflib components

This commit is contained in:
bunnie 2022-09-16 01:25:18 +08:00
parent 57673052bf
commit 5112c395f9

View file

@ -24,6 +24,253 @@ use std::convert::TryInto;
use tungstenite::{WebSocket, stream::MaybeTlsStream};
#[cfg(feature="perfcounter")]
use utralib::generated::*;
#[cfg(feature="perfcounter")]
use core::num::{NonZeroU8, NonZeroU16};
#[cfg(feature="perfcounter")]
use std::cell::RefCell;
#[cfg(feature="perfcounter")]
const BUFLEN: usize = 1024 * 512;
#[cfg(feature="perfcounter")]
#[repr(C)]
pub struct PerfLogEntry {
code: u32,
ts: u32,
}
#[cfg(feature="perfcounter")]
pub struct PerfMgr<'a> {
data: RefCell::<&'a mut [PerfLogEntry]>,
perf_csr: AtomicCsr<u32>,
event_csr: AtomicCsr<u32>,
log_index: RefCell<usize>,
ts_cont: RefCell<u64>,
// these are expressed as raw values to be written to hardware
saturation_limit: u64,
prescaler: u16,
saturate: bool,
event_bit_width: u8,
// debug fields
dbg_buf_count: RefCell<u32>,
}
#[cfg(feature="perfcounter")]
impl <'a> PerfMgr<'a> {
pub fn new(
log_ptr: *mut u8,
perf_csr: AtomicCsr<u32>,
event_csr: AtomicCsr<u32>,
) -> Self {
let data =
unsafe{
core::slice::from_raw_parts_mut(
log_ptr as *mut PerfLogEntry,
BUFLEN / core::mem::size_of::<PerfLogEntry>()
)
};
// initialize the buffer region when the manager is created
for d in data.iter_mut() {
d.code = 0;
d.ts = 0;
}
Self {
data: RefCell::new(data),
perf_csr,
event_csr,
log_index: RefCell::new(0),
ts_cont: RefCell::new(0),
saturation_limit: 0xffff_ffff,
prescaler: 0, // 1 clock per sample
event_bit_width: 31, // 32 bits width
saturate: true,
dbg_buf_count: RefCell::new(0),
}
}
/// Sets the saturation limit for the performance counter. Units in clock cycles.
/// If `None`, the counter will freely rollover without stopping the performance counting process.
#[allow(dead_code)]
pub fn sat_limit(&mut self, limit: Option<u64>) {
if let Some(l) = limit {
self.saturation_limit = l;
self.saturate = true;
} else {
self.saturate = false;
self.saturation_limit = u64::MAX;
}
}
/// 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;
}
/// 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();
if bw > 32 {
self.event_bit_width = 31;
} else {
self.event_bit_width = bw - 1;
}
}
pub fn stop_and_reset(&self) {
for d in self.data.borrow_mut().iter_mut() {
d.code = 0;
d.ts = 0;
}
self.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
while self.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 {
let i = self.perf_csr.r(utra::perfcounter::EVENT_INDEX); // this advances the FIFO until it is empty
log::warn!("FIFO was not drained before reset: {}", i);
}
// stop the counter if it would rollover
self.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT0, self.saturation_limit as u32);
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.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.log_index.replace(0);
self.ts_cont.replace(0);
}
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
///
/// 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> {
self.flush_if_full()?;
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);
}
#[allow(dead_code)]
pub fn flush(&self) -> Result::<(), xous::Error> {
let mut oom = false;
// stop the counter
self.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// copy over the events to the long term buffer
let mut ts_offset = 0;
let mut expected_i = 0;
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);
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;
let i = self.perf_csr.r(utra::perfcounter::EVENT_INDEX); // this advances the FIFO
if i != expected_i & 0xfff {
log::info!("i {} != expected_i {}", i, expected_i);
}
expected_i += 1;
self.log_index.replace(self.log_index.take() + 1);
} else {
oom = true;
break;
}
}
// update the timestamp continuation field with the last timestamp seen; the next line
// resets the timestamp counter to 0 again.
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
self.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
Ok(())
} else {
self.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
Err(xous::Error::OutOfMemory)
}
}
#[allow(dead_code)]
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(())
}
}
/// 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::<u32, xous::Error> {
let mut oom = false;
// stop the counter
self.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// copy over the events to the long term buffer
let mut ts_offset = 0;
let mut expected_i = 0;
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);
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;
let i = self.perf_csr.r(utra::perfcounter::EVENT_INDEX); // this advances the FIFO
if i != expected_i {
log::info!("i {} != expected_i {}", i, expected_i);
}
expected_i += 1;
self.log_index.replace(self.log_index.take() + 1);
} else {
oom = true;
break;
}
}
self.ts_cont.replace(self.ts_cont.take() + ts_offset as u64);
log::info!("FIFO final flush had {} entries", 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)
}
}
#[allow(dead_code)]
pub fn print_page_table(&self) {
log::info!("Buf vmem loc: {:x}", self.data.borrow().as_ptr() as u32);
match xous::syscall::virt_to_phys(self.data.borrow().as_ptr() as usize) {
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!("PerfLogEntry size: {}", core::mem::size_of::<PerfLogEntry>());
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}",
self.data.borrow().as_ptr() as usize + page,
xous::syscall::virt_to_phys(self.data.borrow().as_ptr() as usize + page).unwrap_or(0),
);
}
}
}
pub struct NetCmd {
callback_id: Option<u32>,
@ -33,9 +280,19 @@ pub struct NetCmd {
ping: Option<net::Ping>,
#[cfg(feature="tls")]
ws: Option<WebSocket<MaybeTlsStream<TcpStream>>>,
#[cfg(feature="perfcounter")]
perfbuf: xous::MemoryRange,
}
impl NetCmd {
pub fn new(xns: &xous_names::XousNames) -> Self {
#[cfg(feature="perfcounter")]
let perfbuf = xous::syscall::map_memory(
None,
None,
BUFLEN,
xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::RESERVE,
).expect("couldn't map in the performance buffer");
NetCmd {
callback_id: None,
callback_conn: xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(),
@ -44,6 +301,8 @@ impl NetCmd {
ping: None,
#[cfg(feature="tls")]
ws: None,
#[cfg(feature="perfcounter")]
perfbuf,
}
}
}
@ -501,33 +760,39 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
log::set_max_level(log::LevelFilter::Info);
}
#[cfg(feature="perfcounter")]
"v2p" => {
// don't generate a new object since it clears the data, just do a raw dump
log::info!("Buf vmem loc: {:x}", self.perfbuf.as_ptr() as u32);
log::info!("Buf pmem loc: {:x}", xous::syscall::virt_to_phys(self.perfbuf.as_ptr() as usize).unwrap_or(0));
log::info!("PerfLogEntry size: {}", core::mem::size_of::<PerfLogEntry>());
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}",
self.perfbuf.as_ptr() as usize + page,
xous::syscall::virt_to_phys(self.perfbuf.as_ptr() as usize + page).unwrap_or(0),
);
}
}
#[cfg(feature="perfcounter")]
"cta1" => {
log::info!("constant time RISC-V HW AES test");
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// stop the counter if it would rollover
env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT0, 0xFFFF_FFFF);
env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT1, 0);
// configure the system
env.perf_csr.wo(utra::perfcounter::CONFIG,
env.perf_csr.ms(utra::perfcounter::CONFIG_PRESCALER, 0)
| env.perf_csr.ms(utra::perfcounter::CONFIG_SATURATE, 1)
| env.perf_csr.ms(utra::perfcounter::CONFIG_EVENT_WIDTH_MINUS_ONE, 31)
let pm = PerfMgr::new(
self.perfbuf.as_mut_ptr(),
env.perf_csr.clone(),
env.event_csr.clone()
);
pm.stop_and_reset();
// this starts the performance counter
log::info!("== restart ==");
env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
pm.start();
use aes::Aes256;
use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit};
use aes::cipher::{BlockEncrypt, KeyInit};
use aes::cipher::generic_array::GenericArray;
let mut key_array: [u8; 32];
let mut data_array: [u8; 16];
const MAX_ENTRIES: u32 = 4096;
let mut entries: u32 = 0; // track the number of entries we've used up
for databit in 0..128 {
data_array = [0; 16];
data_array[databit / 8] = 1 << databit % 8;
@ -536,68 +801,43 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
key_array[keybit / 8] = 1 << keybit % 8;
let cipher_hw = Aes256::new(&key_array.into());
let mut block = GenericArray::clone_from_slice(&mut data_array);
// demarcate the encryption operation performance counter events
entries += 1;
env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE,
((databit as u32) << 8) | keybit as u32);
pm.log_event_unchecked(((databit as u32) << 8) | keybit as u32);
cipher_hw.encrypt_block(&mut block);
env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE,
0x100_0000 | ((databit as u32) << 8) | keybit as u32);
entries += 1;
if entries >= MAX_ENTRIES {
// stop the counter
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// read out the events and print them
while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 {
let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0);
let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1);
let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX);
log::info!("{}:{}:{}", index, time, code)
}
// restart the counter
log::info!("== restart ==");
entries = 0;
env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
}
pm.log_event_unchecked(0x100_0000 | ((databit as u32) << 8) | keybit as u32);
}
if databit % 4 == 3 {
pm.flush().ok();
}
}
// stop the counter
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// read out the events and print them
while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 {
let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0);
let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1);
let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX);
log::info!("{}:{}:{}", index, time, code)
match pm.stop_and_flush() {
Ok(entries) => {
log::info!("entries: {}", entries);
}
_ => {
log::info!("Perfcounter OOM'd during run");
}
}
// pm.print_page_table();
}
#[cfg(feature="perfcounter")]
"cta2" => {
use ring::xous_test::aes_nohw::aes_key_st;
log::info!("constant time ring-xous test");
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// stop the counter if it would rollover
env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT0, 0xFFFF_FFFF);
env.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT1, 0);
// configure the system
env.perf_csr.wo(utra::perfcounter::CONFIG,
env.perf_csr.ms(utra::perfcounter::CONFIG_PRESCALER, 0)
| env.perf_csr.ms(utra::perfcounter::CONFIG_SATURATE, 1)
| env.perf_csr.ms(utra::perfcounter::CONFIG_EVENT_WIDTH_MINUS_ONE, 31)
let pm = PerfMgr::new(
self.perfbuf.as_mut_ptr(),
env.perf_csr.clone(),
env.event_csr.clone()
);
pm.stop_and_reset();
// this starts the performance counter
log::info!("== restart ==");
env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
pm.start();
use ring::xous_test::aes_nohw::aes_key_st;
let mut key_array: [u8; 32];
let mut data_array: [u8; 16];
let mut out_array: [u8; 16] = [0u8; 16];
const MAX_ENTRIES: u32 = 4096;
let mut entries: u32 = 0; // track the number of entries we've used up
for databit in 0..128 {
data_array = [0; 16];
data_array[databit / 8] = 1 << databit % 8;
@ -610,38 +850,21 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
};
ring::xous_test::expand_aes_key(&key_array, &mut schedule);
// demarcate the encryption operation performance counter events
entries += 1;
env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE,
((databit as u32) << 8) | keybit as u32);
pm.log_event_unchecked(((databit as u32) << 8) | keybit as u32);
ring::xous_test::aes_encrypt(&data_array, &mut out_array, &schedule);
env.event_csr.wfo(utra::event_source1::PERFEVENT_CODE,
0x100_0000 | ((databit as u32) << 8) | keybit as u32);
entries += 1;
if entries >= MAX_ENTRIES {
// stop the counter
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// read out the events and print them
while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 {
let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0);
let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1);
let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX);
log::info!("{}:{}:{}", index, time, code)
}
// restart the counter
log::info!("== restart ==");
entries = 0;
env.perf_csr.wfo(utra::perfcounter::RUN_RESET_RUN, 1);
}
pm.log_event_unchecked(0x100_0000 | ((databit as u32) << 8) | keybit as u32);
}
if databit % 4 == 3 {
pm.flush().ok();
}
}
// stop the counter
env.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1);
// read out the events and print them
while env.perf_csr.rf(utra::perfcounter::STATUS_READABLE) == 1 {
let code = env.perf_csr.r(utra::perfcounter::EVENT_RAW0);
let time = env.perf_csr.r(utra::perfcounter::EVENT_RAW1);
let index = env.perf_csr.r(utra::perfcounter::EVENT_INDEX);
log::info!("{}:{}:{}", index, time, code)
match pm.stop_and_flush() {
Ok(entries) => {
log::info!("entries: {}", entries);
}
_ => {
log::info!("Perfcounter OOM'd during run");
}
}
}
#[cfg(any(target_os = "none", target_os = "xous"))]