Perl Awk Shell Perl 6 DTrace
Switch branches/tags
Nothing to show
Latest commit a8d807a Aug 19, 2017 @brendangregg fix #125
Failed to load latest commit information.
demos add mixedmode example Oct 11, 2015
dev Make stackcollapse happy when a % is in the function name Jan 1, 2015
docs USAGE message and license file reference May 18, 2013
test add another test source (vertx) Aug 17, 2017
.travis.yml Travis CI test Jul 5, 2016 [#138] Add stackcollapse script for visual studio profiles Jul 28, 2017 AIX stack probes and stack collapse script Dec 28, 2016 add -s for diffing profiles including hex numbers Nov 4, 2014
example-dtrace-stacks.txt more examples Dec 8, 2015
example-dtrace.svg more examples Dec 8, 2015
example-perf-stacks.txt.gz more examples Dec 8, 2015
example-perf.svg more examples Dec 8, 2015 Merge pull request #128 from milianw/loop-files Apr 7, 2017 fix #125 Aug 18, 2017
jmaps use existing AGENT_HOME environment variable if present Aug 9, 2017 update header Aug 11, 2017 add options for different timestamps Feb 24, 2017 update tests Aug 17, 2017 AIX stack probes and stack collapse script Dec 28, 2016 stackcollapse tool for elfutils stack Oct 11, 2015 stackcollapse-gdb: Do not forget the last sample Oct 11, 2015 add stackcollapse for Golang Jan 17, 2017 Support floating point numbers in instruments CSV Dec 6, 2015 stackcollapse-jstack supports command line parameters now. Jan 14, 2016
stackcollapse-ljp.awk undo test Jun 12, 2014
stackcollapse-perf-sched.awk Be less brittle to different arguments to perf script Jan 21, 2016 tweak event warning Aug 17, 2017 Add FreeBSD hwpmc callgraph parser Jun 3, 2014 Recursive call filter: allow floating point values and better regex h… Jul 23, 2015
stackcollapse-sample.awk Allow stacks to be captured from /usr/bin/sample on macOS Mar 9, 2017 USAGE message and license file reference May 18, 2013 [#138] Add stackcollapse script for visual studio profiles Jul 28, 2017 Update Sep 7, 2016 Strip java related things and handle inlined stacks produced by dtrace. Aug 17, 2017 update tests Aug 17, 2017

Flame Graphs visualize profiled code

Main Website:

Example (click to zoom): Example

Other sites:

Flame graphs can be created in three steps:

  1. Capture stacks
  2. Fold stacks

1. Capture stacks

Stack samples can be captured using Linux perf_events, FreeBSD pmcstat (hwpmc), DTrace, SystemTap, and many other profilers. See the stackcollapse-* converters.

Linux perf_events

Using Linux perf_events (aka "perf") to capture 60 seconds of 99 Hertz stack samples, both user- and kernel-level stacks, all processes:

# perf record -F 99 -a -g -- sleep 60
# perf script > out.perf

Now only capturing PID 181:

# perf record -F 99 -p 181 -g -- sleep 60
# perf script > out.perf


Using DTrace to capture 60 seconds of kernel stacks at 997 Hertz:

# dtrace -x stackframes=100 -n 'profile-997 /arg0/ { @[stack()] = count(); } tick-60s { exit(0); }' -o out.kern_stacks

Using DTrace to capture 60 seconds of user-level stacks for PID 12345 at 97 Hertz:

# dtrace -x ustackframes=100 -n 'profile-97 /pid == 12345 && arg1/ { @[ustack()] = count(); } tick-60s { exit(0); }' -o out.user_stacks

60 seconds of user-level stacks, including time spent in-kernel, for PID 12345 at 97 Hertz:

# dtrace -x ustackframes=100 -n 'profile-97 /pid == 12345/ { @[ustack()] = count(); } tick-60s { exit(0); }' -o out.user_stacks

Switch ustack() for jstack() if the application has a ustack helper to include translated frames (eg, node.js frames; see: The rate for user-level stack collection is deliberately slower than kernel, which is especially important when using jstack() as it performs additional work to translate frames.

2. Fold stacks

Use the stackcollapse programs to fold stack samples into single lines. The programs provided are:

  • for DTrace stacks
  • for Linux perf_events "perf script" output
  • for FreeBSD pmcstat -G stacks
  • for SystemTap stacks
  • for XCode Instruments
  • for Intel VTune profiles
  • stackcollapse-ljp.awk: for Lightweight Java Profiler
  • for Java jstack(1) output
  • for gdb(1) stacks
  • for Golang pprof stacks
  • for Microsoft Visual Studio profiles

Usage example:

For perf_events:
$ ./ out.perf > out.folded

For DTrace:
$ ./ out.kern_stacks > out.kern_folded

The output looks like this:

unix`_sys_sysenter_post_swapgs 1401
unix`_sys_sysenter_post_swapgs;genunix`close 5
unix`_sys_sysenter_post_swapgs;genunix`close;genunix`closeandsetf 85
unix`_sys_sysenter_post_swapgs;genunix`close;genunix`closeandsetf;c2audit`audit_closef 26
unix`_sys_sysenter_post_swapgs;genunix`close;genunix`closeandsetf;c2audit`audit_setf 5
unix`_sys_sysenter_post_swapgs;genunix`close;genunix`closeandsetf;genunix`audit_getstate 6
unix`_sys_sysenter_post_swapgs;genunix`close;genunix`closeandsetf;genunix`audit_unfalloc 2
unix`_sys_sysenter_post_swapgs;genunix`close;genunix`closeandsetf;genunix`closef 48


Use to render a SVG.

$ ./ out.kern_folded > kernel.svg

An advantage of having the folded input file (and why this is separate to is that you can use grep for functions of interest. Eg:

$ grep cpuid out.kern_folded | ./ > cpuid.svg

Provided Examples

Linux perf_events

An example output from Linux "perf script" is included, gzip'd, as example-perf-stacks.txt.gz. The resulting flame graph is example-perf.svg:


You can create this using:

$ gunzip -c example-perf-stacks.txt.gz | ./ --all | ./ --color=java --hash > example-perf.svg

This shows my typical workflow: I'll gzip profiles on the target, then copy them to my laptop for analysis. Since I have hundreds of profiles, I leave them gzip'd!

Since this profile included Java, I used the --color=java palette. I've also used --all, which includes all annotations that help use separate colors for kernel and user level code. The resulting flame graph uses: green == Java, yellow == C++, red == user-mode native, orange == kernel.

This profile was from an analysis of vert.x performance. The benchmark client, wrk, is also visible in the flame graph.


An example output from DTrace is also included, example-dtrace-stacks.txt, and the resulting flame graph, example-dtrace.svg:


You can generate this using:

$ ./ example-stacks.txt | ./ > example.svg

This was from a particular performance investigation: the Flame Graph identified that CPU time was spent in the lofs module, and quantified that time.


See the USAGE message (--help) for options:

USAGE: ./ [options] infile > outfile.svg

--title       # change title text
--width       # width of image (default 1200)
--height      # height of each frame (default 16)
--minwidth    # omit smaller functions (default 0.1 pixels)
--fonttype    # font type (default "Verdana")
--fontsize    # font size (default 12)
--countname   # count type label (default "samples")
--nametype    # name type label (default "Function:")
--colors      # set color palette. choices are: hot (default), mem, io,
              # wakeup, chain, java, js, perl, red, green, blue, aqua,
              # yellow, purple, orange
--hash        # colors are keyed by function name hash
--cp          # use consistent palette (
--reverse     # generate stack-reversed flame graph
--inverted    # icicle graph
--negate      # switch differential hues (blue<->red)
--help        # this message

./ --title="Flame Graph: malloc()" trace.txt > graph.svg

As suggested in the example, flame graphs can process traces of any event, such as malloc()s, provided stack traces are gathered.

Consistent Palette

If you use the --cp option, it will use the $colors selection and randomly generate the palette like normal. Any future flamegraphs created using the --cp option will use the same palette map. Any new symbols from future flamegraphs will have their colors randomly generated using the $colors selection.

If you don't like the palette, just delete the file.

This allows your to change your colorscheme between flamegraphs to make the differences REALLY stand out.


Say we have 2 captures, one with a problem, and one when it was working (whatever "it" is):

cat working.folded | ./ --cp > working.svg
# this generates a, as per the normal random generated look.

cat broken.folded | ./ --cp --colors mem > broken.svg
# this svg will use the same for the same events, but a very
# different colorscheme for any new events.

Take a look at the demo directory for an example: