Skip to content

Commit

Permalink
[opentitantool] Optimization, avoid excessive Ctrl-C
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jesultra committed Dec 6, 2024
1 parent e45fbc9 commit 937cb67
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions sw/host/opentitanlib/src/transport/hyperdebug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ pub struct CachedIo {

pub struct Conn {
console_port: RefCell<TTYPort>,
first_use: Cell<bool>,
}

// The way that the HyperDebug allows callers to request optimization for a sequence of operations
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 937cb67

Please sign in to comment.