Skip to content

Commit df478ea

Browse files
committed
Auto merge of #18181 - davidbarsky:davidbarsky/push-nzstpumovmmx, r=davidbarsky
internal: add tracing to project discovery and VFS loading With `"env RA_PROFILE=vfs_load|parallel_prime_caches|discover_command>500`, this results in the following output: ``` 21888ms discover_command 11627ms vfs_load @ total = 701 1503ms vfs_load @ total = 701 30211ms parallel_prime_caches ``` As a followup, I'd like to make hprof emit the information above as JSON.
2 parents 7ab2c17 + 338003f commit df478ea

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

crates/rust-analyzer/src/discover.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use paths::{AbsPathBuf, Utf8Path, Utf8PathBuf};
77
use project_model::ProjectJsonData;
88
use serde::{Deserialize, Serialize};
99
use serde_json::Value;
10+
use tracing::{info_span, span::EnteredSpan};
1011

1112
use crate::command::{CommandHandle, ParseFromLine};
1213

@@ -60,14 +61,19 @@ impl DiscoverCommand {
6061
let mut cmd = Command::new(command);
6162
cmd.args(args);
6263

63-
Ok(DiscoverHandle { _handle: CommandHandle::spawn(cmd, self.sender.clone())? })
64+
Ok(DiscoverHandle {
65+
_handle: CommandHandle::spawn(cmd, self.sender.clone())?,
66+
span: info_span!("discover_command").entered(),
67+
})
6468
}
6569
}
6670

6771
/// A handle to a spawned [Discover].
6872
#[derive(Debug)]
6973
pub(crate) struct DiscoverHandle {
7074
_handle: CommandHandle<DiscoverProjectMessage>,
75+
#[allow(dead_code)] // not accessed, but used to log on drop.
76+
span: EnteredSpan,
7177
}
7278

7379
/// An enum containing either progress messages, an error,

crates/rust-analyzer/src/global_state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ pub(crate) struct GlobalState {
111111
pub(crate) vfs_config_version: u32,
112112
pub(crate) vfs_progress_config_version: u32,
113113
pub(crate) vfs_done: bool,
114+
// used to track how long VFS loading takes. this can't be on `vfs::loader::Handle`,
115+
// as that handle's lifetime is the same as `GlobalState` itself.
116+
pub(crate) vfs_span: Option<tracing::span::EnteredSpan>,
114117
pub(crate) wants_to_switch: Option<Cause>,
115118

116119
/// `workspaces` field stores the data we actually use, while the `OpQueue`
@@ -253,6 +256,7 @@ impl GlobalState {
253256
vfs: Arc::new(RwLock::new((vfs::Vfs::default(), IntMap::default()))),
254257
vfs_config_version: 0,
255258
vfs_progress_config_version: 0,
259+
vfs_span: None,
256260
vfs_done: true,
257261
wants_to_switch: None,
258262

crates/rust-analyzer/src/main_loop.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,13 +794,20 @@ impl GlobalState {
794794
}
795795
}
796796
vfs::loader::Message::Progress { n_total, n_done, dir, config_version } => {
797-
let _p = tracing::info_span!("GlobalState::handle_vfs_mgs/progress").entered();
797+
let _p = span!(Level::INFO, "GlobalState::handle_vfs_mgs/progress").entered();
798798
always!(config_version <= self.vfs_config_version);
799799

800800
let (n_done, state) = match n_done {
801-
LoadingProgress::Started => (0, Progress::Begin),
801+
LoadingProgress::Started => {
802+
self.vfs_span =
803+
Some(span!(Level::INFO, "vfs_load", total = n_total).entered());
804+
(0, Progress::Begin)
805+
}
802806
LoadingProgress::Progress(n_done) => (n_done.min(n_total), Progress::Report),
803-
LoadingProgress::Finished => (n_total, Progress::End),
807+
LoadingProgress::Finished => {
808+
self.vfs_span = None;
809+
(n_total, Progress::End)
810+
}
804811
};
805812

806813
self.vfs_progress_config_version = config_version;
@@ -881,6 +888,7 @@ impl GlobalState {
881888
.expect("No title could be found; this is a bug");
882889
match message {
883890
DiscoverProjectMessage::Finished { project, buildfile } => {
891+
self.discover_handle = None;
884892
self.report_progress(&title, Progress::End, None, None, None);
885893
self.discover_workspace_queue.op_completed(());
886894

@@ -892,6 +900,7 @@ impl GlobalState {
892900
self.report_progress(&title, Progress::Report, Some(message), None, None)
893901
}
894902
DiscoverProjectMessage::Error { error, source } => {
903+
self.discover_handle = None;
895904
let message = format!("Project discovery failed: {error}");
896905
self.discover_workspace_queue.op_completed(());
897906
self.show_and_log_error(message.clone(), source);

crates/rust-analyzer/src/tracing/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ where
5050

5151
let ra_fmt_layer = tracing_subscriber::fmt::layer()
5252
.with_target(false)
53+
.with_ansi(false)
5354
.with_writer(writer)
5455
.with_filter(filter);
5556

0 commit comments

Comments
 (0)