From b08a6ab5ef966c3b85e2fe02ca2735f72dc4b867 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 7 Jul 2021 17:21:18 -0700 Subject: [PATCH] instrument cache hit rates, fix duration histograms --- wezterm-gui/src/cache.rs | 45 ++++++++++++ wezterm-gui/src/glyphcache.rs | 16 ++++- wezterm-gui/src/main.rs | 1 + wezterm-gui/src/stats.rs | 104 ++++++++++++++++++++++++--- wezterm-gui/src/termwindow/mod.rs | 9 ++- wezterm-gui/src/termwindow/render.rs | 1 + wezterm-gui/src/update.rs | 2 +- 7 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 wezterm-gui/src/cache.rs diff --git a/wezterm-gui/src/cache.rs b/wezterm-gui/src/cache.rs new file mode 100644 index 000000000..d11bc8dd6 --- /dev/null +++ b/wezterm-gui/src/cache.rs @@ -0,0 +1,45 @@ +pub struct LruCache { + hit: &'static str, + miss: &'static str, + cache: lru::LruCache, +} + +impl LruCache { + pub fn new(hit: &'static str, miss: &'static str, cap: usize) -> Self { + Self { + hit, + miss, + cache: lru::LruCache::new(cap), + } + } + + pub fn get<'a, Q: ?Sized>(&'a mut self, k: &Q) -> Option<&'a V> + where + lru::KeyRef: std::borrow::Borrow, + Q: std::hash::Hash + Eq, + { + let result = self.cache.get(k); + metrics::histogram!( + if result.is_some() { + self.hit + } else { + self.miss + }, + 1. + ); + result + } +} + +impl std::ops::Deref for LruCache { + type Target = lru::LruCache; + fn deref(&self) -> &lru::LruCache { + &self.cache + } +} + +impl std::ops::DerefMut for LruCache { + fn deref_mut(&mut self) -> &mut lru::LruCache { + &mut self.cache + } +} diff --git a/wezterm-gui/src/glyphcache.rs b/wezterm-gui/src/glyphcache.rs index 80f4df89c..7f62485db 100644 --- a/wezterm-gui/src/glyphcache.rs +++ b/wezterm-gui/src/glyphcache.rs @@ -1,4 +1,5 @@ use super::utilsprites::RenderMetrics; +use crate::cache::LruCache; use ::window::bitmaps::atlas::OutOfTextureSpace; use ::window::bitmaps::atlas::{Atlas, Sprite}; #[cfg(test)] @@ -11,7 +12,6 @@ use ::window::glium::texture::SrgbTexture2d; use ::window::{Point, Rect}; use config::{AllowSquareGlyphOverflow, TextStyle}; use euclid::num::Zero; -use lru::LruCache; use std::collections::HashMap; use std::ops::Range; use std::rc::Rc; @@ -3830,7 +3830,11 @@ impl GlyphCache { Ok(Self { fonts: Rc::clone(fonts), glyph_cache: HashMap::new(), - image_cache: LruCache::new(16), + image_cache: LruCache::new( + "glyph_cache.image_cache.hit.rate", + "glyph_cache.image_cache.miss.rate", + 16, + ), frame_cache: HashMap::new(), atlas, metrics: metrics.clone(), @@ -3859,7 +3863,11 @@ impl GlyphCache { Ok(Self { fonts: Rc::clone(fonts), glyph_cache: HashMap::new(), - image_cache: LruCache::new(16), + image_cache: LruCache::new( + "glyph_cache.image_cache.hit.rate", + "glyph_cache.image_cache.miss.rate", + 16, + ), frame_cache: HashMap::new(), atlas, metrics: metrics.clone(), @@ -3895,8 +3903,10 @@ impl GlyphCache { }; if let Some(entry) = self.glyph_cache.get(&key as &dyn GlyphKeyTrait) { + metrics::histogram!("glyph_cache.glyph_cache.hit.rate", 1.); return Ok(Rc::clone(entry)); } + metrics::histogram!("glyph_cache.glyph_cache.miss.rate", 1.); let glyph = match self.load_glyph(info, style, followed_by_space) { Ok(g) => g, diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index bf421a4c6..fe8da7144 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -20,6 +20,7 @@ use wezterm_gui_subcommands::*; use wezterm_ssh::*; use wezterm_toast_notification::*; +mod cache; mod frontend; mod glyphcache; mod markdown; diff --git a/wezterm-gui/src/stats.rs b/wezterm-gui/src/stats.rs index 07010f5e7..41d46765e 100644 --- a/wezterm-gui/src/stats.rs +++ b/wezterm-gui/src/stats.rs @@ -9,18 +9,72 @@ use tabout::{tabulate_output, Alignment, Column}; static ENABLE_STAT_PRINT: AtomicBool = AtomicBool::new(true); -struct Inner { - histograms: HashMap>, +struct Throughput { + hist: Histogram, + last: Option, + count: u64, +} + +impl Throughput { + fn new() -> Self { + Self { + hist: Histogram::new(2).expect("failed to create histogram"), + last: None, + count: 0, + } + } + + fn add(&mut self, value: u64) { + if let Some(ref last) = self.last { + let elapsed = last.elapsed(); + if elapsed > Duration::from_secs(1) { + self.hist.record(self.count).ok(); + self.count = 0; + self.last = Some(Instant::now()); + } + } else { + // Start a new window + self.last = Some(Instant::now()); + }; + self.count += value; + } } fn pctile_latency(histogram: &Histogram, p: f64) -> Duration { Duration::from_nanos(histogram.value_at_percentile(p)) } +struct Inner { + histograms: HashMap>, + throughput: HashMap, +} + impl Inner { fn run(inner: Arc>) { let mut last_print = Instant::now(); + let rate_cols = vec![ + Column { + name: "STAT".to_string(), + alignment: Alignment::Left, + }, + Column { + name: "current".to_string(), + alignment: Alignment::Left, + }, + Column { + name: "p50".to_string(), + alignment: Alignment::Left, + }, + Column { + name: "p75".to_string(), + alignment: Alignment::Left, + }, + Column { + name: "p95".to_string(), + alignment: Alignment::Left, + }, + ]; let cols = vec![ Column { name: "STAT".to_string(), @@ -51,8 +105,27 @@ impl Inner { continue; } if last_print.elapsed() >= Duration::from_secs(seconds) { - let inner = inner.lock().unwrap(); let mut data = vec![]; + + let inner = inner.lock().unwrap(); + for (key, tput) in &inner.throughput { + let current = tput.count; + let p50 = tput.hist.value_at_percentile(50.); + let p75 = tput.hist.value_at_percentile(75.); + let p95 = tput.hist.value_at_percentile(95.); + data.push(vec![ + key.to_string(), + format!("{:.2?}", current), + format!("{:.2?}", p50), + format!("{:.2?}", p75), + format!("{:.2?}", p95), + ]); + } + data.sort_by(|a, b| a[0].cmp(&b[0])); + eprintln!(); + tabulate_output(&rate_cols, &data, &mut std::io::stderr().lock()).ok(); + + data.clear(); for (key, histogram) in &inner.histograms { if key.to_string().ends_with(".size") { let p50 = histogram.value_at_percentile(50.); @@ -94,6 +167,7 @@ impl Stats { Self { inner: Arc::new(Mutex::new(Inner { histograms: HashMap::new(), + throughput: HashMap::new(), })), } } @@ -137,10 +211,24 @@ impl Recorder for Stats { fn record_histogram(&self, key: &Key, value: f64) { let mut inner = self.inner.lock().unwrap(); - let histogram = inner - .histograms - .entry(key.clone()) - .or_insert_with(|| Histogram::new(2).expect("failed to crate new Histogram")); - histogram.record(value as u64).ok(); + if key.name().ends_with(".rate") { + let tput = inner + .throughput + .entry(key.clone()) + .or_insert_with(|| Throughput::new()); + tput.add(value as u64); + } else { + let value = if key.name().ends_with(".size") { + value + } else { + // Assume seconds; convert to nanoseconds + value * 1_000_000_000.0 + }; + let histogram = inner + .histograms + .entry(key.clone()) + .or_insert_with(|| Histogram::new(2).expect("failed to crate new Histogram")); + histogram.record(value as u64).ok(); + } } } diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 9ee606a51..b8303116d 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -2,6 +2,7 @@ use super::quad::*; use super::renderstate::*; use super::utilsprites::RenderMetrics; +use crate::cache::LruCache; use crate::glium::texture::SrgbTexture2d; use crate::overlay::{ confirm_close_pane, confirm_close_tab, confirm_close_window, confirm_quit_program, launcher, @@ -22,7 +23,6 @@ use config::keyassignment::{ ClipboardCopyDestination, ClipboardPasteSource, InputMap, KeyAssignment, SpawnCommand, }; use config::{configuration, ConfigHandle, WindowCloseConfirmation}; -use lru::LruCache; use luahelper::impl_lua_conversion; use mlua::FromLua; use mux::domain::{DomainId, DomainState}; @@ -479,7 +479,11 @@ impl TermWindow { current_mouse_button: None, last_mouse_click: None, current_highlight: None, - shape_cache: RefCell::new(LruCache::new(65536)), + shape_cache: RefCell::new(LruCache::new( + "shape_cache.hit.rate", + "shape_cache.miss.rate", + 65536, + )), last_blink_paint: Instant::now(), last_status_call: Instant::now(), event_states: HashMap::new(), @@ -836,6 +840,7 @@ impl TermWindow { } fn mux_pane_output_event(&mut self, pane_id: PaneId) { + metrics::histogram!("mux.pane_output_event.rate", 1.); if self.is_pane_visible(pane_id) { if let Some(ref win) = self.window { win.invalidate(); diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 3852cdff0..2f8422e6e 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -131,6 +131,7 @@ impl super::TermWindow { self.call_draw(frame).ok(); log::debug!("paint_impl elapsed={:?}", start.elapsed()); metrics::histogram!("gui.paint.opengl", start.elapsed()); + metrics::histogram!("gui.paint.opengl.rate", 1.); self.update_title_post_status(); } diff --git a/wezterm-gui/src/update.rs b/wezterm-gui/src/update.rs index dc835560d..280158a2e 100644 --- a/wezterm-gui/src/update.rs +++ b/wezterm-gui/src/update.rs @@ -1,5 +1,4 @@ use crate::ICON_DATA; -use std::convert::TryFrom; use anyhow::anyhow; use config::configuration; use config::wezterm_version; @@ -10,6 +9,7 @@ use portable_pty::PtySize; use regex::Regex; use serde::*; use std::collections::HashMap; +use std::convert::TryFrom; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::time::Duration;