@@ -168,18 +168,6 @@ static void DestroyAsyncIdsCallback(uv_timer_t* handle) {
168168}
169169
170170
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-
183171void AsyncWrap::EmitPromiseResolve (Environment* env, double async_id) {
184172 AsyncHooks* async_hooks = env->async_hooks ();
185173
@@ -199,6 +187,21 @@ void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
199187}
200188
201189
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+
202205void AsyncWrap::EmitBefore (Environment* env, double async_id) {
203206 AsyncHooks* async_hooks = env->async_hooks ();
204207
@@ -218,6 +221,21 @@ void AsyncWrap::EmitBefore(Environment* env, double async_id) {
218221}
219222
220223
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+
221239void AsyncWrap::EmitAfter (Environment* env, double async_id) {
222240 AsyncHooks* async_hooks = env->async_hooks ();
223241
@@ -328,8 +346,10 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
328346 if (type == PromiseHookType::kBefore ) {
329347 env->async_hooks ()->push_async_ids (
330348 wrap->get_async_id (), wrap->get_trigger_async_id ());
349+ wrap->EmitTraceEventBefore ();
331350 AsyncWrap::EmitBefore (wrap->env (), wrap->get_async_id ());
332351 } else if (type == PromiseHookType::kAfter ) {
352+ wrap->EmitTraceEventAfter ();
333353 AsyncWrap::EmitAfter (wrap->env (), wrap->get_async_id ());
334354 if (env->execution_async_id () == wrap->get_async_id ()) {
335355 // This condition might not be true if async_hooks was enabled during
@@ -456,7 +476,8 @@ void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
456476
457477void AsyncWrap::QueueDestroyAsyncId (const FunctionCallbackInfo<Value>& args) {
458478 CHECK (args[0 ]->IsNumber ());
459- PushBackDestroyAsyncId (Environment::GetCurrent (args), args[0 ]->NumberValue ());
479+ AsyncWrap::EmitDestroy (
480+ Environment::GetCurrent (args), args[0 ]->NumberValue ());
460481}
461482
462483void AsyncWrap::AddWrapMethods (Environment* env,
@@ -605,7 +626,34 @@ AsyncWrap::AsyncWrap(Environment* env,
605626
606627
607628AsyncWrap::~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);
609657}
610658
611659
@@ -617,6 +665,19 @@ void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
617665 execution_async_id == -1 ? env ()->new_async_id () : execution_async_id;
618666 trigger_async_id_ = env ()->get_init_trigger_async_id ();
619667
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+
620681 if (silent) return ;
621682
622683 AsyncWrap::EmitAsyncInit (
@@ -664,8 +725,15 @@ void AsyncWrap::EmitAsyncInit(Environment* env,
664725MaybeLocal<Value> AsyncWrap::MakeCallback (const Local<Function> cb,
665726 int argc,
666727 Local<Value>* argv) {
728+ EmitTraceEventBefore ();
729+
667730 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;
669737}
670738
671739
@@ -723,8 +791,8 @@ async_context EmitAsyncInit(Isolate* isolate,
723791}
724792
725793void 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 );
728796}
729797
730798} // namespace node
0 commit comments