diff --git a/dprof.js b/dprof.js index 0b83091..88aeb1f 100644 --- a/dprof.js +++ b/dprof.js @@ -1,6 +1,18 @@ 'use strict'; -const asyncHook = require('async-hook'); +const async_hooks = require('async_hooks'); +// process._rawDebug('>>> currentId:', async_hooks.currentId()); + +// const preHook = async_hooks.createHook({ +// init (uid) { +// process._rawDebug('preInit', uid, (new Error()).stack.substring(6)) +// }, +// before(){}, +// after(){}, +// destroy(){} +// }) +// preHook.enable(); + const chain = require('stack-chain'); const zlib = require('zlib'); const fs = require('fs'); @@ -13,6 +25,17 @@ if (process.execArgv.indexOf('--stack_trace_limit') === -1 && Error.stackTraceLi Error.stackTraceLimit = 8; } +// preHook.disable() +// +// Setup hooks +// +const asyncHook = async_hooks.createHook({ + init: asyncInit, + before: asyncBefore, + after: asyncAfter, + destroy: asyncDestroy +}); + // // Define node class // @@ -29,12 +52,11 @@ function timestamp() { return t[0] * 1e9 + t[1]; } -function Node(uid, handle, stack, parent) { +function Node(uid, handle, name, stack, parent) { const self = this; - this.parent = parent === null ? null : parent.uid; + this.name = name; this.uid = uid; - this.name = handle.constructor.name; this._init = timestamp(); this._destroy = Infinity; this._before = []; @@ -65,6 +87,14 @@ function Node(uid, handle, stack, parent) { return ret; }; } + + // asyncHook.disable(); + // if (!this.unrefed && typeof handle.hasRef === 'function') { + // process.nextTick(() => { + // this.unrefed = !handle.hasRef(); + // }); + // } + // asyncHook.enable(); } function getCallSites(skip) { @@ -81,8 +111,8 @@ function getCallSites(skip) { return stack; } -Node.prototype.add = function (uid, handle) { - const node = new Node(uid, handle, getCallSites(3), this); +Node.prototype.add = function (uid, handle, type) { + const node = new Node(uid, handle, type, getCallSites(3), this); this.children.push(uid); return node; }; @@ -121,18 +151,10 @@ Node.prototype.rootIntialize = function () { this._before.push(0); }; -// -// Setup hooks -// -asyncHook.addHooks({ - init: asyncInit, - pre: asyncBefore, - post: asyncAfter, - destroy: asyncDestroy -}); - const root = new Node( - 0, { 'constructor': { name: 'root' } }, + 0, + {}, + 'root', getCallSites(2), null ); @@ -141,16 +163,32 @@ root.rootIntialize(); const nodes = new Map(); const stateStack = [root]; -function asyncInit(uid, handle, provider, parentUid) { +function asyncInit(uid, type, parentUid, handle) { + // process._rawDebug('init:' + uid, parentUid); + + // Ignore our nextTick for the root duration + // TODO(Fishrock123): detect this better. + if (uid === 2) return + + // if (type === 'Timeout') { + // process._rawDebug('Timeout', uid, handle._idleTimeout) + // } + // get parent state const topState = stateStack[stateStack.length - 1]; - const state = (parentUid === null ? topState : nodes.get(parentUid)); + // root is always UID 1 + const state = (parentUid === 1/*null*/ ? topState : nodes.get(parentUid)); // add new state node - nodes.set(uid, state.add(uid, handle)); + // process._rawDebug('>>> UID:', uid) + nodes.set(uid, state.add(uid, handle, type)); } function asyncBefore(uid) { + // Ignore our nextTick for the root duration + // TODO(Fishrock123): detect this better. + if (uid === 2) return + const state = nodes.get(uid); state.before(); @@ -158,6 +196,10 @@ function asyncBefore(uid) { } function asyncAfter(uid) { + // Ignore our nextTick for the root duration + // TODO(Fishrock123): detect this better. + if (uid === 2) return + const state = nodes.get(uid); state.after(); @@ -165,7 +207,17 @@ function asyncAfter(uid) { } function asyncDestroy(uid) { + // Ignore our nextTick for the root duration + // TODO(Fishrock123): detect this better. + if (uid === 2) return + + // process._rawDebug('destroy:' + uid) const state = nodes.get(uid); + // if (state === undefined) return + + // if (!state) { + // process._rawDebug('>>>>', uid) + // } state.destroy(); } diff --git a/package.json b/package.json index a0272ed..bbbbcb0 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "babelify": "^7.3.0", "babel-preset-es2015": "^6.13.2", "startpoint": "0.3.x", - "async-hook": "^1.6.0", "stack-chain": "^1.3.7" }, "devDependencies": { diff --git a/visualizer/flatten.js b/visualizer/flatten.js index 307d2d9..27e810f 100644 --- a/visualizer/flatten.js +++ b/visualizer/flatten.js @@ -83,6 +83,7 @@ function Node(node) { // Info this.name = node.name; + this.uid = node.uid; this.stack = node.stack; this.initRef = node.initRef; diff --git a/visualizer/index.html b/visualizer/index.html index 3521eb6..4217e6e 100644 --- a/visualizer/index.html +++ b/visualizer/index.html @@ -47,7 +47,7 @@ section#info #stacktrace { float: left; width: calc(100% - 155px); - height: 119px; + height: 135px; white-space: pre-wrap; padding: 10px 10px 5px 10px; box-sizing: border-box; @@ -56,7 +56,7 @@ section#info #stats { float: right; width: 155px; - height: 119px; + height: 135px; white-space: pre-wrap; padding: 10px 10px 5px 10px; box-sizing: border-box; diff --git a/visualizer/info.js b/visualizer/info.js index d4e715a..42dc500 100644 --- a/visualizer/info.js +++ b/visualizer/info.js @@ -42,6 +42,7 @@ StatsLayout.prototype.draw = function () { stats += '\n' + `handle: ${this._node.name}\n` + `uid: ${this._node.uid}\n` + + // `weak (unrefed): ${this._node.unrefed}\n` + `start: ${this._node.init.toFixed(8)} sec\n` + `wait: ${toms(wait, 11)} ms\n` + `callback: ${toms(callback, 7)} ms`;