|
| 1 | +use super::collapse::build_collapsed_stack_lines; |
| 2 | +use super::config::{FlamegraphConfig, FlamegraphSampleStats}; |
| 3 | +use super::ram::FlamegraphReadableRam; |
| 4 | +use super::stacktrace::collect_stacktrace_raw; |
| 5 | +use super::symbolizer::Addr2LineContext; |
| 6 | +use crate::vm::{Counters, State}; |
| 7 | + |
| 8 | +/// Coordinates the flamegraph pipeline: |
| 9 | +/// 1) collect lightweight raw samples during execution, |
| 10 | +/// 2) symbolize and render once execution finishes. |
| 11 | +pub struct VmFlamegraphProfiler { |
| 12 | + config: FlamegraphConfig, |
| 13 | + symbol_binary: Vec<u8>, |
| 14 | + raw_frames: Vec<(u32, Vec<u32>)>, |
| 15 | + stats: FlamegraphSampleStats, |
| 16 | +} |
| 17 | + |
| 18 | +impl VmFlamegraphProfiler { |
| 19 | + pub fn new(config: FlamegraphConfig) -> std::io::Result<Self> { |
| 20 | + // Zero would both disable progress and cause division-by-zero. |
| 21 | + if config.frequency_recip == 0 { |
| 22 | + return Err(std::io::Error::new( |
| 23 | + std::io::ErrorKind::InvalidInput, |
| 24 | + "frequency_recip must be greater than zero", |
| 25 | + )); |
| 26 | + } |
| 27 | + |
| 28 | + let symbol_binary = std::fs::read(&config.symbols_path)?; |
| 29 | + |
| 30 | + Ok(Self { |
| 31 | + config, |
| 32 | + symbol_binary, |
| 33 | + raw_frames: Vec::new(), |
| 34 | + stats: FlamegraphSampleStats::default(), |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + pub fn stats(&self) -> FlamegraphSampleStats { |
| 39 | + self.stats |
| 40 | + } |
| 41 | + |
| 42 | + #[inline(always)] |
| 43 | + pub fn sample_cycle<C: Counters, R: FlamegraphReadableRam>( |
| 44 | + &mut self, |
| 45 | + state: &State<C>, |
| 46 | + ram: &R, |
| 47 | + cycle: usize, |
| 48 | + ) { |
| 49 | + // Sampling is on the VM hot path, so we keep this branch and data |
| 50 | + // collection minimal and defer expensive work to finalization. |
| 51 | + if cycle % self.config.frequency_recip != 0 { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + self.stats.samples_total += 1; |
| 56 | + |
| 57 | + let (pc, frames) = collect_stacktrace_raw(state, ram); |
| 58 | + if frames.is_empty() == false { |
| 59 | + // Empty stacks are expected when we cannot reconstruct a valid frame |
| 60 | + // chain; they are tracked via stats but not emitted. |
| 61 | + self.stats.samples_collected += 1; |
| 62 | + self.raw_frames.push((pc, frames)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + pub fn write_flamegraph(&mut self) -> std::io::Result<()> { |
| 67 | + // Symbolization is deferred to here to keep execution-time sampling |
| 68 | + // overhead predictable and low. |
| 69 | + let symbolizer = Addr2LineContext::new(&self.symbol_binary)?; |
| 70 | + |
| 71 | + let collapsed_lines = build_collapsed_stack_lines(&self.raw_frames, &symbolizer); |
| 72 | + |
| 73 | + let collapsed_lines = if collapsed_lines.is_empty() { |
| 74 | + // Produce a minimal graph instead of failing when no usable samples |
| 75 | + // were collected. |
| 76 | + vec![String::from("no_samples 1")] |
| 77 | + } else { |
| 78 | + collapsed_lines |
| 79 | + }; |
| 80 | + |
| 81 | + let output_file = std::fs::File::create(&self.config.output_path)?; |
| 82 | + let mut options = inferno::flamegraph::Options::default(); |
| 83 | + options.reverse_stack_order = self.config.reverse_graph; |
| 84 | + inferno::flamegraph::from_lines( |
| 85 | + &mut options, |
| 86 | + collapsed_lines.iter().map(String::as_str), |
| 87 | + output_file, |
| 88 | + ) |
| 89 | + .map_err(|error| { |
| 90 | + std::io::Error::new( |
| 91 | + std::io::ErrorKind::Other, |
| 92 | + format!("while attempting to generate flamegraph: {error}"), |
| 93 | + ) |
| 94 | + })?; |
| 95 | + |
| 96 | + // The profiler can be reused across VM runs with the same config. |
| 97 | + self.raw_frames.clear(); |
| 98 | + |
| 99 | + Ok(()) |
| 100 | + } |
| 101 | +} |
0 commit comments