11'use strict' ;
22
3- const asyncHook = require ( 'async-hook' ) ;
3+ const asyncHook = require ( 'async_hooks' ) ;
4+
45const chain = require ( 'stack-chain' ) ;
56const zlib = require ( 'zlib' ) ;
67const fs = require ( 'fs' ) ;
@@ -14,9 +15,18 @@ if (process.execArgv.indexOf('--stack_trace_limit') === -1 && Error.stackTraceLi
1415}
1516
1617//
17- // Define node class
18+ // Setup hooks
1819//
20+ const hooks = asyncHook . createHook ( {
21+ init : asyncInit ,
22+ before : asyncBefore ,
23+ after : asyncAfter ,
24+ destroy : asyncDestroy
25+ } ) ;
1926
27+ //
28+ // Define node class
29+ //
2030function Site ( site ) {
2131 this . description = site . toString ( ) ;
2232 this . filename = site . getFileName ( ) ;
@@ -29,12 +39,11 @@ function timestamp() {
2939 return t [ 0 ] * 1e9 + t [ 1 ] ;
3040}
3141
32- function Node ( uid , handle , stack , parent ) {
42+ function Node ( uid , handle , name , stack , parent ) {
3343 const self = this ;
34-
3544 this . parent = parent === null ? null : parent . uid ;
45+ this . name = name ;
3646 this . uid = uid ;
37- this . name = handle . constructor . name ;
3847 this . _init = timestamp ( ) ;
3948 this . _destroy = Infinity ;
4049 this . _before = [ ] ;
@@ -81,8 +90,8 @@ function getCallSites(skip) {
8190 return stack ;
8291}
8392
84- Node . prototype . add = function ( uid , handle ) {
85- const node = new Node ( uid , handle , getCallSites ( 3 ) , this ) ;
93+ Node . prototype . add = function ( uid , handle , type ) {
94+ const node = new Node ( uid , handle , type , getCallSites ( 3 ) , this ) ;
8695 this . children . push ( uid ) ;
8796 return node ;
8897} ;
@@ -121,63 +130,73 @@ Node.prototype.rootIntialize = function () {
121130 this . _before . push ( 0 ) ;
122131} ;
123132
124- //
125- // Setup hooks
126- //
127- asyncHook . addHooks ( {
128- init : asyncInit ,
129- pre : asyncBefore ,
130- post : asyncAfter ,
131- destroy : asyncDestroy
132- } ) ;
133-
134133const root = new Node (
135- 0 , { 'constructor' : { name : 'root' } } ,
134+ 1 ,
135+ { } ,
136+ 'root' ,
136137 getCallSites ( 2 ) ,
137138 null
138139) ;
139140root . rootIntialize ( ) ;
140141
141142const nodes = new Map ( ) ;
142- const stateStack = [ root ] ;
143143
144- function asyncInit ( uid , handle , provider , parentUid ) {
145- // get parent state
146- const topState = stateStack [ stateStack . length - 1 ] ;
147- const state = ( parentUid === null ? topState : nodes . get ( parentUid ) ) ;
144+ // Setup the root: fake hook events
145+ hooks . disable ( ) ;
146+ process . nextTick ( function ( ) {
147+ root . after ( ) ;
148+ root . destroy ( ) ;
149+ } ) ;
150+ hooks . enable ( ) ;
151+
152+
153+ function asyncInit ( uid , type , triggerId , handle ) {
154+ process . _rawDebug ( 'init' , { uid, type, triggerId} ) ;
155+
156+ // get initializing state
157+ let state ;
158+ if ( triggerId === 0 || triggerId === 1 ) {
159+ // 1 is always root
160+ // 0 is not root, but unknown. Use root for now.
161+ state = root ;
162+ } else {
163+ state = nodes . get ( triggerId ) ;
164+ }
148165
149166 // add new state node
150- nodes . set ( uid , state . add ( uid , handle ) ) ;
167+ nodes . set ( uid , state . add ( uid , handle , type ) ) ;
151168}
152169
153170function asyncBefore ( uid ) {
171+ // Ignore our nextTick for the root duration
172+ if ( ! nodes . has ( uid ) ) return ;
173+ process . _rawDebug ( 'before' , { uid} ) ;
174+
154175 const state = nodes . get ( uid ) ;
155176
156177 state . before ( ) ;
157- stateStack . push ( state ) ;
158178}
159179
160180function asyncAfter ( uid ) {
181+ // Ignore our nextTick for the root duration
182+ if ( ! nodes . has ( uid ) ) return ;
183+ process . _rawDebug ( 'after' , { uid} ) ;
184+
161185 const state = nodes . get ( uid ) ;
162186
163187 state . after ( ) ;
164- stateStack . pop ( ) ;
165188}
166189
167190function asyncDestroy ( uid ) {
191+ // Ignore our nextTick for the root duration
192+ if ( ! nodes . has ( uid ) ) return ;
193+ process . _rawDebug ( 'destroy' , { uid} ) ;
194+
168195 const state = nodes . get ( uid ) ;
169196
170197 state . destroy ( ) ;
171198}
172199
173- // The root job is done when process.nextTick is called
174- asyncHook . disable ( ) ;
175- process . nextTick ( function ( ) {
176- root . after ( ) ;
177- root . destroy ( ) ;
178- } ) ;
179- asyncHook . enable ( ) ;
180-
181200//
182201// Print result
183202//
@@ -196,9 +215,9 @@ if (process.argv.indexOf('--dprof-no-sigint') === -1 &&
196215process . on ( 'exit' , writeDataFile ) ;
197216
198217function writeDataFile ( ) {
199- // even though zlib is sync, it still fires async_wrap events,
200- // so disable asyncWrap just to be sure.
201- asyncHook . disable ( ) ;
218+ // even though zlib is sync, it still fires async_hook events,
219+ // so disable the hooks just to be sure.
220+ hooks . disable ( ) ;
202221
203222 const data = {
204223 'total' : timestamp ( ) ,
0 commit comments