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:
{
type: 'mark-sweep-compact',
flags: 0,
timestamp: 905535650119053,
total_heap_size: 6295040,
total_heap_size_executable: 4194304,
total_physical_size: 6295040,
used_heap_size: 2855416,
heap_size_limit: 1535115264
}
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 theAsyncListener
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)
: AFunction
that is called immediately
before the asynchronous callback is about to run. It will be passed both
thecontext
(i.e.this
) of the calling function and theuserData
either returned fromcreate()
or passed during construction (if
either occurred).after(context, userData)
: AFunction
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)
: AFunction
called if the event’s
callback threw. If this registered callback returnstrue
then Node will
assume the error has been properly handled and resume execution normally.
When multipleerror()
callbacks have been registered only one of
those callbacks needs to returntrue
forAsyncListener
to accept that
the error has been handled, but allerror()
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()
.
tracing.createAsyncListener({
create: function listener(value) {
// value === true
return false;
}, {
before: function before(context, value) {
// value === false
}
}, 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 theasyncListener
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:
var fs = require('fs');
var key = tracing.createAsyncListener({
create: function asyncListener() {
// Write directly to stdout or we'll enter a recursive loop
fs.writeSync(1, 'You summoned me?\n');
}
});
// We want to begin capturing async events some time in the future.
setTimeout(function() {
tracing.addAsyncListener(key);
// Perform a few additional async events.
setTimeout(function() {
setImmediate(function() {
process.nextTick(function() { });
});
});
// Removing the listener doesn't mean to stop capturing events that
// have already been added.
tracing.removeAsyncListener(key);
}, 100);
// Output:
// You summoned me?
// You summoned me?
// 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 stacktracing.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.