Skip to content

Commit 44494e2

Browse files
committed
internal: teach hprof to record tracing fields
1 parent 579e98c commit 44494e2

File tree

1 file changed

+51
-17
lines changed
  • crates/rust-analyzer/src/tracing

1 file changed

+51
-17
lines changed

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

Lines changed: 51 additions & 17 deletions
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)