Skip to content

Commit eb7f22a

Browse files
authored
perf_hooks: fix gc elapsed time
PR-URL: #44058 Refs: #44046 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 3711503 commit eb7f22a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/node_perf.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@ void MarkGarbageCollectionStart(
116116
GCCallbackFlags flags,
117117
void* data) {
118118
Environment* env = static_cast<Environment*>(data);
119+
// Prevent gc callback from reentering with different type
120+
// See https://github.com/nodejs/node/issues/44046
121+
if (env->performance_state()->current_gc_type != 0) {
122+
return;
123+
}
119124
env->performance_state()->performance_last_gc_start_mark = PERFORMANCE_NOW();
125+
env->performance_state()->current_gc_type = type;
120126
}
121127

122128
MaybeLocal<Object> GCPerformanceEntryTraits::GetDetails(
@@ -153,6 +159,10 @@ void MarkGarbageCollectionEnd(
153159
void* data) {
154160
Environment* env = static_cast<Environment*>(data);
155161
PerformanceState* state = env->performance_state();
162+
if (type != state->current_gc_type) {
163+
return;
164+
}
165+
env->performance_state()->current_gc_type = 0;
156166
// If no one is listening to gc performance entries, do not create them.
157167
if (LIKELY(!state->observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]))
158168
return;
@@ -178,14 +188,17 @@ void MarkGarbageCollectionEnd(
178188

179189
void GarbageCollectionCleanupHook(void* data) {
180190
Environment* env = static_cast<Environment*>(data);
191+
// Reset current_gc_type to 0
192+
env->performance_state()->current_gc_type = 0;
181193
env->isolate()->RemoveGCPrologueCallback(MarkGarbageCollectionStart, data);
182194
env->isolate()->RemoveGCEpilogueCallback(MarkGarbageCollectionEnd, data);
183195
}
184196

185197
static void InstallGarbageCollectionTracking(
186198
const FunctionCallbackInfo<Value>& args) {
187199
Environment* env = Environment::GetCurrent(args);
188-
200+
// Reset current_gc_type to 0
201+
env->performance_state()->current_gc_type = 0;
189202
env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart,
190203
static_cast<void*>(env));
191204
env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd,

src/node_perf_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class PerformanceState {
7171
AliasedUint32Array observers;
7272

7373
uint64_t performance_last_gc_start_mark = 0;
74+
uint16_t current_gc_type = 0;
7475

7576
void Mark(enum PerformanceMilestone milestone,
7677
uint64_t ts = PERFORMANCE_NOW());

0 commit comments

Comments
 (0)