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 19, 2016
1 parent 58f9ea2 commit cf42e80
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,25 +52,43 @@ function timestamp() {
return t[0] * 1e9 + t[1];
}

function Node(handle, stack) {
this.name = handle.constructor.name;
function Node(handle, id, name, stack) {
this.name = name;
this._id = id;
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 @@ -64,8 +105,8 @@ function getCallSites(skip) {
return stack;
}

Node.prototype.add = function (handle) {
const node = new Node(handle, getCallSites(3));
Node.prototype.add = function (handle, id, type) {
const node = new Node(handle, id, type, getCallSites(3));
this.children.push(node);
return node;
};
Expand All @@ -85,6 +126,7 @@ Node.prototype.destroy = function () {
Node.prototype.toJSON = function () {
return {
name: this.name,
uid: this._id,
init: this._init,
destroy: this._destroy,
before: this._before,
Expand All @@ -100,46 +142,66 @@ Node.prototype.rootIntialize = function () {
this._before.push(0);
};

//
// Setup hooks
//
asyncHook.addHooks({
init: asyncInit,
pre: asyncBefore,
post: asyncAfter,
destroy: asyncDestroy
});

const root = new Node({ constructor: { name: 'root' } }, getCallSites(2));
const root = new Node({}, 1, 'root', getCallSites(2));
root.rootIntialize();

const stateMap = 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 : stateMap.get(parentUid));
// root is always UID 1
const state = (parentUid === 1 ? currState : stateMap.get(parentUid));

// add new state node
stateMap.set(uid, state.add(handle));
// process._rawDebug('>>> UID:', uid)
stateMap.set(uid, state.add(handle, uid, type));
}

function asyncBefore(uid) {
// Ignore our nextTick for the root duration
// TODO(Fishrock123): detect this better.
if (uid === 2) return

const state = stateMap.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 = stateMap.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 = stateMap.get(uid);
// if (state === undefined) return

// if (!state) {
// process._rawDebug('>>>>', uid)
// }

state.destroy();
stateMap.delete(uid);
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 @@ -26,6 +26,7 @@ function Node(parent, node, index) {

// 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 cf42e80

Please sign in to comment.