@@ -167,18 +167,6 @@ static void DestroyAsyncIdsCallback(uv_timer_t* handle) {
167167}
168168
169169
170- static void PushBackDestroyAsyncId (Environment* env, double id) {
171- if (env->async_hooks ()->fields ()[AsyncHooks::kDestroy ] == 0 )
172- return ;
173-
174- if (env->destroy_async_id_list ()->empty ())
175- uv_timer_start (env->destroy_async_ids_timer_handle (),
176- DestroyAsyncIdsCallback, 0 , 0 );
177-
178- env->destroy_async_id_list ()->push_back (id);
179- }
180-
181-
182170void AsyncWrap::EmitPromiseResolve (Environment* env, double async_id) {
183171 AsyncHooks* async_hooks = env->async_hooks ();
184172
@@ -198,6 +186,21 @@ void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
198186}
199187
200188
189+ void AsyncWrap::EmitTraceEventBefore () {
190+ switch (provider_type ()) {
191+ #define V (PROVIDER ) \
192+ case PROVIDER_ ## PROVIDER: \
193+ TRACE_EVENT_NESTABLE_ASYNC_BEGIN0 (" node.async_hooks" , \
194+ #PROVIDER " _CALLBACK" , static_cast <int64_t >(get_async_id ())); \
195+ break ;
196+ NODE_ASYNC_PROVIDER_TYPES (V)
197+ #undef V
198+ default :
199+ UNREACHABLE ();
200+ }
201+ }
202+
203+
201204void AsyncWrap::EmitBefore (Environment* env, double async_id) {
202205 AsyncHooks* async_hooks = env->async_hooks ();
203206
@@ -217,6 +220,21 @@ void AsyncWrap::EmitBefore(Environment* env, double async_id) {
217220}
218221
219222
223+ void AsyncWrap::EmitTraceEventAfter () {
224+ switch (provider_type ()) {
225+ #define V (PROVIDER ) \
226+ case PROVIDER_ ## PROVIDER: \
227+ TRACE_EVENT_NESTABLE_ASYNC_END0 (" node.async_hooks" , \
228+ #PROVIDER " _CALLBACK" , static_cast <int64_t >(get_async_id ())); \
229+ break ;
230+ NODE_ASYNC_PROVIDER_TYPES (V)
231+ #undef V
232+ default :
233+ UNREACHABLE ();
234+ }
235+ }
236+
237+
220238void AsyncWrap::EmitAfter (Environment* env, double async_id) {
221239 AsyncHooks* async_hooks = env->async_hooks ();
222240
@@ -327,8 +345,10 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
327345 if (type == PromiseHookType::kBefore ) {
328346 env->async_hooks ()->push_async_ids (
329347 wrap->get_async_id (), wrap->get_trigger_async_id ());
348+ wrap->EmitTraceEventBefore ();
330349 AsyncWrap::EmitBefore (wrap->env (), wrap->get_async_id ());
331350 } else if (type == PromiseHookType::kAfter ) {
351+ wrap->EmitTraceEventAfter ();
332352 AsyncWrap::EmitAfter (wrap->env (), wrap->get_async_id ());
333353 if (env->execution_async_id () == wrap->get_async_id ()) {
334354 // This condition might not be true if async_hooks was enabled during
@@ -455,7 +475,8 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
455475
456476void AsyncWrap::QueueDestroyAsyncId (const FunctionCallbackInfo<Value>& args) {
457477 CHECK (args[0 ]->IsNumber ());
458- PushBackDestroyAsyncId (Environment::GetCurrent (args), args[0 ]->NumberValue ());
478+ AsyncWrap::EmitDestroy (
479+ Environment::GetCurrent (args), args[0 ]->NumberValue ());
459480}
460481
461482void AsyncWrap::AddWrapMethods (Environment* env,
@@ -604,7 +625,34 @@ AsyncWrap::AsyncWrap(Environment* env,
604625
605626
606627AsyncWrap::~AsyncWrap () {
607- PushBackDestroyAsyncId (env (), get_async_id ());
628+ EmitTraceEventDestroy ();
629+ EmitDestroy (env (), get_async_id ());
630+ }
631+
632+ void AsyncWrap::EmitTraceEventDestroy () {
633+ switch (provider_type ()) {
634+ #define V (PROVIDER ) \
635+ case PROVIDER_ ## PROVIDER: \
636+ TRACE_EVENT_NESTABLE_ASYNC_END0 (" node.async_hooks" , \
637+ #PROVIDER, static_cast <int64_t >(get_async_id ())); \
638+ break ;
639+ NODE_ASYNC_PROVIDER_TYPES (V)
640+ #undef V
641+ default :
642+ UNREACHABLE ();
643+ }
644+ }
645+
646+ void AsyncWrap::EmitDestroy (Environment* env, double async_id) {
647+ if (env->async_hooks ()->fields ()[AsyncHooks::kDestroy ] == 0 )
648+ return ;
649+
650+ if (env->destroy_async_id_list ()->empty ()) {
651+ uv_timer_start (env->destroy_async_ids_timer_handle (),
652+ DestroyAsyncIdsCallback, 0 , 0 );
653+ }
654+
655+ env->destroy_async_id_list ()->push_back (async_id);
608656}
609657
610658
@@ -616,6 +664,19 @@ void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
616664 execution_async_id == -1 ? env ()->new_async_id () : execution_async_id;
617665 trigger_async_id_ = env ()->get_init_trigger_async_id ();
618666
667+ switch (provider_type ()) {
668+ #define V (PROVIDER ) \
669+ case PROVIDER_ ## PROVIDER: \
670+ TRACE_EVENT_NESTABLE_ASYNC_BEGIN1 (" node.async_hooks" , \
671+ #PROVIDER, static_cast <int64_t >(get_async_id ()), \
672+ " triggerAsyncId" , static_cast <int64_t >(get_trigger_async_id ())); \
673+ break ;
674+ NODE_ASYNC_PROVIDER_TYPES (V)
675+ #undef V
676+ default :
677+ UNREACHABLE ();
678+ }
679+
619680 if (silent) return ;
620681
621682 EmitAsyncInit (env (), object (),
@@ -662,8 +723,15 @@ void AsyncWrap::EmitAsyncInit(Environment* env,
662723MaybeLocal<Value> AsyncWrap::MakeCallback (const Local<Function> cb,
663724 int argc,
664725 Local<Value>* argv) {
726+ EmitTraceEventBefore ();
727+
665728 async_context context { get_async_id (), get_trigger_async_id () };
666- return InternalMakeCallback (env (), object (), cb, argc, argv, context);
729+ MaybeLocal<Value> ret = InternalMakeCallback (
730+ env (), object (), cb, argc, argv, context);
731+
732+ EmitTraceEventAfter ();
733+
734+ return ret;
667735}
668736
669737
@@ -713,8 +781,8 @@ async_context EmitAsyncInit(Isolate* isolate,
713781}
714782
715783void EmitAsyncDestroy (Isolate* isolate, async_context asyncContext) {
716- PushBackDestroyAsyncId ( Environment::GetCurrent (isolate),
717- asyncContext.async_id );
784+ AsyncWrap::EmitDestroy (
785+ Environment::GetCurrent (isolate), asyncContext.async_id );
718786}
719787
720788} // namespace node
0 commit comments