This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathudt.js
415 lines (322 loc) · 10.4 KB
/
udt.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var assert = require('assert');
var util = require('util');
var EventEmitter = require('events');
var PlatformProvider, platformProvider, dtraceProbe;
try {
var PROBE_ARGUMENT_MAP = process.binding('dtrace').constants;
} catch(e) {
var PROBE_ARGUMENT_MAP = {
STRING: 1,
JSON: 2,
INT64: 3,
UINT32: 4,
INT32: 5,
UINT16: 6,
INT16: 7,
UINT8: 8,
INT8: 9
}
}
PROBE_ARGUMENT_MAP['CHAR *'] = PROBE_ARGUMENT_MAP.STRING;
for (var argname in PROBE_ARGUMENT_MAP) {
PROBE_ARGUMENT_MAP[argname.toLowerCase()] = PROBE_ARGUMENT_MAP[argname];
PROBE_ARGUMENT_MAP[PROBE_ARGUMENT_MAP[argname]] = argname.toLowerCase();
}
PROBE_ARGUMENT_MAP.INT = PROBE_ARGUMENT_MAP.INT64;
PROBE_ARGUMENT_MAP.int = PROBE_ARGUMENT_MAP.INT64;
var listenerCache = {};
function getListeners(namespace, module, probe) {
var callbacks = [];
Object.keys(listenerCache).forEach(function(namespaceKey) {
if (namespaceKey !== namespace && namespaceKey !== '*')
return;
namespace = listenerCache[namespaceKey];
Object.keys(namespace).forEach(function(moduleKey) {
if (moduleKey !== module && moduleKey !== '*')
return;
var m = namespace[moduleKey];
Object.keys(m).forEach(function(probeKey) {
if (probeKey !== probe && probeKey !== '*')
return;
Object.keys(m[probeKey]).forEach(function(funcKey) {
callbacks.push(m[probeKey][funcKey]);
});
});
});
});
return callbacks;
}
function Probe(provider, name, signature) {
if (!(this instanceof Probe))
return new Probe(provider, name, signature);
this.provider = provider;
this.name = name;
this.signature = signature;
this.info = {
namespace: provider.namespace,
name: provider.name,
probe: name
};
}
Probe.prototype.fire = function(fargs, callback) {
this.provider._throwDisabled('fire', this.name);
var info = this.info;
var namespace = info.namespace;
var provider = info.name;
var listeners = getListeners(namespace, provider, this.name);
// We have no one listening for this event don't marshal arguments
if (!this.binding && !listeners.length) {
return;
}
var args;
if (!util.isFunction(callback)) {
callback = fargs;
fargs = [];
}
if (!util.isFunction(callback))
callback = function() { return []; };
if (!util.isArray(fargs))
fargs = [fargs];
// we are either calling a JS or C++ function, but we only want to call the
// provided marshalling callback once per firing, so cache those results.
// event handlers could potentially muck with this array, and mess up
// subsequent handlers, they shouldn't do that.
args = callback(fargs);
if (this.binding)
this.binding(args);
var i = 0;
for (i = 0; i < listeners.length; i++) {
var f = listeners[i];
f(args, info);
}
};
function Provider(options) {
if (!(this instanceof Provider))
return new Provider(options);
this.namespace = options.namespace;
this.name = options.name;
this.enabled = false;
this.probes = {};
}
Provider.prototype.enable = function() {
this.enabled = true;
};
Provider.prototype.disable = function() {
this.enabled = false;
};
Provider.prototype._signatureEqual = function(a, b) {
return true;
};
Provider.prototype._probeFormat = function(name) {
return util.format('%s:%s:%s', this.namespace, this.name, name);
};
Provider.prototype._getProbe = function(name) {
return this.probes[name];
};
Provider.prototype._addProbe = function(probe) {
this.probes[probe.name] = probe;
};
Provider.prototype._verifySignature = function(probe, signature) {
// TODO(tjfontaine) should this be deferred until enable()?
if (this.enabled && probe &&
!this._signatureEqual(probe.signature, signature)) {
var msg = 'Probe %s:%s enabled with signature %j, new signature: %j';
msg = util.format(msg, this.name, name, probe.signature, signature);
throw new Error(msg);
}
};
Provider.prototype.addProbe = function(name, signature/*[, arg2, arg3]*/) {
var probe = this._getProbe(name);
if (!util.isArray(signature))
signature = Array.prototype.slice.call(arguments, 1);
this._verifySignature(probe, signature);
if (!probe) {
probe = new Probe(this, name, signature);
this._addProbe(probe);
}
return probe;
};
Provider.prototype.removeProbe = function(name) {
this._throwDisabled('remove', name);
delete this.probes[name];
};
Provider.prototype._throwDisabled = function(action, name) {
if (!this.enabled) {
var msg = util.format('Cannot %s probe %s while disabled',
action,
this._probeFormat(name));
throw new Error(msg);
}
return true;
};
Provider.prototype.fire = function(name, fargs, callback) {
var probe = this.probes[name];
if (!probe)
return;
probe.fire(fargs, callback);
};
function StaticProvider(name) {
Provider.call(this, {
namespace: 'node',
name: name
});
}
util.inherits(StaticProvider, Provider);
StaticProvider.prototype.addProbe = function(name, signature, binding) {
var probe = Provider.prototype.addProbe.call(this, name, signature);
probe.binding = binding;
return probe;
};
StaticProvider.prototype.enable = function() {
this.enabled = true;
};
function DTraceProbe(provider, name, signature, handle) {
if (!(this instanceof DTraceProbe))
return new DTraceProbe(provider, name, signature, handle);
Probe.call(this, provider, name, signature);
var mappedSignature = new Array(signature.length);
for (var i = 0; i < signature.length; i++) {
var originalType = signature[i];
var mappedType = PROBE_ARGUMENT_MAP[originalType];
if (!mappedType)
throw new TypeError(util.format('%s is not a valid type', originalType));
mappedSignature[i] = mappedType;
}
this.dprobe = new dtraceProbe(name, mappedSignature);
handle.addProbe(this.dprobe);
}
util.inherits(DTraceProbe, Probe);
DTraceProbe.prototype.binding = function(args) {
this.dprobe.fire(args);
};
function DTraceProvider(options) {
Provider.call(this, options);
this._handle = new platformProvider(options.namespace, options.name);
}
util.inherits(DTraceProvider, Provider);
DTraceProvider.prototype.addProbe = function(name, signature) {
var probe = this._getProbe(name);
if (!util.isArray(signature))
signature = Array.prototype.slice.call(arguments, 1);
this._verifySignature(probe, signature);
var probe = new DTraceProbe(this, name, signature, this._handle);
this._addProbe(probe);
return probe;
};
DTraceProvider.prototype.removeProbe = function(name) {
var probe = this.probes[name];
probe.binding = null;
var dprobe = probe.dprobe;
delete probe.dprobe;
this._handle.removeProbe(dprobe);
Provider.prototype.removeProbe.call(this, name);
};
DTraceProvider.prototype.enable = function() {
Provider.prototype.enable.call(this);
return this._handle.enable();
};
DTraceProvider.prototype.disable = function() {
Provider.prototype.disable.call(this);
return this._handle.disable();
};
var namespaces = {};
function _registerProvider(provider) {
var providers = namespaces[provider.namespace];
if (!providers)
providers = namespaces[provider.namespace] = {};
delete providers[provider.name];
providers[provider.name] = provider;
}
function _makeProviderOptions(options) {
if (util.isString(options)) {
options = {
namespace: options,
name: options
};
}
return options;
}
exports.createProvider = function(options) {
if (!PlatformProvider) {
if (process.config.variables.node_use_dtrace) {
try {
platformProvider = process.binding('dtrace_provider').DTraceProvider;
dtraceProbe = process.binding('dtrace_provider').DTraceProbe;
PlatformProvider = DTraceProvider;
} catch(e) {
PlatformProvider = Provider;
}
} else {
PlatformProvider = Provider;
}
}
options = _makeProviderOptions(options);
if (options.namespace === 'node') {
throw new Error(util.format(
'Cannot create provider %s in the node namespace',
options.name));
}
var m = exports.getProvider(options);
if (!m) {
m = new PlatformProvider(options);
_registerProvider(m);
}
return m;
};
exports.getProvider = function(options) {
options = _makeProviderOptions(options);
var namespace = namespaces[options.namespace];
if (!namespace) return;
return namespace[options.name];
};
exports.list = function() {
var ret = {};
Object.keys(namespaces).forEach(function(namespace) {
var ns = ret[namespace] = {};
namespace = namespaces[namespace];
Object.keys(namespace).forEach(function(provider) {
var ps = ns[provider] = {};
provider = namespace[provider];
Object.keys(provider.probes).forEach(function(probe) {
var p = ps[probe] = {};
probe = provider.probes[probe];
p.signature = probe.signature;
});
});
});
return ret;
};
function getListenerCache(namespace, module, probe) {
var lNamespace = listenerCache[namespace] = listenerCache[namespace] || {};
var lModule = lNamespace[module] = lNamespace[module] || {};
var lCb = lModule[probe] = lModule[probe] || {};
return lCb;
}
exports.on = function(namespace, module, probe, cb) {
var lCb = getListenerCache(namespace, module, probe)[cb] = cb;
};
exports.removeListener = function(module, probe, cb) {
var lCb = getListenerCache(namespace, module, probe);
if (lCb)
delete lCb[cb];
};