diff --git a/Cargo.lock b/Cargo.lock index 6982793..c8faaff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -172,7 +172,7 @@ dependencies = [ "objc2-foundation 0.3.1", "parking_lot", "percent-encoding", - "windows-sys 0.52.0", + "windows-sys 0.59.0", "x11rb", ] @@ -1358,7 +1358,7 @@ version = "0.31.1" source = "git+https://github.com/damus-io/egui?rev=73a831ed43d3a8592611e2948b505add88d8aba2#73a831ed43d3a8592611e2948b505add88d8aba2" dependencies = [ "bytemuck", - "emath", + "emath 0.31.1 (git+https://github.com/damus-io/egui?rev=73a831ed43d3a8592611e2948b505add88d8aba2)", "serde", ] @@ -1407,7 +1407,7 @@ dependencies = [ "ahash", "backtrace", "bitflags 2.9.1", - "emath", + "emath 0.31.1 (git+https://github.com/damus-io/egui?rev=73a831ed43d3a8592611e2948b505add88d8aba2)", "epaint", "log", "nohash-hasher", @@ -1533,6 +1533,12 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "emath" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e4cadcff7a5353ba72b7fea76bf2122b5ebdbc68e8155aa56dfdea90083fe1b" + [[package]] name = "emath" version = "0.31.1" @@ -1622,6 +1628,15 @@ dependencies = [ "syn 2.0.103", ] +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", +] + [[package]] name = "epaint" version = "0.31.1" @@ -1631,7 +1646,7 @@ dependencies = [ "ahash", "bytemuck", "ecolor", - "emath", + "emath 0.31.1 (git+https://github.com/damus-io/egui?rev=73a831ed43d3a8592611e2948b505add88d8aba2)", "epaint_default_fonts", "log", "nohash-hasher", @@ -2625,6 +2640,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.15" @@ -2855,6 +2879,15 @@ version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +[[package]] +name = "log-once" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d8a05e3879b317b1b6dbf353e5bba7062bedcc59815267bb23eaa0c576cebf0" +dependencies = [ + "log", +] + [[package]] name = "loop9" version = "0.1.5" @@ -2928,6 +2961,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "memory-stats" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c73f5c649995a115e1a0220b35e4df0a1294500477f97a91d0660fb5abeb574a" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "metal" version = "0.31.0" @@ -3275,6 +3318,7 @@ dependencies = [ "profiling", "puffin", "puffin_egui", + "re_memory", "serde", "serde_json", "strum", @@ -3386,6 +3430,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -4426,6 +4479,60 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b42e27ef78c35d3998403c1d26f3efd9e135d3e5121b0a4845cc5cc27547f4f" +[[package]] +name = "re_format" +version = "0.23.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2911aed8ef5b658871fc109072e056d77e8a8af9c438f9662ed5c88ac6850313" +dependencies = [ + "num-traits", +] + +[[package]] +name = "re_log" +version = "0.23.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1179e5409f57aebeedcea10d5cc4bea2bf5a9f237901501cb9a0e505ae06112f" +dependencies = [ + "env_filter", + "log", + "log-once", + "parking_lot", + "tracing", +] + +[[package]] +name = "re_memory" +version = "0.23.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ee6d1efd5f0478116ca816ee1689995c7878107fe0ee06b7aecee8cd454ce00" +dependencies = [ + "ahash", + "backtrace", + "emath 0.31.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.14.0", + "memory-stats", + "nohash-hasher", + "once_cell", + "parking_lot", + "re_format", + "re_log", + "re_tracing", + "smallvec", + "sysinfo", + "wasm-bindgen", + "web-time 1.1.0", +] + +[[package]] +name = "re_tracing" +version = "0.23.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f4ddc0564f0ae8fbe75a5ade3c66c9ddecf51bfcdf6aa4fa8b9ccce695a8fc" +dependencies = [ + "puffin", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -5252,6 +5359,20 @@ dependencies = [ "syn 2.0.103", ] +[[package]] +name = "sysinfo" +version = "0.30.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a5b4ddaee55fb2bea2bf0e5000747e5f5c0de765e5a5ff87f4cd106439f4bb3" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "windows 0.52.0", +] + [[package]] name = "system-deps" version = "6.2.2" @@ -6351,7 +6472,7 @@ dependencies = [ "wasm-bindgen", "web-sys", "wgpu-types", - "windows", + "windows 0.58.0", ] [[package]] @@ -6409,6 +6530,16 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core 0.52.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows" version = "0.58.0" @@ -6419,6 +6550,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-core" version = "0.58.0" diff --git a/Cargo.toml b/Cargo.toml index d603e7e..a0f5cb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ lightning-invoice = "0.33.1" secp256k1 = "0.30.0" hashbrown = "0.15.2" openai-api-rs = "6.0.3" +re_memory = "0.23.4" [profile.small] inherits = 'release' diff --git a/crates/notedeck_chrome/Cargo.toml b/crates/notedeck_chrome/Cargo.toml index be2a8a8..63d54a2 100644 --- a/crates/notedeck_chrome/Cargo.toml +++ b/crates/notedeck_chrome/Cargo.toml @@ -28,6 +28,7 @@ tracing-appender = { workspace = true } tracing-subscriber = { workspace = true } tracing = { workspace = true } profiling = { workspace = true } +re_memory = { workspace = true, optional = true } [dev-dependencies] tempfile = { workspace = true } @@ -45,6 +46,7 @@ path = "src/notedeck.rs" [features] default = [] +memory = ["re_memory"] puffin = ["profiling/profile-with-puffin", "dep:puffin"] debug-widget-callstack = ["egui/callstack"] debug-interactive-widgets = [] diff --git a/crates/notedeck_chrome/src/chrome.rs b/crates/notedeck_chrome/src/chrome.rs index 9a3754f..e1042c1 100644 --- a/crates/notedeck_chrome/src/chrome.rs +++ b/crates/notedeck_chrome/src/chrome.rs @@ -19,6 +19,9 @@ pub struct Chrome { open: bool, tab_selected: i32, apps: Vec, + + #[cfg(feature = "memory")] + show_memory_debug: bool, } impl Default for Chrome { @@ -28,6 +31,9 @@ impl Default for Chrome { tab_selected: 0, open: true, apps: vec![], + + #[cfg(feature = "memory")] + show_memory_debug: false, } } } @@ -220,7 +226,7 @@ impl Chrome { }); ui.with_layout(Layout::bottom_up(egui::Align::Center), |ui| { - if let Some(action) = bottomup_sidebar(app_ctx, ui) { + if let Some(action) = bottomup_sidebar(self, app_ctx, ui) { got_action = Some(action); } }); @@ -656,7 +662,11 @@ fn pfp_button(ctx: &mut AppContext, ui: &mut egui::Ui) -> egui::Response { /// The section of the chrome sidebar that starts at the /// bottom and goes up -fn bottomup_sidebar(ctx: &mut AppContext, ui: &mut egui::Ui) -> Option { +fn bottomup_sidebar( + _chrome: &mut Chrome, + ctx: &mut AppContext, + ui: &mut egui::Ui, +) -> Option { ui.add_space(8.0); let pfp_resp = pfp_button(ctx, ui); @@ -701,6 +711,26 @@ fn bottomup_sidebar(ctx: &mut AppContext, ui: &mut egui::Ui) -> Option Option>().join("\n")); + }); + } + }); +} + +/// Pretty format a number of bytes by using SI notation (base2), e.g. +/// +/// ``` +/// # use re_format::format_bytes; +/// assert_eq!(format_bytes(123.0), "123 B"); +/// assert_eq!(format_bytes(12_345.0), "12.1 KiB"); +/// assert_eq!(format_bytes(1_234_567.0), "1.2 MiB"); +/// assert_eq!(format_bytes(123_456_789.0), "118 MiB"); +/// ``` +#[cfg(feature = "memory")] +pub fn format_bytes(number_of_bytes: f64) -> String { + /// The minus character: + /// Looks slightly different from the normal hyphen `-`. + const MINUS: char = '−'; + + if number_of_bytes < 0.0 { + format!("{MINUS}{}", format_bytes(-number_of_bytes)) + } else if number_of_bytes == 0.0 { + "0 B".to_owned() + } else if number_of_bytes < 1.0 { + format!("{number_of_bytes} B") + } else if number_of_bytes < 20.0 { + let is_integer = number_of_bytes.round() == number_of_bytes; + if is_integer { + format!("{number_of_bytes:.0} B") + } else { + format!("{number_of_bytes:.1} B") + } + } else if number_of_bytes < 10.0_f64.exp2() { + format!("{number_of_bytes:.0} B") + } else if number_of_bytes < 20.0_f64.exp2() { + let decimals = (10.0 * number_of_bytes < 20.0_f64.exp2()) as usize; + format!("{:.*} KiB", decimals, number_of_bytes / 10.0_f64.exp2()) + } else if number_of_bytes < 30.0_f64.exp2() { + let decimals = (10.0 * number_of_bytes < 30.0_f64.exp2()) as usize; + format!("{:.*} MiB", decimals, number_of_bytes / 20.0_f64.exp2()) + } else { + let decimals = (10.0 * number_of_bytes < 40.0_f64.exp2()) as usize; + format!("{:.*} GiB", decimals, number_of_bytes / 30.0_f64.exp2()) + } +} diff --git a/crates/notedeck_chrome/src/notedeck.rs b/crates/notedeck_chrome/src/notedeck.rs index 25eb439..a566a5f 100644 --- a/crates/notedeck_chrome/src/notedeck.rs +++ b/crates/notedeck_chrome/src/notedeck.rs @@ -1,6 +1,14 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release +#[cfg(feature = "memory")] +use re_memory::AccountingAllocator; + +#[cfg(feature = "memory")] +#[global_allocator] +static GLOBAL: AccountingAllocator = + AccountingAllocator::new(std::alloc::System); + use notedeck::{DataPath, DataPathType, Notedeck}; use notedeck_chrome::{ setup::{generate_native_options, setup_chrome}, @@ -66,6 +74,9 @@ fn setup_logging(path: &DataPath) -> Option { #[cfg(not(target_arch = "wasm32"))] #[tokio::main] async fn main() { + #[cfg(feature = "memory")] + re_memory::accounting_allocator::set_tracking_callstacks(true); + let base_path = DataPath::default_base_or_cwd(); let path = DataPath::new(base_path.clone());