forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobserve.js
More file actions
345 lines (295 loc) · 8.19 KB
/
Copy pathobserve.js
File metadata and controls
345 lines (295 loc) · 8.19 KB
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
'use strict';
const {
ArrayFrom,
ArrayIsArray,
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSort,
ObjectDefineProperties,
ObjectFreeze,
ObjectKeys,
SafeMap,
SafeSet,
Symbol,
} = primordials;
const {
constants: {
NODE_PERFORMANCE_ENTRY_TYPE_GC,
NODE_PERFORMANCE_ENTRY_TYPE_HTTP2,
NODE_PERFORMANCE_ENTRY_TYPE_HTTP,
},
installGarbageCollectionTracking,
observerCounts,
removeGarbageCollectionTracking,
setupObservers,
} = internalBinding('performance');
const {
InternalPerformanceEntry,
isPerformanceEntry,
} = require('internal/perf/performance_entry');
const {
codes: {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_MISSING_ARGS,
},
} = require('internal/errors');
const {
validateCallback,
validateObject,
} = require('internal/validators');
const {
customInspectSymbol: kInspect,
deprecate,
} = require('internal/util');
const {
setImmediate,
} = require('timers');
const { inspect } = require('util');
const kBuffer = Symbol('kBuffer');
const kCallback = Symbol('kCallback');
const kDispatch = Symbol('kDispatch');
const kEntryTypes = Symbol('kEntryTypes');
const kMaybeBuffer = Symbol('kMaybeBuffer');
const kDeprecatedFields = Symbol('kDeprecatedFields');
const kType = Symbol('kType');
const kDeprecationMessage =
'Custom PerformanceEntry accessors are deprecated. ' +
'Please use the detail property.';
const kTypeSingle = 0;
const kTypeMultiple = 1;
let gcTrackingInstalled = false;
const kSupportedEntryTypes = ObjectFreeze([
'function',
'gc',
'http',
'http2',
'mark',
'measure',
]);
const kObservers = new SafeSet();
const kPending = new SafeSet();
let isPending = false;
function queuePending() {
if (isPending) return;
isPending = true;
setImmediate(() => {
isPending = false;
for (const pending of kPending)
pending[kDispatch]();
kPending.clear();
});
}
function getObserverType(type) {
switch (type) {
case 'gc': return NODE_PERFORMANCE_ENTRY_TYPE_GC;
case 'http2': return NODE_PERFORMANCE_ENTRY_TYPE_HTTP2;
case 'http': return NODE_PERFORMANCE_ENTRY_TYPE_HTTP;
}
}
function maybeDecrementObserverCounts(entryTypes) {
for (const type of entryTypes) {
const observerType = getObserverType(type);
if (observerType !== undefined) {
observerCounts[observerType]--;
if (observerType === NODE_PERFORMANCE_ENTRY_TYPE_GC &&
observerCounts[observerType] === 0) {
removeGarbageCollectionTracking();
}
}
}
}
function maybeIncrementObserverCount(type) {
const observerType = getObserverType(type);
if (observerType !== undefined) {
observerCounts[observerType]++;
if (!gcTrackingInstalled &&
observerType === NODE_PERFORMANCE_ENTRY_TYPE_GC) {
installGarbageCollectionTracking();
gcTrackingInstalled = true;
}
}
}
class PerformanceObserverEntryList {
constructor(entries) {
this[kBuffer] = ArrayPrototypeSort(entries, (first, second) => {
if (first.startTime < second.startTime) return -1;
if (first.startTime > second.startTime) return 1;
return 0;
});
}
getEntries() {
return ArrayPrototypeSlice(this[kBuffer]);
}
getEntriesByType(type) {
type = `${type}`;
return ArrayPrototypeFilter(
this[kBuffer],
(entry) => entry.entryType === type);
}
getEntriesByName(name) {
name = `${name}`;
return ArrayPrototypeFilter(
this[kBuffer],
(entry) => entry.name === name);
}
[kInspect](depth, options) {
if (depth < 0) return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};
return `PerformanceObserverEntryList ${inspect(this[kBuffer], opts)}`;
}
}
class PerformanceObserver {
constructor(callback) {
// TODO(joyeecheung): V8 snapshot does not support instance member
// initializers for now:
// https://bugs.chromium.org/p/v8/issues/detail?id=10704
this[kBuffer] = [];
this[kEntryTypes] = new SafeSet();
this[kType] = undefined;
validateCallback(callback);
this[kCallback] = callback;
}
observe(options = {}) {
validateObject(options, 'options');
const {
entryTypes,
type,
} = { ...options };
if (entryTypes === undefined && type === undefined)
throw new ERR_MISSING_ARGS('options.entryTypes', 'options.type');
switch (this[kType]) {
case undefined:
if (entryTypes !== undefined) this[kType] = kTypeMultiple;
if (type !== undefined) this[kType] = kTypeSingle;
break;
case kTypeSingle:
if (entryTypes !== undefined)
throw new ERR_INVALID_ARG_VALUE('options.entryTypes', entryTypes);
break;
case kTypeMultiple:
if (type !== undefined)
throw new ERR_INVALID_ARG_VALUE('options.type', type);
break;
}
if (this[kType] === kTypeMultiple) {
if (!ArrayIsArray(entryTypes)) {
throw new ERR_INVALID_ARG_TYPE(
'options.entryTypes',
'string[]',
entryTypes);
}
maybeDecrementObserverCounts(this[kEntryTypes]);
this[kEntryTypes].clear();
for (let n = 0; n < entryTypes.length; n++) {
if (ArrayPrototypeIncludes(kSupportedEntryTypes, entryTypes[n])) {
this[kEntryTypes].add(entryTypes[n]);
maybeIncrementObserverCount(entryTypes[n]);
}
}
} else {
if (!ArrayPrototypeIncludes(kSupportedEntryTypes, type))
return;
this[kEntryTypes].add(type);
maybeIncrementObserverCount(type);
}
if (this[kEntryTypes].size)
kObservers.add(this);
else
this.disconnect();
}
disconnect() {
maybeDecrementObserverCounts(this[kEntryTypes]);
kObservers.delete(this);
kPending.delete(this);
this[kBuffer] = [];
this[kEntryTypes].clear();
this[kType] = undefined;
}
takeRecords() {
const list = this[kBuffer];
this[kBuffer] = [];
return new PerformanceObserverEntryList(list);
}
static get supportedEntryTypes() {
return kSupportedEntryTypes;
}
[kMaybeBuffer](entry) {
if (!this[kEntryTypes].has(entry.entryType))
return;
ArrayPrototypePush(this[kBuffer], entry);
kPending.add(this);
if (kPending.size)
queuePending();
}
[kDispatch]() { this[kCallback](this.takeRecords(), this); }
[kInspect](depth, options) {
if (depth < 0) return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};
return `PerformanceObserver ${inspect({
connected: kObservers.has(this),
pending: kPending.has(this),
entryTypes: ArrayFrom(this[kEntryTypes]),
buffer: this[kBuffer],
}, opts)}`;
}
}
function enqueue(entry) {
if (!isPerformanceEntry(entry))
throw new ERR_INVALID_ARG_TYPE('entry', 'PerformanceEntry', entry);
for (const obs of kObservers) {
obs[kMaybeBuffer](entry);
}
}
function observerCallback(name, type, startTime, duration, details) {
const entry =
new InternalPerformanceEntry(
name,
type,
startTime,
duration,
details);
if (details !== undefined) {
// GC, HTTP2, and HTTP PerformanceEntry used additional
// properties directly off the entry. Those have been
// moved into the details property. The existing accessors
// are still included but are deprecated.
entry[kDeprecatedFields] = new SafeMap();
const detailKeys = ObjectKeys(details);
const props = {};
for (let n = 0; n < detailKeys.length; n++) {
const key = detailKeys[n];
entry[kDeprecatedFields].set(key, details[key]);
props[key] = {
configurable: true,
enumerable: true,
get: deprecate(() => {
return entry[kDeprecatedFields].get(key);
}, kDeprecationMessage, 'DEP0152'),
set: deprecate((value) => {
entry[kDeprecatedFields].set(key, value);
}, kDeprecationMessage, 'DEP0152'),
};
}
ObjectDefineProperties(entry, props);
}
enqueue(entry);
}
setupObservers(observerCallback);
function hasObserver(type) {
const observerType = getObserverType(type);
return observerCounts[observerType] > 0;
}
module.exports = {
PerformanceObserver,
enqueue,
hasObserver,
};