Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

wezterm-ssh: stop napping on the wakeups that are not busy loops

RoversX254464a37b29
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index ee3942e4..5986b90b 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -449,14 +449,28 @@ macro_rules! pdu {
R: AsyncRead,
R: std::fmt::Debug
{
+ let started = std::time::Instant::now();
let decoded = decode_raw_async(r, max_serial).await.context("decoding a PDU")?;
+ let read_took = started.elapsed();
match decoded.ident {
$(
$vers => {
metrics::histogram!("pdu.size", "pdu" => stringify!($name)).record(decoded.data.len() as f64);
+ let deserialize_started = std::time::Instant::now();
+ let pdu = Pdu::$name(deserialize(decoded.data.as_slice(), decoded.is_compressed)?);
+ if decoded.data.len() > 64 * 1024 {
+ log::debug!(
+ "decode {} serial {}: {} bytes read in {:?}, deserialized in {:?}",
+ stringify!($name),
+ decoded.serial,
+ decoded.data.len(),
+ read_took,
+ deserialize_started.elapsed()
+ );
+ }
Ok(DecodedPdu {
serial: decoded.serial,
- pdu: Pdu::$name(deserialize(decoded.data.as_slice(), decoded.is_compressed)?)
+ pdu,
})
}
,)*
diff --git a/wezterm-ssh/src/sessioninner.rs b/wezterm-ssh/src/sessioninner.rs
index 4819b75c..52f44124 100644
--- a/wezterm-ssh/src/sessioninner.rs
+++ b/wezterm-ssh/src/sessioninner.rs
@@ -477,10 +477,18 @@ impl SessionInner {
let initial_sleep_delay = Duration::from_millis(100);
let max_sleep_delay = Duration::from_secs(2);
let immediate_wakeup = Duration::from_millis(10);
- let busy_loop_throttle = Duration::from_millis(50);
+ // Every event that could wake poll without there being anything to
+ // do is masked above; what remains is the odd spurious wakeup, and
+ // a long nap for one of those is throughput thrown away.
+ let busy_loop_throttle = Duration::from_millis(5);
let busy_loop_warn_every = Duration::from_secs(30);
let mut busy_loop_started: Option<Instant> = None;
let mut busy_loop_last_warn: Option<Instant> = None;
+ // Immediate wakeups in a row that moved nothing. One is ordinary:
+ // libssh reports its socket readable while it holds half a packet,
+ // and the read that completes the packet hands over no bytes yet.
+ // Napping on every such turn cost a busy channel most of its time.
+ let mut idle_wakes: u32 = 0;
loop {
self.do_keepalive(sess)?;
@@ -538,7 +546,16 @@ impl SessionInner {
poll_array.push(pollfd {
fd: fd.as_socket_descriptor(),
events: if fd_num == 0 {
- POLLIN
+ // Only while there is room to read into:
+ // with the stdin buffer full (libssh is
+ // not taking writes yet), a readable pipe
+ // woke poll at once for nothing, and the
+ // loop took itself for a busy loop.
+ if state.buf.len() < state.buf.capacity() {
+ POLLIN
+ } else {
+ 0
+ }
} else if !state.buf.is_empty() || info.exited {
POLLOUT
} else {
@@ -617,6 +634,9 @@ impl SessionInner {
if made_progress || !saw_revents || poll_elapsed >= immediate_wakeup {
busy_loop_started = None;
busy_loop_last_warn = None;
+ idle_wakes = 0;
+ } else if idle_wakes < 3 {
+ idle_wakes += 1;
} else {
// Repeated immediate wakeups with no IO progress. The
// throttle below already caps this at ~20 iterations/sec,