Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
zed: Log worktree info when memory jumps unexpectedly (#61283)
Lukas Wirthf14fea9bf3c9
diff --git a/crates/project/src/worktree_store.rs b/crates/project/src/worktree_store.rsindex 3fcadabd4ba..375dde99120 100644--- a/crates/project/src/worktree_store.rs+++ b/crates/project/src/worktree_store.rs@@ -182,6 +182,28 @@ impl WorktreeIdCounter {impl Global for WorktreeIdCounter {}+/// Summarizes worktree ownership and current snapshot sizes.+#[derive(Debug, Default)]+pub struct WorktreeStoreDiagnostics {+ pub worktree_slots: usize,+ pub live_worktrees: usize,+ pub visible_worktrees: usize,+ pub strong_handles: usize,+ pub dead_weak_handles: usize,+ pub loading_worktrees: usize,+ pub total_entries: usize,+ pub visible_entries: usize,+ pub largest_worktree: Option<LargestWorktreeDiagnostics>,+}++/// Identifies the worktree with the largest current snapshot.+#[derive(Debug)]+pub struct LargestWorktreeDiagnostics {+ pub path: PathBuf,+ pub entries: usize,+ pub visible_entries: usize,+}+pub struct WorktreeStore {next_entry_id: Arc<AtomicUsize>,next_worktree_id: WorktreeIdCounter,@@ -348,6 +370,57 @@ impl WorktreeStore {.detach();}+ /// Returns worktree ownership and current snapshot size diagnostics.+ pub fn diagnostics(&self, cx: &App) -> WorktreeStoreDiagnostics {+ let mut diagnostics = WorktreeStoreDiagnostics {+ worktree_slots: self.worktrees.len(),+ loading_worktrees: self.loading_worktrees.len(),+ ..WorktreeStoreDiagnostics::default()+ };++ for handle in &self.worktrees {+ let worktree = match handle {+ WorktreeHandle::Strong(worktree) => {+ diagnostics.strong_handles += 1;+ Some(worktree.clone())+ }+ WorktreeHandle::Weak(worktree) => {+ let worktree = worktree.upgrade();+ if worktree.is_none() {+ diagnostics.dead_weak_handles += 1;+ }+ worktree+ }+ };+ let Some(worktree) = worktree else {+ continue;+ };++ diagnostics.live_worktrees += 1;+ let worktree = worktree.read(cx);+ diagnostics.visible_worktrees += usize::from(worktree.is_visible());+ let snapshot = worktree.snapshot();+ let entries = snapshot.entry_count();+ let visible_entries = snapshot.visible_entry_count();+ diagnostics.total_entries += entries;+ diagnostics.visible_entries += visible_entries;++ let is_largest = diagnostics+ .largest_worktree+ .as_ref()+ .is_none_or(|largest| entries > largest.entries);+ if is_largest {+ diagnostics.largest_worktree = Some(LargestWorktreeDiagnostics {+ path: worktree.abs_path().to_path_buf(),+ entries,+ visible_entries,+ });+ }+ }++ diagnostics+ }+/// Iterates through all worktrees, including ones that don't appear in the project panelpub fn worktrees(&self) -> impl '_ + DoubleEndedIterator<Item = Entity<Worktree>> {self.worktreesdiff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rsindex eb933cc16e6..f640d204a65 100644--- a/crates/zed/src/main.rs+++ b/crates/zed/src/main.rs@@ -659,7 +659,7 @@ fn main() {auto_update::init(client.clone(), cx);dap_adapters::init(cx);auto_update_ui::init(cx);- reliability::init(client.clone(), cx);+ reliability::init(client.clone(), app_state.workspace_store.clone(), cx);extension_host::init(extension_host_proxy.clone(),app_state.fs.clone(),diff --git a/crates/zed/src/reliability.rs b/crates/zed/src/reliability.rsindex 00917069d20..3ad758f0ae2 100644--- a/crates/zed/src/reliability.rs+++ b/crates/zed/src/reliability.rs@@ -2,9 +2,9 @@ use anyhow::{Context as _, Result};use client::{Client, telemetry::MINIDUMP_ENDPOINT};use feature_flags::FeatureFlagAppExt;use futures::{AsyncReadExt, TryStreamExt};-use gpui::{App, AppContext, TaskExt};+use gpui::{App, AppContext, Entity, TaskExt};use http_client::{AsyncBody, HttpClient, Request};-use project::Project;+use project::{Project, worktree_store::WorktreeStoreDiagnostics};use proto::{CrashReport, GetCrashFilesResponse};use reqwest::{Method,@@ -13,6 +13,7 @@ use reqwest::{use serde::Deserialize;use smol::stream::StreamExt;use std::{+ collections::HashSet,ffi::OsStr,fs,sync::Arc,@@ -20,12 +21,13 @@ use std::{};use sysinfo::{MemoryRefreshKind, ProcessRefreshKind, ProcessesToUpdate, RefreshKind, System};use util::ResultExt;+use workspace::WorkspaceStore;mod hang_detection;-pub fn init(client: Arc<Client>, cx: &mut App) {+pub fn init(client: Arc<Client>, workspace_store: Entity<WorkspaceStore>, cx: &mut App) {hang_detection::start(client.clone(), cx);- start_memory_usage_logging(cx);+ start_memory_usage_logging(workspace_store, cx);cx.on_flags_ready({let client = client.clone();@@ -97,7 +99,15 @@ const MEMORY_USAGE_MINIMUM_LOGGED_DELTA: u64 = 64 * 1024 * 1024;/// Logs on a fixed heartbeat, and additionally whenever resident memory changed/// significantly since the last logged value, so that bursts of growth are timestamped/// against the surrounding log entries.-fn start_memory_usage_logging(cx: &App) {+fn start_memory_usage_logging(workspace_store: Entity<WorkspaceStore>, cx: &App) {+ let (diagnostics_sender, mut diagnostics_receiver) = futures::channel::mpsc::unbounded();+ cx.spawn(async move |cx| {+ while diagnostics_receiver.next().await.is_some() {+ cx.update(|cx| log_worktree_diagnostics(&workspace_store, cx));+ }+ })+ .detach();+let executor = cx.background_executor().clone();cx.background_spawn(async move {let Some(pid) = sysinfo::get_current_pid().log_err() else {@@ -134,6 +144,9 @@ fn start_memory_usage_logging(cx: &App) {resident / MIB,process.virtual_memory() / MIB,);+ if diagnostics_sender.unbounded_send(()).is_err() {+ return;+ }last_logged_resident = Some(resident);last_logged_at = Instant::now();}@@ -144,6 +157,78 @@ fn start_memory_usage_logging(cx: &App) {.detach();}+fn log_worktree_diagnostics(workspace_store: &Entity<WorkspaceStore>, cx: &App) {+ let workspaces = workspace_store+ .read(cx)+ .workspaces()+ .filter_map(|workspace| workspace.upgrade())+ .collect::<Vec<_>>();+ let mut worktree_store_ids = HashSet::new();+ let mut store_count = 0;+ let mut aggregate = WorktreeStoreDiagnostics::default();++ for workspace in workspaces {+ let project = workspace.read(cx).project().clone();+ let worktree_store = project.read(cx).worktree_store();+ if !worktree_store_ids.insert(worktree_store.entity_id()) {+ continue;+ }+ store_count += 1;++ let WorktreeStoreDiagnostics {+ worktree_slots,+ live_worktrees,+ visible_worktrees,+ strong_handles,+ dead_weak_handles,+ loading_worktrees,+ total_entries,+ visible_entries,+ largest_worktree,+ } = worktree_store.read(cx).diagnostics(cx);+ aggregate.worktree_slots += worktree_slots;+ aggregate.live_worktrees += live_worktrees;+ aggregate.visible_worktrees += visible_worktrees;+ aggregate.strong_handles += strong_handles;+ aggregate.dead_weak_handles += dead_weak_handles;+ aggregate.loading_worktrees += loading_worktrees;+ aggregate.total_entries += total_entries;+ aggregate.visible_entries += visible_entries;++ if let Some(largest_worktree) = largest_worktree+ && aggregate+ .largest_worktree+ .as_ref()+ .is_none_or(|largest| largest_worktree.entries > largest.entries)+ {+ aggregate.largest_worktree = Some(largest_worktree);+ }+ }++ let WorktreeStoreDiagnostics {+ worktree_slots,+ live_worktrees,+ visible_worktrees,+ strong_handles,+ dead_weak_handles,+ loading_worktrees,+ total_entries,+ visible_entries,+ largest_worktree,+ } = aggregate;+ match largest_worktree {+ Some(largest_worktree) => log::info!(+ "worktree diagnostics: stores {store_count}, slots {worktree_slots}, live {live_worktrees}, visible {visible_worktrees}, strong {strong_handles}, dead weak {dead_weak_handles}, loading {loading_worktrees}, entries {total_entries}, visible entries {visible_entries}, largest {} ({} entries, {} visible)",+ largest_worktree.path.display(),+ largest_worktree.entries,+ largest_worktree.visible_entries,+ ),+ None => log::info!(+ "worktree diagnostics: stores {store_count}, slots {worktree_slots}, live {live_worktrees}, visible {visible_worktrees}, strong {strong_handles}, dead weak {dead_weak_handles}, loading {loading_worktrees}, entries {total_entries}, visible entries {visible_entries}, largest none",+ ),+ }+}+pub async fn upload_previous_minidumps(client: Arc<Client>) -> anyhow::Result<()> {let Some(minidump_endpoint) = MINIDUMP_ENDPOINT.as_ref() else {log::warn!("Minidump endpoint not set");