Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf_hooks: only enable GC tracking when it's requested #25853

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const {
timeOrigin,
timeOriginTimestamp,
timerify,
constants
constants,
setupGarbageCollectionTracking
} = internalBinding('performance');

const {
Expand Down Expand Up @@ -273,6 +274,8 @@ class PerformanceObserverEntryList {
}
}

let gcTrackingIsEnabled = false;

class PerformanceObserver extends AsyncResource {
constructor(callback) {
if (typeof callback !== 'function') {
Expand Down Expand Up @@ -334,6 +337,11 @@ class PerformanceObserver extends AsyncResource {
if (entryTypes.length === 0) {
throw new errors.ERR_VALID_PERFORMANCE_ENTRY_TYPE();
}
if (entryTypes.includes(NODE_PERFORMANCE_ENTRY_TYPE_GC) &&
!gcTrackingIsEnabled) {
setupGarbageCollectionTracking();
gcTrackingIsEnabled = true;
}
this.disconnect();
this[kBuffer][kEntries] = [];
L.init(this[kBuffer][kEntries]);
Expand Down
8 changes: 5 additions & 3 deletions src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ void MarkGarbageCollectionEnd(Isolate* isolate,
entry);
}

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

inline void SetupGarbageCollectionTracking(Environment* env) {
env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart,
static_cast<void*>(env));
env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd,
Expand Down Expand Up @@ -416,6 +418,8 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "markMilestone", MarkMilestone);
env->SetMethod(target, "setupObservers", SetupPerformanceObservers);
env->SetMethod(target, "timerify", Timerify);
env->SetMethod(
target, "setupGarbageCollectionTracking", SetupGarbageCollectionTracking);

Local<Object> constants = Object::New(isolate);

Expand Down Expand Up @@ -452,8 +456,6 @@ void Initialize(Local<Object> target,
env->constants_string(),
constants,
attr).ToChecked();

SetupGarbageCollectionTracking(env);
}

} // namespace performance
Expand Down