From 937cb671b86ac8e26be4f036df3eccf019c8606b Mon Sep 17 00:00:00 2001 From: "Jes B. Klinke" Date: Wed, 20 Nov 2024 10:43:34 -0800 Subject: [PATCH] [opentitantool] Optimization, avoid excessive Ctrl-C HyperDebug has a text-based console, which opentitantool/lib uses for sending all sorts of commands. For the case a previous invocation has left a partially typed command in HyperDebug's buffers, opentitantool always precedes any command with Ctrl-C to clear the buffer. This is slightly inefficient, when opentitantool is sending many commands in succession, and has locked the character device preventing any other Linux process from injecting any input. This CL changes the code to only emit Ctrl-C before the first time a command is sent while the lock is held. Change-Id: Id209f5ef5125ec2bb3f0489c33b67a291207cf6b Signed-off-by: Jes B. Klinke --- .../opentitanlib/src/transport/hyperdebug/mod.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sw/host/opentitanlib/src/transport/hyperdebug/mod.rs b/sw/host/opentitanlib/src/transport/hyperdebug/mod.rs index 3f2d398f92254..f3caaa66c3c6f 100644 --- a/sw/host/opentitanlib/src/transport/hyperdebug/mod.rs +++ b/sw/host/opentitanlib/src/transport/hyperdebug/mod.rs @@ -443,6 +443,7 @@ pub struct CachedIo { pub struct Conn { console_port: RefCell, + first_use: Cell, } // The way that the HyperDebug allows callers to request optimization for a sequence of operations @@ -471,6 +472,7 @@ impl Inner { flock_serial(&port, port_name)?; let conn = Rc::new(Conn { console_port: RefCell::new(port), + first_use: Cell::new(true), }); // Return a (strong) reference to the newly opened connection, while keeping a weak // reference to the same in this `Inner` object. The result is that if the caller keeps @@ -564,10 +566,16 @@ impl Conn { fn execute_command(&self, cmd: &str, mut callback: impl FnMut(&str)) -> Result<()> { let port: &mut TTYPort = &mut self.console_port.borrow_mut(); - // Send Ctrl-C, followed by the command, then newline. This will discard any previous - // partial input, before executing our command. - port.write(format!("\x03{}\n", cmd).as_bytes()) - .context("writing to HyperDebug console")?; + if self.first_use.get() { + // Send Ctrl-C, followed by the command, then newline. This will discard any previous + // partial input, before executing our command. + port.write(format!("\x03{}\n", cmd).as_bytes()) + .context("writing to HyperDebug console")?; + self.first_use.set(false); + } else { + port.write(format!("{}\n", cmd).as_bytes()) + .context("writing to HyperDebug console")?; + } // Now process response from HyperDebug. First we expect to see the echo of the command // we just "typed". Then zero, one or more lines of useful output, which we want to pass