Skip to content

Commit 0396811

Browse files
committed
Auto merge of #16456 - davidbarsky:david/tracing-followups, r=Veykril
internal: `tracing` improvements and followups Hi folks! Building on #16394, I've got a few small tweaks: - Removed the accidental `mod.rs` usage that I introduced. - Removed a panic in `pat_analysis.rs`. - Recorded the event kind in `handle_event` to better distinguish what _kind_ of event is being handled. - Did a small refactor of `hprof` to have somewhat more linear control flow, and more importantly, write the recorded fields to the output. The end result is the following: <img width="1530" alt="A screenshot of Visual Studio Code on a Mac. `hprof.rs` is open, with " src="https://github.com/rust-lang/rust-analyzer/assets/2067774/bd11dde5-b2da-4774-bc38-bcb4772d1192">
2 parents 355c944 + 44494e2 commit 0396811

File tree

5 files changed

+73
-21
lines changed

5 files changed

+73
-21
lines changed

crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
460460
_f: &mut fmt::Formatter<'_>,
461461
_pat: &rustc_pattern_analysis::pat::DeconstructedPat<'_, Self>,
462462
) -> fmt::Result {
463-
unimplemented!()
463+
// FIXME: implement this, as using `unimplemented!()` causes panics in `tracing`.
464+
Ok(())
464465
}
465466

466467
fn bug(&self, fmt: fmt::Arguments<'_>) -> ! {

crates/rust-analyzer/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ mod handlers {
3737
pub(crate) mod request;
3838
}
3939

40+
pub mod tracing {
41+
pub mod config;
42+
pub use config::Config;
43+
pub mod hprof;
44+
}
45+
4046
pub mod config;
4147
pub mod lsp;
42-
pub mod tracing;
4348
use self::lsp::ext as lsp_ext;
4449

4550
#[cfg(test)]

crates/rust-analyzer/src/main_loop.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ enum Event {
6060
Flycheck(flycheck::Message),
6161
}
6262

63+
impl fmt::Display for Event {
64+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65+
match self {
66+
Event::Lsp(_) => write!(f, "Event::Lsp"),
67+
Event::Task(_) => write!(f, "Event::Task"),
68+
Event::Vfs(_) => write!(f, "Event::Vfs"),
69+
Event::Flycheck(_) => write!(f, "Event::Flycheck"),
70+
}
71+
}
72+
}
73+
6374
#[derive(Debug)]
6475
pub(crate) enum Task {
6576
Response(lsp_server::Response),
@@ -196,7 +207,8 @@ impl GlobalState {
196207
fn handle_event(&mut self, event: Event) -> anyhow::Result<()> {
197208
let loop_start = Instant::now();
198209
// NOTE: don't count blocking select! call as a loop-turn time
199-
let _p = tracing::span!(tracing::Level::INFO, "GlobalState::handle_event").entered();
210+
let _p = tracing::span!(tracing::Level::INFO, "GlobalState::handle_event", event = %event)
211+
.entered();
200212

201213
let event_dbg_msg = format!("{event:?}");
202214
tracing::debug!("{:?} handle_event({})", loop_start, event_dbg_msg);

crates/rust-analyzer/src/tracing/mod.rs renamed to crates/rust-analyzer/src/tracing/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tracing_subscriber::{
1414
};
1515
use tracing_tree::HierarchicalLayer;
1616

17-
pub mod hprof;
17+
use crate::tracing::hprof;
1818

1919
pub struct Config<T> {
2020
pub writer: T,

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

+51-17
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
//! ```
3333
3434
use std::{
35-
fmt, mem,
35+
fmt::Write,
36+
mem,
3637
time::{Duration, Instant},
3738
};
3839

@@ -99,21 +100,37 @@ impl SpanTree {
99100
struct Data {
100101
start: Instant,
101102
children: Vec<Node>,
103+
fields: String,
102104
}
103105

104106
impl Data {
105107
fn new(attrs: &Attributes<'_>) -> Self {
106-
let mut span = Self { start: Instant::now(), children: Vec::new() };
107-
attrs.record(&mut span);
108-
span
108+
let mut data = Self { start: Instant::now(), children: Vec::new(), fields: String::new() };
109+
110+
let mut visitor = DataVisitor { string: &mut data.fields };
111+
attrs.record(&mut visitor);
112+
data
109113
}
114+
110115
fn into_node(self, name: &'static str) -> Node {
111-
Node { name, count: 1, duration: self.start.elapsed(), children: self.children }
116+
Node {
117+
name,
118+
fields: self.fields,
119+
count: 1,
120+
duration: self.start.elapsed(),
121+
children: self.children,
122+
}
112123
}
113124
}
114125

115-
impl Visit for Data {
116-
fn record_debug(&mut self, _field: &Field, _value: &dyn fmt::Debug) {}
126+
pub struct DataVisitor<'a> {
127+
string: &'a mut String,
128+
}
129+
130+
impl<'a> Visit for DataVisitor<'a> {
131+
fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
132+
write!(self.string, "{} = {:?} ", field.name(), value).unwrap();
133+
}
117134
}
118135

119136
impl<S> Layer<S> for SpanTree
@@ -151,6 +168,7 @@ where
151168
#[derive(Default)]
152169
struct Node {
153170
name: &'static str,
171+
fields: String,
154172
count: u32,
155173
duration: Duration,
156174
children: Vec<Node>,
@@ -163,16 +181,22 @@ impl Node {
163181

164182
fn go(&self, level: usize, filter: &WriteFilter) {
165183
if self.duration > filter.longer_than && level < filter.depth {
166-
let duration = format!("{:3.2?}", self.duration);
167-
let count = if self.count > 1 { self.count.to_string() } else { String::new() };
168-
eprintln!(
169-
"{:width$} {:<9} {:<6} {}",
170-
"",
171-
duration,
172-
count,
173-
self.name,
174-
width = level * 2
175-
);
184+
let duration = ms(self.duration);
185+
let current_indent = level * 2;
186+
187+
let mut out = String::new();
188+
let _ = write!(out, "{:current_indent$} {duration} {:<6}", "", self.name);
189+
190+
if !self.fields.is_empty() {
191+
let _ = write!(out, " @ {}", self.fields);
192+
}
193+
194+
if self.count > 1 {
195+
let _ = write!(out, " ({} calls)", self.count);
196+
}
197+
198+
eprintln!("{}", out);
199+
176200
for child in &self.children {
177201
child.go(level + 1, filter)
178202
}
@@ -236,3 +260,13 @@ impl WriteFilter {
236260
(WriteFilter { depth, longer_than }, allowed)
237261
}
238262
}
263+
264+
#[allow(non_camel_case_types)]
265+
struct ms(Duration);
266+
267+
impl std::fmt::Display for ms {
268+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
269+
let n = self.0.as_millis();
270+
write!(f, "{n:5}ms")
271+
}
272+
}

0 commit comments

Comments
 (0)