From 7d62ca85b8357d96aed97a62f66b4dcef5f19be2 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 1 Aug 2022 01:32:19 +0800 Subject: [PATCH] some preliminary schwags at making flamegraphs turns out, they're pretty useless. I mean, for debugging actual running code. it tells us basically what we know already which is 99.99% of the time is spent waiting on inter-thread messaging in hosted mode. --- Cargo.lock | 1 + services/pddb/Cargo.toml | 6 +++- services/pddb/src/main.rs | 5 +++ services/pddb/src/profiling.rs | 66 ++++++++++++++++++++++++++++++++++ xtask/src/main.rs | 27 ++++++++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 services/pddb/src/profiling.rs diff --git a/Cargo.lock b/Cargo.lock index f6127a681..a2e4235eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2364,6 +2364,7 @@ dependencies = [ "cipher 0.4.3", "digest 0.9.0", "gam", + "hex 0.4.3", "hkdf", "llio", "locales", diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 7c3034e5f..e2ee5e5fd 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -52,6 +52,8 @@ utralib = { path = "../../utralib"} rand = "0.7.3" rand_chacha = "0.3.1" +hex = { version = "0.4.3", default-features = false, features = ["alloc"], optional = true } + [features] # when selected, physical disk addresses are set to 64 bits, otherwise, they are 32 bits. # 32 bit addressing is recommended for Precursor, as its disk is only 128MiB and it has limited RAM for bookkeeping. @@ -76,4 +78,6 @@ migration1 = [] # hazardous debug flag decorates any debug paths that might accidentally leak key material hazardous-debug = [] test-rekey = [] -default = ["mbbb"] +# routines to help with performance profiling of the PDDB in hosted mode +pddb-flamegraph = ["hex"] +default = ["mbbb", "pddb-flamegraph"] diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index d6852bcdf..71cbfbeb9 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -381,6 +381,9 @@ mod tests; #[allow(unused_imports)] use tests::*; +#[cfg(feature="pddb-flamegraph")] +mod profiling; + use num_traits::*; use xous::{send_message, Message, msg_blocking_scalar_unpack}; use xous_ipc::Buffer; @@ -663,6 +666,8 @@ fn wrapped_main() -> ! { latest_cache = basis_cache.cache_size(); log::info!("PDDB post-mount caching stats: {} heap, {} cache", latest_heap, latest_cache); scrub_run.store(true, Ordering::SeqCst); + #[cfg(feature="pddb-flamegraph")] + profiling::do_query_work(); } } }), diff --git a/services/pddb/src/profiling.rs b/services/pddb/src/profiling.rs new file mode 100644 index 000000000..5363fdb99 --- /dev/null +++ b/services/pddb/src/profiling.rs @@ -0,0 +1,66 @@ +use std::thread; +#[allow(unused_imports)] +use std::io::{Error, ErrorKind, Write, Read}; + +const PROFILING_DICT: &'static str = "pdict"; +const NUMKEYS: usize = 200; +const PROFILE_ITERS: usize = 200; + +fn gen_guid(trng: &mut trng::Trng) -> String { + let mut guid = [0u8; 16]; + trng.fill_bytes(&mut guid); + hex::encode(guid) +} + +/// A test that repeatedly queries a dictionary with many entries +pub fn do_query_work() { + let _ = thread::spawn(|| { + let xns = xous_names::XousNames::new().unwrap(); + let pddb = pddb::Pddb::new(); + let mut trng = trng::Trng::new(&xns).unwrap(); + + let key_count = match pddb.list_keys(PROFILING_DICT, None) { + Ok(kl) => kl.len(), + Err(_e) => { + 0 + } + }; + + // populate the dictionary if we're short the number of keys we want to have + log::info!("test setup"); + if key_count < NUMKEYS { + for _ in 0..NUMKEYS - key_count { + let guid = gen_guid(&mut trng); + match pddb.get( + PROFILING_DICT, + &guid, + None, + true, + true, + Some(256), + None:: + ) { + Ok(mut key) => { + let mut rdata = [0u8; 192]; + trng.fill_bytes(&mut rdata); + key.write_all(&rdata).ok(); + } + Err(e) => log::error!("couldn't create test key {:?}", e), + } + } + } + log::info!("starting main loop"); + let modals = modals::Modals::new(&xns).unwrap(); + modals.show_notification("press any key to start profiling run.", None).ok(); + + modals.dynamic_notification(Some("running"), None).ok(); + for i in 0..PROFILE_ITERS { + let klist = pddb.list_keys(PROFILING_DICT, None).unwrap(); + log::info!("iter {}, klist: {}", i, klist.len()); + } + modals.dynamic_notification_close().ok(); + modals.show_notification("done", None).ok(); + + log::info!("quitting"); + }); +} \ No newline at end of file diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4427dba12..b1bf38407 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -382,6 +382,33 @@ fn main() -> Result<(), Box> { Some(&["--features", "renode-bypass"]), )?; } + Some("pddb-flamegraph") => { + let mut args = env::args(); + args.nth(1); + let mut pkgs = pddb_dev_pkgs.to_vec(); + // add pkgs required for functional testing. Quite a bit. :-/ + pkgs.push("llio"); + pkgs.push("root-keys"); + pkgs.push("jtag"); + pkgs.push("com"); + pkgs.push("gam"); + pkgs.push("net"); + pkgs.push("dns"); + pkgs.push("graphics-server"); + pkgs.push("modals"); + pkgs.push("keyboard"); + pkgs.push("ime-frontend"); + pkgs.push("ime-plugin-shell"); + pkgs.push("status"); + pkgs.push("usb-device-xous"); + generate_app_menus(&vec![]); + run(false, &pkgs, + Some(&[ + "--features", "pddbtest", + "--features", "pddb-flamegraph", + ]), false)? + + } Some("renode-aes-test") => { generate_app_menus(&Vec::::new()); renode_image(false, &aestest_pkgs, &[], None, None)?