Skip to content

Commit 6b3d1e3

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 6b54437 commit 6b3d1e3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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
/// Initalizes the logger.
198199
pub fn init(&self) {
@@ -226,7 +227,20 @@ impl LoggerConfig {
226227

227228
let fmt_layer = fmt_layer.with_writer(writer.with_max_level(level));
228229

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

0 commit comments

Comments
 (0)