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.app
that 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 Profiler
in the window that is presented to you and open it - Run your
node
process with the--perf-basic-prof
flag. This flag tellsv8
to generate aperf-<pid>.map
file (in/tmp/
by default) which can be consumed by theperf
tool on *nix systems. You can read more here
- Example
node --perf-basic-prof app.js
- It's a good idea to print the
process.pid
inapp.js
as we'll be using it laterconsole.log(process.pid)
should suffice
- It's a good idea to print the
- Go back to the
Time Profiler
window ininstruments.app
- Click
All Processes
in the UI and select yournode
process (based on your processpid
) - Hit the
Record
button (the red button in the left corner in theTime Profiler
UI) to start recording a trace - Click
Stop
when 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 Tree
section of theTime Profiler
UI, expand a tree fully (option + right arrow
) - In the menu bar for
instruments.app
, click onInstrument
->Export track for 'Time Profiler - node'...
and save thecsv
file - Goto the flamegraph app by thlorenz.
- Upload you
csv
you saved instep 9
as the file forCallgraph
- You should see a
flamegraph
appear with hex memory addresses. The next step will make those addresses take on human readable names
- Upload the
map
file as the file forMapfile
- To get to the
map
file, do the following- Click
choose file
in theflamegraph
web app UI - In the file dialog that opens up, press
cmd + shift + g
- Type
/tmp
in the dialog that pops up and hitGo
- In the folder that opens up, select
perf-<your node process pid>.map
and clickOpen
- Click
- Aaand we're done. The
svg
file 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-I
in 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>.map
file 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.