Those of you with a Solaris background would remember pstack well. For those who don’t, it is a tool from the ptools family, which attaches to the process and prints an execution stack trace. Like the other tools in the ptools family (pmap, pfiles..), its really awesome for debugging, and insightful as a poor man’s profiler.
Then there is jstack, a tool that prints stack traces from JVM. Which made me wonder, why should there not be a jsstack, to print a stack trace for node.js. After all, what is good for the goose is good for the gander, and the debugging proces could be much improved if we had a jsstack like tool for node.js.
With that thought in mind, and an idle Saturday Morning to spare, I cobbled up jsstack.
Run it as
and it shall reward you with a precious stack trace. And the bonus - there is only one execution stack (hail the mighty event loop that saved us from mind boggling complexity of threads).
Here’s some sample output from our express.js app under load.
If you are interested in how it works, it uses the built in V8 debugger in node.js to fetch a stack trace. Sends a SIGUSR1 to the the process to enable the debugger, and then uses the standard debugger client that ships with node to fetch a backtrace. This technique can also be used to enable code injection, e.g. for dynamically modifying configuration without restarting the process. The source is less than 50 lines - you can check it on github
This post is the second in my series on Profiling node.js apps. Check out some other stuff I have blogged about here
Related
- What I learnt profiling a node.js app.
- Real Time Dashboard using GA and dashing
- 10 steps to nodejs nirvana in production
- Profiling node apps
- A Magento Api client in nodejs