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 Sep 26, 2016
1 parent 7330cea commit 09641f9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 29 deletions.
114 changes: 88 additions & 26 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,27 +52,44 @@ function timestamp() {
return t[0] * 1e9 + t[1];
}

function Node(uid, handle, stack, parent) {
function Node(uid, handle, name, stack, parent) {
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 = [];
this._after = [];
this.unrefed = this.name === 'TTY' || this.name === 'Pipe';
this.unrefed = this.name === 'TTY' || this.name === 'Pipe' || handle._timerUnref === true;
this.children = [];
this.stack = stack.map(function (site) {
return new Site(site);
});

asyncHook.disable();
if (!this.unrefed && typeof handle.hasRef === 'function') {
process.nextTick(() => {
this.unrefed = !handle.hasRef();
});
if (typeof handle.unref === 'function') {
const unref = handle.unref;
handle.unref = () => {
const ret = unref.call(handle);
this.unrefed = true;
return ret;
}
}
if (typeof handle.ref === 'function') {
const ref = handle.ref;
handle.ref = () => {
const ret = ref.call(handle);
this.unrefed = false;
return ret;
}
}
asyncHook.enable();

// asyncHook.disable();
// if (!this.unrefed && typeof handle.hasRef === 'function') {
// process.nextTick(() => {
// this.unrefed = !handle.hasRef();
// });
// }
// asyncHook.enable();
}

function getCallSites(skip) {
Expand All @@ -66,8 +106,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 @@ -104,18 +144,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 @@ -124,30 +156,60 @@ root.rootIntialize();
const nodes = new Map();
let currState = 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 state = (parentUid === null ? currState : nodes.get(parentUid));
// root is always UID 1
const state = (parentUid === 1 ? currState : 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();
currState = 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();
currState = root;
}

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.unrefed = node.unrefed;

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 @@ -41,6 +41,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` +
Expand Down

0 comments on commit 09641f9

Please sign in to comment.