From 14ed35758eb29cc00a133e8296daa68260259080 Mon Sep 17 00:00:00 2001 From: Timon Date: Wed, 28 Sep 2022 16:20:47 +0200 Subject: [PATCH] Show/hide flamegraph puffin threads --- Cargo.lock | 19 +++++++- puffin_egui/Cargo.toml | 1 + puffin_egui/src/flamegraph.rs | 81 +++++++++++++++++++++++++---------- 3 files changed, 77 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da2fa7d4..f7bc2404 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1017,7 +1017,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c75712fff1702bac51b7eaa5a5ca9f9853b8055ef5906088a32f4fe196595a1d" dependencies = [ - "hashbrown", + "hashbrown 0.9.1", "ttf-parser 0.12.3", ] @@ -1314,6 +1314,12 @@ dependencies = [ "ahash 0.4.7", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "heck" version = "0.3.3" @@ -1417,6 +1423,16 @@ dependencies = [ "winit 0.25.0", ] +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + [[package]] name = "instant" version = "0.1.12" @@ -2287,6 +2303,7 @@ dependencies = [ "eframe", "egui", "egui-macroquad", + "indexmap", "macroquad", "natord", "once_cell", diff --git a/puffin_egui/Cargo.toml b/puffin_egui/Cargo.toml index 0db11536..62191e56 100644 --- a/puffin_egui/Cargo.toml +++ b/puffin_egui/Cargo.toml @@ -28,6 +28,7 @@ once_cell = "1.7" puffin = { version = "0.13.3", path = "../puffin", features = ["packing"] } serde = { version = "1.0", features = ["derive"], optional = true } vec1 = "1.8" +indexmap = "1.9.1" [dev-dependencies] eframe = "0.18.0" diff --git a/puffin_egui/src/flamegraph.rs b/puffin_egui/src/flamegraph.rs index d2f2ae5c..3925e66f 100644 --- a/puffin_egui/src/flamegraph.rs +++ b/puffin_egui/src/flamegraph.rs @@ -1,5 +1,10 @@ +use std::{ + vec, +}; + use super::{SelectedFrames, ERROR_COLOR, HOVER_COLOR}; use egui::*; +use indexmap::IndexMap; use puffin::*; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -132,6 +137,8 @@ pub struct Options { pub sorting: Sorting, + show_flamegraph_threads: IndexMap, + #[cfg_attr(feature = "serde", serde(skip))] filter: Filter, @@ -164,6 +171,7 @@ impl Default for Options { filter: Default::default(), zoom_to_relative_ns_range: None, + show_flamegraph_threads: IndexMap::new(), } } } @@ -224,35 +232,54 @@ pub fn ui(ui: &mut egui::Ui, options: &mut Options, frames: &SelectedFrames) { ui.memory().data.insert_temp(num_frames_id, num_frames); } - ui.horizontal(|ui| { - ui.horizontal(|ui| { - let changed = ui - .checkbox(&mut options.merge_scopes, "Merge children with same ID") - .changed(); - - // If we have multiple frames selected this will toggle - // if we view all the frames, or an average of them, - // and that difference is pretty massive, so help the user: - if changed && num_frames > 1 { - reset_view = true; - } - }); - ui.separator(); - ui.colored_label(ui.visuals().widgets.inactive.text_color(), "Help!") - .on_hover_text( - "Drag to pan.\n\ + ui.columns(2, |ui| { + ui[0].set_width(80.0); + + egui::ScrollArea::vertical() + .id_source("first_s") + .max_height(150.0) + .show(&mut ui[0], |ui| { + ui.label("show threads:"); + for f in frames.threads.keys() { + let entry = options + .show_flamegraph_threads + .entry(f.name.clone()) + .or_insert(true); + ui.checkbox(entry, f.name.clone()); + } + }); + + egui::ScrollArea::vertical() + .id_source("second_s") + .max_height(150.0) + .show(&mut ui[1], |ui| { + let changed = ui + .checkbox(&mut options.merge_scopes, "Merge children with same ID") + .changed(); + // If we have multiple frames selected this will toggle + // if we view all the frames, or an average of them, + // and that difference is pretty massive, so help the user: + if changed && num_frames > 1 { + reset_view = true; + } + + ui.colored_label(ui.visuals().widgets.inactive.text_color(), "Help!") + .on_hover_text( + "Drag to pan.\n\ Zoom: Ctrl/cmd + scroll, or drag with secondary mouse button.\n\ Click on a scope to zoom to it.\n\ Double-click to reset view.\n\ Press spacebar to pause/resume.", - ); - ui.separator(); + ); + + // The number of threads can change between frames, so always show this even if there currently is only one thread: + options.sorting.ui(ui); - // The number of threads can change between frames, so always show this even if there currently is only one thread: - options.sorting.ui(ui); + options.filter.ui(ui); + }); }); - options.filter.ui(ui); + ui.separator(); Frame::dark_canvas(ui.style()).show(ui, |ui| { let available_height = ui.max_rect().bottom() - ui.min_rect().bottom(); @@ -326,13 +353,21 @@ fn ui_canvas( let threads = options.sorting.sort(threads); for thread_info in threads { + if !*options + .show_flamegraph_threads + .get(&thread_info.name) + .unwrap_or(&false) + { + continue; + } + // Visual separator between threads: cursor_y += 2.0; let line_y = cursor_y; cursor_y += 2.0; let text_pos = pos2(info.canvas.min.x, cursor_y); - paint_thread_info(info, &thread_info, text_pos); + paint_thread_info(info, &thread_info, text_pos); cursor_y += info.text_height; // draw on top of thread info background: