tracy: Hook up tracy CPU and memory profiler behind feature flags

Jakub Konka created

Change summary

Cargo.lock                 | 24 ++++++++++++++++++++++++
Cargo.toml                 |  1 +
crates/gpui/Cargo.toml     |  2 ++
crates/gpui/src/app.rs     |  6 ++++++
crates/zed/Cargo.toml      |  5 +++++
crates/zed/src/main.rs     | 18 +++++++++++++++++-
crates/zed/src/zed-main.rs |  6 ++++++
7 files changed, 61 insertions(+), 1 deletion(-)

Detailed changes

Cargo.lock 🔗

@@ -7107,6 +7107,7 @@ dependencies = [
  "sum_tree",
  "taffy",
  "thiserror 2.0.12",
+ "tracy-client",
  "unicode-segmentation",
  "usvg",
  "util",
@@ -16889,6 +16890,28 @@ dependencies = [
  "tracing-serde",
 ]
 
+[[package]]
+name = "tracy-client"
+version = "0.18.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ef54005d3d760186fd662dad4b7bb27ecd5531cdef54d1573ebd3f20a9205ed7"
+dependencies = [
+ "loom",
+ "once_cell",
+ "rustc-demangle",
+ "tracy-client-sys",
+]
+
+[[package]]
+name = "tracy-client-sys"
+version = "0.26.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "319c70195101a93f56db4c74733e272d720768e13471f400c78406a326b172b0"
+dependencies = [
+ "cc",
+ "windows-targets 0.52.6",
+]
+
 [[package]]
 name = "trait-variant"
 version = "0.1.2"
@@ -20335,6 +20358,7 @@ dependencies = [
  "time",
  "title_bar",
  "toolchain_selector",
+ "tracy-client",
  "tree-sitter-md",
  "tree-sitter-rust",
  "ui",

Cargo.toml 🔗

@@ -667,6 +667,7 @@ tokio = { version = "1" }
 tokio-tungstenite = { version = "0.26", features = ["__rustls-tls"] }
 toml = "0.8"
 tower-http = "0.4.4"
+tracy-client = { version = "0.18.2", default-features = false, features = ["enable", "demangle"] }
 tree-sitter = { version = "0.25.10", features = ["wasm"] }
 tree-sitter-bash = "0.25.0"
 tree-sitter-c = "0.23"

crates/gpui/Cargo.toml 🔗

@@ -72,6 +72,7 @@ screen-capture = [
     "scap",
 ]
 windows-manifest = []
+tracy = ["dep:tracy-client"]
 
 [lib]
 path = "src/gpui.rs"
@@ -124,6 +125,7 @@ strum.workspace = true
 sum_tree.workspace = true
 taffy = "=0.9.0"
 thiserror.workspace = true
+tracy-client = { workspace = true, optional = true }
 util.workspace = true
 uuid.workspace = true
 waker-fn = "1.2.0"

crates/gpui/src/app.rs 🔗

@@ -130,6 +130,9 @@ impl Application {
     /// Builds an app with the given asset source.
     #[allow(clippy::new_without_default)]
     pub fn new() -> Self {
+        #[cfg(feature = "tracy")]
+        let _zone = tracy_client::span!();
+
         #[cfg(any(test, feature = "test-support"))]
         log::info!("GPUI was compiled in test mode");
 
@@ -175,6 +178,9 @@ impl Application {
     where
         F: 'static + FnOnce(&mut App),
     {
+        #[cfg(feature = "tracy")]
+        let _zone = tracy_client::span!();
+
         let this = self.0.clone();
         let platform = self.0.borrow().platform.clone();
         platform.run(Box::new(move || {

crates/zed/Cargo.toml 🔗

@@ -18,6 +18,10 @@ path = "src/zed-main.rs"
 name = "zed"
 path = "src/main.rs"
 
+[features]
+tracy = ["dep:tracy-client", "gpui/tracy"]
+tracy-memory = ["tracy"]
+
 [dependencies]
 acp_tools.workspace = true
 activity_indicator.workspace = true
@@ -146,6 +150,7 @@ theme_selector.workspace = true
 time.workspace = true
 title_bar.workspace = true
 toolchain_selector.workspace = true
+tracy-client = { workspace = true, optional = true }
 ui.workspace = true
 ui_input.workspace = true
 ui_prompt.workspace = true

crates/zed/src/main.rs 🔗

@@ -59,10 +59,20 @@ use zed::{
 
 use crate::zed::OpenRequestKind;
 
-#[cfg(feature = "mimalloc")]
+#[cfg(all(feature = "mimalloc", not(features = "tracy-memory")))]
 #[global_allocator]
 static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
+#[cfg(all(feature = "mimalloc", features = "tracy-memory"))]
+#[global_allocator]
+static GLOBAL: tracy_client::ProfiledAllocator<mimalloc::MiMalloc> =
+    tracy_client::ProfiledAllocator::new(mimalloc::MiMalloc, 100);
+
+#[cfg(all(not(feature = "mimalloc"), feature = "tracy-memory"))]
+#[global_allocator]
+static GLOBAL: tracy_client::ProfiledAllocator<std::alloc::System> =
+    tracy_client::ProfiledAllocator::new(std::alloc::System, 100);
+
 fn files_not_created_on_launch(errors: HashMap<io::ErrorKind, Vec<&Path>>) {
     let message = "Zed failed to launch";
     let error_details = errors
@@ -165,6 +175,9 @@ fn fail_to_open_window(e: anyhow::Error, _cx: &mut App) {
 }
 
 pub fn main() {
+    #[cfg(feature = "tracy")]
+    let _zone = tracy_client::span!();
+
     #[cfg(unix)]
     util::prevent_root_execution();
 
@@ -371,6 +384,9 @@ pub fn main() {
     });
 
     app.run(move |cx| {
+        #[cfg(feature = "tracy")]
+        let _zone = tracy_client::span!();
+
         menu::init();
         zed_actions::init();
 

crates/zed/src/zed-main.rs 🔗

@@ -2,6 +2,12 @@
 #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
 
 pub fn main() {
+    #[cfg(feature = "tracy")]
+    {
+        tracy_client::register_demangler!();
+        tracy_client::Client::start();
+    }
+
     // separated out so that the file containing the main function can be imported by other crates,
     // while having all gpui resources that are registered in main (primarily actions) initialized
     zed::main();