add continuous-time profiling of AES blocks to the net command

This commit is contained in:
bunnie 2022-09-16 01:45:32 +08:00
parent 91eb81000a
commit 48269651e8
3 changed files with 18 additions and 255 deletions

View file

@ -18,7 +18,7 @@ llio = {path = "../llio"}
log = "0.4.14"
log-server = {path = "../log-server"}
ticktimer-server = {path = "../ticktimer-server"}
xous = {path = "../../xous-rs"}
xous = {path = "../../xous-rs", features=["v2p"]}
xous-ipc = {path = "../../xous-ipc"}
xous-names = {path = "../xous-names"}
keyboard = {path = "../keyboard"}
@ -79,6 +79,7 @@ webpki-roots = {version = "0.22.4", optional = true}
ring = {version = "0.16.20", optional = true} # for testing ring math functions
tungstenite = {version = "0.17.3", optional = true, features = ["rustls-tls-webpki-roots"]}
url = {version = "2.2.2", optional = true}
perflib = {path="../../tools/perflib", optional = true}
# for the curve25519 tests
# hardware acceleration adaptations are inserted into a fork of the main branch.
@ -118,5 +119,5 @@ dbg-ecupdate = [] # for issuing manual commands to initiate an EC update (vs UX)
test-rekey = []
nettest = [] # batch network tests
tls = ["rustls", "webpki-roots", "ring", "tungstenite", "url"]
perfcounter = ["ring"]
perfcounter = ["ring", "perflib"]
default = [] # "debugprint"

View file

@ -52,9 +52,9 @@ pub struct CommonEnv {
boot_instant: std::time::Instant,
/// make this communal so any number of commands can trigger or reset the performance counter, and/or perform logging
#[cfg(feature="perfcounter")]
perf_csr: CSR<u32>,
perf_csr: AtomicCsr<u32>,
#[cfg(feature="perfcounter")]
event_csr: CSR::<u32>,
event_csr: AtomicCsr::<u32>,
}
impl CommonEnv {
pub fn register_handler(&mut self, verb: String::<256>) -> u32 {
@ -196,9 +196,9 @@ impl CmdEnv {
netmgr: net::NetManager::new(),
boot_instant: std::time::Instant::now(),
#[cfg(feature="perfcounter")]
perf_csr: CSR::new(perf_csr.as_mut_ptr() as *mut u32),
perf_csr: AtomicCsr::new(perf_csr.as_mut_ptr() as *mut u32),
#[cfg(feature="perfcounter")]
event_csr: CSR::new(event1_csr.as_mut_ptr() as *mut u32),
event_csr: AtomicCsr::new(event1_csr.as_mut_ptr() as *mut u32),
};
//let fcc = Fcc::new(&mut common);
#[cfg(feature="benchmarks")]

View file

@ -23,255 +23,7 @@ use std::convert::TryInto;
#[cfg(feature="tls")]
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),
);
}
}
}
use perflib::*;
pub struct NetCmd {
callback_id: Option<u32>,
callback_conn: u32,
@ -759,6 +511,16 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
log::info!("{}", std::str::from_utf8(&plaintext).unwrap_or("utf-error"));
log::set_max_level(log::LevelFilter::Info);
}
// note: to use the `perfcounter` option, you need to load a version of the SOC that has the performance counters built in.
// this can be generated using the command `python3 .\betrusted_soc.py -e .\dummy.nky --perfcounter` in the betrusted-soc repo.
//
// 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
// formatted in a fashion that can be automatically extracted by the usb_update script.
// the command below can generate this page table; it must be manually copy/pasted from the console
// into the `v2p.txt` file. Note that it will change on every boot, but it is stable throughout each boot,
// even across multiple runs of the command.
#[cfg(feature="perfcounter")]
"v2p" => {
// don't generate a new object since it clears the data, just do a raw dump