Skip to content

Commit ddedc10

Browse files
author
Jonathan Woollett-Light
committed
fix: tracing-flame
Adds `tracing-flame` to generate profiles for flamegraphs. Signed-off-by: Jonathan Woollett-Light <jcawl@amazon.co.uk>
1 parent 5b4b3f2 commit ddedc10

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/vmm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ vm-fdt = "0.2.0"
3030
vm-superio = "0.7.0"
3131
tracing = { version = "0.1.37", features = ["attributes"] }
3232
tracing-subscriber = "0.3.17"
33+
tracing-flame = "0.2.0"
3334
log = { version = "0.4.17", features = ["serde"] }
3435

3536
dumbo = { path = "../dumbo" }

src/vmm/src/vmm_config/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ pub struct LoggerConfig {
190190
pub show_level: Option<bool>,
191191
/// When enabled, the logger will append the origin of the log entry.
192192
pub show_log_origin: Option<bool>,
193+
/// The profile file to output.
194+
pub profile_file: Option<std::path::PathBuf>,
193195
}
194196

195-
196197
impl LoggerConfig {
197198
/// Initializes the logger.
198199
pub fn init(&self) {
@@ -229,7 +230,20 @@ impl LoggerConfig {
229230

230231
let fmt_layer = fmt_layer.with_writer(writer.with_max_level(level));
231232

232-
tracing_subscriber::registry().with(fmt_layer).init();
233+
if let Some(profile_file) = &self.profile_file {
234+
// We can discard the flush guard as
235+
// > This type is only needed when using
236+
// > `tracing::subscriber::set_global_default`, which prevents the drop
237+
// > implementation of layers from running when the program exits.
238+
// See https://docs.rs/tracing-flame/0.2.0/tracing_flame/struct.FlushGuard.html
239+
let (flame_layer, _guard) = tracing_flame::FlameLayer::with_file(profile_file).unwrap();
240+
tracing_subscriber::registry()
241+
.with(fmt_layer)
242+
.with(flame_layer)
243+
.init();
244+
} else {
245+
tracing_subscriber::registry().with(fmt_layer).init();
246+
}
233247
}
234248
}
235249

0 commit comments

Comments
 (0)