This document will help you generate flamegraphs for your node processes on OS X.
You can read about the various types of flamegraphs and how they are useful
in Brendan Gregg's wonderful write up here.
By the end of this document, you should have a flamegraph for you node app to play with.
Let's get started.
- Install Xcode from the App Store if you don't have it already
- In this exercise, we need Xcode mainly so that we can use the
instruments.appthat comes packaged with it. Though it is quite a joy to have when you're trying to bridgeC/C++code with js land (see lldb-jbt)
- In this exercise, we need Xcode mainly so that we can use the
- Install
node(version >0.11.x). Distributions can be found here
- Open
instruments.app - Look for
Time Profilerin the window that is presented to you and open it - Run your
nodeprocess with the--perf-basic-profflag. This flag tellsv8to generate aperf-<pid>.mapfile (in/tmp/by default) which can be consumed by theperftool on *nix systems. You can read more here
- Example
node --perf-basic-prof app.js- It's a good idea to print the
process.pidinapp.jsas we'll be using it laterconsole.log(process.pid)should suffice
- It's a good idea to print the
- Go back to the
Time Profilerwindow ininstruments.app - Click
All Processesin the UI and select yournodeprocess (based on your processpid) - Hit the
Recordbutton (the red button in the left corner in theTime ProfilerUI) to start recording a trace - Click
Stopwhen you've a large enough sample to work with (this is completely subjective as it depends on what you're actually looking for) - In the
Call Treesection of theTime ProfilerUI, expand a tree fully (option + right arrow) - In the menu bar for
instruments.app, click onInstrument->Export track for 'Time Profiler - node'...and save thecsvfile - Goto the flamegraph app by thlorenz.
- Upload you
csvyou saved instep 9as the file forCallgraph
- You should see a
flamegraphappear with hex memory addresses. The next step will make those addresses take on human readable names
- Upload the
mapfile as the file forMapfile
- To get to the
mapfile, do the following- Click
choose filein theflamegraphweb app UI - In the file dialog that opens up, press
cmd + shift + g - Type
/tmpin the dialog that pops up and hitGo - In the folder that opens up, select
perf-<your node process pid>.mapand clickOpen
- Click
- Aaand we're done. The
svgfile should now have human readable symbols in place of hex memory addresses.
Happy debuggin'!




Actually there is a faster way to launch instruments if you got a local Node.js copy.
Load it into Xcode, set your args in Run tab and configure Profiling tab.
Then simply press
CMD-Iin order to start profiling with Instruments. That's faster than attaching to process every time and the preferred way if you do this repeatedly.That also allows you to quickly patch v8 in order to ensure that
perf-<pid>.mapfile gets flushed continuously.I'm pretty sure I used the other technique in this talk and it quickly becomes obvious how annoying it is to attach repeatedly.