Skip to content

Commit e31c9f3

Browse files
committed
Auto merge of #17102 - davidbarsky:david/add-some-tracing-to-project-loading, r=lnicola
chore: add some `tracing` to project loading I wanted to see what's happening during project loading and if it could be parallelized. I'm thinking maybe, but it's not this PR :)
2 parents 47a901b + 2a030ba commit e31c9f3

File tree

7 files changed

+33
-5
lines changed

7 files changed

+33
-5
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/load-cargo/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use itertools::Itertools;
1717
use proc_macro_api::{MacroDylib, ProcMacroServer};
1818
use project_model::{CargoConfig, PackageRoot, ProjectManifest, ProjectWorkspace};
1919
use span::Span;
20+
use tracing::{instrument, Level};
2021
use vfs::{file_set::FileSetConfig, loader::Handle, AbsPath, AbsPathBuf, VfsPath};
2122

2223
pub struct LoadCargoConfig {
@@ -50,6 +51,7 @@ pub fn load_workspace_at(
5051
load_workspace(workspace, &cargo_config.extra_env, load_config)
5152
}
5253

54+
#[instrument(skip_all)]
5355
pub fn load_workspace(
5456
ws: ProjectWorkspace,
5557
extra_env: &FxHashMap<String, String>,
@@ -350,6 +352,7 @@ fn load_crate_graph(
350352
}
351353
}
352354
vfs::loader::Message::Loaded { files } | vfs::loader::Message::Changed { files } => {
355+
let _p = tracing::span!(Level::INFO, "LoadCargo::load_file_contents").entered();
353356
for (path, contents) in files {
354357
vfs.set_file_contents(path.into(), contents);
355358
}

crates/project-model/src/workspace.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
1515
use semver::Version;
1616
use span::Edition;
1717
use toolchain::Tool;
18+
use tracing::instrument;
1819
use triomphe::Arc;
1920

2021
use crate::{
@@ -885,6 +886,7 @@ impl ProjectWorkspace {
885886
}
886887
}
887888

889+
#[instrument(skip_all)]
888890
fn project_json_to_crate_graph(
889891
rustc_cfg: Vec<CfgFlag>,
890892
load: FileLoader<'_>,

crates/rust-analyzer/src/global_state.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use project_model::{
2323
WorkspaceBuildScripts,
2424
};
2525
use rustc_hash::{FxHashMap, FxHashSet};
26+
use tracing::{span, Level};
2627
use triomphe::Arc;
2728
use vfs::{AnchoredPathBuf, ChangedFile, Vfs};
2829

@@ -252,8 +253,7 @@ impl GlobalState {
252253
}
253254

254255
pub(crate) fn process_changes(&mut self) -> bool {
255-
let _p = tracing::span!(tracing::Level::INFO, "GlobalState::process_changes").entered();
256-
256+
let _p = span!(Level::INFO, "GlobalState::process_changes").entered();
257257
let mut file_changes = FxHashMap::<_, (bool, ChangedFile)>::default();
258258
let (change, modified_rust_files, workspace_structure_change) = {
259259
let mut change = ChangeWithProcMacros::new();
@@ -263,6 +263,8 @@ impl GlobalState {
263263
return false;
264264
}
265265

266+
let _p =
267+
span!(Level::INFO, "GlobalState::process_changes/gather_changed_files").entered();
266268
// downgrade to read lock to allow more readers while we are normalizing text
267269
let guard = RwLockWriteGuard::downgrade_to_upgradable(guard);
268270
let vfs: &Vfs = &guard.0;
@@ -301,6 +303,8 @@ impl GlobalState {
301303
}
302304
}
303305

306+
let _p = span!(Level::INFO, "GlobalState::process_changes/calculate_changed_files")
307+
.entered();
304308
let changed_files: Vec<_> = file_changes
305309
.into_iter()
306310
.filter(|(_, (just_created, change))| {
@@ -366,6 +370,7 @@ impl GlobalState {
366370
(change, modified_rust_files, workspace_structure_change)
367371
};
368372

373+
let _p = span!(Level::INFO, "GlobalState::process_changes/apply_change").entered();
369374
self.analysis_host.apply_change(change);
370375

371376
{
@@ -379,6 +384,9 @@ impl GlobalState {
379384
// but something's going wrong with the source root business when we add a new local
380385
// crate see https://github.com/rust-lang/rust-analyzer/issues/13029
381386
if let Some((path, force_crate_graph_reload)) = workspace_structure_change {
387+
let _p = span!(Level::INFO, "GlobalState::process_changes/ws_structure_change")
388+
.entered();
389+
382390
self.fetch_workspaces_queue.request_op(
383391
format!("workspace vfs file change: {path}"),
384392
force_crate_graph_reload,

crates/rust-analyzer/src/main_loop.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ide_db::base_db::{SourceDatabase, SourceDatabaseExt, VfsPath};
1212
use lsp_server::{Connection, Notification, Request};
1313
use lsp_types::{notification::Notification as _, TextDocumentIdentifier};
1414
use stdx::thread::ThreadIntent;
15+
use tracing::{span, Level};
1516
use vfs::FileId;
1617

1718
use crate::{
@@ -229,8 +230,7 @@ impl GlobalState {
229230
fn handle_event(&mut self, event: Event) -> anyhow::Result<()> {
230231
let loop_start = Instant::now();
231232
// NOTE: don't count blocking select! call as a loop-turn time
232-
let _p = tracing::span!(tracing::Level::INFO, "GlobalState::handle_event", event = %event)
233-
.entered();
233+
let _p = tracing::span!(Level::INFO, "GlobalState::handle_event", event = %event).entered();
234234

235235
let event_dbg_msg = format!("{event:?}");
236236
tracing::debug!(?loop_start, ?event, "handle_event");
@@ -669,9 +669,12 @@ impl GlobalState {
669669
}
670670

671671
fn handle_vfs_msg(&mut self, message: vfs::loader::Message) {
672+
let _p = tracing::span!(Level::INFO, "GlobalState::handle_vfs_msg").entered();
672673
let is_changed = matches!(message, vfs::loader::Message::Changed { .. });
673674
match message {
674675
vfs::loader::Message::Changed { files } | vfs::loader::Message::Loaded { files } => {
676+
let _p = tracing::span!(Level::INFO, "GlobalState::handle_vfs_msg{changed/load}")
677+
.entered();
675678
let vfs = &mut self.vfs.write().0;
676679
for (path, contents) in files {
677680
let path = VfsPath::from(path);
@@ -685,6 +688,8 @@ impl GlobalState {
685688
}
686689
}
687690
vfs::loader::Message::Progress { n_total, n_done, dir, config_version } => {
691+
let _p =
692+
tracing::span!(Level::INFO, "GlobalState::handle_vfs_mgs/progress").entered();
688693
always!(config_version <= self.vfs_config_version);
689694

690695
let state = match n_done {
@@ -867,6 +872,8 @@ impl GlobalState {
867872

868873
/// Registers and handles a request. This should only be called once per incoming request.
869874
fn on_new_request(&mut self, request_received: Instant, req: Request) {
875+
let _p =
876+
span!(Level::INFO, "GlobalState::on_new_request", req.method = ?req.method).entered();
870877
self.register_request(&req, request_received);
871878
self.on_request(req);
872879
}
@@ -980,6 +987,8 @@ impl GlobalState {
980987

981988
/// Handles an incoming notification.
982989
fn on_notification(&mut self, not: Notification) -> anyhow::Result<()> {
990+
let _p =
991+
span!(Level::INFO, "GlobalState::on_notification", not.method = ?not.method).entered();
983992
use crate::handlers::notification as handlers;
984993
use lsp_types::notification as notifs;
985994

crates/vfs/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ doctest = false
1313

1414
[dependencies]
1515
rustc-hash.workspace = true
16+
tracing.workspace = true
1617
fst = "0.4.7"
1718
indexmap.workspace = true
1819
nohash-hasher.workspace = true
@@ -21,4 +22,4 @@ paths.workspace = true
2122
stdx.workspace = true
2223

2324
[lints]
24-
workspace = true
25+
workspace = true

crates/vfs/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub use crate::{
5656
};
5757
pub use paths::{AbsPath, AbsPathBuf};
5858

59+
use tracing::{span, Level};
60+
5961
/// Handle to a file in [`Vfs`]
6062
///
6163
/// Most functions in rust-analyzer use this when they need to refer to a file.
@@ -210,6 +212,7 @@ impl Vfs {
210212
/// If the path does not currently exists in the `Vfs`, allocates a new
211213
/// [`FileId`] for it.
212214
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
215+
let _p = span!(Level::INFO, "Vfs::set_file_contents").entered();
213216
let file_id = self.alloc_file_id(path);
214217
let state = self.get(file_id);
215218
let change_kind = match (state, contents) {
@@ -236,6 +239,7 @@ impl Vfs {
236239

237240
/// Drain and returns all the changes in the `Vfs`.
238241
pub fn take_changes(&mut self) -> Vec<ChangedFile> {
242+
let _p = span!(Level::INFO, "Vfs::take_changes").entered();
239243
for file_id in self.created_this_cycle.drain(..) {
240244
if self.data[file_id.0 as usize] == FileState::Created {
241245
// downgrade the file from `Created` to `Exists` as the cycle is done

0 commit comments

Comments
 (0)