-
Notifications
You must be signed in to change notification settings - Fork 30k
/
report.js
346 lines (318 loc) Β· 13.7 KB
/
report.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
'use strict';
const assert = require('assert');
const fs = require('fs');
const net = require('net');
const os = require('os');
const path = require('path');
const util = require('util');
const cpus = os.cpus();
function findReports(pid, dir) {
// Default filenames are of the form
// report.<date>.<time>.<pid>.<tid>.<seq>.json
const format = '^report\\.\\d+\\.\\d+\\.' + pid + '\\.\\d+\\.\\d+\\.json$';
const filePattern = new RegExp(format);
const files = fs.readdirSync(dir);
const results = [];
files.forEach((file) => {
if (filePattern.test(file))
results.push(path.join(dir, file));
});
return results;
}
function validate(filepath, fields) {
const report = fs.readFileSync(filepath, 'utf8');
if (process.report.compact) {
const end = report.indexOf('\n');
assert.strictEqual(end, report.length - 1);
}
validateContent(JSON.parse(report), fields);
}
function validateContent(report, fields = []) {
if (typeof report === 'string') {
try {
report = JSON.parse(report);
} catch {
throw new TypeError(
'validateContent() expects a JSON string or JavaScript Object');
}
}
try {
_validateContent(report, fields);
} catch (err) {
try {
err.stack += util.format('\n------\nFailing Report:\n%O', report);
} catch {
// Continue regardless of error.
}
throw err;
}
}
function _validateContent(report, fields = []) {
const isWindows = process.platform === 'win32';
const isJavaScriptThreadReport = report.javascriptHeap != null;
// Verify that all sections are present as own properties of the report.
const sections = ['header', 'nativeStack', 'javascriptStack', 'libuv',
'sharedObjects', 'resourceUsage', 'workers'];
if (!process.report.excludeEnv) {
sections.push('environmentVariables');
}
if (!isWindows)
sections.push('userLimits');
if (report.uvthreadResourceUsage)
sections.push('uvthreadResourceUsage');
if (isJavaScriptThreadReport)
sections.push('javascriptHeap');
checkForUnknownFields(report, sections);
sections.forEach((section) => {
assert(Object.hasOwn(report, section));
assert(typeof report[section] === 'object' && report[section] !== null);
});
fields.forEach((field) => {
function checkLoop(actual, rest, expect) {
actual = actual[rest.shift()];
if (rest.length === 0 && actual !== undefined) {
assert.strictEqual(actual, expect);
} else {
assert(actual);
checkLoop(actual, rest, expect);
}
}
let actual, expect;
if (Array.isArray(field)) {
[actual, expect] = field;
} else {
actual = field;
expect = undefined;
}
checkLoop(report, actual.split('.'), expect);
});
// Verify the format of the header section.
const header = report.header;
const headerFields = ['event', 'trigger', 'filename', 'dumpEventTime',
'dumpEventTimeStamp', 'processId', 'commandLine',
'nodejsVersion', 'wordSize', 'arch', 'platform',
'componentVersions', 'release', 'osName', 'osRelease',
'osVersion', 'osMachine', 'cpus', 'host',
'glibcVersionRuntime', 'glibcVersionCompiler', 'cwd',
'reportVersion', 'networkInterfaces', 'threadId'];
checkForUnknownFields(header, headerFields);
assert.strictEqual(header.reportVersion, 5); // Increment as needed.
assert.strictEqual(typeof header.event, 'string');
assert.strictEqual(typeof header.trigger, 'string');
assert(typeof header.filename === 'string' || header.filename === null);
assert.notStrictEqual(new Date(header.dumpEventTime).toString(),
'Invalid Date');
assert(String(+header.dumpEventTimeStamp), header.dumpEventTimeStamp);
assert(Number.isSafeInteger(header.processId));
assert(Number.isSafeInteger(header.threadId) || header.threadId === null);
assert.strictEqual(typeof header.cwd, 'string');
assert(Array.isArray(header.commandLine));
header.commandLine.forEach((arg) => {
assert.strictEqual(typeof arg, 'string');
});
assert.strictEqual(header.nodejsVersion, process.version);
assert(Number.isSafeInteger(header.wordSize));
assert.strictEqual(header.arch, os.arch());
assert.strictEqual(header.platform, os.platform());
assert.deepStrictEqual(header.componentVersions, process.versions);
assert.deepStrictEqual(header.release, process.release);
assert.strictEqual(header.osName, os.type());
assert.strictEqual(header.osRelease, os.release());
assert.strictEqual(typeof header.osVersion, 'string');
assert.strictEqual(typeof header.osMachine, 'string');
assert(Array.isArray(header.cpus));
assert.strictEqual(header.cpus.length, cpus.length);
header.cpus.forEach((cpu) => {
assert.strictEqual(typeof cpu.model, 'string');
assert.strictEqual(typeof cpu.speed, 'number');
assert.strictEqual(typeof cpu.user, 'number');
assert.strictEqual(typeof cpu.nice, 'number');
assert.strictEqual(typeof cpu.sys, 'number');
assert.strictEqual(typeof cpu.idle, 'number');
assert.strictEqual(typeof cpu.irq, 'number');
assert(cpus.some((c) => {
return c.model === cpu.model;
}));
});
assert(Array.isArray(header.networkInterfaces));
header.networkInterfaces.forEach((iface) => {
assert.strictEqual(typeof iface.name, 'string');
assert.strictEqual(typeof iface.internal, 'boolean');
assert.match(iface.mac, /^([0-9A-F][0-9A-F]:){5}[0-9A-F]{2}$/i);
if (iface.family === 'IPv4') {
assert.strictEqual(net.isIPv4(iface.address), true);
assert.strictEqual(net.isIPv4(iface.netmask), true);
assert.strictEqual(iface.scopeid, undefined);
} else if (iface.family === 'IPv6') {
assert.strictEqual(net.isIPv6(iface.address), true);
assert.strictEqual(net.isIPv6(iface.netmask), true);
assert(Number.isInteger(iface.scopeid));
} else {
assert.strictEqual(iface.family, 'unknown');
assert.strictEqual(iface.address, undefined);
assert.strictEqual(iface.netmask, undefined);
assert.strictEqual(iface.scopeid, undefined);
}
});
assert.strictEqual(header.host, os.hostname());
// Verify the format of the nativeStack section.
assert(Array.isArray(report.nativeStack));
report.nativeStack.forEach((frame) => {
assert(typeof frame === 'object' && frame !== null);
checkForUnknownFields(frame, ['pc', 'symbol']);
assert.strictEqual(typeof frame.pc, 'string');
assert.match(frame.pc, /^0x[0-9a-f]+$/);
assert.strictEqual(typeof frame.symbol, 'string');
});
if (isJavaScriptThreadReport) {
// Verify the format of the javascriptStack section.
checkForUnknownFields(report.javascriptStack,
['message', 'stack', 'errorProperties']);
assert.strictEqual(typeof report.javascriptStack.errorProperties,
'object');
assert.strictEqual(typeof report.javascriptStack.message, 'string');
if (report.javascriptStack.stack !== undefined) {
assert(Array.isArray(report.javascriptStack.stack));
report.javascriptStack.stack.forEach((frame) => {
assert.strictEqual(typeof frame, 'string');
});
}
// Verify the format of the javascriptHeap section.
const heap = report.javascriptHeap;
// See `PrintGCStatistics` in node_report.cc
const jsHeapFields = [
'totalMemory',
'executableMemory',
'totalCommittedMemory',
'availableMemory',
'totalGlobalHandlesMemory',
'usedGlobalHandlesMemory',
'usedMemory',
'memoryLimit',
'mallocedMemory',
'externalMemory',
'peakMallocedMemory',
'nativeContextCount',
'detachedContextCount',
'doesZapGarbage',
'heapSpaces',
];
checkForUnknownFields(heap, jsHeapFields);
// Do not check `heapSpaces` here
for (let i = 0; i < jsHeapFields.length - 1; i++) {
assert(
Number.isSafeInteger(heap[jsHeapFields[i]]),
`heap.${jsHeapFields[i]} is not a safe integer`,
);
}
assert(typeof heap.heapSpaces === 'object' && heap.heapSpaces !== null);
const heapSpaceFields = ['memorySize', 'committedMemory', 'capacity',
'used', 'available'];
Object.keys(heap.heapSpaces).forEach((spaceName) => {
const space = heap.heapSpaces[spaceName];
checkForUnknownFields(space, heapSpaceFields);
heapSpaceFields.forEach((field) => {
assert(Number.isSafeInteger(space[field]));
});
});
}
// Verify the format of the resourceUsage section.
const usage = { ...report.resourceUsage };
// Delete it, otherwise checkForUnknownFields will throw error
delete usage.constrained_memory;
const resourceUsageFields = ['userCpuSeconds', 'kernelCpuSeconds',
'cpuConsumptionPercent', 'userCpuConsumptionPercent',
'kernelCpuConsumptionPercent',
'maxRss', 'rss', 'free_memory', 'total_memory',
'available_memory', 'pageFaults', 'fsActivity'];
checkForUnknownFields(usage, resourceUsageFields);
assert.strictEqual(typeof usage.userCpuSeconds, 'number');
assert.strictEqual(typeof usage.kernelCpuSeconds, 'number');
assert.strictEqual(typeof usage.cpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.userCpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.kernelCpuConsumptionPercent, 'number');
assert(typeof usage.rss, 'string');
assert(typeof usage.maxRss, 'string');
assert(typeof usage.free_memory, 'string');
assert(typeof usage.total_memory, 'string');
assert(typeof usage.available_memory, 'string');
// This field may not exist
if (report.resourceUsage.constrained_memory) {
assert(typeof report.resourceUsage.constrained_memory, 'string');
}
assert(typeof usage.pageFaults === 'object' && usage.pageFaults !== null);
checkForUnknownFields(usage.pageFaults, ['IORequired', 'IONotRequired']);
assert(Number.isSafeInteger(usage.pageFaults.IORequired));
assert(Number.isSafeInteger(usage.pageFaults.IONotRequired));
assert(typeof usage.fsActivity === 'object' && usage.fsActivity !== null);
checkForUnknownFields(usage.fsActivity, ['reads', 'writes']);
assert(Number.isSafeInteger(usage.fsActivity.reads));
assert(Number.isSafeInteger(usage.fsActivity.writes));
// Verify the format of the uvthreadResourceUsage section, if present.
if (report.uvthreadResourceUsage) {
const usage = report.uvthreadResourceUsage;
const threadUsageFields = ['userCpuSeconds', 'kernelCpuSeconds',
'cpuConsumptionPercent', 'fsActivity',
'userCpuConsumptionPercent',
'kernelCpuConsumptionPercent'];
checkForUnknownFields(usage, threadUsageFields);
assert.strictEqual(typeof usage.userCpuSeconds, 'number');
assert.strictEqual(typeof usage.kernelCpuSeconds, 'number');
assert.strictEqual(typeof usage.cpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.userCpuConsumptionPercent, 'number');
assert.strictEqual(typeof usage.kernelCpuConsumptionPercent, 'number');
assert(typeof usage.fsActivity === 'object' && usage.fsActivity !== null);
checkForUnknownFields(usage.fsActivity, ['reads', 'writes']);
assert(Number.isSafeInteger(usage.fsActivity.reads));
assert(Number.isSafeInteger(usage.fsActivity.writes));
}
// Verify the format of the libuv section.
assert(Array.isArray(report.libuv));
report.libuv.forEach((resource) => {
assert.strictEqual(typeof resource.type, 'string');
assert.strictEqual(typeof resource.address, 'string');
assert.match(resource.address, /^0x[0-9a-f]+$/);
assert.strictEqual(typeof resource.is_active, 'boolean');
assert.strictEqual(typeof resource.is_referenced,
resource.type === 'loop' ? 'undefined' : 'boolean');
});
if (!process.report.excludeEnv) {
// Verify the format of the environmentVariables section.
for (const [key, value] of Object.entries(report.environmentVariables)) {
assert.strictEqual(typeof key, 'string');
assert.strictEqual(typeof value, 'string');
}
}
// Verify the format of the userLimits section on non-Windows platforms.
if (!isWindows) {
const userLimitsFields = ['core_file_size_blocks', 'data_seg_size_bytes',
'file_size_blocks', 'max_locked_memory_bytes',
'max_memory_size_bytes', 'open_files',
'stack_size_bytes', 'cpu_time_seconds',
'max_user_processes', 'virtual_memory_bytes'];
checkForUnknownFields(report.userLimits, userLimitsFields);
for (const [type, limits] of Object.entries(report.userLimits)) {
assert.strictEqual(typeof type, 'string');
assert(typeof limits === 'object' && limits !== null);
checkForUnknownFields(limits, ['soft', 'hard']);
assert(typeof limits.soft === 'number' || limits.soft === 'unlimited',
`Invalid ${type} soft limit of ${limits.soft}`);
assert(typeof limits.hard === 'number' || limits.hard === 'unlimited',
`Invalid ${type} hard limit of ${limits.hard}`);
}
}
// Verify the format of the sharedObjects section.
assert(Array.isArray(report.sharedObjects));
report.sharedObjects.forEach((sharedObject) => {
assert.strictEqual(typeof sharedObject, 'string');
});
// Verify the format of the workers section.
assert(Array.isArray(report.workers));
report.workers.forEach((worker) => _validateContent(worker));
}
function checkForUnknownFields(actual, expected) {
Object.keys(actual).forEach((field) => {
assert(expected.includes(field), `'${field}' not expected in ${expected}`);
});
}
module.exports = { findReports, validate, validateContent };