From 1481e5b5c124e848a12eb730ae269d71787a6c65 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 12 Mar 2019 18:34:22 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/26605 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- lib/internal/bootstrap/node.js | 7 +++++++ lib/internal/bootstrap/pre_execution.js | 26 +++---------------------- lib/internal/process/per_thread.js | 17 ++++++++++++++++ src/node_trace_events.cc | 10 ---------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index e0c220d2a8c3fa..6cd480ffe08f3c 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -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() { diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 52a6be6dee63c3..607951a0bb4aba 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -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, diff --git a/lib/internal/process/per_thread.js b/lib/internal/process/per_thread.js index 37b9e31167a336..9e53ac3a1982da 100644 --- a/lib/internal/process/per_thread.js +++ b/lib/internal/process/per_thread.js @@ -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 diff --git a/src/node_trace_events.cc b/src/node_trace_events.cc index 5e30df41c4f4fa..1fad37743fb418 100644 --- a/src/node_trace_events.cc +++ b/src/node_trace_events.cc @@ -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 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