From 758c02ec5dce2426869362b4e3c4bf4bd5684314 Mon Sep 17 00:00:00 2001 From: legendecas Date: Thu, 5 May 2022 23:53:41 +0800 Subject: [PATCH] src: replace TraceEventScope with sync events According to the chrome trace event format document, works that are performed on one single thread should be traced with sync duration events. In this way, these events can be grouped under one thread and the trace event viewer can estimate the CPU usage of that thread. PR-URL: https://github.com/nodejs/node/pull/42977 Reviewed-By: Darshan Sen --- src/api/hooks.cc | 3 +-- src/env.cc | 16 ++++++---------- src/node_contextify.cc | 32 +++++++++----------------------- src/node_internals.h | 17 ----------------- 4 files changed, 16 insertions(+), 52 deletions(-) diff --git a/src/api/hooks.cc b/src/api/hooks.cc index 4076a5523e0f34..bd26e6d150d53e 100644 --- a/src/api/hooks.cc +++ b/src/api/hooks.cc @@ -32,8 +32,7 @@ void EmitBeforeExit(Environment* env) { } Maybe EmitProcessBeforeExit(Environment* env) { - TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), - "BeforeExit", env); + TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "BeforeExit"); if (!env->destroy_async_id_list()->empty()) AsyncWrap::DestroyAsyncIdsCallback(env); diff --git a/src/env.cc b/src/env.cc index e2e4854f5e02b6..88acd1d9e02cae 100644 --- a/src/env.cc +++ b/src/env.cc @@ -673,8 +673,7 @@ void Environment::PrintSyncTrace() const { void Environment::RunCleanup() { started_cleanup_ = true; - TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), - "RunCleanup", this); + TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup"); bindings_.clear(); CleanupHandles(); @@ -716,8 +715,7 @@ void Environment::RunCleanup() { } void Environment::RunAtExitCallbacks() { - TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), - "AtExit", this); + TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "AtExit"); for (ExitCallback at_exit : at_exit_functions_) { at_exit.cb_(at_exit.arg_); } @@ -743,8 +741,8 @@ void Environment::RunAndClearInterrupts() { } void Environment::RunAndClearNativeImmediates(bool only_refed) { - TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), - "RunAndClearNativeImmediates", this); + TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), + "RunAndClearNativeImmediates"); HandleScope handle_scope(isolate_); InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 }); @@ -848,8 +846,7 @@ void Environment::ToggleTimerRef(bool ref) { void Environment::RunTimers(uv_timer_t* handle) { Environment* env = Environment::from_timer_handle(handle); - TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), - "RunTimers", env); + TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunTimers"); if (!env->can_call_into_js()) return; @@ -910,8 +907,7 @@ void Environment::RunTimers(uv_timer_t* handle) { void Environment::CheckImmediate(uv_check_t* handle) { Environment* env = Environment::from_immediate_check_handle(handle); - TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), - "CheckImmediate", env); + TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "CheckImmediate"); HandleScope scope(env->isolate()); Context::Scope context_scope(env->context()); diff --git a/src/node_contextify.cc b/src/node_contextify.cc index b2df392697c9ad..e7a4a4dd142cb2 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -732,11 +732,10 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( TRACING_CATEGORY_NODE2(vm, script)) != 0) { Utf8Value fn(isolate, filename); - TRACE_EVENT_NESTABLE_ASYNC_BEGIN1( - TRACING_CATEGORY_NODE2(vm, script), - "ContextifyScript::New", - contextify_script, - "filename", TRACE_STR_COPY(*fn)); + TRACE_EVENT_BEGIN1(TRACING_CATEGORY_NODE2(vm, script), + "ContextifyScript::New", + "filename", + TRACE_STR_COPY(*fn)); } ScriptCompiler::CachedData* cached_data = nullptr; @@ -786,10 +785,8 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { no_abort_scope.Close(); if (!try_catch.HasTerminated()) try_catch.ReThrow(); - TRACE_EVENT_NESTABLE_ASYNC_END0( - TRACING_CATEGORY_NODE2(vm, script), - "ContextifyScript::New", - contextify_script); + TRACE_EVENT_END0(TRACING_CATEGORY_NODE2(vm, script), + "ContextifyScript::New"); return; } contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked()); @@ -818,10 +815,7 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { env->cached_data_produced_string(), Boolean::New(isolate, cached_data_produced)).Check(); } - TRACE_EVENT_NESTABLE_ASYNC_END0( - TRACING_CATEGORY_NODE2(vm, script), - "ContextifyScript::New", - contextify_script); + TRACE_EVENT_END0(TRACING_CATEGORY_NODE2(vm, script), "ContextifyScript::New"); } bool ContextifyScript::InstanceOf(Environment* env, @@ -857,8 +851,7 @@ void ContextifyScript::RunInThisContext( ContextifyScript* wrapped_script; ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); - TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( - TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext", wrapped_script); + TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext"); // TODO(addaleax): Use an options object or otherwise merge this with // RunInContext(). @@ -884,9 +877,6 @@ void ContextifyScript::RunInThisContext( break_on_first_line, nullptr, // microtask_queue args); - - TRACE_EVENT_NESTABLE_ASYNC_END0( - TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext", wrapped_script); } void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { @@ -908,8 +898,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { if (context.IsEmpty()) return; - TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( - TRACING_CATEGORY_NODE2(vm, script), "RunInContext", wrapped_script); + TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInContext"); CHECK(args[1]->IsNumber()); int64_t timeout = args[1]->IntegerValue(env->context()).FromJust(); @@ -932,9 +921,6 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { break_on_first_line, contextify_context->microtask_queue(), args); - - TRACE_EVENT_NESTABLE_ASYNC_END0( - TRACING_CATEGORY_NODE2(vm, script), "RunInContext", wrapped_script); } bool ContextifyScript::EvalMachine(Environment* env, diff --git a/src/node_internals.h b/src/node_internals.h index 80cfa96c23a3e6..feeef6d1be34f9 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -383,23 +383,6 @@ namespace heap { bool WriteSnapshot(v8::Isolate* isolate, const char* filename); } -class TraceEventScope { - public: - TraceEventScope(const char* category, - const char* name, - void* id) : category_(category), name_(name), id_(id) { - TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_, name_, id_); - } - ~TraceEventScope() { - TRACE_EVENT_NESTABLE_ASYNC_END0(category_, name_, id_); - } - - private: - const char* category_; - const char* name_; - void* id_; -}; - namespace heap { void DeleteHeapSnapshot(const v8::HeapSnapshot* snapshot);