Skip to content

Commit 85a1369

Browse files
addaleaxMylesBorins
authored andcommitted
perf_hooks: make GC tracking state per-Environment
Otherwise this is global state that may be subject to race conditions e.g. when running `perf_hooks` inside of Worker threads. Tracking the GC type is removed entirely since the variable was unused. PR-URL: #25053 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7b2eefc commit 85a1369

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/node_perf.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ const double timeOriginTimestamp = GetCurrentTimeInMicroseconds();
4242
uint64_t performance_node_start;
4343
uint64_t performance_v8_start;
4444

45-
uint64_t performance_last_gc_start_mark_ = 0;
46-
GCType performance_last_gc_type_ = GCType::kGCTypeAll;
47-
4845
void performance_state::Mark(enum PerformanceMilestone milestone,
4946
uint64_t ts) {
5047
this->milestones[milestone] = ts;
@@ -268,9 +265,10 @@ void PerformanceGCCallback(Environment* env, void* ptr) {
268265
// Marks the start of a GC cycle
269266
void MarkGarbageCollectionStart(Isolate* isolate,
270267
GCType type,
271-
GCCallbackFlags flags) {
272-
performance_last_gc_start_mark_ = PERFORMANCE_NOW();
273-
performance_last_gc_type_ = type;
268+
GCCallbackFlags flags,
269+
void* data) {
270+
Environment* env = static_cast<Environment*>(data);
271+
env->performance_state()->performance_last_gc_start_mark = PERFORMANCE_NOW();
274272
}
275273

276274
// Marks the end of a GC cycle
@@ -279,21 +277,23 @@ void MarkGarbageCollectionEnd(Isolate* isolate,
279277
GCCallbackFlags flags,
280278
void* data) {
281279
Environment* env = static_cast<Environment*>(data);
280+
performance_state* state = env->performance_state();
282281
// If no one is listening to gc performance entries, do not create them.
283-
if (!env->performance_state()->observers[NODE_PERFORMANCE_ENTRY_TYPE_GC])
282+
if (!state->observers[NODE_PERFORMANCE_ENTRY_TYPE_GC])
284283
return;
285284
GCPerformanceEntry* entry =
286285
new GCPerformanceEntry(env,
287286
static_cast<PerformanceGCKind>(type),
288-
performance_last_gc_start_mark_,
287+
state->performance_last_gc_start_mark,
289288
PERFORMANCE_NOW());
290289
env->SetUnrefImmediate(PerformanceGCCallback,
291290
entry);
292291
}
293292

294293

295294
inline void SetupGarbageCollectionTracking(Environment* env) {
296-
env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart);
295+
env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart,
296+
static_cast<void*>(env));
297297
env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd,
298298
static_cast<void*>(env));
299299
}

src/node_perf_common.h

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class performance_state {
7575
AliasedBuffer<double, v8::Float64Array> milestones;
7676
AliasedBuffer<uint32_t, v8::Uint32Array> observers;
7777

78+
uint64_t performance_last_gc_start_mark = 0;
79+
7880
void Mark(enum PerformanceMilestone milestone,
7981
uint64_t ts = PERFORMANCE_NOW());
8082

0 commit comments

Comments
 (0)