cl-flamegraph icon indicating copy to clipboard operation
cl-flamegraph copied to clipboard

A wrapper around SBCL's statistical profiler, to generate FlameGraph charts for Common Lisp programs

  • cl-flamegraph

** Rationale

Flamegraphs are a cool way to search for hotspots in your code.

[[images/quickload.svg]]

[[images/speedscope-flamegraph.gif]]

** Installation

Setup an [[https://ultralisp.org][Ultralisp.org]] distribution:

#+BEGIN_SRC lisp

(ql-dist:install-dist "http://dist.ultralisp.org/" :prompt nil)

#+END_SRC

Or clone repository to your ~/quicklisp/local-projects folder.

Then install the system:

#+BEGIN_SRC lisp

(ql:quickload :flamegraph)

#+END_SRC

Download the [[https://github.com/brendangregg/FlameGraph][FlameGraph chart generator]]. It is written in Perl. This tool prepares data in a simple text format that can be processed by the flamegraph.pl script to generate an SVG diagram.

Or you can upload results to the [[https://speedscope.app][Speedscope.app]] site and inspect it in the browser!

** Usage

Wrap the code with flamegraph:save-flame-graph macro:

#+BEGIN_SRC lisp CL-USER> (defun foo () (sleep 0.01)) FOO CL-USER> (defun bar () (sleep 0.05)) BAR CL-USER> (defun blah () (loop repeat 1000 do (foo) (bar))) BLAH CL-USER> (flamegraph:save-flame-graph ("/tmp/foo.stack") (blah)) ; No values #+END_SRC

This will generate a file with the content:

#+BEGIN_SRC text BLAH 8 BLAH;BAR 5 BLAH;BAR;NANOSLEEP 5 BLAH;BAR;NANOSLEEP;foreign function __syscall 2 BLAH;BAR;NANOSLEEP;foreign function sb_nanosleep 3 BLAH;BAR;NANOSLEEP;foreign function sb_nanosleep;foreign function clock_get_time 3 BLAH;BAR;NANOSLEEP;foreign function sb_nanosleep;foreign function clock_get_time;foreign function mach_msg_trap 3 BLAH;FOO 3 BLAH;FOO;NANOSLEEP 3 BLAH;FOO;NANOSLEEP;foreign function __syscall 1 BLAH;FOO;NANOSLEEP;foreign function sb_nanosleep 2 BLAH;FOO;NANOSLEEP;foreign function sb_nanosleep;foreign function clock_get_time 2 BLAH;FOO;NANOSLEEP;foreign function sb_nanosleep;foreign function clock_get_time;foreign function mach_msg_trap 2 #+END_SRC

Pipe it through flamegraph.pl to get a nice SVG:

#+BEGIN_SRC sh cat /tmp/foo.stack | flamegraph.pl > /tmp/foo.svg #+END_SRC

And here is the result:

[[images/foo.svg]] ** Similar projects

  • [[https://github.com/scymtym/clim.flamegraph/tree/future][clim.flamegraph]] :: SBCL's sprof flamegraph inspector with it's own UI built with McClim.
  • [[https://github.com/TeMPOraL/tracer][TeMPOraL's tracer]] :: a hack on SBCL's tracing implementation which is able to produce data for loading into Chrome's chrome://tracing tool.
  • [[https://mr.gy/blog/sam.html][SAM]] :: Simple sampling profiler for ClozureCL. Without graphical interface.
  • [[https://github.com/svspire/ccl-metering][ccl-metering]] :: a tool similar to TeMPOraL's tracer, but for ClozureCL and without any graphical interface.