Quick note: Generate flamegraph for Crystal app

Michael Nikitochkin - Dec 11 '22 - - Dev Community

Quick note for me to remember how to generate flamegraphs to profile Crystal lang app. Let’s see how it works…

Flamegraph?

Installation

$ brew install flamegraph
$ ls /opt/homebrew/Cellar/flamegraph/1.0_1/bin
difffolded.pl                pkgsplit-perf.pl             stackcollapse-elfutils.pl    stackcollapse-instruments.pl stackcollapse-perf-sched.awk stackcollapse-recursive.pl   stackcollapse-vsprof.pl
files.pl                     range-perf.pl                stackcollapse-gdb.pl         stackcollapse-jstack.pl      stackcollapse-perf.pl        stackcollapse-sample.awk     stackcollapse-vtune.pl
flamegraph.pl                stackcollapse-aix.pl         stackcollapse-go.pl          stackcollapse-ljp.awk        stackcollapse-pmc.pl         stackcollapse-stap.pl        stackcollapse.pl
Enter fullscreen mode Exit fullscreen mode

Profile

Run application and extract Process ID(PID), that requires to record data.
For example I am looking to debug my app that has name crystal-run:

$ sudo dtrace -x ustackframes=100 -n 'profile-97 /pid == $(pgrep -n crystal-run)/ { @[ustack()] = count(); } tick-60s { exit(0); }' -o out.user_stacks
$ stackcollapse.pl out.user_stacks | flamegraph.pl > user.svg
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . .