From e515092636cf99143c0015b87602c811978b4f3a Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 23 Oct 2022 23:14:57 +0800 Subject: [PATCH] add performance profiling options to PDDB --- services/pddb/Cargo.toml | 4 +++ services/pddb/src/backend/dictionary.rs | 19 +++++++++- services/pddb/src/backend/hw.rs | 47 +++++++++++++++++++++++++ services/pddb/src/main.rs | 18 ++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 6ec3918a2..89de6d5ea 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -47,6 +47,9 @@ modals = {path="../modals"} utralib = { version = "0.1.6", 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..dea4ef425 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -1,10 +1,11 @@ -use crate::api::*; +use crate::{api::*, FILE_ID_SERVICES_PDDB_SRC_DICTIONARY}; use super::*; use std::num::NonZeroU32; use core::ops::{Deref, DerefMut}; use core::mem::size_of; use aes_gcm_siv::Aes256GcmSiv; +use perflib::{PERFMETA_STARTBLOCK, PERFMETA_NONE, PERFMETA_ENDBLOCK}; use std::collections::{HashMap, BinaryHeap, BTreeSet}; use std::io::{Result, Error, ErrorKind}; use bitfield::bitfield; @@ -104,6 +105,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 +122,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 +131,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 +197,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 7f5d5091a..ef5dc068b 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(any(feature="hosted"))] @@ -288,6 +322,19 @@ impl PddbOs { // placeholder } #[allow(dead_code)] + #[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) { diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 91dc1397d..ef81f2e95 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, @@ -1177,6 +1185,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 +1203,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 +1221,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 +1270,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() {