Skip to content

Commit fe56548

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 d947037 commit fe56548

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/vmm/Cargo.toml

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

src/vmm/src/vmm_config/mod.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ pub struct LoggerConfig {
278278
pub show_log_origin: Option<bool>,
279279
/// Use the new logger format.
280280
pub new_format: Option<bool>,
281+
/// The profile file to output.
282+
pub profile_file: Option<std::path::PathBuf>,
281283
}
282284

283285
/// Error with actions on the `LoggerConfig`.
@@ -318,7 +320,21 @@ impl LoggerConfig {
318320

319321
let fmt_layer = fmt_layer.with_writer(writer.with_max_level(level));
320322

321-
tracing_subscriber::registry().with(fmt_layer).init();
323+
if let Some(profile_file) = &self.profile_file {
324+
// We can discard the flush guard as
325+
// > This type is only needed when using
326+
// > `tracing::subscriber::set_global_default`, which prevents the drop
327+
// > implementation of layers from running when the program exits.
328+
// See https://docs.rs/tracing-flame/0.2.0/tracing_flame/struct.FlushGuard.html
329+
let (flame_layer, _guard) = tracing_flame::FlameLayer::with_file(profile_file).unwrap();
330+
tracing_subscriber::registry()
331+
.with(fmt_layer)
332+
.with(flame_layer)
333+
.init();
334+
} else {
335+
tracing_subscriber::registry().with(fmt_layer).init();
336+
}
337+
322338
Ok(())
323339
}
324340
/// Initializes the logger with the new format.
@@ -353,7 +369,20 @@ impl LoggerConfig {
353369

354370
let fmt_layer = fmt_layer.with_writer(writer.with_max_level(level));
355371

356-
tracing_subscriber::registry().with(fmt_layer).init();
372+
if let Some(profile_file) = &self.profile_file {
373+
// We can discard the flush guard as
374+
// > This type is only needed when using
375+
// > `tracing::subscriber::set_global_default`, which prevents the drop
376+
// > implementation of layers from running when the program exits.
377+
// See https://docs.rs/tracing-flame/0.2.0/tracing_flame/struct.FlushGuard.html
378+
let (flame_layer, _guard) = tracing_flame::FlameLayer::with_file(profile_file).unwrap();
379+
tracing_subscriber::registry()
380+
.with(fmt_layer)
381+
.with(flame_layer)
382+
.init();
383+
} else {
384+
tracing_subscriber::registry().with(fmt_layer).init();
385+
}
357386

358387
Ok(())
359388
}

0 commit comments

Comments
 (0)