-
Notifications
You must be signed in to change notification settings - Fork 3
/
random1.js
239 lines (213 loc) · 8.85 KB
/
random1.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const Register = require('../register');
const HBGraph = require('../hbgraph/hbgraph');
const installNjstrace = require('../helpers/installnjstrace');
const async_hooks = require('async_hooks');
const random = require('random');
const falafel = require('falafel');
/**
* Strategy: random 1
* - When a callback can be postponed, it decides randomly
* - A callback may be postponed only once
*/
class Random1Strategy {
constructor(hbi, runid, logDir, config) {
this.hbi = hbi;
this.hbgraph = new HBGraph(hbi);
this.register = new Register(logDir, runid);
this.installNjstrace = installNjstrace;
this.config = config;
this.postponeTick = true; //default value - true (used in experiments)
}
install() {
this.installNjstrace(this.config);
this.installAsyncHooks();
this.installFunctionInstrumentation();
this.installStrategyFunctions();
}
installAsyncHooks() {
let self = this;
function before() {
self.isTopLevelCallbackExecuted = false;
}
const asyncHook = async_hooks.createHook({ before });
asyncHook.enable();
}
installFunctionInstrumentation() {
let self = this;
global.NR_HACK_NJS_SHOULD_INSTRUMENT = function (file, name, line, node) {
if (self.hbi.asyncObjects.hasFunction({ file, name, line })) {
self.setGlobalVariablesForInstrumentation(file, name, line, node);
return true;
}
return false;
}
}
isCallBack() {
return !this.isTopLevelCallbackExecuted;
}
makeDecision(asyncObject) {
if (!this.postponeTick) {
if(asyncObject.entry.type === 'TickObject')
return false;
}
return random.int(1, 10) <= 5;
}
installStrategyFunctions() {
let self = this;
self.isTopLevelCallbackExecuted = false;
global.NR_POSTPONE_TIME = 500;
global.NR_SHOULD_POSTPONE = function (fn) {
if (self.isCallBack()) {
let asyncObject = self.hbgraph.find(fn); //Find the callback in the h-b graph
if (asyncObject === null) {
self.register.put({ entry: 'no_callback_found', ...fn });
return {
isCallback: false,
postpone: false
};
}
let postpone = false;
if (self.hbgraph.mayHappen(asyncObject)) { //no dependency on h-b
if (self.hbgraph.mayPostpone(asyncObject))
postpone = self.makeDecision(asyncObject);
else
postpone = false; //only callback available, it should run
}
else {
if (!this.postponeTick && asyncObject.entry.type === 'TickObject') {
postpone = false;
}
else
postpone = true; //postpone and hope its dependencies are resolved
}
self.register.put({ entry: 'should_postpone', postpone, asyncObject });
return {
isCallback: true,
postpone: postpone,
asyncObjectId: asyncObject.id
};
}
else {
return {
isCallback: false,
postpone: false
};
}
}
global.NR_NOTIFY = function (fn, info) { // called every time a callback is actually executed
self.isTopLevelCallbackExecuted = true;
if (info.isCallback) {
if (!self.hbgraph.mayHappen({ id: info.asyncObjectId })) {
self.register.put({ entry: 'error_cb_may_not_occur', asyncObjectId: info.asyncObjectId });
}
self.hbgraph.notifyRunOf(info.asyncObjectId); //Remove it from th H-B graph
self.register.put({ entry: 'run', asyncObjectId: info.asyncObjectId, ...fn });
}
}
}
setGlobalVariablesForInstrumentation(file, name, line, node) {
let nr_functionSignature = node.params.map(p => {
if (p.type === 'RestElement') {
return '...' + p.argument.name;
}
return p.name;
}).join(',');
// console.log('------------');
// console.log(node.body.source());
// console.log('------------');
let nr_functionname = name;
if (nr_functionname === '[Anonymous]')
nr_functionname = 'NR_ANONYMOUS';
nr_functionname = '__' + nr_functionname.replace(/\./g, '_');
if (nr_functionname !== '__NR_ANONYMOUS') {
this.dealWithRecursion(name, nr_functionname, node);
}
let nr_fileinfo = `{file : ${JSON.stringify(file)}, name : ${JSON.stringify(name)}, line : ${line}}`;
if (node.type === 'ArrowFunctionExpression') {
global.NR_HACK_NJS_ENTRY =
` let nr_myThis = this;
let ${nr_functionname} = (${nr_functionSignature}) => { `;
global.NR_HACK_NJS_EXIT =
` };
let nr_info = global.NR_SHOULD_POSTPONE(${nr_fileinfo});
if ( nr_info.postpone ) {
let nr_p = new Promise(function (resolve, reject) {
setTimeout(() => {
global.NR_NOTIFY(${nr_fileinfo}, nr_info);
resolve( ${nr_functionname}.call(nr_myThis, ${nr_functionSignature}) );
}, global.NR_POSTPONE_TIME);
});
nr_p.then((v) => {
return v;
});
return nr_p;
}
else {
global.NR_NOTIFY(${nr_fileinfo}, nr_info);
let nr_ret = ${nr_functionname}.call(nr_myThis, ${nr_functionSignature});
return nr_ret;
} `;
}
else {
global.NR_HACK_NJS_ENTRY =
` let nr_myArgs = arguments;
let nr_myThis = this;
function ${nr_functionname}(${nr_functionSignature}) { `;
global.NR_HACK_NJS_EXIT =
` }
${nr_functionname} = ${nr_functionname}.bind(nr_myThis);
let nr_info = global.NR_SHOULD_POSTPONE(${nr_fileinfo}, nr_myArgs);
if ( nr_info.postpone ) {
let nr_p = new Promise(function (resolve, reject) {
setTimeout(() => {
global.NR_NOTIFY(${nr_fileinfo}, nr_info);
resolve( ${nr_functionname}.apply(nr_myThis, nr_myArgs) );
}, global.NR_POSTPONE_TIME);
});
nr_p.then((v) => {
return v;
});
return nr_p;
}
else {
global.NR_NOTIFY(${nr_fileinfo}, nr_info);
let nr_ret = ${nr_functionname}.apply(nr_myThis, nr_myArgs);
return nr_ret;
} `;
}
}
dealWithRecursion(name, newname, node) {
// any function call for the original function name should be changed to nr_functionname
//check the function body for calls to the original name
global.NR_RECURSION_BODY = undefined;
let someRec = false;
let origFuncBody = node.body.source();
origFuncBody = origFuncBody.slice(1, origFuncBody.length - 1); // Remove the open and close braces "{}"
origFuncBody = 'function ___test(){' + origFuncBody + '}';
try {
let output = falafel(origFuncBody, {
ranges: true,
locations: true,
ecmaVersion: 9
}, function processASTNode(node) {
if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
if (node.callee.name === name) {
someRec = true;
node.callee.update(newname);
}
}
});
if (someRec) { //some recursion, change the body
output = '' + output;
output = output.replace('function ___test()', '');
output = output.slice(1, output.length - 1);
global.NR_RECURSION_BODY = output;
}
}
catch (e) {
console.log('Error: Dealing with recursion');
console.log(e);
}
}
}
module.exports = Random1Strategy;