process: simplify the setup of async hooks trace events
- Remove `trace_category_state` from `Environment` - since this is only accessed in the bootstrap process and later in the trace category update handler, we could just pass the initial values into JS land via the trace_events binding, and pass the dynamic values directly to the handler later, instead of accessing them out-of-band via the AliasedBuffer. - Instead of creating the hooks directly in `trace_events_async_hooks.js`, export the hook factory and create the hooks in trace category state toggle. PR-URL: https://github.com/nodejs/node/pull/26062 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
435eea6400
commit
c435f069eb
@ -1,6 +1,9 @@
|
||||
'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();
|
||||
@ -27,32 +30,27 @@ function prepareMainThreadExecution() {
|
||||
|
||||
function setupTraceCategoryState() {
|
||||
const {
|
||||
traceCategoryState,
|
||||
asyncHooksEnabledInitial,
|
||||
setTraceCategoryStateUpdateHandler
|
||||
} = internalBinding('trace_events');
|
||||
const kCategoryAsyncHooks = 0;
|
||||
let traceEventsAsyncHook;
|
||||
|
||||
function toggleTraceCategoryState() {
|
||||
// Dynamically enable/disable the traceEventsAsyncHook
|
||||
const asyncHooksEnabled = !!traceCategoryState[kCategoryAsyncHooks];
|
||||
|
||||
if (asyncHooksEnabled) {
|
||||
// Lazy load internal/trace_events_async_hooks only if the async_hooks
|
||||
// trace event category is enabled.
|
||||
if (!traceEventsAsyncHook) {
|
||||
traceEventsAsyncHook = require('internal/trace_events_async_hooks');
|
||||
}
|
||||
traceEventsAsyncHook.enable();
|
||||
} else if (traceEventsAsyncHook) {
|
||||
traceEventsAsyncHook.disable();
|
||||
}
|
||||
}
|
||||
|
||||
toggleTraceCategoryState();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// In general deprecations are intialized wherever the APIs are implemented,
|
||||
// this is used to deprecate APIs implemented in C++ where the deprecation
|
||||
// utitlities are not easily accessible.
|
||||
|
@ -83,4 +83,4 @@ function createHook() {
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = createHook();
|
||||
exports.createHook = createHook;
|
||||
|
@ -455,11 +455,6 @@ Environment::should_abort_on_uncaught_toggle() {
|
||||
return should_abort_on_uncaught_toggle_;
|
||||
}
|
||||
|
||||
inline AliasedBuffer<uint8_t, v8::Uint8Array>&
|
||||
Environment::trace_category_state() {
|
||||
return trace_category_state_;
|
||||
}
|
||||
|
||||
inline AliasedBuffer<int32_t, v8::Int32Array>&
|
||||
Environment::stream_base_state() {
|
||||
return stream_base_state_;
|
||||
|
12
src/env.cc
12
src/env.cc
@ -21,6 +21,7 @@
|
||||
namespace node {
|
||||
|
||||
using errors::TryCatchScope;
|
||||
using v8::Boolean;
|
||||
using v8::Context;
|
||||
using v8::EmbedderGraph;
|
||||
using v8::External;
|
||||
@ -152,9 +153,8 @@ void Environment::TrackingTraceStateObserver::UpdateTraceCategoryState() {
|
||||
return;
|
||||
}
|
||||
|
||||
env_->trace_category_state()[0] =
|
||||
*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
|
||||
TRACING_CATEGORY_NODE1(async_hooks));
|
||||
bool async_hooks_enabled = (*(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
|
||||
TRACING_CATEGORY_NODE1(async_hooks)))) != 0;
|
||||
|
||||
Isolate* isolate = env_->isolate();
|
||||
HandleScope handle_scope(isolate);
|
||||
@ -163,8 +163,9 @@ void Environment::TrackingTraceStateObserver::UpdateTraceCategoryState() {
|
||||
return;
|
||||
TryCatchScope try_catch(env_);
|
||||
try_catch.SetVerbose(true);
|
||||
cb->Call(env_->context(), Undefined(isolate),
|
||||
0, nullptr).ToLocalChecked();
|
||||
Local<Value> args[] = {Boolean::New(isolate, async_hooks_enabled)};
|
||||
cb->Call(env_->context(), Undefined(isolate), arraysize(args), args)
|
||||
.ToLocalChecked();
|
||||
}
|
||||
|
||||
static std::atomic<uint64_t> next_thread_id{0};
|
||||
@ -183,7 +184,6 @@ Environment::Environment(IsolateData* isolate_data,
|
||||
tick_info_(context->GetIsolate()),
|
||||
timer_base_(uv_now(isolate_data->event_loop())),
|
||||
should_abort_on_uncaught_toggle_(isolate_, 1),
|
||||
trace_category_state_(isolate_, kTraceCategoryCount),
|
||||
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
|
||||
flags_(flags),
|
||||
thread_id_(thread_id == kNoThreadId ? AllocateThreadId() : thread_id),
|
||||
|
@ -707,7 +707,6 @@ class Environment {
|
||||
inline AliasedBuffer<uint32_t, v8::Uint32Array>&
|
||||
should_abort_on_uncaught_toggle();
|
||||
|
||||
inline AliasedBuffer<uint8_t, v8::Uint8Array>& trace_category_state();
|
||||
inline AliasedBuffer<int32_t, v8::Int32Array>& stream_base_state();
|
||||
|
||||
// The necessary API for async_hooks.
|
||||
@ -1022,8 +1021,6 @@ class Environment {
|
||||
AliasedBuffer<uint32_t, v8::Uint32Array> should_abort_on_uncaught_toggle_;
|
||||
int should_not_abort_scope_counter_ = 0;
|
||||
|
||||
// Attached to a Uint8Array that tracks the state of trace category
|
||||
AliasedBuffer<uint8_t, v8::Uint8Array> trace_category_state_;
|
||||
std::unique_ptr<TrackingTraceStateObserver> trace_state_observer_;
|
||||
|
||||
AliasedBuffer<int32_t, v8::Int32Array> stream_base_state_;
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "base_object-inl.h"
|
||||
#include "env.h"
|
||||
#include "node.h"
|
||||
#include "node_internals.h"
|
||||
#include "node_v8_platform-inl.h"
|
||||
#include "tracing/agent.h"
|
||||
|
||||
@ -10,6 +11,7 @@
|
||||
namespace node {
|
||||
|
||||
using v8::Array;
|
||||
using v8::Boolean;
|
||||
using v8::Context;
|
||||
using v8::Function;
|
||||
using v8::FunctionCallbackInfo;
|
||||
@ -148,9 +150,14 @@ void NodeCategorySet::Initialize(Local<Object> target,
|
||||
target->Set(context, trace,
|
||||
binding->Get(context, trace).ToLocalChecked()).FromJust();
|
||||
|
||||
target->Set(context,
|
||||
FIXED_ONE_BYTE_STRING(env->isolate(), "traceCategoryState"),
|
||||
env->trace_category_state().GetJSArray()).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
|
||||
|
Loading…
x
Reference in New Issue
Block a user