Nodejs CPU profiling while in production
Every once in a while it’s handy to profile your nodejs server while running in production. This does not allow any profiling technique that would harm performance. Luckily there is linux’s perf
!
The following guide is tested Ubuntu 16.04.
Prerequisites
linux-tools
package for your kernel version must be installed.
1 | sudo apt-get install -y linux-tools-common linux-tools-generic linux-tools-`uname -r` |
You node process must run with --perf_basic_prof_only_functions
1 | node --perf_basic_prof_only_functions index.js |
Keep in mind that this will write ~2mb per hour to /tmp/perf-<pid>.map
!
Record
Collect data for $PID, 100 times per second for 30 seconds. Creates perf.data
in cwd.
1 | sudo perf record -p $PID -F 100 -g -- sleep 30 |
Give root access to /tmp/perf-<pid>.map
and generate stack report
1 | sudo chown root /tmp/perf-$PID.map |
Visualize
1 | perf report |
For a graphic representation try FlameGraph.
1 | git clone http://github.com/brendangregg/FlameGraph |
Open out.nodestacks.svg
and click away ;)
Read more
The Mysterious Fiber Bomb Problem: A Debugging Story