Working on performance analysis with an interpreted language poses some interesting problems. Fortunately, Node.js has built in support for DTrace thanks to the guys at Joyent. These built-in facilities make it easy to trace Node.js, and profile its time on the CPU to get an in-depth view of the execution of the program. Tools also exist that use DTrace’s ability to profile Node.js to create visual representations of the execution stack, such as Brendan Gregg’s Flame Graph Tool.

However, when tracking latency from an application-aware point-of-view, it is necessary to inject probes into the code itself, setting checkpoints, or entry and return points. Using the DTrace Provider Node.js Module written by Chris Andrews, I’ve created a repository of examples on Github showing the DTrace provider in action.

Check out the repository here https://github.com/bahamas10/node-dtrace-examples/.

Function Tracing

Description

An example from the repository shows how to trace function call depth and latency in Node.js with DTrace

Inspired by dapptrace by Brendan Gregg

Terminal 1:

root@Operationss-MacBook-Pro ~ #  ./simple-nested-functions.js
Looping with 5 second intervals... Beging DTracing now! (provider "nodefunc", pid 39677)

Terminal 2:

root@Operationss-MacBook-Pro ~ #  ./func-trace.d
Tracing...
[2012 Apr 26 16:06:04]  --> func-a-entry
[2012 Apr 26 16:06:04]    --> func-b-entry
[2012 Apr 26 16:06:04]      --> func-c-entry
[2012 Apr 26 16:06:04]      <-- func-c-return (24268 ns / 24 us / 0 ms)
[2012 Apr 26 16:06:04]    <-- func-b-return (95626 ns / 95 us / 0 ms)
[2012 Apr 26 16:06:04]  <-- func-a-return (182276 ns / 182 us / 0 ms)
[2012 Apr 26 16:06:09]  --> func-a-entry
[2012 Apr 26 16:06:09]    --> func-b-entry
[2012 Apr 26 16:06:09]      --> func-c-entry
[2012 Apr 26 16:06:09]      <-- func-c-return (20761 ns / 20 us / 0 ms)
[2012 Apr 26 16:06:09]    <-- func-b-return (81974 ns / 81 us / 0 ms)
[2012 Apr 26 16:06:09]  <-- func-a-return (163436 ns / 163 us / 0 ms)

Node DTrace Example Repository

Usage

This repository is broken up into separate folders. Enter any of the folders to find example Node and DTrace scripts using the dtrace-provider.

Installation

Run npm install before using any of the examples.

Contribute

The more examples we can get in here, the better. If you have any good example Node.js or DTrace scripts, fork the repo, commit them, and submit a pull request.