Skip to content

Commit

Permalink
perf_hooks: fix gc elapsed time
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Jul 30, 2022
1 parent ade5fd4 commit e32e351
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 @@ -116,7 +116,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 @@ -153,6 +159,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 @@ -178,14 +188,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 e32e351

Please sign in to comment.