10

node.js Flame Graphs on Linux

 4 years ago
source link: http://www.brendangregg.com/blog/2014-09-17/node-flame-graphs-on-linux.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

node.js Flame Graphs on Linux

17 Sep 2014

CPU flame graphs are a useful visualization application stack traces, allowing you to quickly identify and quantify what to tune to improve performance. For Node.js they have solved countless problems on systems which have DTrace for sampling stack traces. But what about Linux?

At Netflix we have node.js in production at scale, on Linux instances in AWS EC2, and we create flame graphs using Linux perf_events and v8's --perf_basic_prof option (also works as --perf-basic-prof). In this quick blog post, I'll share how it works and how you can do it, and what needs to be fixed to improve it further.

1. The problem

Using perf_events to profile CPU usage on node.js 0.10.23:

It's interactive: mouse-over elements for details, and click the SVG to zoom. The CPU flame graphs page explains how to interpret these, and this was created using the instructions in the Linux perf section.

This flame graph is partially-useful, as I can see system and v8 library symbols. However, it is missing JavaScript symbols (the blank rectangles), since v8, like the JVM, compiles and places symbols just in time (JIT).

2. Linux perf_events JIT support

In 2009, Linux perf_events added JIT symbol support, so that symbols from language virtual machines like the JVM could be inspected. It works in the following amazingly simple way:

  1. Your JIT application must be modified to create a /tmp/perf-PID.map file, which is a simple text database containing symbol addresses (in hex), sizes, and symbol names.
  2. That's it.

perf already looks for the /tmp/perf-PID.map file, and if it finds it, it uses it for symbol translations. So only v8 needed to be modified.

3. v8 --perf-basic-prof support

In November 2013, v8 added perf_events support, enabled using the --perf-basic-prof option. This made it into node v0.11.13. It works like this:

# ~/node-v0.11.13-linux-x64/bin/node --perf-basic-prof hello.js &
[1] 31441
# ls -l /tmp/perf-31441.map
-rw-r--r-- 1 root root 81920 Sep 17 20:41 /tmp/perf-31441.map
# tail /tmp/perf-31441.map
14cec4db98a0 f Stub:BinaryOpICWithAllocationSiteStub(ADD_CreateAllocationMementos:String*Generic->String)
14cec4db9920 f Stub:BinaryOpICWithAllocationSiteStub(ADD_CreateAllocationMementos:String*String->String)
14cec4db99a0 f Stub:BinaryOpICWithAllocationSiteStub(ADD_CreateAllocationMementos:String*Smi->String)
14cec4db9a20 22c LazyCompile:~nextTick node.js:389
14cec4db9cc0 156 Stub:KeyedLoadElementStub
14cec4db9e80 22 KeyedLoadIC:
14cec4db9f20 22 KeyedLoadIC:
14cec4db9fc0 56 Stub:DoubleToIStub
14cec4dba080 10c Stub:KeyedStoreElementStub

This text file is what perf_events reads.

4. node.js Flame Graphs

Now that we have node 0.11.13+ running with --perf-basic-prof, we can create a flame graph using:

$ sudo bash
# perf record -F 99 -p `pgrep -n node` -g -- sleep 30
# perf script > out.nodestacks01
# git clone --depth 1 http://github.com/brendangregg/FlameGraph
# cd FlameGraph
# ./stackcollapse-perf.pl < ../out.nodestacks01 | ./flamegraph.pl > ../out.nodestacks01.svg

You can also use stackvis, by Dave Pacheco, a node.js implementation which has extra features.

Here's an example result:

Note the JavaScript symbols are now readable. Click the SVG to zoom in. This actual flame graph isn't very interesting, as I'm just testing a dummy app to test out --perf-basic-prof.

Thanks to Trevor Norris for first posting the instructions for doing this in a short gist, which you may find useful to read. He also provides a script to facilitate this.

WARNING: map file growth

We can currently only use --perf-basic-prof for short periods (hours), due to bug 3453: the perf.map file can grow endlessly, eating Gbytes in a few days. It looks like symbols are moving location (they are supposed to stay put with --perf-basic-prof), causing the map file to keep growing.

UPDATE (2016): A new option, --perf_basic_prof_only_functions (or --perf-basic-prof-only-functions) was introduced to address this bug by only logging interesting types of symbols, cutting down on map file growth. If map file growth is a problem for you, try out this option instead.

We're doing more at Netflix with node.js analysis. Stay tuned, and also see the Netflix Tech Blog.


Recommend

  • 11
    • www.brendangregg.com 4 years ago
    • Cache

    Coloring Flame Graphs: Code Hues

    Coloring Flame Graphs: Code Hues 30 Jul 2017 I recently improved flame graph code coloring. If you're automating or implementing flame graphs, this is a small detail that may interest you. (For an intro to flame graphs, see...

  • 10

    USENIX/LISA 2013 Blazing Performance with Flame Graphs 23 Apr 2017 In 2013 I gave a plenary at USENIX LISA on flame graphs: my visualiza...

  • 9
    • www.brendangregg.com 4 years ago
    • Cache

    Flame Graphs vs Tree Maps vs Sunburst

    Yesterday I posted about flame graphs for file systems, showing how they can visualize where disk space is consumed. Many people have

  • 5

    Where has my disk space gone? Flame graphs for file systems 05 Feb 2017 My laptop was recently running low on available disk space, and it was a mystery as to why. I have different tools to explore the file system, including...

  • 7
    • www.brendangregg.com 4 years ago
    • Cache

    Unikernel Profiling: Flame Graphs from dom0

    Unikernel Profiling: Flame Graphs from dom0 27 Jan 2016 Is a unikernel an impenetrable black box, resistant to profilers, tracers, fire, and poison? At SCaLE14x this wee...

  • 8

    Java Mixed-Mode Flame Graphs at Netflix, JavaOne 2015 06 Nov 2015 At JavaOne this year I gave a talk on Java mixed-mode flame graphs, which we are rolling out at Netflix for CPU analysis and more. These make use of a new fea...

  • 8
    • www.brendangregg.com 4 years ago
    • Cache

    FreeBSD Off-CPU Flame Graphs

    FreeBSD Off-CPU Flame Graphs 12 Mar 2015 While I was giving a talk on FreeBSD flame graphs, a new technique was suggested by the audience for gathering off-CPU stacks, which is something I'd like to see improved and included...

  • 6
    • www.brendangregg.com 4 years ago
    • Cache

    CPI Flame Graphs: Catching Your CPUs Napping

    CPI Flame Graphs: Catching Your CPUs Napping 31 Oct 2014 When you see high CPU usage, you may be forgiven for believing that your CPUs must be furiously executing software, like obedient robots that never rest. In reality, y...

  • 5
    • www.brendangregg.com 3 years ago
    • Cache

    FreeBSD Flame Graphs

    FreeBSD Flame Graphs 10 Mar 2015 At the last FreeBSD Developer and Vendor Summit, I gave a talk on "Flame Graphs for FreeBSD", where I summarized the different types (CPU, memory, disk I/O, off-CPU, chain graphs), showed how...

  • 9

    Java Performance Analysis on Linux with Flame Graphs 1. Sep 2016 Java Performance Analysis on Linux with Flame Graphs Brendan Gregg Senior Performanc...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK