|
| 1 | +'use strict'; |
| 2 | +// Flags: --expose-gc |
| 3 | + |
| 4 | +const assert = require('assert'); |
| 5 | +const async_hooks = require('async_hooks'); |
| 6 | +const util = require('util'); |
| 7 | +const print = process._rawDebug; |
| 8 | +require('../common'); |
| 9 | + |
| 10 | +if (typeof global.gc === 'function') { |
| 11 | + (function exity(cntr) { |
| 12 | + process.once('beforeExit', () => { |
| 13 | + global.gc(); |
| 14 | + if (cntr < 4) setImmediate(() => exity(cntr + 1)); |
| 15 | + }); |
| 16 | + })(0); |
| 17 | +} |
| 18 | + |
| 19 | +function noop() {} |
| 20 | + |
| 21 | +class ActivityCollector { |
| 22 | + constructor(start, { |
| 23 | + allowNoInit = false, |
| 24 | + oninit, |
| 25 | + onbefore, |
| 26 | + onafter, |
| 27 | + ondestroy, |
| 28 | + logid = null, |
| 29 | + logtype = null |
| 30 | + } = {}) { |
| 31 | + this._start = start; |
| 32 | + this._allowNoInit = allowNoInit; |
| 33 | + this._activities = new Map(); |
| 34 | + this._logid = logid; |
| 35 | + this._logtype = logtype; |
| 36 | + |
| 37 | + // register event handlers if provided |
| 38 | + this.oninit = typeof oninit === 'function' ? oninit : noop; |
| 39 | + this.onbefore = typeof onbefore === 'function' ? onbefore : noop; |
| 40 | + this.onafter = typeof onafter === 'function' ? onafter : noop; |
| 41 | + this.ondestroy = typeof ondestroy === 'function' ? ondestroy : noop; |
| 42 | + |
| 43 | + // create the hook with which we'll collect activity data |
| 44 | + this._asyncHook = async_hooks.createHook({ |
| 45 | + init: this._init.bind(this), |
| 46 | + before: this._before.bind(this), |
| 47 | + after: this._after.bind(this), |
| 48 | + destroy: this._destroy.bind(this) |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + enable() { |
| 53 | + this._asyncHook.enable(); |
| 54 | + } |
| 55 | + |
| 56 | + disable() { |
| 57 | + this._asyncHook.disable(); |
| 58 | + } |
| 59 | + |
| 60 | + sanityCheck(types) { |
| 61 | + if (types != null && !Array.isArray(types)) types = [ types ]; |
| 62 | + |
| 63 | + function activityString(a) { |
| 64 | + return util.inspect(a, false, 5, true); |
| 65 | + } |
| 66 | + |
| 67 | + const violations = []; |
| 68 | + function v(msg) { violations.push(msg); } |
| 69 | + for (const a of this._activities.values()) { |
| 70 | + if (types != null && types.indexOf(a.type) < 0) continue; |
| 71 | + |
| 72 | + if (a.init && a.init.length > 1) { |
| 73 | + v('Activity inited twice\n' + activityString(a) + |
| 74 | + '\nExpected "init" to be called at most once'); |
| 75 | + } |
| 76 | + if (a.destroy && a.destroy.length > 1) { |
| 77 | + v('Activity destroyed twice\n' + activityString(a) + |
| 78 | + '\nExpected "destroy" to be called at most once'); |
| 79 | + } |
| 80 | + if (a.before && a.after) { |
| 81 | + if (a.before.length < a.after.length) { |
| 82 | + v('Activity called "after" without calling "before"\n' + |
| 83 | + activityString(a) + |
| 84 | + '\nExpected no "after" call without a "before"'); |
| 85 | + } |
| 86 | + if (a.before.some((x, idx) => x > a.after[idx])) { |
| 87 | + v('Activity had an instance where "after" ' + |
| 88 | + 'was invoked before "before"\n' + |
| 89 | + activityString(a) + |
| 90 | + '\nExpected "after" to be called after "before"'); |
| 91 | + } |
| 92 | + } |
| 93 | + if (a.before && a.destroy) { |
| 94 | + if (a.before.some((x, idx) => x > a.destroy[idx])) { |
| 95 | + v('Activity had an instance where "destroy" ' + |
| 96 | + 'was invoked before "before"\n' + |
| 97 | + activityString(a) + |
| 98 | + '\nExpected "destroy" to be called after "before"'); |
| 99 | + } |
| 100 | + } |
| 101 | + if (a.after && a.destroy) { |
| 102 | + if (a.after.some((x, idx) => x > a.destroy[idx])) { |
| 103 | + v('Activity had an instance where "destroy" ' + |
| 104 | + 'was invoked before "after"\n' + |
| 105 | + activityString(a) + |
| 106 | + '\nExpected "destroy" to be called after "after"'); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + if (violations.length) { |
| 111 | + console.error(violations.join('\n')); |
| 112 | + assert.fail(violations.length, 0, 'Failed sanity check'); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + inspect(opts = {}) { |
| 117 | + if (typeof opts === 'string') opts = { types: opts }; |
| 118 | + const { types = null, depth = 5, stage = null } = opts; |
| 119 | + const activities = types == null ? |
| 120 | + Array.from(this._activities.values()) : |
| 121 | + this.activitiesOfTypes(types); |
| 122 | + |
| 123 | + if (stage != null) console.log('\n%s', stage); |
| 124 | + console.log(util.inspect(activities, false, depth, true)); |
| 125 | + } |
| 126 | + |
| 127 | + activitiesOfTypes(types) { |
| 128 | + if (!Array.isArray(types)) types = [ types ]; |
| 129 | + return this.activities.filter((x) => types.indexOf(x.type) >= 0); |
| 130 | + } |
| 131 | + |
| 132 | + get activities() { |
| 133 | + return Array.from(this._activities.values()); |
| 134 | + } |
| 135 | + |
| 136 | + _stamp(h, hook) { |
| 137 | + if (h == null) return; |
| 138 | + if (h[hook] == null) h[hook] = []; |
| 139 | + const time = process.hrtime(this._start); |
| 140 | + h[hook].push((time[0] * 1e9) + time[1]); |
| 141 | + } |
| 142 | + |
| 143 | + _getActivity(uid, hook) { |
| 144 | + const h = this._activities.get(uid); |
| 145 | + if (!h) { |
| 146 | + // if we allowed handles without init we ignore any further life time |
| 147 | + // events this makes sense for a few tests in which we enable some hooks |
| 148 | + // later |
| 149 | + if (this._allowNoInit) { |
| 150 | + const stub = { uid, type: 'Unknown' }; |
| 151 | + this._activities.set(uid, stub); |
| 152 | + return stub; |
| 153 | + } else { |
| 154 | + const err = new Error('Found a handle who\'s ' + hook + |
| 155 | + ' hook was invoked but not it\'s init hook'); |
| 156 | + // Don't throw if we see invocations due to an assertion in a test |
| 157 | + // failing since we want to list the assertion failure instead |
| 158 | + if (/process\._fatalException/.test(err.stack)) return null; |
| 159 | + throw err; |
| 160 | + } |
| 161 | + } |
| 162 | + return h; |
| 163 | + } |
| 164 | + |
| 165 | + _init(uid, type, triggerId, handle) { |
| 166 | + const activity = { uid, type, triggerId }; |
| 167 | + this._stamp(activity, 'init'); |
| 168 | + this._activities.set(uid, activity); |
| 169 | + this._maybeLog(uid, type, 'init'); |
| 170 | + this.oninit(uid, type, triggerId, handle); |
| 171 | + } |
| 172 | + |
| 173 | + _before(uid) { |
| 174 | + const h = this._getActivity(uid, 'before'); |
| 175 | + this._stamp(h, 'before'); |
| 176 | + this._maybeLog(uid, h && h.type, 'before'); |
| 177 | + this.onbefore(uid); |
| 178 | + } |
| 179 | + |
| 180 | + _after(uid) { |
| 181 | + const h = this._getActivity(uid, 'after'); |
| 182 | + this._stamp(h, 'after'); |
| 183 | + this._maybeLog(uid, h && h.type, 'after'); |
| 184 | + this.onafter(uid); |
| 185 | + } |
| 186 | + |
| 187 | + _destroy(uid) { |
| 188 | + const h = this._getActivity(uid, 'destroy'); |
| 189 | + this._stamp(h, 'destroy'); |
| 190 | + this._maybeLog(uid, h && h.type, 'destroy'); |
| 191 | + this.ondestroy(uid); |
| 192 | + } |
| 193 | + |
| 194 | + _maybeLog(uid, type, name) { |
| 195 | + if (this._logid && |
| 196 | + (type == null || this._logtype == null || this._logtype === type)) { |
| 197 | + print(this._logid + '.' + name + '.uid-' + uid); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +exports = module.exports = function initHooks({ |
| 203 | + oninit, |
| 204 | + onbefore, |
| 205 | + onafter, |
| 206 | + ondestroy, |
| 207 | + allowNoInit, |
| 208 | + logid, |
| 209 | + logtype } = {}) { |
| 210 | + return new ActivityCollector(process.hrtime(), { |
| 211 | + oninit, |
| 212 | + onbefore, |
| 213 | + onafter, |
| 214 | + ondestroy, |
| 215 | + allowNoInit, |
| 216 | + logid, |
| 217 | + logtype |
| 218 | + }); |
| 219 | +}; |
0 commit comments