diff --git a/Cargo.lock b/Cargo.lock index e636fdbb0..16dffc0d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2529,6 +2529,7 @@ dependencies = [ "modals", "num-derive", "num-traits", + "perflib", "rand 0.7.3", "rand_chacha 0.3.1", "rkyv", @@ -3471,6 +3472,7 @@ dependencies = [ "png", "rand 0.8.5", "rand_chacha 0.3.1", + "random-pick", "ring", "rkyv", "root-keys", @@ -4244,6 +4246,7 @@ dependencies = [ "num-traits", "passwords", "pddb", + "perflib", "rand_core 0.6.3", "random-pick", "rkyv", @@ -4255,6 +4258,7 @@ dependencies = [ "tts-frontend", "usb-device-xous", "usbd-human-interface-device 0.2.1", + "utralib", "xous 0.9.18 (registry+https://github.com/rust-lang/crates.io-index)", "xous-api-log", "xous-api-names", diff --git a/apps/vault/Cargo.toml b/apps/vault/Cargo.toml index c08bc3d33..91afae79f 100644 --- a/apps/vault/Cargo.toml +++ b/apps/vault/Cargo.toml @@ -51,6 +51,10 @@ sntpc = {version = "0.3.1"} net = {path="../../services/net"} com_rs-ref = {path = "../../imports/com_rs-ref"} +# performance profiling +perflib = {path="../../tools/perflib", optional = true} +utralib = {version = "0.1.6", optional = true, default-features = false } + # testing random-pick = {version = "1.2.15", optional = true} [dev-dependencies] @@ -65,5 +69,7 @@ with_ctap1 = ["ctap-crypto/with_ctap1"] with_ctap2_1 = [] autotest = [] multireset = [] # allows credential reset more than once per boot -default = ["with_ctap1", "with_ctap2_1"] # testing removed for release tag -testing = ["random-pick"] \ No newline at end of file +vaultperf = ["perflib", "xous/v2p", "utralib", "utralib/precursor"] # vault is the performance logging manager. Do not use concurrently with "shellperf", as there can only be one manager. +vault-testing = ["random-pick"] + +default = ["with_ctap1", "with_ctap2_1"] # testing removed for release tag \ No newline at end of file diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 5b36f6469..69d297071 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -17,6 +17,11 @@ use crate::{VaultMode, SelectedEntry}; use crate::fido::U2F_APP_DICT; use crate::totp::TotpAlgorithm; +#[cfg(feature="vaultperf")] +use perflib::*; +#[cfg(feature="vaultperf")] +const FILE_ID_APPS_VAULT_SRC_ACTIONS: u32 = 1; + pub(crate) const VAULT_PASSWORD_DICT: &'static str = "vault.passwords"; pub(crate) const VAULT_TOTP_DICT: &'static str = "vault.totp"; /// bytes to reserve for a key entry. Making this slightly larger saves on some churn as stuff gets updated @@ -38,7 +43,7 @@ pub(crate) enum ActionOp { /// Internal ops UpdateMode, Quit, - #[cfg(feature="testing")] + #[cfg(feature="vault-testing")] /// Testing GenerateTests, } @@ -107,7 +112,7 @@ pub(crate) fn start_actions_thread( None => { log::error!("msg could not be decoded {:?}", msg); } - #[cfg(feature="testing")] + #[cfg(feature="vault-testing")] Some(ActionOp::GenerateTests) => { manager.populate_tests(); manager.retrieve_db(); @@ -119,11 +124,11 @@ pub(crate) fn start_actions_thread( }); } -struct ActionManager { +struct ActionManager<'a> { modals: modals::Modals, storage: RefCell, - #[cfg(feature="testing")] + #[cfg(feature="vault-testing")] trng: RefCell::, mode: Arc::>, @@ -133,23 +138,43 @@ struct ActionManager { action_active: Arc::, mode_cache: VaultMode, main_conn: xous::CID, + #[cfg(feature="vaultperf")] + perfbuf: xous::MemoryRange, + #[cfg(feature="vaultperf")] + pm: PerfMgr<'a>, + #[cfg(feature="vaultperf")] + pid: u32, + // this is necessary to keep rustc quiet when not using `vaultperf` build option... + phantom: core::marker::PhantomData<&'a u32>, } -impl ActionManager { +impl<'a> ActionManager<'a> { pub fn new( main_conn: xous::CID, mode: Arc::>, item_list: Arc::>>, action_active: Arc:: - ) -> ActionManager { + ) -> ActionManager<'a> { let xns = xous_names::XousNames::new().unwrap(); let storage_manager = storage::Manager::new(&xns); + // notes: to use vault as the performance manager, build with `cargo xtask perf-image vault --feature vaultperf`. + // this will override shellchat as the performance manager, while enabling all the other performance reporting agents + #[cfg(feature="vaultperf")] + 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"); + #[cfg(feature="vaultperf")] + let pm = build_perf_mgr(perfbuf.as_mut_ptr()); + let mc = (*mode.lock().unwrap()).clone(); ActionManager { modals: modals::Modals::new(&xns).unwrap(), storage: RefCell::new(storage_manager), - #[cfg(feature="testing")] + #[cfg(feature="vault-testing")] trng: RefCell::new(trng::Trng::new(&xns).unwrap()), mode_cache: mc, @@ -159,6 +184,13 @@ impl ActionManager { tt: ticktimer_server::Ticktimer::new().unwrap(), action_active, main_conn, + #[cfg(feature="vaultperf")] + perfbuf, + #[cfg(feature="vaultperf")] + pm, + #[cfg(feature="vaultperf")] + pid: xous::process::id() as u32, + phantom: core::marker::PhantomData, } } pub(crate) fn activate(&mut self) { @@ -626,10 +658,24 @@ impl ActionManager { } } + #[cfg(feature="vaultperf")] + #[inline] + /// create performance logger entries + pub fn perfentry(&self, pm: &PerfMgr, meta: u32, index: u32, line: u32) { + let event = perf_entry!(self.pid, FILE_ID_APPS_VAULT_SRC_ACTIONS, meta, index, line); + pm.log_event_unchecked(event); + } /// Populate the display list with data from the PDDB. Limited by total available RAM; probably /// would stop working if you have over 500-1k records with the current heap limits. pub(crate) fn retrieve_db(&mut self) { + #[cfg(feature="vaultperf")] + self.pm.stop_and_reset(); + #[cfg(feature="vaultperf")] + self.pm.start(); + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 0, std::line!()); + self.mode_cache = { (*self.mode.lock().unwrap()).clone() }; @@ -638,6 +684,8 @@ impl ActionManager { match self.mode_cache { VaultMode::Password => { let start = self.tt.elapsed_ms(); + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 1, std::line!()); let keylist = match self.pddb.borrow().list_keys(VAULT_PASSWORD_DICT, None) { Ok(keylist) => keylist, Err(e) => { @@ -653,11 +701,18 @@ impl ActionManager { Vec::new() } }; + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 1, std::line!()); log::info!("listing took {} ms", self.tt.elapsed_ms() - start); let start = self.tt.elapsed_ms(); let klen = keylist.len(); + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 2, std::line!()); + let pddb = self.pddb.borrow(); for key in keylist { - match self.pddb.borrow().get( + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_STARTBLOCK, 3, std::line!()); + match pddb.get( VAULT_PASSWORD_DICT, &key, None, @@ -671,9 +726,13 @@ impl ActionManager { let len = record.attributes().unwrap().len; let mut data = Vec::::with_capacity(len); data.resize(len, 0); + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!()); match record.read_exact(&mut data) { Ok(_len) => { if let Some(pw) = storage::PasswordRecord::try_from(data).ok() { + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!()); let extra = format!("{}; {}{}", crate::ux::atime_to_str(pw.atime), t!("vault.u2f.appinfo.authcount", xous::LANG), @@ -687,6 +746,8 @@ impl ActionManager { guid: key, }; il.push(li); + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!()); } else { log::error!("Couldn't deserialize password"); self.report_err("Couldn't deserialize password:", Some(key)); @@ -703,7 +764,13 @@ impl ActionManager { self.report_err("Couldn't access password key", Some(e)) }, } + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 3, std::line!()); + #[cfg(feature="vaultperf")] + self.pm.flush().ok(); } + #[cfg(feature="vaultperf")] + self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 2, std::line!()); log::info!("readout took {} ms for {} elements", self.tt.elapsed_ms() - start, klen); } VaultMode::Fido => { @@ -872,6 +939,29 @@ impl ActionManager { } } il.sort(); + #[cfg(feature="vaultperf")] + { + self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 0, std::line!()); + self.pm.flush().ok(); + match self.pm.stop_and_flush() { + Ok(entries) => { + log::info!("entries: {}", entries); + } + _ => { + log::info!("Perfcounter OOM'd during run"); + } + } + 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::()); + 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), + ); + } + } } pub(crate) fn unlock_basis(&mut self) { @@ -963,7 +1053,7 @@ impl ActionManager { } } - #[cfg(feature="testing")] + #[cfg(feature="vault-testing")] pub(crate) fn populate_tests(&mut self) { use crate::ux::serialize_app_info; @@ -972,8 +1062,8 @@ impl ActionManager { "bunnie", "foo", "turtle.net", "Fox.ng", "Bear", "dog food", "Cat.com", "FUzzy", "1off", "www_test_site_com/long_name/stupid/foo.htm", "._weird~yy%\":'test", "//WHYwhyWHY", "Xyz|zy", "foo:bar", "f🍕🍔🍟🌭d", "💎🙌", "some ノート", "笔录4u", "@u", "sane text", "Käsesoßenrührlöffel"]; let weights = [1; 21]; - const TARGET_ENTRIES: usize = 35; - const TARGET_ENTRIES_PW: usize = 100; + const TARGET_ENTRIES: usize = 15; + const TARGET_ENTRIES_PW: usize = 200; // for each database, populate up to TARGET_ENTRIES // as this is testing code, it's written a bit more fragile in terms of error handling (fail-panic, rather than fail-dialog) // --- passwords --- @@ -1220,3 +1310,28 @@ fn count_validator(input: TextEntryPayload) -> Option> { _ => Some(xous_ipc::String::<256>::from_str(t!("vault.illegal_count", xous::LANG))), } } + +#[cfg(feature="vaultperf")] +fn build_perf_mgr<'a>(bufptr: *mut u8) -> PerfMgr<'a> { + use utralib::generated::*; + let perf_csr = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::perfcounter::HW_PERFCOUNTER_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map perfcounter CSR range: check that no other performance managers are active"); + // this is the range used by the shellchat performance manager + let event1_csr = xous::syscall::map_memory( + xous::MemoryAddress::new(utra::event_source1::HW_EVENT_SOURCE1_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map event1 CSR range"); + PerfMgr::new( + bufptr, + AtomicCsr::new(perf_csr.as_mut_ptr() as *mut u32), + AtomicCsr::new(event1_csr.as_mut_ptr() as *mut u32) // event_source1 + ) +} \ No newline at end of file diff --git a/apps/vault/src/submenu.rs b/apps/vault/src/submenu.rs index b9949858e..b189d3200 100644 --- a/apps/vault/src/submenu.rs +++ b/apps/vault/src/submenu.rs @@ -43,7 +43,7 @@ pub fn create_submenu(vault_conn: xous::CID, actions_conn: xous::CID, menu_mgr: action_payload: MenuPayload::Scalar([0, 0, 0, 0]), close_on_select: true, }); - #[cfg(feature="testing")] + #[cfg(feature="vault-testing")] menu_items.push(MenuItem { name: xous_ipc::String::from_str("Generate test vectors"), action_conn: Some(actions_conn), diff --git a/precursors/soc_csr_perf-c809403.bin b/precursors/soc_csr_perf-c809403.bin index 5da84235b..1e65249ea 100644 Binary files a/precursors/soc_csr_perf-c809403.bin and b/precursors/soc_csr_perf-c809403.bin differ diff --git a/services/graphics-server/Cargo.toml b/services/graphics-server/Cargo.toml index d948c03c7..4d04713a1 100644 --- a/services/graphics-server/Cargo.toml +++ b/services/graphics-server/Cargo.toml @@ -31,6 +31,6 @@ hosted = ["utralib/hosted"] renode = ["utralib/renode", "xous/renode"] debugprint = [] braille = [] -testing = [] +gfx-testing = [] ditherpunk = [] default = [] diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index fe77fda22..00029f6cc 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -36,7 +36,7 @@ use std::sync::Arc; use crate::wordwrap::*; use core::ops::Add; -#[cfg(feature = "testing")] +#[cfg(feature = "gfx-testing")] mod testing; fn draw_boot_logo(display: &mut XousDisplay) { @@ -154,7 +154,7 @@ fn wrapped_main() -> ! { let ticktimer = ticktimer_server::Ticktimer::new().unwrap(); - #[cfg(feature = "testing")] + #[cfg(feature = "gfx-testing")] testing::tests(); loop { if !is_panic.load(Ordering::Relaxed) { // non-panic graphics operations if we are in a panic situation diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 45f2316a2..11de8286b 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -47,6 +47,9 @@ modals = {path="../modals"} utralib = { version = "0.1.7", optional = true, default-features = false } +# performance profiling +perflib = {path="../../tools/perflib", optional = true} + [target.'cfg(any(windows,unix))'.dependencies] rand = "0.7.3" rand_chacha = "0.3.1" @@ -84,4 +87,5 @@ test-rekey = [] hwtest = [] # routines to help with performance profiling of the PDDB in hosted mode pddb-flamegraph = ["hex"] +perfcounter = ["perflib"] default = ["mbbb"] diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index daf704aa5..8156a7c90 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -5,6 +5,10 @@ use std::num::NonZeroU32; use core::ops::{Deref, DerefMut}; use core::mem::size_of; use aes_gcm_siv::Aes256GcmSiv; +#[cfg(feature="perfcounter")] +use perflib::{PERFMETA_STARTBLOCK, PERFMETA_NONE, PERFMETA_ENDBLOCK}; +#[cfg(feature="perfcounter")] +use crate::FILE_ID_SERVICES_PDDB_SRC_DICTIONARY; use std::collections::{HashMap, BinaryHeap, BTreeSet}; use std::io::{Result, Error, ErrorKind}; use bitfield::bitfield; @@ -104,6 +108,8 @@ impl DictCacheEntry { // we've got an entry for every key, so we can safely skip the deep index search let mut smallkeys_to_fill = Vec::::new(); // work around inner mutability problem by copying the names of keys to fill for (name, key) in self.keys.iter() { + // #[cfg(feature="perfcounter")] + // hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, key_count, std::line!()); if key.flags.valid() { if key.start + key.reserved > alloc_top.get() { // if the key is within the large pool space, note its allocation for the basis overall @@ -119,6 +125,8 @@ impl DictCacheEntry { // the cache may not be very effectively used because the key order is random, but let's try anyways. let mut data_cache = PlaintextCache { data: None, tag: None }; for key_to_fill in smallkeys_to_fill.iter() { + #[cfg(feature="perfcounter")] + hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, key_count, std::line!()); self.try_fill_small_key(hw, v2p_map, cipher, &mut data_cache, key_to_fill); } } else { @@ -126,6 +134,8 @@ impl DictCacheEntry { let mut data_cache = PlaintextCache { data: None, tag: None }; let mut errcnt = 0; while try_entry < KEY_MAXCOUNT && key_count < self.key_count { + #[cfg(feature="perfcounter")] + hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, key_count, std::line!()); // cache our decryption data -- there's about 32 entries per page, and the scan is largely linear/sequential, so this should // be a substantial savings in effort. let req_vaddr = dict_indices_to_vaddr(self.index, try_entry); @@ -190,22 +200,32 @@ impl DictCacheEntry { self.last_disk_key_index = try_entry as u32; // now build the small_pool_free binary heap structure + #[cfg(feature="perfcounter")] + hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, key_count, std::line!()); self.rebuild_free_pool(); + #[cfg(feature="perfcounter")] + hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, key_count, std::line!()); } alloc_top } /// merges the list of keys in this dict cache entry into a merge_list. /// The `merge_list` is used because keys are presented as a union across all open basis. pub(crate) fn key_list(&mut self, hw: &mut PddbOs, v2p_map: &HashMap::, cipher: &Aes256GcmSiv, merge_list: &mut BTreeSet) { + #[cfg(feature="perfcounter")] + hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_STARTBLOCK, 0, std::line!()); // ensure that the key cache is filled if self.keys.len() < self.key_count as usize { self.fill(hw, v2p_map, cipher); } + #[cfg(feature="perfcounter")] + hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_NONE, 0, std::line!()); for (key, kcache) in self.keys.iter() { if kcache.flags.valid() { merge_list.insert(key.to_string()); } } + #[cfg(feature="perfcounter")] + hw.perf_entry(FILE_ID_SERVICES_PDDB_SRC_DICTIONARY, PERFMETA_ENDBLOCK, 0, std::line!()); } /// Simply ensures we have the description of a key in cache. Only tries to load small key data. /// Required by meta-operations on the keys that operate only out of the cache. diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index ee9b81a84..526c45d29 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -36,6 +36,11 @@ use digest::Digest; #[cfg(feature="migration1")] use crate::backend::migration1to2::*; +#[cfg(feature="perfcounter")] +use perflib::*; +#[cfg(feature="perfcounter")] +use utralib::AtomicCsr; + /// Implementation-specific PDDB structures: for Precursor/Xous OS pair pub(crate) const MBBB_PAGES: usize = 10; pub(crate) const FSCB_PAGES: usize = 16; @@ -170,6 +175,14 @@ pub(crate) struct PddbOs { failed_logins: u64, #[cfg(all(feature="pddbtest", feature="autobasis"))] testnames: HashSet::, + /// Performance counter elements + #[cfg(feature="perfcounter")] + perfclient: PerfClient, + #[cfg(feature="perfcounter")] + pc_id: u32, + #[cfg(feature="perfcounter")] + /// used to toggle performance profiling on or off + use_perf: bool, } impl PddbOs { @@ -196,6 +209,21 @@ impl PddbOs { let llio = llio::Llio::new(&xns); let dna = llio.soc_dna().unwrap(); + + // performance counter infrastructure, if selected + #[cfg(feature="perfcounter")] + let event2_csr = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::generated::utra::event_source2::HW_EVENT_SOURCE2_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map event2 CSR range"); + #[cfg(feature="perfcounter")] + let perfclient = PerfClient::new(AtomicCsr::new(event2_csr.as_mut_ptr() as *mut u32)); + #[cfg(feature="perfcounter")] + let pc_id = xous::process::id() as u32; + // native hardware #[cfg(any(feature="precursor", feature="renode"))] let ret = PddbOs { @@ -223,6 +251,12 @@ impl PddbOs { failed_logins: 0, #[cfg(all(feature="pddbtest", feature="autobasis"))] testnames: HashSet::new(), + #[cfg(feature="perfcounter")] + perfclient, + #[cfg(feature="perfcounter")] + pc_id, + #[cfg(feature="perfcounter")] + use_perf: true, }; // emulated #[cfg(not(target_os = "xous"))] @@ -288,7 +322,20 @@ impl PddbOs { // placeholder } #[allow(dead_code)] - #[cfg(not(target_os = "xous"))] + #[cfg(feature="perfcounter")] + pub fn set_use_perf(&mut self, use_perf: bool) { + self.use_perf = use_perf; + } + #[allow(dead_code)] + #[cfg(feature="perfcounter")] + pub fn perf_entry(&mut self, file_id: u32, meta: u32, index: u32, line: u32) { + if self.use_perf { + let entry = perf_entry!(self.pc_id, file_id, meta, index, line); + self.perfclient.log_event_unchecked(entry); + } + } + #[allow(dead_code)] + #[cfg(any(feature="hosted"))] /// used to reset the hardware structure for repeated runs of testing within a single invocation pub fn test_reset(&mut self) { self.fspace_cache = FspaceSet::new(); diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index bbc77a4c8..143ff3087 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -398,6 +398,11 @@ use std::sync::Arc; use locales::t; +#[cfg(feature="perfcounter")] +const FILE_ID_SERVICES_PDDB_SRC_MAIN: u32 = 0; +#[cfg(feature="perfcounter")] +const FILE_ID_SERVICES_PDDB_SRC_DICTIONARY: u32 = 1; + #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub(crate) struct BasisRequestPassword { db_name: xous_ipc::String::<{crate::api::BASIS_NAME_LEN}>, @@ -604,6 +609,9 @@ fn wrapped_main() -> ! { } let tt = ticktimer_server::Ticktimer::new().unwrap(); + // turn on (or off) performance profiling, if the feature is enabled + #[cfg(feature="perfcounter")] + pddb_os.set_use_perf(true); // register a suspend/resume listener let mut susres = susres::Susres::new(Some(susres::SuspendOrder::Early), &xns, @@ -905,6 +913,8 @@ fn wrapped_main() -> ! { buffer.replace(mgmt).unwrap(); } Opcode::KeyRequest => { + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_STARTBLOCK, 3, std::line!()); for basis in basis_cache.access_list().iter() { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req: PddbKeyRequest = buffer.to_original::().unwrap(); @@ -916,6 +926,8 @@ fn wrapped_main() -> ! { let dict = req.dict.as_str().expect("dict utf-8 decode error"); let key = req.key.as_str().expect("key utf-8 decode error"); log::debug!("get: {:?} {}", bname, key); + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 3, std::line!()); if basis_cache.dict_attributes(&mut pddb_os, dict, bname).is_err() { if req.create_dict { match basis_cache.dict_add(&mut pddb_os, dict, bname) { @@ -934,6 +946,8 @@ fn wrapped_main() -> ! { } } let alloc_hint = if let Some(hint) = req.alloc_hint {Some(hint as usize)} else {None}; + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 3, std::line!()); if basis_cache.key_attributes(&mut pddb_os, dict, key, bname).is_err() { if !req.create_key { req.result = PddbRequestCode::NotFound; @@ -959,6 +973,8 @@ fn wrapped_main() -> ! { } } } + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 3, std::line!()); // at this point, we have established a basis/dict/key tuple. let token: ApiToken = [pddb_os.trng_u32(), pddb_os.trng_u32(), pddb_os.trng_u32()]; let cid = if let Some(cb_sid) = req.cb_sid { @@ -979,6 +995,8 @@ fn wrapped_main() -> ! { buffer.replace(req).unwrap(); break; // if we got here, entry was found, stop searching } + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_ENDBLOCK, 3, std::line!()); } Opcode::OpenKeyStd => { if let Some(mem) = msg.body.memory_message_mut() { @@ -1177,6 +1195,8 @@ fn wrapped_main() -> ! { } } Opcode::KeyCountInDict => { + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_STARTBLOCK, 0, std::line!()); let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req = buffer.to_original::().unwrap(); if key_token.is_some() { @@ -1193,6 +1213,8 @@ fn wrapped_main() -> ! { }; let dict = req.dict.as_str().expect("dict utf-8 decode error"); log::debug!("counting keys in dict {} basis {:?}", dict, bname); + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 0, std::line!()); key_list = match basis_cache.key_list(&mut pddb_os, dict, bname) { Ok(list) => { log::debug!("count: {}", list.len()); @@ -1209,8 +1231,12 @@ fn wrapped_main() -> ! { } }; buffer.replace(req).unwrap(); + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_ENDBLOCK, 0, std::line!()); } Opcode::ListKeyV2 => { + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_STARTBLOCK, 1, std::line!()); let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut req = buffer.to_original::().unwrap(); if let Some(token) = key_token { @@ -1254,6 +1280,8 @@ fn wrapped_main() -> ! { req.retcode = PddbRetcode::AccessDenied; } buffer.replace(req).unwrap(); + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_ENDBLOCK, 1, std::line!()); } Opcode::ListKeyStd => { if let Some(mem) = msg.body.memory_message_mut() { @@ -1324,6 +1352,8 @@ fn wrapped_main() -> ! { } } Opcode::ReadKey => { + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_STARTBLOCK, 4, std::line!()); let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let pbuf = PddbBuf::from_slice_mut(buffer.as_mut()); // direct translation, no serialization necessary for performance let token = pbuf.token; @@ -1331,6 +1361,8 @@ fn wrapped_main() -> ! { for basis in basis_cache.access_list().iter() { let temp = if let Some (name) = &rec.basis {Some(name)} else {Some(basis)}; log::debug!("read (spec: {:?}){:?} {} len {} pos {}", rec.basis, temp, rec.key, pbuf.len, pbuf.position); + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 4, std::line!()); match basis_cache.key_read(&mut pddb_os, &rec.dict, &rec.key, &mut pbuf.data[..pbuf.len as usize], Some(pbuf.position as usize), @@ -1340,6 +1372,8 @@ fn wrapped_main() -> ! { // the key will be a hit, so, we let it stand. if let Some (name) = &rec.basis {Some(&name)} else {Some(basis)}) { Ok(readlen) => { + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_NONE, 4, std::line!()); pbuf.len = readlen as u16; pbuf.retcode = PddbRetcode::Ok; break; @@ -1356,6 +1390,8 @@ fn wrapped_main() -> ! { pbuf.retcode = PddbRetcode::BasisLost; } // we don't nede a "replace" operation because all ops happen in-place + #[cfg(feature="perfcounter")] + pddb_os.perf_entry(FILE_ID_SERVICES_PDDB_SRC_MAIN, perflib::PERFMETA_ENDBLOCK, 4, std::line!()); } Opcode::SeekKeyStd => { diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 86dbc0d67..3ec35b540 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -80,7 +80,10 @@ webpki-roots = {version = "0.22.4", optional = true} ring = {version = "0.16.20", optional = true} # for testing ring math functions # note requirement for patch to xous-ring in workspace Cargo.toml tungstenite = {version = "0.17.3", optional = true, features = ["rustls-tls-webpki-roots"]} url = {version = "2.2.2", optional = true} + +# for performance testing perflib = {path="../../tools/perflib", optional = true} +random-pick = {version = "1.2.15", optional = true} # for the curve25519 tests # hardware acceleration adaptations are inserted into a fork of the main branch. @@ -121,5 +124,5 @@ test-rekey = [] no-codec = [] nettest = [] # batch network tests tls = ["rustls", "webpki-roots", "ring", "tungstenite", "url"] -perfcounter = ["ring", "perflib", "xous/v2p"] +shellperf = ["ring", "perflib", "xous/v2p", "random-pick"] # this also needs the project-wide "perfcounter" feature to be selected. shellchat is the exclusive manager of the performance counter, do not use with e.g. "vaultperf" feature default = [] # "debugprint" diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index e73a8a6a6..397eeb8e9 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -1,7 +1,7 @@ use xous::{MessageEnvelope}; use xous_ipc::String; use core::fmt::Write; -#[cfg(feature="perfcounter")] +#[cfg(feature="shellperf")] use utralib::generated::*; use std::collections::HashMap; @@ -52,9 +52,9 @@ pub struct CommonEnv { xns: xous_names::XousNames, 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")] + #[cfg(feature="shellperf")] perf_csr: AtomicCsr, - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] event_csr: AtomicCsr::, } impl CommonEnv { @@ -175,7 +175,7 @@ pub struct CmdEnv { impl CmdEnv { pub fn new(xns: &xous_names::XousNames) -> CmdEnv { let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] let perf_csr = xous::syscall::map_memory( xous::MemoryAddress::new(utra::perfcounter::HW_PERFCOUNTER_BASE), None, @@ -183,7 +183,7 @@ impl CmdEnv { xous::MemoryFlags::R | xous::MemoryFlags::W, ) .expect("couldn't map perfcounter CSR range"); - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] let event1_csr = xous::syscall::map_memory( xous::MemoryAddress::new(utra::event_source1::HW_EVENT_SOURCE1_BASE), None, @@ -202,9 +202,9 @@ impl CmdEnv { xns: xous_names::XousNames::new().unwrap(), netmgr: net::NetManager::new(), boot_instant: std::time::Instant::now(), - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] perf_csr: AtomicCsr::new(perf_csr.as_mut_ptr() as *mut u32), - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] event_csr: AtomicCsr::new(event1_csr.as_mut_ptr() as *mut u32), }; //let fcc = Fcc::new(&mut common); diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 2bcf12204..94fb1a365 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -22,7 +22,7 @@ use gam::DecodePng; use std::convert::TryInto; #[cfg(feature="tls")] use tungstenite::{WebSocket, stream::MaybeTlsStream}; -#[cfg(feature="perfcounter")] +#[cfg(feature="shellperf")] use perflib::*; pub struct NetCmd { callback_id: Option, @@ -32,12 +32,12 @@ pub struct NetCmd { ping: Option, #[cfg(feature="tls")] ws: Option>>, - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] perfbuf: xous::MemoryRange, } impl NetCmd { pub fn new(xns: &xous_names::XousNames) -> Self { - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] let perfbuf = xous::syscall::map_memory( None, None, @@ -53,7 +53,7 @@ impl NetCmd { ping: None, #[cfg(feature="tls")] ws: None, - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] perfbuf, } } @@ -511,7 +511,7 @@ 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. + // note: to use the `shellperf` 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: @@ -521,7 +521,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { // 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")] + #[cfg(feature="shellperf")] "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); @@ -535,7 +535,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { ); } } - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] "cta1" => { log::info!("constant time RISC-V HW AES test"); @@ -583,7 +583,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } // pm.print_page_table(); } - #[cfg(feature="perfcounter")] + #[cfg(feature="shellperf")] "cta2" => { let pm = PerfMgr::new( self.perfbuf.as_mut_ptr(), diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index b66c0c0b9..bb63bb419 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -6,15 +6,46 @@ use xous_ipc::String; use std::io::{Write, Read, Seek, SeekFrom}; use core::fmt::Write as FmtWrite; +#[cfg(feature="shellperf")] +const TEST_DICT: &'static str = "perftest"; +#[cfg(feature="shellperf")] +use perflib::*; +#[cfg(feature="shellperf")] +use sha2::Digest; +#[cfg(feature="shellperf")] +const FILE_ID_SERVICES_SHELLCHAT_SRC_CMDS_PDDB_CMD: u32 = 2; + pub struct PddbCmd { pddb: pddb::Pddb, + #[cfg(feature="shellperf")] + perfbuf: xous::MemoryRange, + #[cfg(feature="shellperf")] + my_pid: u32, } impl PddbCmd { pub fn new(_xns: &xous_names::XousNames) -> PddbCmd { + #[cfg(feature="shellperf")] + 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"); + PddbCmd { pddb: pddb::Pddb::new(), + #[cfg(feature="shellperf")] + perfbuf, + #[cfg(feature="shellperf")] + my_pid: xous::process::id() as u32, } } + #[cfg(feature="shellperf")] + #[inline] + pub fn perfentry(&self, pm: &PerfMgr, meta: u32, index: u32, line: u32) { + let event = perf_entry!(self.my_pid, FILE_ID_SERVICES_SHELLCHAT_SRC_CMDS_PDDB_CMD, meta, index, line); + pm.log_event_unchecked(event); + } } impl<'a> ShellCmdApi<'a> for PddbCmd { @@ -896,6 +927,123 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { } } } + #[cfg(feature="shellperf")] + "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::()); + 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="shellperf")] + "perf" => { + log::info!("PDDB performance profiling"); + + // event_source0 is for the kernel + // event_source1 is for shellchat + // event_source2 is for pddb process + + let pm = PerfMgr::new( + self.perfbuf.as_mut_ptr(), + _env.perf_csr.clone(), + _env.event_csr.clone() // event_source1 + ); + log::info!("cleaning out old records (some stale records are expected)"); + pm.stop_and_reset(); + log::info!("run starting..."); + + // this starts the performance counter + pm.start(); + + for index in 0..10 { + self.perfentry(&pm, PERFMETA_STARTBLOCK, index, std::line!()); + let pws = self.pddb.list_keys(TEST_DICT, None); + self.perfentry(&pm, PERFMETA_NONE, index, std::line!()); + let entries = pws.unwrap_or(Vec::new()); + self.perfentry(&pm, PERFMETA_NONE, index, std::line!()); + log::info!("total pws found: {}", entries.len()); + self.perfentry(&pm, PERFMETA_ENDBLOCK, index, std::line!()); + pm.flush().ok(); + } + + match pm.stop_and_flush() { + Ok(entries) => { + log::info!("entries: {}", entries); + } + _ => { + log::info!("Perfcounter OOM'd during run"); + } + } + } + #[cfg(feature="shellperf")] + "perfprep" => { + log::info!("Preparing test set for performance profiling"); + let words = [ + "bunnie", "foo", "turtle.net", "Fox.ng", "Bear", "dog food", "Cat.com", "FUzzy", "1off", "www_test_site_com/long_name/stupid/foo.htm", + "._weird~yy%\":'test", "//WHYwhyWHY", "Xyz|zy", "foo:bar", "f🍕🍔🍟🌭d", "💎🙌", "some ノート", "笔录4u", "@u", "sane text", "Käsesoßenrührlöffel"]; + let weights = [1; 21]; + const TARGET_ENTRIES: usize = 300; + + let pws = self.pddb.list_keys(TEST_DICT, None).unwrap_or(Vec::new()); + if pws.len() < TARGET_ENTRIES { + let extra_count = TARGET_ENTRIES - pws.len(); + for _index in 0..extra_count { + let desc = random_pick::pick_multiple_from_slice(&words, &weights, 3); + let description = format!("{} {} {}", desc[0], desc[1], desc[2]); + let username = random_pick::pick_from_slice(&words, &weights).unwrap().to_string(); + let notes = random_pick::pick_from_slice(&words, &weights).unwrap().to_string(); + let record = format!( + "{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n{}:{}\n", + "version", + 2, + "description", + description, + "username", + username, + "password", + "dummy password", + "notes", + notes, + "ctime", + 0, + "atime", + 0, + "count", + 1, + ) + .into_bytes(); + + let mut h = sha2::Sha256::new(); + h.update(description.as_bytes()); + h.update(username.as_bytes()); + let hash = h.finalize().to_vec(); + + match self.pddb.get( + TEST_DICT, + &hex(hash), + None, + true, + true, + Some(128), + None::, + ) { + Ok(mut data) => match data.write(&record) { + Ok(_) => {}, + Err(e) => log::info!("error creating test keys: {:?}", e), + }, + Err(e) => log::info!("error creating test keys: {:?}", e), + } + } + log::info!("test dictionary should have {} keys", pws.len() + extra_count); + self.pddb.sync().unwrap(); + } + } _ => { write!(ret, "{}", helpstring).unwrap(); } @@ -967,3 +1115,14 @@ fn join_tokens<'a>(buf: &mut String<1024>, tokens: impl Iterator } } } + +#[cfg(feature="perflib")] +fn hex(data: Vec) -> std::string::String { + use std::fmt::Write; + let mut s = std::string::String::with_capacity(2 * data.len()); + for byte in data { + write!(s, "{:02X}", byte).unwrap(); + } + + s +} \ No newline at end of file diff --git a/tools/perf_analyze.py b/tools/perf_analyze.py new file mode 100644 index 000000000..ac19caf2c --- /dev/null +++ b/tools/perf_analyze.py @@ -0,0 +1,150 @@ +#! /usr/bin/env python3 +import argparse +import numpy as np +import os + +# manually extracted from source files for now. A dictionary of lists +# whose entry indices correspond to the path to the source file. +FILEMAP = { + 'pddb' : [ + "services/pddb/src/main.rs", + "services/pddb/src/dictionary.rs", + ], + 'shellchat' : [ + "reserved", + "reserved", + "services/shellchat/src/cmds/pddb_cmd.rs", + ], + 'vault' : [ + "reserved", + "app/vault/src/actions.rs", + ] +} +# manually extracted from build log message +PID_DICT = { + 1: 'kernel', + 2: 'xous-ticktimer', + 3: 'xous-log', + 4: 'xous-names', + 5: 'xous-susres', + 6: 'graphics-server', + 7: 'keyboard', + 8: 'spinor', + 9: 'llio', + 10: 'com', + 11: 'net', + 12: 'dns', + 13: 'gam', + 14: 'ime-frontend', + 15: 'ime-plugin-shell', + 16: 'codec', + 17: 'modals', + 18: 'root-keys', + 19: 'trng', + 20: 'sha2', + 21: 'engine-25519', + 22: 'jtag', + 23: 'status', + 24: 'shellchat', + 25: 'pddb', + 26: 'usb-device-xous', + 27: 'vault', +} + +# display time in milliseconds +TIMEBASE = 1000.0 * (10e-9) + +def main(): + parser = argparse.ArgumentParser(description="Analyze performance logs") + parser.add_argument( + "--file", help="file to analyze", type=str + ) + parser.add_argument( + "--average", help="average over a series. `file` is a root name; argument is number of data series, 1-offset indexed", type=int + ) + args = parser.parse_args() + + if args.file is None: + print("Must specify a file to analyze with --file") + exit(0) + + if args.average: + flist = [] + for i in range(1,args.average + 1): + flist += [args.file + str(i) + ".bin"] + else: + flist = [args.file] + + for fname in flist: + with open(fname, "rb") as f: + data = f.read() + entries = [data[i:i+8] for i in range(0, len(data), 8)] + + timers = [] + last_start = 0 + + for entry in entries: + code = int.from_bytes(entry[:4], 'little') + timestamp = int.from_bytes(entry[4:], 'little') + + if code == 0 and timestamp == 0: + continue + + file_id = (code >> 29) & 0x7 + process_id = (code >> 24) & 0x1F + meta = (code >> 21) & 0x7 + index = (code >> 13) & 0xFF + line = code & 0x1FFF + + if process_id in PID_DICT: + pname = PID_DICT[process_id] + if pname in FILEMAP: + map = FILEMAP[pname] + if file_id < len(map): + fname = map[file_id] + else: + fname = 'fid{}'.format(file_id) + else: + fname = 'fid{}'.format(file_id) + else: + pname = 'pid{}'.format(process_id) + fname = 'fid{}'.format(file_id) + + stampname = pname + fname + str(index) + + if meta == 0: + mname = '' + elif meta == 1: + mname = 'START' + last_start = timestamp + timers += [[stampname, timestamp]] + elif meta == 2: + mname = 'STOP' + else: + mname = 'INVALID' + + stampstr = '' + for t in timers: + stampstr += ' {:7.3f}'.format( (timestamp - t[1]) * TIMEBASE ) + + print('{:8.3f}{}: {}[{}] {} {}:{}'.format( + timestamp * TIMEBASE, + stampstr, + mname, + index, + pname, + fname, + line, + )) + + if mname == 'STOP': + remove = None + for idx, t in enumerate(timers): + if stampname in t: + remove = idx + if remove is not None: + timers.pop(remove) + +if __name__ == "__main__": + main() + exit(0) diff --git a/tools/perflib/src/lib.rs b/tools/perflib/src/lib.rs index fbd979cc9..e138d133a 100644 --- a/tools/perflib/src/lib.rs +++ b/tools/perflib/src/lib.rs @@ -2,8 +2,36 @@ use utralib::generated::*; use core::num::{NonZeroU8, NonZeroU16}; use std::cell::RefCell; +// 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 + pub const BUFLEN: usize = 1024 * 512; +/// ($pid:expr, $fid:expr, $meta:expr, $index:expr, $line:expr) +/// general format of a performance counter entry: +/// file ID | process ID | meta codes | index | line number +/// 31 29 28 24 23 21 20 13 12 0 +/// This gives tracking across: +/// - 8 files +/// - 32 processes +/// - 8 "meta" codes +/// - a loop index of up to 255 times +/// - a file line number of up to 8192 +#[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) + }; +} +pub const PERFMETA_NONE: u32 = 0; +pub const PERFMETA_STARTBLOCK: u32 = 1; +pub const PERFMETA_ENDBLOCK: u32 = 2; + #[repr(C)] pub struct PerfLogEntry { code: u32, @@ -117,9 +145,18 @@ impl <'a> PerfMgr<'a> { d.ts = 0; } self.perf_csr.wfo(utra::perfcounter::RUN_STOP, 1); + let mut first_i: Option = None; + let mut last_i = 0; 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); + if first_i.is_none() { + first_i = Some(i); + } else { + last_i = i; + } + } + if let Some(i) = first_i { + log::warn!("FIFO was not drained before reset: indices [{}..{}]", i, last_i); } // stop the counter if it would rollover self.perf_csr.wo(utra::perfcounter::SATURATE_LIMIT0, self.saturation_limit as u32); @@ -237,7 +274,7 @@ impl <'a> PerfMgr<'a> { } } self.ts_cont.replace(self.ts_cont.take() + ts_offset as u64); - log::info!("FIFO final flush had {} entries", expected_i - 1); + 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()) diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index ad085aa3a..7f29122d3 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -257,6 +257,10 @@ impl Builder { self.features.retain(|x| x != feature); self } + /// test if a feature is present + pub fn has_feature(&self, feature: &str) -> bool { + self.features.contains(&feature.to_string()) + } /// add a feature to be passed on to just the loader pub fn add_loader_feature<'a>(&'a mut self, feature: &str) -> &'a mut Builder { self.loader_features.push(feature.into()); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2baf64211..11f6f2c2e 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -272,6 +272,11 @@ fn main() -> Result<(), Box> { .add_apps(&get_cratespecs()); } Some("perf-image") => { + // `--feature vaultperf` will make `vault` the performance manager, in exclusion of shellchat + if !builder.has_feature("shellperf") && !builder.has_feature("vaultperf") { + // select `shellchat` as the performance manager by default. + builder.add_feature("shellperf"); + } // note: to use this image, 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. //