Tracing

Be very careful with callbacks used in conjunction with this module

Many of these callbacks interact directly with asynchronous subsystems in a
synchronous fashion. That is to say, you may be in a callback where a call to
could result in an infinite recursive loop. Also of note, many
of these callbacks are in hot execution code paths. That is to say your
callbacks are executed quite often in the normal operation of Node, so be wary
of doing CPU bound or synchronous workloads in these functions. Consider a ring
buffer and a timer to defer processing.

require('tracing') to use this module.

The v8 property is an , it exposes events and interfaces
specific to the version of v8 built with node. These interfaces are subject
to change by upstream and are therefore not covered under the stability index.

function (before, after) { }

Emitted each time a GC run is completed.

before and after are objects with the following properties:

  1. {
  2. type: 'mark-sweep-compact',
  3. flags: 0,
  4. timestamp: 905535650119053,
  5. total_heap_size: 6295040,
  6. total_heap_size_executable: 4194304,
  7. total_physical_size: 6295040,
  8. used_heap_size: 2855416,
  9. heap_size_limit: 1535115264
  10. }

getHeapStatistics()

Returns an object with the following properties

Async Listeners

The AsyncListener API is the JavaScript interface for the AsyncWrap
class which allows developers to be notified about key events in the
lifetime of an asynchronous event. Node performs a lot of asynchronous
events internally, and significant use of this API may have a
significant performance impact on your application.

  • callbacksObj {Object} Contains optional callbacks that will fire at
    specific times in the life cycle of the asynchronous event.
  • userData {Value} a value that will be passed to all callbacks.

To begin capturing asynchronous events pass either the callbacksObj or
pass an existing AsyncListener instance to tracing.addAsyncListener().
The same AsyncListener instance can only be added once to the active
queue, and subsequent attempts to add the instance will be ignored.

To stop capturing pass the AsyncListener instance to
. This does not mean the
AsyncListener previously added will stop triggering callbacks. Once
attached to an asynchronous event it will persist with the lifetime of the
asynchronous call stack.

Explanation of function parameters:

callbacksObj: An Object which may contain several optional fields:

  • before(context, userData): A Function that is called immediately
    before the asynchronous callback is about to run. It will be passed both
    the context (i.e. this) of the calling function and the userData
    either returned from create() or passed during construction (if
    either occurred).

  • after(context, userData): A Function called immediately after
    the asynchronous event’s callback has run. Note this will not be called
    if the callback throws and the error is not handled.

  • error(userData, error): A Function called if the event’s
    callback threw. If this registered callback returns true then Node will
    assume the error has been properly handled and resume execution normally.
    When multiple error() callbacks have been registered only one of
    those callbacks needs to return true for AsyncListener to accept that
    the error has been handled, but all error() callbacks will always be run.

userData: A Value (i.e. anything) that will be, by default,
attached to all new event instances. This will be overwritten if a Value
is returned by create().

  1. tracing.createAsyncListener({
  2. create: function listener(value) {
  3. // value === true
  4. return false;
  5. }, {
  6. before: function before(context, value) {
  7. // value === false
  8. }
  9. }, true);

Note: The EventEmitter, while used to emit status of an asynchronous
event, is not itself asynchronous. So create() will not fire when
an event is added, and before()/after() will not fire when emitted
callbacks are called.

Returns a constructed AsyncListener object and immediately adds it to
the listening queue to begin capturing asynchronous events.

Function parameters can either be the same as
, or a constructed
object.

Example usage for capturing errors:

Removes the AsyncListener from the listening queue.

Removing the AsyncListener from the active queue does not mean the
asyncListener callbacks will cease to fire on the events they’ve been
registered. Subsequently, any asynchronous events fired during the
execution of a callback will also have the same asyncListener callbacks
attached for future execution. For example:

  1. var fs = require('fs');
  2. var key = tracing.createAsyncListener({
  3. create: function asyncListener() {
  4. // Write directly to stdout or we'll enter a recursive loop
  5. fs.writeSync(1, 'You summoned me?\n');
  6. }
  7. });
  8. // We want to begin capturing async events some time in the future.
  9. setTimeout(function() {
  10. tracing.addAsyncListener(key);
  11. // Perform a few additional async events.
  12. setTimeout(function() {
  13. setImmediate(function() {
  14. process.nextTick(function() { });
  15. });
  16. });
  17. // Removing the listener doesn't mean to stop capturing events that
  18. // have already been added.
  19. tracing.removeAsyncListener(key);
  20. }, 100);
  21. // Output:
  22. // You summoned me?
  23. // You summoned me?
  24. // You summoned me?

The fact that we logged 4 asynchronous events is an implementation detail
of Node’s Timers.

To stop capturing from a specific asynchronous event stack
tracing.removeAsyncListener() must be called from within the call
stack itself. For example:

The user must be explicit and always pass the they wish
to remove. It is not possible to simply remove all listeners at once.