Skip to content

Commit

Permalink
[refactor] initial rewrite to use native async_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishrock123 committed Mar 28, 2017
1 parent 54b3596 commit f54aeb3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 23 deletions.
92 changes: 72 additions & 20 deletions dprof.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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
//
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
};
Expand Down Expand Up @@ -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
);
Expand All @@ -141,31 +163,61 @@ 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();
stateStack.push(state);
}

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();
stateStack.pop();
}

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();
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions visualizer/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function Node(node) {

// Info
this.name = node.name;
this.uid = node.uid;
this.stack = node.stack;
this.initRef = node.initRef;

Expand Down
4 changes: 2 additions & 2 deletions visualizer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions visualizer/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down

0 comments on commit f54aeb3

Please sign in to comment.