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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Joyee Cheung 2019-03-12 18:34:22 +08:00
parent e2a2f9398e
commit a91d36fcc1
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
4 changed files with 27 additions and 33 deletions

View File

@ -197,6 +197,13 @@ if (!config.noBrowserGlobals) {
defineOperation(global, 'setImmediate', timers.setImmediate);
}
// 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() {

View File

@ -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() {
// Patch the process object with legacy properties and normalizations
@ -162,26 +159,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,

View File

@ -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

View File

@ -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