-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathasyncMonitor.js
152 lines (131 loc) · 5.52 KB
/
asyncMonitor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const async_hooks = require('async_hooks');
const {performance, PerformanceObserver} = require('perf_hooks');
const funcInfo = require('./funcInfo/funcInfoModel.js');
const {funcInfoParser, errMessageParser} = require('./funcInfo/funcInfoParser.js');
const ioController = require('./server/ioController_mnode.js');
console.log = process._rawDebug;
// Return the id of the current execution context. Useful for tracking state
// and retrieving the resource of the current trigger without needing to use an
// AsyncHook.
const cid = async_hooks.executionAsyncId();
// process._rawDebug('currentId',async_hooks.executionAsyncId(), 'triggerId',async_hooks.triggerAsyncId());
function asyncMonitor(portNumber) {
ioController.startServer(portNumber);
const hooks = {init: init, before: before, after: after, destroy: destroy};
const asyncHook = async_hooks.createHook(hooks);
asyncHook.enable();
let previousTime = Date.now();
const CHECK_DURATION = 10000;
const activeAsyncProcess = new Map();
const activeTimeTrack = new Map();
const idDeleted = [];
function check_ActiveTimeTrack() {
activeTimeTrack.forEach( (initTime, id) => {
if (initTime < previousTime) {
activeTimeTrack.delete(id);
const funcInfoNode = activeAsyncProcess.get(id);
ioController.sendInfo(funcInfoNode);
}
});
previousTime = Date.now();
return;
}
setInterval(check_ActiveTimeTrack,CHECK_DURATION);
function deleteEntireBranch(triggerAsyncId) {
if (triggerAsyncId < 7) return;
const rootAsyncId = findRootId(triggerAsyncId)
deleteThisBranch(rootAsyncId);
function deleteThisBranch(id) { // delete branch based on root async id ,won't catch the broken subbranch if middle nodes are missing
activeAsyncProcess.forEach((funcNode, asyncId) => {
if (funcNode.triggerAsyncId === id) {
deleteThisBranch(asyncId);
}
});
idDeleted.push(id);
activeTimeTrack.delete(id);
activeAsyncProcess.delete(id);
return;
}
function findRootId(asyncId) {
let rootId = asyncId;
let funcNode = activeAsyncProcess.get(asyncId);
while (funcNode && funcNode.triggerAsyncId>=7) {
rootId = funcNode.triggerAsyncId;
funcNode = activeAsyncProcess.get(rootId);
}
return rootId;
}
}
function init(asyncId, type, triggerAsyncId, resource) {
const err = new Error().stack;
const errMessage = err.split('\n');
const newErr = errMessageParser(errMessage);
if(type === 'TCPSERVERWRAP' && triggerAsyncId === cid){
const funcInfoNode = new funcInfo(asyncId, triggerAsyncId, type);
funcInfoNode.errMessage = newErr;
ioController.sendInfo(funcInfoNode);
}
if(resource.constructor.name === 'Socket' && resource.server && resource.server._connectionKey === `6::::${portNumber}`){
deleteEntireBranch(triggerAsyncId);
return;
}
if(resource.args && resource.args[0] && resource.args[0].url) {
if(resource.args[0].url.includes('socket.io') && resource.args[0].socket.server._connectionKey === `6::::${portNumber}`) {
deleteEntireBranch(triggerAsyncId);
return;
}
}
if(resource.args && resource.args[0] && resource.args[0].constructor.name === 'Socket'){
if(resource.args[0].server && resource.args[0].server._connectionKey === `6::::${portNumber}`){
deleteEntireBranch(triggerAsyncId);
return;
}
}
if( err.includes('ioController_mnode.js') ||
err.includes('/async-optics/node_modules/') ||
err.includes('/async-optics/packageMonitor.js') ||
err.includes(`at AsyncHook.init (${__dirname}/asyncMonitor.js)`) &&
err.includes('at TCP.emitInitNative (internal/async_hooks.js)') ) {
return;
} else if(triggerAsyncId < 8 || activeAsyncProcess.get(triggerAsyncId)) {
const p = funcInfoParser(asyncId, type, triggerAsyncId, resource, newErr);
if (p.shouldKeep) {
const funcInfoNode = new funcInfo(asyncId, triggerAsyncId, type);
funcInfoNode.errMessage = newErr;
funcInfoNode.resourceInfo = p.resourceInfo;
activeAsyncProcess.set(asyncId, funcInfoNode);
activeTimeTrack.set(asyncId, Date.now());
performance.mark(`${type}-${asyncId}-Init`);
}
}
return;
}
function destroy(asyncId) {
if (activeAsyncProcess.has(asyncId)) {
const type = activeAsyncProcess.get(asyncId).type;
performance.mark(`${type}-${asyncId}-Destroy`);
performance.measure(`${type}-${asyncId}`,
`${type}-${asyncId}-Init`,
`${type}-${asyncId}-Destroy`);
}
}
const obs = new PerformanceObserver((list, observer) => {
const funcInfoEntries = list.getEntries()[0];
const asyncId = Number(funcInfoEntries.name.split('-')[1]);
const funcInfoNode = activeAsyncProcess.get(asyncId);
funcInfoNode.duration = funcInfoEntries.duration;
funcInfoNode.startTime = funcInfoEntries.startTime;
funcInfoNode.endTime = funcInfoEntries.startTime + funcInfoEntries.duration;
activeAsyncProcess.delete(asyncId);
activeTimeTrack.delete(asyncId);
performance.clearMeasures(funcInfoEntries.name);
performance.clearMarks(`${funcInfoEntries.name}-Init`);
performance.clearMarks(`${funcInfoEntries.name}-Destroy`);
ioController.sendInfo(funcInfoNode);
});
//entryTypes can be: 'node', 'mark', 'measure', 'gc', or 'function'
obs.observe({ entryTypes: ['measure'], buffered: false });
function before(asyncId) {}
function after(asyncId) {}
}
module.exports = asyncMonitor;