Skip to content

Commit

Permalink
process: set the trace category update handler during bootstrap
Browse files Browse the repository at this point in the history
Set the trace category update handler during bootstrap, but delay
the initial invocation of it until pre-execution. In addition, do
not serialize the `node.async_hooks` category state when loading
the trace_event binding during bootstrap, since it depends on
run time states (e.g. CLI flags). Instead, use the
`isTraceCategoryEnabled` v8 intrinsics to query that value during
pre-execution.

PR-URL: #26605
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
joyeecheung authored and targos committed Mar 27, 2019
1 parent 9ef0a29 commit 1481e5b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
7 changes: 7 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ if (process.platform === 'win32') {
}
}

// Set the per-Environment callback that will be called
// when the TrackingTraceStateObserver updates trace state.
// Note that when NODE_USE_V8_PLATFORM is true, the observer is
// attached to the per-process TracingController.
const { setTraceCategoryStateUpdateHandler } = internalBinding('trace_events');
setTraceCategoryStateUpdateHandler(perThreadSetup.toggleTraceCategoryState);

// process.allowedNodeEnvironmentFlags
Object.defineProperty(process, 'allowedNodeEnvironmentFlags', {
get() {
Expand Down
26 changes: 3 additions & 23 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';

const { getOptionValue } = require('internal/options');
// Lazy load internal/trace_events_async_hooks only if the async_hooks
// trace event category is enabled.
let traceEventsAsyncHook;

function prepareMainThreadExecution() {
setupTraceCategoryState();
Expand Down Expand Up @@ -136,26 +133,9 @@ function initializeReportSignalHandlers() {
}

function setupTraceCategoryState() {
const {
asyncHooksEnabledInitial,
setTraceCategoryStateUpdateHandler
} = internalBinding('trace_events');

toggleTraceCategoryState(asyncHooksEnabledInitial);
setTraceCategoryStateUpdateHandler(toggleTraceCategoryState);
}

// Dynamically enable/disable the traceEventsAsyncHook
function toggleTraceCategoryState(asyncHooksEnabled) {
if (asyncHooksEnabled) {
if (!traceEventsAsyncHook) {
traceEventsAsyncHook =
require('internal/trace_events_async_hooks').createHook();
}
traceEventsAsyncHook.enable();
} else if (traceEventsAsyncHook) {
traceEventsAsyncHook.disable();
}
const { isTraceCategoryEnabled } = internalBinding('trace_events');
const { toggleTraceCategoryState } = require('internal/process/per_thread');
toggleTraceCategoryState(isTraceCategoryEnabled('node.async_hooks'));
}

// In general deprecations are intialized wherever the APIs are implemented,
Expand Down
17 changes: 17 additions & 0 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,24 @@ function buildAllowedFlags() {
));
}

// Lazy load internal/trace_events_async_hooks only if the async_hooks
// trace event category is enabled.
let traceEventsAsyncHook;
// Dynamically enable/disable the traceEventsAsyncHook
function toggleTraceCategoryState(asyncHooksEnabled) {
if (asyncHooksEnabled) {
if (!traceEventsAsyncHook) {
traceEventsAsyncHook =
require('internal/trace_events_async_hooks').createHook();
}
traceEventsAsyncHook.enable();
} else if (traceEventsAsyncHook) {
traceEventsAsyncHook.disable();
}
}

module.exports = {
toggleTraceCategoryState,
assert,
buildAllowedFlags,
wrapProcessMethods
Expand Down
10 changes: 0 additions & 10 deletions src/node_trace_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace node {

using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
Expand Down Expand Up @@ -149,15 +148,6 @@ void NodeCategorySet::Initialize(Local<Object> target,
.FromJust();
target->Set(context, trace,
binding->Get(context, trace).ToLocalChecked()).FromJust();

// Initial value of async hook trace events
bool async_hooks_enabled = (*(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE1(async_hooks)))) != 0;
target
->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "asyncHooksEnabledInitial"),
Boolean::New(env->isolate(), async_hooks_enabled))
.FromJust();
}

} // namespace node
Expand Down

0 comments on commit 1481e5b

Please sign in to comment.