Skip to content
Permalink
Browse files

process: set the trace category update handler during bootstrap

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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information...
joyeecheung authored and targos committed Mar 12, 2019
1 parent 9ef0a29 commit 1481e5b5c124e848a12eb730ae269d71787a6c65
@@ -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() {
@@ -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();
@@ -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,
@@ -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
@@ -11,7 +11,6 @@
namespace node {

using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
@@ -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

0 comments on commit 1481e5b

Please sign in to comment.
You can’t perform that action at this time.