Skip to content

Commit

Permalink
src: replace TraceEventScope with sync events
Browse files Browse the repository at this point in the history
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: #42977
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
legendecas authored and bengl committed May 30, 2022
1 parent 0000654 commit 9577878
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 52 deletions.
3 changes: 1 addition & 2 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ void EmitBeforeExit(Environment* env) {
}

Maybe<bool> 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);

Expand Down
16 changes: 6 additions & 10 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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_);
}
Expand All @@ -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 });

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
32 changes: 9 additions & 23 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,10 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& 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;
Expand Down Expand Up @@ -786,10 +785,8 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& 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());
Expand Down Expand Up @@ -818,10 +815,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& 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,
Expand Down Expand Up @@ -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().
Expand All @@ -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<Value>& args) {
Expand All @@ -908,8 +898,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& 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();
Expand All @@ -932,9 +921,6 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& 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,
Expand Down
17 changes: 0 additions & 17 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,23 +383,6 @@ namespace heap {
v8::Maybe<void> WriteSnapshot(Environment* env, 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);
Expand Down

0 comments on commit 9577878

Please sign in to comment.