From 7ce845210d3af82a57a7518e0abe8c167d60cc6a Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sat, 17 Jan 2026 20:29:19 +0100 Subject: [PATCH] ztracing: Enable memory profiling and callstack sampling (#47052) While at it, annotate more functions that are potentially related to language parsing in buffers. Also, on macOS, in order to actually have callstack frames properly recorded by Tracy, you need to manually run `dsymutil` on the binary. Release Notes: - N/A --- Cargo.lock | 13 +++++++ crates/buffer_diff/Cargo.toml | 5 +++ crates/buffer_diff/src/buffer_diff.rs | 2 + crates/extension/Cargo.toml | 5 +++ crates/extension/src/extension_host_proxy.rs | 2 + crates/extension_host/Cargo.toml | 5 +++ crates/extension_host/src/extension_host.rs | 1 + crates/git_ui/src/project_diff.rs | 1 + crates/language/Cargo.toml | 5 +++ crates/language/src/buffer.rs | 6 +++ crates/language/src/language_registry.rs | 4 ++ crates/language/src/syntax_map.rs | 18 ++++++--- crates/language_extension/Cargo.toml | 5 +++ .../src/language_extension.rs | 1 + crates/multi_buffer/src/multi_buffer.rs | 2 + crates/project/src/buffer_store.rs | 3 ++ crates/project/src/git_store.rs | 4 ++ crates/project/src/project.rs | 2 + crates/worktree/Cargo.toml | 5 +++ crates/worktree/src/worktree.rs | 1 + crates/ztracing/Cargo.toml | 3 +- crates/ztracing/build.rs | 7 +++- crates/ztracing/src/lib.rs | 37 ++++++++++++++++++- 23 files changed, 127 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c05093a1eb84a57019b1a3639053b322524c0970..7d216658353c692613499640b04c591c60b6d033 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2373,9 +2373,11 @@ dependencies = [ "settings", "sum_tree", "text", + "tracing", "unindent", "util", "zlog", + "ztracing", ] [[package]] @@ -5972,10 +5974,12 @@ dependencies = [ "task", "tempfile", "toml 0.8.23", + "tracing", "url", "util", "wasm-encoder 0.221.3", "wasmparser 0.221.3", + "ztracing", ] [[package]] @@ -6044,12 +6048,14 @@ dependencies = [ "theme", "theme_extension", "toml 0.8.23", + "tracing", "url", "util", "wasmparser 0.221.3", "wasmtime", "wasmtime-wasi", "zlog", + "ztracing", ] [[package]] @@ -8929,6 +8935,7 @@ dependencies = [ "text", "theme", "toml 0.8.23", + "tracing", "tree-sitter", "tree-sitter-elixir", "tree-sitter-embedded-template", @@ -8945,6 +8952,7 @@ dependencies = [ "util", "watch", "zlog", + "ztracing", ] [[package]] @@ -8964,7 +8972,9 @@ dependencies = [ "project", "serde", "serde_json", + "tracing", "util", + "ztracing", ] [[package]] @@ -20419,8 +20429,10 @@ dependencies = [ "smol", "sum_tree", "text", + "tracing", "util", "zlog", + "ztracing", ] [[package]] @@ -21351,6 +21363,7 @@ dependencies = [ "tracing", "tracing-subscriber", "tracing-tracy", + "tracy-client", "zlog", "ztracing_macro", ] diff --git a/crates/buffer_diff/Cargo.toml b/crates/buffer_diff/Cargo.toml index e0c56c6d3d8d71b1749d0fdc99ef515511fe5196..06cb6cfa76c66c2d5a7b3b4197566cdef3e0c18c 100644 --- a/crates/buffer_diff/Cargo.toml +++ b/crates/buffer_diff/Cargo.toml @@ -26,7 +26,9 @@ rope.workspace = true settings = { workspace = true, optional = true } sum_tree.workspace = true text.workspace = true +tracing.workspace = true util.workspace = true +ztracing.workspace = true [dev-dependencies] ctor.workspace = true @@ -37,3 +39,6 @@ settings.workspace = true text = { workspace = true, features = ["test-support"] } unindent.workspace = true zlog.workspace = true + +[package.metadata.cargo-machete] +ignored = ["tracing"] diff --git a/crates/buffer_diff/src/buffer_diff.rs b/crates/buffer_diff/src/buffer_diff.rs index e80cf250a4272c58732ab7c4db9d7d472212a6c2..385edad20ff0e385fe76c107576ee311b8d81357 100644 --- a/crates/buffer_diff/src/buffer_diff.rs +++ b/crates/buffer_diff/src/buffer_diff.rs @@ -216,6 +216,7 @@ impl BufferDiffSnapshot { self.secondary_diff.as_deref() } + #[ztracing::instrument(skip_all)] pub fn hunks_intersecting_range<'a>( &'a self, range: Range, @@ -1279,6 +1280,7 @@ impl BufferDiff { }) } + #[ztracing::instrument(skip_all)] pub fn language_changed( &mut self, language: Option>, diff --git a/crates/extension/Cargo.toml b/crates/extension/Cargo.toml index 307a3a19bd5ec6502270ae2f579cbd6b6f378746..71477f86b4d2110864530171e947306b57bd1047 100644 --- a/crates/extension/Cargo.toml +++ b/crates/extension/Cargo.toml @@ -31,10 +31,12 @@ serde.workspace = true serde_json.workspace = true task.workspace = true toml.workspace = true +tracing.workspace = true url.workspace = true util.workspace = true wasm-encoder.workspace = true wasmparser.workspace = true +ztracing.workspace = true [dev-dependencies] fs = { workspace = true, "features" = ["test-support"] } @@ -42,3 +44,6 @@ gpui = { workspace = true, "features" = ["test-support"] } indoc.workspace = true pretty_assertions.workspace = true tempfile.workspace = true + +[package.metadata.cargo-machete] +ignored = ["tracing"] diff --git a/crates/extension/src/extension_host_proxy.rs b/crates/extension/src/extension_host_proxy.rs index b445878389015d4b3b8c3e25a0d103586462fd86..5efc015483f10915a2e6646c3359bd47b90cfe69 100644 --- a/crates/extension/src/extension_host_proxy.rs +++ b/crates/extension/src/extension_host_proxy.rs @@ -223,6 +223,7 @@ pub trait ExtensionGrammarProxy: Send + Sync + 'static { } impl ExtensionGrammarProxy for ExtensionHostProxy { + #[ztracing::instrument(skip_all)] fn register_grammars(&self, grammars: Vec<(Arc, PathBuf)>) { let Some(proxy) = self.grammar_proxy.read().clone() else { return; @@ -250,6 +251,7 @@ pub trait ExtensionLanguageProxy: Send + Sync + 'static { } impl ExtensionLanguageProxy for ExtensionHostProxy { + #[ztracing::instrument(skip_all, fields(lang = language.0.as_str()))] fn register_language( &self, language: LanguageName, diff --git a/crates/extension_host/Cargo.toml b/crates/extension_host/Cargo.toml index 328b808b1310e3402405c52ce27a8ae15c4d5ece..2305bf5e2a19c446e810a68c3cf80e7af36ff8e9 100644 --- a/crates/extension_host/Cargo.toml +++ b/crates/extension_host/Cargo.toml @@ -47,11 +47,13 @@ task.workspace = true telemetry.workspace = true tempfile.workspace = true toml.workspace = true +tracing.workspace = true url.workspace = true util.workspace = true wasmparser.workspace = true wasmtime-wasi.workspace = true wasmtime.workspace = true +ztracing.workspace = true [dev-dependencies] criterion.workspace = true @@ -71,3 +73,6 @@ zlog.workspace = true [[bench]] name = "extension_compilation_benchmark" harness = false + +[package.metadata.cargo-machete] +ignored = ["tracing"] diff --git a/crates/extension_host/src/extension_host.rs b/crates/extension_host/src/extension_host.rs index 600e87a71338aa56296a52909cdb84c44458009d..219b72f92d1588422c6eec818fd3f73acd044907 100644 --- a/crates/extension_host/src/extension_host.rs +++ b/crates/extension_host/src/extension_host.rs @@ -1074,6 +1074,7 @@ impl ExtensionStore { /// no longer in the manifest, or whose files have changed on disk. /// Then it loads any themes, languages, or grammars that are newly /// added to the manifest, or whose files have changed on disk. + #[ztracing::instrument(skip_all)] fn extensions_updated( &mut self, mut new_index: ExtensionIndex, diff --git a/crates/git_ui/src/project_diff.rs b/crates/git_ui/src/project_diff.rs index af6f5a587a7d99e2e8040e875c3aa011949bedd7..7d8c439e85344e1599da8b68d638e27e1d319f1b 100644 --- a/crates/git_ui/src/project_diff.rs +++ b/crates/git_ui/src/project_diff.rs @@ -690,6 +690,7 @@ impl ProjectDiff { } } + #[instrument(skip_all)] pub async fn refresh( this: WeakEntity, reason: RefreshReason, diff --git a/crates/language/Cargo.toml b/crates/language/Cargo.toml index 06d41e729bfabbf4f7e050409d2675dd909941d6..58db79afe59f0e6d27e23eceb9861ea493d853fd 100644 --- a/crates/language/Cargo.toml +++ b/crates/language/Cargo.toml @@ -62,6 +62,7 @@ sum_tree.workspace = true task.workspace = true text.workspace = true theme.workspace = true +tracing.workspace = true tree-sitter-md = { workspace = true, optional = true } tree-sitter-python = { workspace = true, optional = true } tree-sitter-rust = { workspace = true, optional = true } @@ -71,6 +72,7 @@ unicase = "2.6" util.workspace = true watch.workspace = true zlog.workspace = true +ztracing.workspace = true diffy = "0.4.2" [dev-dependencies] @@ -98,3 +100,6 @@ toml.workspace = true unindent.workspace = true util = { workspace = true, features = ["test-support"] } zlog.workspace = true + +[package.metadata.cargo-machete] +ignored = ["tracing"] diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 8640288d563ef9e0d577896a57a2e3c5ab2e30f3..94093ee3b180d0dbbff0b00066a2c86ef59fc332 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -1087,6 +1087,7 @@ impl Buffer { } /// Assign a language to the buffer, blocking for up to 1ms to reparse the buffer, returning the buffer. + #[ztracing::instrument(skip_all, fields(lang = language.config.name.0.as_str()))] pub fn with_language(mut self, language: Arc, cx: &mut Context) -> Self { self.set_language(Some(language), cx); self @@ -1414,6 +1415,7 @@ impl Buffer { /// Retrieve a snapshot of the buffer's raw text, without any /// language-related state like the syntax tree or diagnostics. + #[ztracing::instrument(skip_all)] pub fn text_snapshot(&self) -> text::BufferSnapshot { self.text.snapshot() } @@ -1463,6 +1465,7 @@ impl Buffer { self.set_language_(language, true, cx); } + #[ztracing::instrument(skip_all)] fn set_language_( &mut self, language: Option>, @@ -1745,6 +1748,7 @@ impl Buffer { /// initiate an additional reparse recursively. To avoid concurrent parses /// for the same buffer, we only initiate a new parse if we are not already /// parsing in the background. + #[ztracing::instrument(skip_all)] pub fn reparse(&mut self, cx: &mut Context, may_block: bool) { if self.text.version() != *self.tree_sitter_data.version() { self.invalidate_tree_sitter_data(self.text.snapshot()); @@ -3590,6 +3594,7 @@ impl BufferSnapshot { None } + #[ztracing::instrument(skip_all)] fn get_highlights(&self, range: Range) -> (SyntaxMapCaptures<'_>, Vec) { let captures = self.syntax.captures(range, &self.text, |grammar| { grammar @@ -3609,6 +3614,7 @@ impl BufferSnapshot { /// in an arbitrary way due to being stored in a [`Rope`](text::Rope). The text is also /// returned in chunks where each chunk has a single syntax highlighting style and /// diagnostic status. + #[ztracing::instrument(skip_all)] pub fn chunks(&self, range: Range, language_aware: bool) -> BufferChunks<'_> { let range = range.start.to_offset(self)..range.end.to_offset(self); diff --git a/crates/language/src/language_registry.rs b/crates/language/src/language_registry.rs index 9dff354fa32ad09c15f8d9f5dfccbe907d9fb2e3..53906ab3bcd740191c4cf2abd497e73a5397ac31 100644 --- a/crates/language/src/language_registry.rs +++ b/crates/language/src/language_registry.rs @@ -922,6 +922,7 @@ impl LanguageRegistry { available_language } + #[ztracing::instrument(skip_all)] pub fn load_language( self: &Arc, language: &AvailableLanguage, @@ -1009,6 +1010,7 @@ impl LanguageRegistry { rx } + #[ztracing::instrument(skip_all)] fn get_or_load_language( self: &Arc, callback: impl Fn( @@ -1030,6 +1032,8 @@ impl LanguageRegistry { self: &Arc, name: Arc, ) -> impl Future> { + let span = ztracing::debug_span!("get_or_load_grammar", name = &*name.clone()); + let _enter = span.enter(); let (tx, rx) = oneshot::channel(); let mut state = self.state.write(); diff --git a/crates/language/src/syntax_map.rs b/crates/language/src/syntax_map.rs index 07dacd5216fc0f437f588f34b11e2a2acf715502..272f37353fe474b9fc596a210cb9ca7a14e8c90b 100644 --- a/crates/language/src/syntax_map.rs +++ b/crates/language/src/syntax_map.rs @@ -21,7 +21,7 @@ use sum_tree::{Bias, Dimensions, SeekTarget, SumTree}; use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, Rope, ToOffset, ToPoint}; use tree_sitter::{ Node, Query, QueryCapture, QueryCaptures, QueryCursor, QueryMatch, QueryMatches, - QueryPredicateArg, Tree, + QueryPredicateArg, }; pub const MAX_BYTES_TO_QUERY: usize = 16 * 1024; @@ -117,7 +117,7 @@ impl SyntaxLayerContent { } } - fn tree(&self) -> Option<&Tree> { + fn tree(&self) -> Option<&tree_sitter::Tree> { match self { SyntaxLayerContent::Parsed { tree, .. } => Some(tree), SyntaxLayerContent::Pending { .. } => None, @@ -133,7 +133,7 @@ pub struct SyntaxLayer<'a> { pub language: &'a Arc, pub included_sub_ranges: Option<&'a [Range]>, pub(crate) depth: usize, - tree: &'a Tree, + tree: &'a tree_sitter::Tree, pub(crate) offset: (usize, tree_sitter::Point), } @@ -296,6 +296,7 @@ impl SyntaxSnapshot { self.update_count } + #[ztracing::instrument(skip_all)] pub fn interpolate(&mut self, text: &BufferSnapshot) { let edits = text .anchored_edits_since::>(&self.interpolated_version) @@ -434,6 +435,7 @@ impl SyntaxSnapshot { self.reparse_(text, registry, root_language, Some(budget)) } + #[ztracing::instrument(skip_all, fields(lang = root_language.config.name.0.as_str()))] fn reparse_( &mut self, text: &BufferSnapshot, @@ -497,6 +499,7 @@ impl SyntaxSnapshot { Ok(()) } + #[ztracing::instrument(skip_all)] fn reparse_with_ranges( &mut self, text: &BufferSnapshot, @@ -885,7 +888,7 @@ impl SyntaxSnapshot { pub fn single_tree_captures<'a>( range: Range, text: &'a Rope, - tree: &'a Tree, + tree: &'a tree_sitter::Tree, language: &'a Arc, query: fn(&Grammar) -> Option<&Query>, ) -> SyntaxMapCaptures<'a> { @@ -1422,14 +1425,15 @@ impl std::fmt::Display for ParseTimeout { } } +#[ztracing::instrument(skip_all)] fn parse_text( grammar: &Grammar, text: &Rope, start_byte: usize, ranges: &[tree_sitter::Range], - old_tree: Option, + old_tree: Option, parse_budget: &mut Option, -) -> anyhow::Result { +) -> anyhow::Result { with_parser(|parser| { let mut timed_out = false; let now = Instant::now(); @@ -1474,6 +1478,7 @@ fn parse_text( }) } +#[ztracing::instrument(skip_all)] fn get_injections( config: &InjectionConfig, text: &BufferSnapshot, @@ -1705,6 +1710,7 @@ pub(crate) fn splice_included_ranges( /// different lines. For performance, only iterate through the given range of /// indices. All of the ranges in the array are relative to a given start byte /// and point. +#[ztracing::instrument(skip_all)] fn insert_newlines_between_ranges( indices: Range, ranges: &mut Vec, diff --git a/crates/language_extension/Cargo.toml b/crates/language_extension/Cargo.toml index de5af2246c9dfb2dd385875894a694da5e2a9c23..2bbd48b9ecc948e6be8bbc705a75609559f156d1 100644 --- a/crates/language_extension/Cargo.toml +++ b/crates/language_extension/Cargo.toml @@ -25,4 +25,9 @@ lsp.workspace = true project.workspace = true serde.workspace = true serde_json.workspace = true +tracing.workspace = true util.workspace = true +ztracing.workspace = true + +[package.metadata.cargo-machete] +ignored = ["tracing"] diff --git a/crates/language_extension/src/language_extension.rs b/crates/language_extension/src/language_extension.rs index 510f870ce8afbda090817e0ce515d4c5c2e3c63b..96536b6c021c6cd180fd3f50e6cb851d3a8d4895 100644 --- a/crates/language_extension/src/language_extension.rs +++ b/crates/language_extension/src/language_extension.rs @@ -37,6 +37,7 @@ struct LanguageServerRegistryProxy { } impl ExtensionGrammarProxy for LanguageServerRegistryProxy { + #[ztracing::instrument(skip_all)] fn register_grammars(&self, grammars: Vec<(Arc, PathBuf)>) { self.language_registry.register_wasm_grammars(grammars) } diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index 4dc676e2192344a63916eb88b78e666c4a5f69b9..55f7b8f7dccfbd64b2e0f27e46e719cdab5e4cfc 100644 --- a/crates/multi_buffer/src/multi_buffer.rs +++ b/crates/multi_buffer/src/multi_buffer.rs @@ -7928,6 +7928,7 @@ impl<'a> MultiBufferChunks<'a> { } } + #[ztracing::instrument(skip_all)] fn next_excerpt_chunk(&mut self) -> Option> { loop { if self.excerpt_offset_range.is_empty() { @@ -7975,6 +7976,7 @@ impl<'a> Iterator for ReversedMultiBufferChunks<'a> { impl<'a> Iterator for MultiBufferChunks<'a> { type Item = Chunk<'a>; + #[ztracing::instrument(skip_all)] fn next(&mut self) -> Option> { if self.range.start >= self.range.end { return None; diff --git a/crates/project/src/buffer_store.rs b/crates/project/src/buffer_store.rs index cb68445be24c7d633eed173f1915660807acd4d2..8b65caca7e2c2eed048128bf6570cbaec154858b 100644 --- a/crates/project/src/buffer_store.rs +++ b/crates/project/src/buffer_store.rs @@ -86,6 +86,7 @@ enum OpenBuffer { pub enum BufferStoreEvent { BufferAdded(Entity), + // TODO(jk): this event seems unused BufferOpened { buffer: Entity, project_path: ProjectPath, @@ -634,6 +635,7 @@ impl LocalBufferStore { self.save_local_buffer(buffer, worktree, path.path, true, cx) } + #[ztracing::instrument(skip_all)] fn open_buffer( &self, path: Arc, @@ -844,6 +846,7 @@ impl BufferStore { } } + #[ztracing::instrument(skip_all)] pub fn open_buffer( &mut self, project_path: ProjectPath, diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs index 205f1e92e4e394262901b5eb608b19ffd1e72e60..28617b8902a54ebc305924f6073bc297f93d519e 100644 --- a/crates/project/src/git_store.rs +++ b/crates/project/src/git_store.rs @@ -738,6 +738,7 @@ impl GitStore { }) } + #[ztracing::instrument(skip_all)] pub fn open_uncommitted_diff( &mut self, buffer: Entity, @@ -790,6 +791,7 @@ impl GitStore { cx.background_spawn(async move { task.await.map_err(|e| anyhow!("{e}")) }) } + #[ztracing::instrument(skip_all)] async fn open_diff_internal( this: WeakEntity, kind: DiffKind, @@ -2924,6 +2926,7 @@ impl BufferGitState { } } + #[ztracing::instrument(skip_all)] fn buffer_language_changed(&mut self, buffer: Entity, cx: &mut Context) { self.language = buffer.read(cx).language().cloned(); self.language_changed = true; @@ -3080,6 +3083,7 @@ impl BufferGitState { self.recalculate_diffs(buffer, cx) } + #[ztracing::instrument(skip_all)] fn recalculate_diffs(&mut self, buffer: text::BufferSnapshot, cx: &mut Context) { *self.recalculating_tx.borrow_mut() = true; diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index e3747f85d9d895aa77f387bd16481dd163457bcb..ac87e2405ee8e00fa75281da75285b5dec5c2ebf 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -2820,6 +2820,7 @@ impl Project { } } + #[ztracing::instrument(skip_all)] pub fn open_buffer( &mut self, path: impl Into, @@ -2872,6 +2873,7 @@ impl Project { .update(cx, |git_store, cx| git_store.open_unstaged_diff(buffer, cx)) } + #[ztracing::instrument(skip_all)] pub fn open_uncommitted_diff( &mut self, buffer: Entity, diff --git a/crates/worktree/Cargo.toml b/crates/worktree/Cargo.toml index e7d3ac34e1886bd76e0a0f5d23ea981b6626909a..3d902b22af65454c381b2cc56a544b0fc8791e24 100644 --- a/crates/worktree/Cargo.toml +++ b/crates/worktree/Cargo.toml @@ -48,7 +48,9 @@ smallvec.workspace = true smol.workspace = true sum_tree.workspace = true text.workspace = true +tracing.workspace = true util.workspace = true +ztracing.workspace = true [dev-dependencies] clock = { workspace = true, features = ["test-support"] } @@ -63,3 +65,6 @@ rpc = { workspace = true, features = ["test-support"] } settings = { workspace = true, features = ["test-support"] } util = { workspace = true, features = ["test-support"] } zlog.workspace = true + +[package.metadata.cargo-machete] +ignored = ["tracing"] diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rs index 2873a664eba7a78a851b153b102a66bc276ac6f9..eefd01f1760d6da50dae84fbff24adfe1ac88bfa 100644 --- a/crates/worktree/src/worktree.rs +++ b/crates/worktree/src/worktree.rs @@ -1342,6 +1342,7 @@ impl LocalWorktree { }) } + #[ztracing::instrument(skip_all)] fn load_file(&self, path: &RelPath, cx: &Context) -> Task> { let path = Arc::from(path); let abs_path = self.absolutize(&path); diff --git a/crates/ztracing/Cargo.toml b/crates/ztracing/Cargo.toml index 0d9f15b9afccca4a1a05036c013562c8ad1ae8f4..ba4fea9a34e5224af31e1a086e7aed388e933836 100644 --- a/crates/ztracing/Cargo.toml +++ b/crates/ztracing/Cargo.toml @@ -9,7 +9,7 @@ license = "GPL-3.0-or-later" workspace = true [features] -tracy = ["tracing-tracy"] +tracy = ["tracing-tracy", "tracy-client"] [dependencies] zlog.workspace = true @@ -17,5 +17,6 @@ tracing.workspace = true tracing-subscriber = "0.3.22" tracing-tracy = { version = "0.11.4", optional = true, features = ["enable", "ondemand"] } +tracy-client = { version = "0.18.2", optional = true, features = ["enable", "ondemand"] } ztracing_macro.workspace = true diff --git a/crates/ztracing/build.rs b/crates/ztracing/build.rs index dc0d0ad704d49c4c0ab639d769024330e10d2481..37e719320dc70a49db548ce568901780233a2f46 100644 --- a/crates/ztracing/build.rs +++ b/crates/ztracing/build.rs @@ -2,8 +2,13 @@ use std::env; fn main() { if env::var_os("ZTRACING").is_some() { - println!(r"cargo::rustc-cfg=ztracing"); + println!("cargo::rustc-cfg=ztracing"); + } + if env::var_os("ZTRACING_WITH_MEMORY").is_some() { + println!("cargo::rustc-cfg=ztracing"); + println!("cargo::rustc-cfg=ztracing_with_memory"); } println!("cargo::rerun-if-changed=build.rs"); println!("cargo::rerun-if-env-changed=ZTRACING"); + println!("cargo::rerun-if-env-changed=ZTRACING_WITH_MEMORY"); } diff --git a/crates/ztracing/src/lib.rs b/crates/ztracing/src/lib.rs index c9007be1ed43150ef877d51c882aee77845e5bd6..bae75dbb0a2207af9342a34596411f48aa13a908 100644 --- a/crates/ztracing/src/lib.rs +++ b/crates/ztracing/src/lib.rs @@ -7,6 +7,11 @@ pub use tracing::{ #[cfg(not(ztracing))] pub use ztracing_macro::instrument; +#[cfg(all(ztracing, ztracing_with_memory))] +#[global_allocator] +static GLOBAL: tracy_client::ProfiledAllocator = + tracy_client::ProfiledAllocator::new(std::alloc::System, 100); + #[cfg(not(ztracing))] pub use __consume_all_tokens as trace_span; #[cfg(not(ztracing))] @@ -46,10 +51,38 @@ impl Span { #[cfg(ztracing)] pub fn init() { - zlog::info!("Starting tracy subscriber, you can now connect the profiler"); + use tracing_subscriber::fmt::format::DefaultFields; use tracing_subscriber::prelude::*; + + #[derive(Default)] + struct TracyLayerConfig { + fmt: DefaultFields, + } + + impl tracing_tracy::Config for TracyLayerConfig { + type Formatter = DefaultFields; + + fn formatter(&self) -> &Self::Formatter { + &self.fmt + } + + fn stack_depth(&self, _: &tracing::Metadata) -> u16 { + 8 + } + + fn format_fields_in_zone_name(&self) -> bool { + true + } + + fn on_error(&self, client: &tracy_client::Client, error: &'static str) { + client.color_message(error, 0xFF000000, 0); + } + } + + zlog::info!("Starting tracy subscriber, you can now connect the profiler"); tracing::subscriber::set_global_default( - tracing_subscriber::registry().with(tracing_tracy::TracyLayer::default()), + tracing_subscriber::registry() + .with(tracing_tracy::TracyLayer::new(TracyLayerConfig::default())), ) .expect("setup tracy layer"); }