@@ -96,8 +96,14 @@ NAN_METHOD(GetAllocationProfile) {
96
96
}
97
97
98
98
// Time profiler
99
-
100
- #if NODE_MODULE_VERSION > NODE_8_0_MODULE_VERSION
99
+ #if NODE_MODULE_VERSION >= NODE_12_0_MODULE_VERSION
100
+ // For Node 12 and Node 14, a new CPU profiler object will be created each
101
+ // time profiling is started to work around
102
+ // https://bugs.chromium.org/p/v8/issues/detail?id=11051.
103
+ CpuProfiler* cpuProfiler;
104
+ // Default sampling interval is 1000us.
105
+ int samplingIntervalUS = 1000 ;
106
+ #elif NODE_MODULE_VERSION > NODE_8_0_MODULE_VERSION
101
107
// This profiler exists for the lifetime of the program. Not calling
102
108
// CpuProfiler::Dispose() is intentional.
103
109
CpuProfiler* cpuProfiler = CpuProfiler::New(v8::Isolate::GetCurrent());
@@ -264,6 +270,17 @@ NAN_METHOD(StartProfiling) {
264
270
return Nan::ThrowTypeError (" Second argument must be a boolean." );
265
271
}
266
272
273
+ #if NODE_MODULE_VERSION >= NODE_12_0_MODULE_VERSION
274
+ // Since the CPU profiler is created and destroyed each time a CPU
275
+ // profile is collected, there cannot be multiple CPU profiling requests
276
+ // inflight in parallel.
277
+ if (cpuProfiler) {
278
+ return Nan::ThrowError (" CPU profiler is already started." );
279
+ }
280
+ cpuProfiler = CpuProfiler::New (v8::Isolate::GetCurrent ());
281
+ cpuProfiler->SetSamplingInterval (samplingIntervalUS);
282
+ #endif
283
+
267
284
Local<String> name =
268
285
Nan::MaybeLocal<String>(info[0 ].As <String>()).ToLocalChecked ();
269
286
@@ -289,6 +306,11 @@ NAN_METHOD(StartProfiling) {
289
306
// Signature:
290
307
// stopProfiling(runName: string, includeLineInfo: boolean): TimeProfile
291
308
NAN_METHOD (StopProfiling) {
309
+ #if NODE_MODULE_VERSION >= NODE_12_0_MODULE_VERSION
310
+ if (!cpuProfiler) {
311
+ return Nan::ThrowError (" StopProfiling called without an active CPU profiler." );
312
+ }
313
+ #endif
292
314
if (info.Length () != 2 ) {
293
315
return Nan::ThrowTypeError (" StopProfling must have two arguments." );
294
316
}
@@ -307,6 +329,11 @@ NAN_METHOD(StopProfiling) {
307
329
Local<Value> translated_profile =
308
330
TranslateTimeProfile (profile, includeLineInfo);
309
331
profile->Delete ();
332
+ #if NODE_MODULE_VERSION >= NODE_12_0_MODULE_VERSION
333
+ // Dispose of CPU profiler to work around memory leak.
334
+ cpuProfiler->Dispose ();
335
+ cpuProfiler = NULL ;
336
+ #endif
310
337
info.GetReturnValue ().Set (translated_profile);
311
338
}
312
339
@@ -318,7 +345,11 @@ NAN_METHOD(SetSamplingInterval) {
318
345
#else
319
346
int us = info[0 ].As <Integer>()->IntegerValue ();
320
347
#endif
348
+ #if NODE_MODULE_VERSION >= NODE_12_0_MODULE_VERSION
349
+ samplingIntervalUS = us;
350
+ #else
321
351
cpuProfiler->SetSamplingInterval (us);
352
+ #endif
322
353
}
323
354
324
355
NAN_MODULE_INIT (InitAll) {
0 commit comments