use std::io::Read;
use std::time::Duration;
use supermachine::Image;
use tracing_flame::FlameLayer;
use tracing_subscriber::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let flame_path = std::env::var("SUPERMACHINE_FLAME_OUT")
.unwrap_or_else(|_| "/tmp/supermachine-flame.folded".to_owned());
eprintln!("[flame] writing folded stacks → {flame_path}");
let (flame_layer, _guard) = FlameLayer::with_file(&flame_path)?;
tracing_subscriber::registry()
.with(flame_layer)
.init();
let image = Image::builder("rust:1-slim")
.with_name("rust_1_slim_flame")
.with_memory_mib(2048)
.with_warmup(|vm| {
vm.exec_builder()
.stage_file(
"/tmp/seed.rs",
b"fn main() { println!(\"flame\"); }".to_vec(),
)
.argv(["rustc", "-O", "/tmp/seed.rs", "-o", "/tmp/seed"])
.chain(["/tmp/seed"])
.timeout(Duration::from_secs(60))
.output()?;
Ok(())
})
.with_warmup_tag("flame_v1")
.build()?;
let pool = image
.pool()
.min(2)
.max(2)
.restore_on_release(false)
.build()?;
for i in 0..5 {
let vm = pool.acquire()?;
let mut child = vm.exec(["sh", "-c", &format!("echo cycle-{i}")])?;
let mut buf = String::new();
if let Some(mut stdout) = child.stdout() {
let _ = stdout.read_to_string(&mut buf);
}
let _ = buf; let _ = child.wait();
}
drop(_guard);
eprintln!(
"[flame] done. Convert with:\n inferno-flamegraph < {flame_path} > /tmp/flame.svg"
);
Ok(())
}