From 58edcc56f276743c2193b6995316b4202c8c603b Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 21 May 2021 15:56:58 +0800 Subject: [PATCH] sha512 tests fully implemented, working through sus/res note: sha512 will fail if a suspend is forced upon it!! but it will vociferously complain if this happens. --- services/engine-sha512/src/main.rs | 11 +++++++++++ services/shellchat/src/cmds/sha.rs | 19 +++++++++++++++++++ services/shellchat/src/main.rs | 2 +- services/trng/src/api.rs | 2 ++ services/trng/src/lib.rs | 8 ++++++-- services/trng/src/main.rs | 6 ++++-- services/xous-names/src/lib.rs | 10 +++++++--- 7 files changed, 50 insertions(+), 8 deletions(-) diff --git a/services/engine-sha512/src/main.rs b/services/engine-sha512/src/main.rs index 8a3e7998e..238fd352f 100644 --- a/services/engine-sha512/src/main.rs +++ b/services/engine-sha512/src/main.rs @@ -77,6 +77,17 @@ mod implementation { let sha = self.fifo.as_mut_ptr() as *mut u32; let sha_byte = self.fifo.as_mut_ptr() as *mut u8; + // this unsafe version is very slightly faster than the safe version below + /* + let src_bfr = buf.as_ptr() as *mut u32; + for offset in 0 .. buf.len() / 4 { + unsafe { sha.write_volatile(src_bfr.add(offset).read_volatile()); } + } + if (buf.len() % 4) != 0 { + for index in (buf.len() - (buf.len() % 4))..buf.len() { + unsafe{ sha_byte.write_volatile(buf[index]); } + } + } */ for (_reg, chunk) in buf.chunks(4).enumerate() { let mut temp: [u8; 4] = Default::default(); if chunk.len() == 4 { diff --git a/services/shellchat/src/cmds/sha.rs b/services/shellchat/src/cmds/sha.rs index e5d18f8ed..72fb089e9 100644 --- a/services/shellchat/src/cmds/sha.rs +++ b/services/shellchat/src/cmds/sha.rs @@ -34,6 +34,14 @@ TEST_MAX_LEN = 512 (random length) / TEST_ITERS = 1000: hw 7.332ms/hash, sw 5.48 TEST_MAX_LEN = 1024 (fixed length) / TEST_ITERS = 1000: hw 7.257ms/hash, sw 11.676ms/hash TEST_MAX_LEN = 8192 (random length) / TEST_ITERS = 1000: hw 10.528ms/hash, sw 40.631ms/hash TEST_MAX_LEN = 8192 (fixed length) / TEST_ITERS = 1000: hw 17.035ms/hash, sw 78.633ms/hash + +power consumption - +4.1V system voltage +159mA nominal +182mA while running hw benchmark (17.569ms/hash) +~ 23mA for SHA hardware unit doing 8k fixed length hashes, ~14% extra power, 1.65mJ/hash +172mA while running sw benchmark (78.525ms/hash) -> ~10mA excess power for software -> 3.22mJ/hash +~50% power savings to use hardware hasher */ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { @@ -211,6 +219,17 @@ impl<'a> ShellCmdApi<'a> for Sha { ).unwrap(); write!(ret, "Starting Sha512 software benchmark with {} iters, {} max_len, {} fixed_len", TEST_ITERS, TEST_MAX_LEN, TEST_FIXED_LEN).unwrap(); } + "susres" => { + let start = env.ticktimer.elapsed_ms(); + self.start_time = Some(start); + xous::send_message(self.benchmark_cid, + xous::Message::new_scalar(BenchOp::StartSha512Hw.to_usize().unwrap(), 0, 0, 0, 0) + ).unwrap(); + let wait_time = (env.trng.get_u32().unwrap() % 2000) + 500; // at least half a second wait, up to 2 seconds + env.ticktimer.sleep_ms(wait_time as _).unwrap(); + self.susres.initiate_suspend().unwrap(); + write!(ret, "Interrupted Sha512 hardware benchmark with a suspend/resume").unwrap(); + } _ => { write!(ret, "{}", helpstring).unwrap(); } diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index e26f6b904..e0369fb64 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -258,7 +258,7 @@ pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_"; #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Debug); + log::set_max_level(log::LevelFilter::Info); info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); diff --git a/services/trng/src/api.rs b/services/trng/src/api.rs index eddb3e1cc..48af9d1b6 100644 --- a/services/trng/src/api.rs +++ b/services/trng/src/api.rs @@ -8,4 +8,6 @@ pub(crate) enum Opcode { /// Suspend/resume callback SuspendResume, + + Quit, } diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 51e3fda86..9bcc0d2bc 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -11,6 +11,7 @@ pub struct Trng { impl Trng { pub fn new(xns: &xous_names::XousNames) -> Result { let conn = xns.request_connection_blocking(api::SERVER_NAME_TRNG).expect("Can't connect to TRNG server"); + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); Ok(Trng { conn }) @@ -39,10 +40,13 @@ impl Trng { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Trng { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} - + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } \ No newline at end of file diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index 4fc6d7502..ad6d03655 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -400,9 +400,11 @@ fn xmain() -> ! { susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); trng.resume(); }), - None => { - log::error!("couldn't convert opcode"); + Some(api::Opcode::Quit) => { break + }, + None => { + log::error!("couldn't convert opcode, ignoring"); } } } diff --git a/services/xous-names/src/lib.rs b/services/xous-names/src/lib.rs index 1ba7f9606..010a92651 100644 --- a/services/xous-names/src/lib.rs +++ b/services/xous-names/src/lib.rs @@ -12,6 +12,7 @@ pub struct XousNames { } impl XousNames { pub fn new() -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xous::connect(xous::SID::from_bytes(b"xous-name-server").unwrap()).expect("Couldn't connect to XousNames"); Ok(XousNames { conn, @@ -88,10 +89,13 @@ impl XousNames { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for XousNames { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} - + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } -} \ No newline at end of file +}