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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
gui: count what the render caches really hold, on every frame
RoversX41e82c868dd0
diff --git a/thinkterm-render/src/quad.rs b/thinkterm-render/src/quad.rsindex 519c5f9b..a643490f 100644--- a/thinkterm-render/src/quad.rs+++ b/thinkterm-render/src/quad.rs@@ -768,11 +768,37 @@ impl HeapQuadAllocator {/// capacity, not length: a buffer that grew during recording keeps that/// allocation until it is dropped, and capacity is what a byte-accounted/// cache needs to know about.+ ///+ /// Counts the vectors only, so it is O(1) and does not see the corner+ /// gradient box a quad may own. That is what the per-line size checks+ /// want; a cache weighing what an entry really holds wants+ /// [`Self::cache_entry_bytes`] instead.pub fn resident_bytes(&self) -> usize {(self.layer0.capacity() + self.layer1.capacity() + self.layer2.capacity()).saturating_mul(std::mem::size_of::<BoxedQuad>())}+ /// Resident heap bytes including each quad's corner gradient box, which+ /// `BoxedQuad` holds behind its own allocation and `resident_bytes()`+ /// cannot see.+ ///+ /// Walks the quads, so this is for the once-per-insert weight of a+ /// byte-budgeted cache, not for a per-line check on the hot path. A+ /// gradient-free recording -- every terminal line today -- weighs the+ /// same either way; a gradient-bearing one is 64 bytes per quad heavier+ /// than the vectors suggest, which is what a byte budget must not miss.+ pub fn cache_entry_bytes(&self) -> usize {+ let gradients = self+ .layer0+ .iter()+ .chain(self.layer1.iter())+ .chain(self.layer2.iter())+ .filter(|quad| quad.fg_color_corners.is_some())+ .count();+ self.resident_bytes()+ .saturating_add(gradients.saturating_mul(std::mem::size_of::<[[f32; 4]; 4]>()))+ }+/// Where the next quad will land, for later use with [`Self::apply_before`],/// [`Self::apply_between`] and [`Self::apply_after`].pub fn mark(&self) -> HeapQuadMark {@@ -1238,6 +1264,44 @@ fn boxed_quad_preserves_vertical_gradient_colors() {assert_eq!(vertices[V_BOT_RIGHT].fg_color, bottom);}+#[cfg(test)]+#[test]+fn cache_entry_bytes_counts_the_corner_gradient_boxes_resident_bytes_cannot_see() {+ const QUADS: usize = 256;+ let mut heap = HeapQuadAllocator::default();+ {+ let layers = &mut heap;+ for _ in 0..QUADS {+ let mut quad = layers.allocate(0).unwrap();+ quad.set_position(0.0, 0.0, 1.0, 1.0);+ quad.set_corner_gradient(+ LinearRgba::with_components(1.0, 0.0, 0.0, 1.0),+ LinearRgba::with_components(0.0, 1.0, 0.0, 1.0),+ LinearRgba::with_components(0.0, 0.0, 1.0, 1.0),+ LinearRgba::with_components(1.0, 1.0, 0.0, 1.0),+ );+ }+ }+ let exact = heap.take_exact();+ let vectors = QUADS * std::mem::size_of::<BoxedQuad>();+ let boxes = QUADS * std::mem::size_of::<[[f32; 4]; 4]>();+ // The O(1) counterpart sees the vectors only, which is all the per-line+ // size check needs; the cache weight must see the boxes too, or a+ // gradient-bearing entry is charged 40% less than it holds.+ assert_eq!(exact.resident_bytes(), vectors);+ assert_eq!(exact.cache_entry_bytes(), vectors + boxes);+}++#[cfg(test)]+#[test]+fn cache_entry_bytes_matches_resident_bytes_without_gradients() {+ let mut heap = HeapQuadAllocator::default();+ record_quads(&mut heap, [5, 3, 2]);+ let exact = heap.take_exact();+ // Every terminal line today: the walk must not invent bytes.+ assert_eq!(exact.cache_entry_bytes(), exact.resident_bytes());+}+#[cfg(test)]#[test]fn boxed_quad_preserves_corner_gradient_colors() {diff --git a/wezterm-gui/src/termwindow/render/mod.rs b/wezterm-gui/src/termwindow/render/mod.rsindex d17c52a5..4a38dfd1 100644--- a/wezterm-gui/src/termwindow/render/mod.rs+++ b/wezterm-gui/src/termwindow/render/mod.rs@@ -155,12 +155,17 @@ pub const LINE_STATE_ENTRY_BYTES: usize =/// Estimated resident bytes for one `line_quad_cache` entry. The dominant/// term is the `HeapQuadAllocator` capacity.+///+/// `cache_entry_bytes()` rather than `resident_bytes()`: the entry is what+/// the byte budget holds, so a quad's corner gradient box counts even+/// though no line painter mints one today. The scratch's own size check+/// stays on the O(1) `resident_bytes()`.pub fn estimate_line_quad_entry_bytes(key: &LineQuadCacheKey, value: &LineQuadCacheValue) -> usize {CACHE_ENTRY_FIXED_OVERHEAD.saturating_add(std::mem::size_of::<LineQuadCacheKey>()).saturating_add(key.composing.as_ref().map_or(0, |s| s.capacity())).saturating_add(std::mem::size_of::<LineQuadCacheValue>())- .saturating_add(value.layers.resident_bytes())+ .saturating_add(value.layers.cache_entry_bytes())}/// Estimated resident bytes for one `line_to_ele_shape_cache` entry. The@@ -1489,4 +1494,43 @@ mod line_quad_cache_tests {+ quads);}++ #[test]+ fn a_gradient_bearing_entry_is_charged_for_its_gradient_boxes() {+ const QUADS: usize = 16;+ let mut scratch = HeapQuadAllocator::default();+ {+ let layers = &mut scratch;+ for _ in 0..QUADS {+ let mut quad = layers.allocate(0).unwrap();+ quad.set_position(0.0, 0.0, 8.0, 16.0);+ quad.set_corner_gradient(+ LinearRgba::with_components(1.0, 0.0, 0.0, 1.0),+ LinearRgba::with_components(0.0, 1.0, 0.0, 1.0),+ LinearRgba::with_components(0.0, 0.0, 1.0, 1.0),+ LinearRgba::with_components(1.0, 1.0, 0.0, 1.0),+ );+ }+ }+ let value = LineQuadCacheValue {+ expires: None,+ layers: scratch.take_exact(),+ current_highlight: None,+ invalidate_on_hover_change: false,+ };+ // No line painter mints a gradient today, so this is the guard for+ // the day one does: the budget must see the boxes, not just the+ // vectors, or the cache holds well past what it thinks it holds.+ let vectors = QUADS * std::mem::size_of::<crate::quad::BoxedQuad>();+ let boxes = QUADS * std::mem::size_of::<[[f32; 4]; 4]>();+ assert_eq!(value.layers.resident_bytes(), vectors);+ assert_eq!(+ estimate_line_quad_entry_bytes(&key(80, 800), &value),+ CACHE_ENTRY_FIXED_OVERHEAD+ + std::mem::size_of::<LineQuadCacheKey>()+ + std::mem::size_of::<LineQuadCacheValue>()+ + vectors+ + boxes+ );+ }}diff --git a/wezterm-gui/src/termwindow/render/paint.rs b/wezterm-gui/src/termwindow/render/paint.rsindex 070e72b5..3e60233e 100644--- a/wezterm-gui/src/termwindow/render/paint.rs+++ b/wezterm-gui/src/termwindow/render/paint.rs@@ -790,6 +790,127 @@ impl crate::TermWindow {}}+ /// The render memory series for one paint: how many passes it took, and+ /// what every CPU-side cache weighed when it finished.+ ///+ /// Called on every exit from `paint_impl`'s pass loop, not just the+ /// successful one. A frame that gave up at the atlas retry cap is+ /// precisely the frame a memory investigation wants to see, and emitting+ /// this only where the frame completed left a hole in the series across+ /// the blow-up. `frame_complete` distinguishes a capped frame from a+ /// healthy one, so absent-from-the-log and gave-up stay separable.+ ///+ /// Safe to read the caches here: `paint_pass()` has returned by the time+ /// this runs on any path, so nothing holds a borrow across it.+ fn log_render_memory_counters(&self, passes: usize, frame_complete: bool) {+ if !crate::perf::enabled() {+ return;+ }+ // Recorded even for the ordinary single-pass frame, so the counter+ // can be read as a distribution rather than as "something went+ // wrong once".+ crate::perf::log_counter("paint_passes", passes);+ if !frame_complete {+ // The pass loop gave up (retry cap or error). Marked rather+ // than left absent so a post-processor can tell the two apart.+ crate::perf::log_counter("paint_capped", 1);+ }+ // Entry counts alone cannot say whether the render caches' byte+ // budgets are set sensibly: these caches hold entries of wildly+ // different sizes. The weights are already computed on every+ // insert, and without these counters the only way to read them is+ // the settings window, which a scripted measurement cannot open.+ crate::perf::log_counter("shape_cache_bytes", self.shape_cache.borrow().total_weight());+ crate::perf::log_counter(+ "line_shape_cache_bytes",+ self.line_to_ele_shape_cache.borrow().total_weight(),+ );+ crate::perf::log_counter(+ "line_quad_cache_bytes",+ self.line_quad_cache.borrow().total_weight(),+ );+ crate::perf::log_counter(+ "line_state_cache_bytes",+ self.line_state_cache.borrow().total_weight(),+ );+ // The UI shape caches, the glyph map and the atlas are what the four+ // counters above cannot see, and together they are the larger half+ // of the render memory: note alone is budgeted 32 MiB, file_preview+ // 16, and the glyph map has no bound at all. Every value here is a+ // field read or a len(), so unlike the GPU allocator report below+ // this needs no throttle.+ {+ use crate::shapecache::UiTextDomain;+ let ui = self.ui_shape_caches.borrow();+ for domain in [+ UiTextDomain::Chrome,+ UiTextDomain::Note,+ UiTextDomain::FilePreview,+ ] {+ // The same name the diagnostics panel publishes this under+ // (`publish_ui_shape_cache_diagnostics`), taken from the+ // same table: the two surfaces have separate enable flags,+ // so both are needed, but one value must not have two names.+ crate::perf::log_counter(+ domain.gauge_names().bytes,+ ui.domain(domain).total_weight(),+ );+ }+ }+ // Skipped rather than logged as 0 if a line is somehow still being+ // recorded: a 0 would read as "empty", which is the healthy value.+ if let Ok(scratch) = self.line_quad_scratch.try_borrow() {+ crate::perf::log_counter("line_quad_scratch_bytes", scratch.resident_bytes());+ }+ // The overview's thumbnails: one recorded heap per live card, plus+ // the one rebuild that may be sliced across frames. Unlike the line+ // cache these are stored with their doubling slack and are dropped+ // only when the atlas is repacked, so they are the largest CPU-side+ // quad population nothing else here can see. Capacity only, like+ // every other figure in this block.+ {+ let cards = self.preview_quad_cache.borrow();+ let partial = self.preview_rebuild_partial.borrow();+ let card_bytes: usize = cards+ .values()+ .map(|entry| entry.heap.resident_bytes())+ .sum();+ // The rebuild owns the heap it took from its cache entry, so+ // summing the map alone under-reads exactly while one is in+ // flight.+ let partial_bytes = partial+ .as_ref()+ .map_or(0, |partial| partial.heap.resident_bytes());+ crate::perf::log_counter("preview_quad_cache_cards", cards.len());+ crate::perf::log_counter(+ "preview_quad_cache_bytes",+ card_bytes.saturating_add(partial_bytes),+ );+ }+ crate::perf::log_counter(+ "content_view_last_frame_bytes",+ self.content_view_last_frame+ .as_ref()+ .map_or(0, |heap| heap.resident_bytes()),+ );+ if let Some(render_state) = self.render_state.as_ref() {+ let glyphs = render_state.glyph_cache.borrow();+ crate::perf::log_counter("glyph_cache_entries", glyphs.glyph_entries());+ // Texel counts, not bytes: the atlas is side x side x 4 bytes,+ // and allocated_px is a high-water mark of packed area that only+ // Atlas::clear resets.+ crate::perf::log_counter("atlas_side_texels", glyphs.atlas.size());+ crate::perf::log_counter(+ "atlas_allocated_texels_high_water",+ glyphs.atlas.usage().allocated_px,+ );+ }+ // The GPU allocator is the other half of the picture and the cache+ // counters cannot see it. Throttled because generating the report+ // walks every live allocation.+ self.log_gpu_allocator_throttled();+ }+pub(crate) fn paint_impl(&mut self, frame: &mut RenderFrame) -> anyhow::Result<PaintOutcome> {self.num_frames += 1;// If nothing on screen needs animating, then we can avoid@@ -862,101 +983,18 @@ impl crate::TermWindow {// present undefined content, which is worse than a partial one.let mut present_frame = true;let mut frame_complete = false;+ // Held outside the loop so the memory counters can be emitted on+ // every way out of it, the retry cap and the error arms included.+ let mut passes_used = 0usize;'pass: for pass in 0.. {+ passes_used = pass + 1;self.frame_pane_output_generations.clear();self.track_pane_output_generations_for_frame = false;match self.paint_pass() {Ok(_) => match self.render_state.as_mut().unwrap().allocated_more_quads() {Ok(allocated) => {if !allocated {- // Recorded even for the ordinary single-pass frame,- // so the counter can be read as a distribution- // rather than as "something went wrong once".- crate::perf::log_counter("paint_passes", pass + 1);- // Entry counts alone cannot say whether the render- // caches' byte budgets are set sensibly: these- // caches hold entries of wildly different sizes.- // The weights are already computed on every insert,- // and without these counters the only way to read- // them is the settings window, which a scripted- // measurement cannot open.- if crate::perf::enabled() {- crate::perf::log_counter(- "shape_cache_bytes",- self.shape_cache.borrow().total_weight(),- );- crate::perf::log_counter(- "line_shape_cache_bytes",- self.line_to_ele_shape_cache.borrow().total_weight(),- );- crate::perf::log_counter(- "line_quad_cache_bytes",- self.line_quad_cache.borrow().total_weight(),- );- crate::perf::log_counter(- "line_state_cache_bytes",- self.line_state_cache.borrow().total_weight(),- );- // The UI shape caches, the glyph map and the- // atlas are what the four counters above- // cannot see, and together they are the larger- // half of the render memory: note alone is- // budgeted 32 MiB, file_preview 16, and the- // glyph map has no bound at all. Every value- // here is a field read or a len(), so unlike- // the GPU allocator report below this needs no- // throttle.- {- use crate::shapecache::UiTextDomain;- let ui = self.ui_shape_caches.borrow();- crate::perf::log_counter(- "ui_shape_chrome_bytes",- ui.domain(UiTextDomain::Chrome).total_weight(),- );- crate::perf::log_counter(- "ui_shape_note_bytes",- ui.domain(UiTextDomain::Note).total_weight(),- );- crate::perf::log_counter(- "ui_shape_file_preview_bytes",- ui.domain(UiTextDomain::FilePreview).total_weight(),- );- }- // Skipped rather than logged as 0 if a line is- // somehow still being recorded: a 0 would read- // as "empty", which is the healthy value.- if let Ok(scratch) = self.line_quad_scratch.try_borrow() {- crate::perf::log_counter(- "line_quad_scratch_bytes",- scratch.resident_bytes(),- );- }- if let Some(render_state) = self.render_state.as_ref() {- let glyphs = render_state.glyph_cache.borrow();- crate::perf::log_counter(- "glyph_cache_entries",- glyphs.glyph_entries(),- );- // Texel counts, not bytes: the atlas is- // side x side x 4 bytes, and allocated_px- // is a high-water mark of packed area- // that only Atlas::clear resets.- crate::perf::log_counter(- "atlas_side_texels",- glyphs.atlas.size(),- );- crate::perf::log_counter(- "atlas_allocated_texels_high_water",- glyphs.atlas.usage().allocated_px,- );- }- // The GPU allocator is the other half of the- // picture and the cache counters cannot see it.- // Throttled because generating the report walks- // every live allocation.- self.log_gpu_allocator_throttled();- }frame_complete = true;break 'pass;}@@ -1141,6 +1179,10 @@ impl crate::TermWindow {}}}+ // After the loop rather than inside its successful arm: every exit+ // above reaches here, so a frame that gave up still reports what the+ // caches weighed when it did.+ self.log_render_memory_counters(passes_used, frame_complete);log::debug!("paint_impl before call_draw elapsed={:?}", start.elapsed());// One paint, presented or not: retire dedicated textures nobody