Skip to content

Commit

Permalink
perf_hooks: fix gc elapsed time
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#44058
Refs: nodejs/node#44046
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
theanarkh authored and guangwong committed Jan 3, 2023
1 parent ee882ac commit 177e44e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ void MarkGarbageCollectionStart(
GCCallbackFlags flags,
void* data) {
Environment* env = static_cast<Environment*>(data);
// Prevent gc callback from reentering with different type
// See https://github.com/nodejs/node/issues/44046
if (env->performance_state()->current_gc_type != 0) {
return;
}
env->performance_state()->performance_last_gc_start_mark = PERFORMANCE_NOW();
env->performance_state()->current_gc_type = type;
}

MaybeLocal<Object> GCPerformanceEntryTraits::GetDetails(
Expand Down Expand Up @@ -155,6 +161,10 @@ void MarkGarbageCollectionEnd(
void* data) {
Environment* env = static_cast<Environment*>(data);
PerformanceState* state = env->performance_state();
if (type != state->current_gc_type) {
return;
}
env->performance_state()->current_gc_type = 0;
// If no one is listening to gc performance entries, do not create them.
if (LIKELY(!state->observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]))
return;
Expand All @@ -179,14 +189,17 @@ void MarkGarbageCollectionEnd(

void GarbageCollectionCleanupHook(void* data) {
Environment* env = static_cast<Environment*>(data);
// Reset current_gc_type to 0
env->performance_state()->current_gc_type = 0;
env->isolate()->RemoveGCPrologueCallback(MarkGarbageCollectionStart, data);
env->isolate()->RemoveGCEpilogueCallback(MarkGarbageCollectionEnd, data);
}

static void InstallGarbageCollectionTracking(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

// Reset current_gc_type to 0
env->performance_state()->current_gc_type = 0;
env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart,
static_cast<void*>(env));
env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd,
Expand Down
1 change: 1 addition & 0 deletions src/node_perf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class PerformanceState {
AliasedUint32Array observers;

uint64_t performance_last_gc_start_mark = 0;
uint16_t current_gc_type = 0;

void Mark(enum PerformanceMilestone milestone,
uint64_t ts = PERFORMANCE_NOW());
Expand Down

0 comments on commit 177e44e

Please sign in to comment.