@@ -168,18 +168,6 @@ static void DestroyAsyncIdsCallback(uv_timer_t* handle) {
168
168
}
169
169
170
170
171
- static void PushBackDestroyAsyncId (Environment* env, double id) {
172
- if (env->async_hooks ()->fields ()[AsyncHooks::kDestroy ] == 0 )
173
- return ;
174
-
175
- if (env->destroy_async_id_list ()->empty ())
176
- uv_timer_start (env->destroy_async_ids_timer_handle (),
177
- DestroyAsyncIdsCallback, 0 , 0 );
178
-
179
- env->destroy_async_id_list ()->push_back (id);
180
- }
181
-
182
-
183
171
void AsyncWrap::EmitPromiseResolve (Environment* env, double async_id) {
184
172
AsyncHooks* async_hooks = env->async_hooks ();
185
173
@@ -199,6 +187,21 @@ void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
199
187
}
200
188
201
189
190
+ void AsyncWrap::EmitTraceEventBefore () {
191
+ switch (provider_type ()) {
192
+ #define V (PROVIDER ) \
193
+ case PROVIDER_ ## PROVIDER: \
194
+ TRACE_EVENT_NESTABLE_ASYNC_BEGIN0 (" node.async_hooks" , \
195
+ #PROVIDER " _CALLBACK" , static_cast <int64_t >(get_async_id ())); \
196
+ break ;
197
+ NODE_ASYNC_PROVIDER_TYPES (V)
198
+ #undef V
199
+ default :
200
+ UNREACHABLE ();
201
+ }
202
+ }
203
+
204
+
202
205
void AsyncWrap::EmitBefore (Environment* env, double async_id) {
203
206
AsyncHooks* async_hooks = env->async_hooks ();
204
207
@@ -218,6 +221,21 @@ void AsyncWrap::EmitBefore(Environment* env, double async_id) {
218
221
}
219
222
220
223
224
+ void AsyncWrap::EmitTraceEventAfter () {
225
+ switch (provider_type ()) {
226
+ #define V (PROVIDER ) \
227
+ case PROVIDER_ ## PROVIDER: \
228
+ TRACE_EVENT_NESTABLE_ASYNC_END0 (" node.async_hooks" , \
229
+ #PROVIDER " _CALLBACK" , static_cast <int64_t >(get_async_id ())); \
230
+ break ;
231
+ NODE_ASYNC_PROVIDER_TYPES (V)
232
+ #undef V
233
+ default :
234
+ UNREACHABLE ();
235
+ }
236
+ }
237
+
238
+
221
239
void AsyncWrap::EmitAfter (Environment* env, double async_id) {
222
240
AsyncHooks* async_hooks = env->async_hooks ();
223
241
@@ -328,8 +346,10 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
328
346
if (type == PromiseHookType::kBefore ) {
329
347
env->async_hooks ()->push_async_ids (
330
348
wrap->get_async_id (), wrap->get_trigger_async_id ());
349
+ wrap->EmitTraceEventBefore ();
331
350
AsyncWrap::EmitBefore (wrap->env (), wrap->get_async_id ());
332
351
} else if (type == PromiseHookType::kAfter ) {
352
+ wrap->EmitTraceEventAfter ();
333
353
AsyncWrap::EmitAfter (wrap->env (), wrap->get_async_id ());
334
354
if (env->execution_async_id () == wrap->get_async_id ()) {
335
355
// This condition might not be true if async_hooks was enabled during
@@ -456,7 +476,8 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
456
476
457
477
void AsyncWrap::QueueDestroyAsyncId (const FunctionCallbackInfo<Value>& args) {
458
478
CHECK (args[0 ]->IsNumber ());
459
- PushBackDestroyAsyncId (Environment::GetCurrent (args), args[0 ]->NumberValue ());
479
+ AsyncWrap::EmitDestroy (
480
+ Environment::GetCurrent (args), args[0 ]->NumberValue ());
460
481
}
461
482
462
483
void AsyncWrap::AddWrapMethods (Environment* env,
@@ -605,7 +626,34 @@ AsyncWrap::AsyncWrap(Environment* env,
605
626
606
627
607
628
AsyncWrap::~AsyncWrap () {
608
- PushBackDestroyAsyncId (env (), get_async_id ());
629
+ EmitTraceEventDestroy ();
630
+ EmitDestroy (env (), get_async_id ());
631
+ }
632
+
633
+ void AsyncWrap::EmitTraceEventDestroy () {
634
+ switch (provider_type ()) {
635
+ #define V (PROVIDER ) \
636
+ case PROVIDER_ ## PROVIDER: \
637
+ TRACE_EVENT_NESTABLE_ASYNC_END0 (" node.async_hooks" , \
638
+ #PROVIDER, static_cast <int64_t >(get_async_id ())); \
639
+ break ;
640
+ NODE_ASYNC_PROVIDER_TYPES (V)
641
+ #undef V
642
+ default :
643
+ UNREACHABLE ();
644
+ }
645
+ }
646
+
647
+ void AsyncWrap::EmitDestroy (Environment* env, double async_id) {
648
+ if (env->async_hooks ()->fields ()[AsyncHooks::kDestroy ] == 0 )
649
+ return ;
650
+
651
+ if (env->destroy_async_id_list ()->empty ()) {
652
+ uv_timer_start (env->destroy_async_ids_timer_handle (),
653
+ DestroyAsyncIdsCallback, 0 , 0 );
654
+ }
655
+
656
+ env->destroy_async_id_list ()->push_back (async_id);
609
657
}
610
658
611
659
@@ -617,6 +665,19 @@ void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
617
665
execution_async_id == -1 ? env ()->new_async_id () : execution_async_id;
618
666
trigger_async_id_ = env ()->get_init_trigger_async_id ();
619
667
668
+ switch (provider_type ()) {
669
+ #define V (PROVIDER ) \
670
+ case PROVIDER_ ## PROVIDER: \
671
+ TRACE_EVENT_NESTABLE_ASYNC_BEGIN1 (" node.async_hooks" , \
672
+ #PROVIDER, static_cast <int64_t >(get_async_id ()), \
673
+ " triggerAsyncId" , static_cast <int64_t >(get_trigger_async_id ())); \
674
+ break ;
675
+ NODE_ASYNC_PROVIDER_TYPES (V)
676
+ #undef V
677
+ default :
678
+ UNREACHABLE ();
679
+ }
680
+
620
681
if (silent) return ;
621
682
622
683
AsyncWrap::EmitAsyncInit (
@@ -664,8 +725,15 @@ void AsyncWrap::EmitAsyncInit(Environment* env,
664
725
MaybeLocal<Value> AsyncWrap::MakeCallback (const Local<Function> cb,
665
726
int argc,
666
727
Local<Value>* argv) {
728
+ EmitTraceEventBefore ();
729
+
667
730
async_context context { get_async_id (), get_trigger_async_id () };
668
- return InternalMakeCallback (env (), object (), cb, argc, argv, context);
731
+ MaybeLocal<Value> ret = InternalMakeCallback (
732
+ env (), object (), cb, argc, argv, context);
733
+
734
+ EmitTraceEventAfter ();
735
+
736
+ return ret;
669
737
}
670
738
671
739
@@ -723,8 +791,8 @@ async_context EmitAsyncInit(Isolate* isolate,
723
791
}
724
792
725
793
void EmitAsyncDestroy (Isolate* isolate, async_context asyncContext) {
726
- PushBackDestroyAsyncId ( Environment::GetCurrent (isolate),
727
- asyncContext.async_id );
794
+ AsyncWrap::EmitDestroy (
795
+ Environment::GetCurrent (isolate), asyncContext.async_id );
728
796
}
729
797
730
798
} // namespace node
0 commit comments