zflame is a cutting-edge flamegraph profiling tool designed for the Zig programming language, aimed at simplifying performance analysis and optimization. By leveraging Zig's low-level capabilities, zflame provides detailed, interactive flamegraphs that help developers identify and address performance bottlenecks in their applications.
- π₯ Generate flamegraphs from various profiler formats (perf, DTrace, sample, etc.)
- π Differential flamegraphs for performance regression analysis
- π¨ Customizable color schemes and rendering options
- π Stack trace collapsing with multiple algorithm implementations
- π Streaming parser design for handling large datasets
- π§ Both CLI tool and library APIs available
- Zig 0.15.1 or later
- No external dependencies required
git clone https://github.com/hendriknielaender/zflame
cd zflame
zig build -Doptimize=ReleaseFastThe binary will be available at zig-out/bin/zflame.
Generate a flamegraph from perf output:
# Record performance data
perf record -F 99 -g ./your_program
# Generate perf script output
perf script > perf.out
# Create flamegraph
zflame perf perf.out > flamegraph.svgSupported input formats:
perf- Linux perf eventsdtrace- DTrace stack tracessample- Instruments.app sample formatvtune- Intel VTune Profilerxctrace- Xcode Instruments
Compare performance between two runs:
zflame diff-folded before.folded after.folded | zflame flamegraph > diff.svgconst std = @import("std");
const zflame = @import("zflame");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Parse perf output
const perf_data = try std.fs.cwd().readFileAlloc(allocator, "perf.out", 1024 * 1024);
defer allocator.free(perf_data);
// Collapse stack traces
var folder = try zflame.perf.Folder.init(.{});
defer folder.deinit();
const collapsed = try folder.collapse(allocator, perf_data);
defer allocator.free(collapsed);
// Generate flamegraph
const options = zflame.flamegraph.Options{
.title = "CPU Profile",
.count_name = "samples",
.color_scheme = .hot,
};
const svg = try zflame.flamegraph.generate(allocator, collapsed, options);
defer allocator.free(svg);
try std.fs.cwd().writeFile("flamegraph.svg", svg);
}Benchmarks available in benchmarks/ directory.
The project follows a modular design:
src/
βββ collapse/ # Stack trace collapsing algorithms
β βββ perf.zig # Linux perf format
β βββ dtrace.zig # DTrace stacks
β βββ ... # Other formats
βββ flamegraph/ # SVG generation
β βββ color.zig # Color schemes
β βββ parser.zig # Folded format parser
βββ differential.zig # Differential analysis
βββ main.zig # CLI entry point
This project is a Zig port of inferno by Jon Gjengset. The original Rust implementation provided the algorithmic foundation and design inspiration for zflame.
Additional thanks to:
- Brendan Gregg for inventing flamegraphs and the original implementation
MIT License - see LICENSE for details.
- inferno - The original Rust implementation
- FlameGraph - Original Perl implementation
