How 0x works

When we use 0x to start our server, two processes are started. The first is theĀ node binary, 0x simply passes all non-0x arguments to node so our code is executed.

The second process is a system stack tracing tool (perf in the case of Linux and dtrace in the case of macOS and SmartOS) to capture every function call in the C layer.

When 0x starts the node process, it adds a flag named --perf-basic-prof, which is a V8 option (the JavaScript engine), which essentially enables the mapping of the C++ V8 function calls to the corresponding JavaScript function calls.

The stacks are output to a folder, and in the case of macOS further mapping is applied. The stack output contains snapshots of the call stack for each 1 millisecond period the CPU was sampled. If a function is observed at the top of the stack for a particular snapshot (and it's not the ultimate parent of the stack) then it's taken a full millisecond on stack. If this happens multiple times, that's a strong indicator that it's a bottleneck.

To generate the flamegraph 0x processes these stacks into a JSON tree of parent and child stacksĀ then creates an HTML file containing the JSON plus a script that uses D3.js to visualize the parent child relationships and other metadata in a flamegraph format.