-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathinspector-util.ts
More file actions
571 lines (540 loc) · 20 KB
/
inspector-util.ts
File metadata and controls
571 lines (540 loc) · 20 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import * as inspector from 'inspector';
import * as stream from 'stream';
import { once } from 'events';
import { createServer, Server } from 'http';
import * as express from 'express';
import { asyncHandler } from './api/async-handler';
import { parsePort, pipelineAsync } from './helpers';
import { Socket } from 'net';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import { startProfiler, stopProfiler } from '@stacks/codec';
import { logger } from './logger';
import { stopwatch, Stopwatch, timeout } from '@hirosystems/api-toolkit';
type CpuProfileResult = inspector.Profiler.Profile;
interface ProfilerInstance<TStopResult = void> {
start: () => Promise<void>;
stop: () => Promise<TStopResult>;
dispose: () => Promise<void>;
session: inspector.Session;
sessionType: 'cpu' | 'memory';
stopwatch: Stopwatch;
}
function isInspectorNotConnectedError(error: unknown): boolean {
const ERR_INSPECTOR_NOT_CONNECTED = 'ERR_INSPECTOR_NOT_CONNECTED';
const isNodeError = (r: unknown): r is NodeJS.ErrnoException => r instanceof Error && 'code' in r;
return isNodeError(error) && error.code === ERR_INSPECTOR_NOT_CONNECTED;
}
/**
* Connects and enables a new `inspector` session, then starts an internal v8 CPU profiling process.
* @returns A function to stop the profiling, and return the CPU profile result object.
* The result object can be used to create a `.cpuprofile` file using JSON.stringify.
* Use VSCode or Chrome's 'DevTools for Node' (under chrome://inspect) to visualize the `.cpuprofile` file.
* @param samplingInterval - Optionally set sampling interval in microseconds, default is 1000 microseconds.
*/
function initCpuProfiling(samplingInterval?: number): ProfilerInstance<CpuProfileResult> {
const sessionStopwatch = stopwatch();
const session = new inspector.Session();
session.connect();
logger.info(`[CpuProfiler] Connect session took ${sessionStopwatch.getElapsedAndRestart()}ms`);
const start = async () => {
const sw = stopwatch();
logger.info(`[CpuProfiler] Enabling profiling...`);
await new Promise<void>((resolve, reject) => {
try {
session.post('Profiler.enable', error => {
if (error) {
logger.error(error, '[CpuProfiler] Error enabling profiling');
reject(error);
} else {
logger.info(`[CpuProfiler] Profiling enabled`);
resolve();
}
});
} catch (error) {
logger.error(error, '[CpuProfiler] Error enabling profiling');
reject(error);
}
});
logger.info(`[CpuProfiler] Enable session took ${sw.getElapsedAndRestart()}ms`);
if (samplingInterval !== undefined) {
logger.info(`[CpuProfiler] Setting sampling interval to ${samplingInterval} microseconds`);
await new Promise<void>((resolve, reject) => {
try {
session.post('Profiler.setSamplingInterval', { interval: samplingInterval }, error => {
if (error) {
logger.error(error, '[CpuProfiler] Error setting sampling interval');
reject(error);
} else {
logger.info(`[CpuProfiler] Set sampling interval`);
resolve();
}
});
} catch (error) {
logger.error(error, '[CpuProfiler] Error setting sampling interval');
reject(error);
}
});
logger.info(`[CpuProfiler] Set sampling interval took ${sw.getElapsedAndRestart()}ms`);
}
logger.info(`[CpuProfiler] Profiling starting...`);
await new Promise<void>((resolve, reject) => {
try {
session.post('Profiler.start', error => {
if (error) {
logger.error(error, '[CpuProfiler] Error starting profiling');
reject(error);
} else {
sessionStopwatch.restart();
logger.info(`[CpuProfiler] Profiling started`);
resolve();
}
});
} catch (error) {
logger.error(error, '[CpuProfiler] Error starting profiling');
reject(error);
}
});
logger.info(`[CpuProfiler] Start profiler took ${sw.getElapsedAndRestart()}ms`);
};
const stop = async () => {
const sw = stopwatch();
logger.info(`[CpuProfiler] Profiling stopping...`);
try {
return await new Promise<CpuProfileResult>((resolve, reject) => {
try {
session.post('Profiler.stop', (error, profileResult) => {
if (error) {
logger.error(error, '[CpuProfiler] Error stopping profiling');
reject(error);
} else {
logger.info(`[CpuProfiler] Profiling stopped`);
resolve(profileResult.profile);
}
});
} catch (error) {
reject(error);
}
});
} finally {
logger.info(`[CpuProfiler] Stop profiler took ${sw.getElapsedAndRestart()}ms`);
}
};
const dispose = async () => {
const sw = stopwatch();
try {
logger.info(`[CpuProfiler] Disabling profiling...`);
await new Promise<void>((resolve, reject) => {
try {
session.post('Profiler.disable', error => {
if (error && isInspectorNotConnectedError(error)) {
logger.info(`[CpuProfiler] Profiler already disconnected`);
resolve();
} else if (error) {
logger.error(error, '[CpuProfiler] Error disabling profiling');
reject(error);
} else {
logger.info(`[CpuProfiler] Profiling disabled`);
resolve();
}
});
} catch (error) {
if (isInspectorNotConnectedError(error)) {
logger.info(`[CpuProfiler] Profiler already disconnected`);
resolve();
} else {
reject();
}
}
});
} finally {
session.disconnect();
logger.info(
`[CpuProfiler] Disable and disconnect profiler took ${sw.getElapsedAndRestart()}ms`
);
}
};
return { start, stop, dispose, session, sessionType: 'cpu', stopwatch: sessionStopwatch };
}
/**
* Connects and enables a new `inspector` session, then creates an internal v8 Heap profiler snapshot.
* @param outputStream - An output stream that heap snapshot chunks are written to.
* The result stream can be used to create a `.heapsnapshot` file.
* Use Chrome's 'DevTools for Node' (under chrome://inspect) to visualize the `.heapsnapshot` file.
*/
function initHeapSnapshot(
outputStream: stream.Writable
): ProfilerInstance<{ totalSnapshotByteSize: number }> {
const sw = stopwatch();
const session = new inspector.Session();
session.connect();
let totalSnapshotByteSize = 0;
const start = async () => {
logger.info(`[HeapProfiler] Enabling profiling...`);
await new Promise<void>((resolve, reject) => {
try {
session.post('HeapProfiler.enable', error => {
if (error) {
logger.error(error, '[HeapProfiler] Error enabling profiling');
reject(error);
} else {
sw.restart();
logger.info(`[HeapProfiler] Profiling enabled`);
resolve();
}
});
} catch (error) {
logger.error(error, '[HeapProfiler] Error enabling profiling');
reject(error);
}
});
session.on('HeapProfiler.addHeapSnapshotChunk', message => {
// Note: this doesn't handle stream back-pressure, but we don't have control over the
// `HeapProfiler.addHeapSnapshotChunk` callback in order to use something like piping.
// So in theory on a slow `outputStream` (usually an http connection response) this can cause OOM.
logger.info(
`[HeapProfiler] Writing heap snapshot chunk of size ${message.params.chunk.length}`
);
totalSnapshotByteSize += message.params.chunk.length;
outputStream.write(message.params.chunk, error => {
if (error) {
logger.error(
error,
`[HeapProfiler] Error writing heap profile chunk to output stream: ${error.message}`
);
}
});
});
};
const stop = async () => {
logger.info(`[HeapProfiler] Taking snapshot...`);
await new Promise<void>((resolve, reject) => {
try {
session.post('HeapProfiler.takeHeapSnapshot', undefined, (error: Error | null) => {
if (error) {
logger.error(error, '[HeapProfiler] Error taking snapshot');
reject(error);
} else {
logger.info(
`[HeapProfiler] Taking snapshot completed, ${totalSnapshotByteSize} bytes...`
);
resolve();
}
});
} catch (error) {
logger.error(error, '[HeapProfiler] Error taking snapshot');
reject(error);
}
});
logger.info(`[HeapProfiler] Draining snapshot buffer to stream...`);
const writeFinishedPromise = new Promise<void>((resolve, reject) => {
outputStream.on('finish', () => resolve());
outputStream.on('error', error => reject(error));
});
outputStream.end();
await writeFinishedPromise;
logger.info(`[HeapProfiler] Finished draining snapshot buffer to stream`);
return { totalSnapshotByteSize };
};
const dispose = async () => {
try {
logger.info(`[HeapProfiler] Disabling profiling...`);
await new Promise<void>((resolve, reject) => {
try {
session.post('HeapProfiler.disable', error => {
if (error && isInspectorNotConnectedError(error)) {
logger.info(`[HeapProfiler] Profiler already disconnected`);
resolve();
} else if (error) {
logger.error(error, '[HeapProfiler] Error disabling profiling');
reject(error);
} else {
logger.info(`[HeapProfiler] Profiling disabled`);
resolve();
}
});
} catch (error) {
if (isInspectorNotConnectedError(error)) {
logger.info(`[HeapProfiler] Profiler already disconnected`);
resolve();
} else {
reject();
}
}
});
} finally {
session.disconnect();
}
};
return { start, stop, dispose, session, sessionType: 'memory', stopwatch: sw };
}
export async function startProfilerServer(httpServerPort?: number | string): Promise<{
server: Server;
address: string;
close: () => Promise<void>;
}> {
let serverPort: number | undefined = undefined;
if (httpServerPort !== undefined) {
serverPort = parsePort(httpServerPort);
}
const app = express();
let existingSession:
| { instance: ProfilerInstance<unknown>; response: express.Response }
| undefined;
app.get(
'/profile/cpu',
asyncHandler(async (req, res) => {
if (existingSession) {
res.status(409).json({ error: 'Profile session already in progress' });
return;
}
const durationParam = req.query['duration'];
const seconds = Number.parseFloat(durationParam as string);
if (!Number.isFinite(seconds) || seconds < 0) {
res.status(400).json({ error: `Invalid 'duration' query parameter "${durationParam}"` });
return;
}
const samplingIntervalParam = req.query['sampling_interval'];
let samplingInterval: number | undefined;
if (samplingIntervalParam !== undefined) {
samplingInterval = Number.parseFloat(samplingIntervalParam as string);
if (!Number.isInteger(samplingInterval) || samplingInterval < 0) {
res.status(400).json({
error: `Invalid 'sampling_interval' query parameter "${samplingIntervalParam}"`,
});
return;
}
}
const cpuProfiler = initCpuProfiling(samplingInterval);
existingSession = { instance: cpuProfiler, response: res };
try {
const filename = `cpu_${Math.round(Date.now() / 1000)}_${seconds}-seconds.cpuprofile`;
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.flushHeaders();
await cpuProfiler.start();
const ac = new AbortController();
const timeoutPromise = timeout(seconds * 1000, ac);
await Promise.race([timeoutPromise, once(res, 'close')]);
if (res.writableEnded || res.destroyed) {
// session was cancelled
ac.abort();
return;
}
const result = await cpuProfiler.stop();
const resultString = JSON.stringify(result);
logger.info(
`[CpuProfiler] Completed, total profile report JSON string length: ${resultString.length}`
);
res.end(resultString);
} finally {
const session = existingSession;
existingSession = undefined;
await session?.instance.dispose().catch();
}
})
);
let neonProfilerRunning: boolean = false;
app.get(
'/profile/cpu/start',
asyncHandler(async (req, res) => {
if (existingSession) {
res.status(409).json({ error: 'Profile session already in progress' });
return;
}
const samplingIntervalParam = req.query['sampling_interval'];
let samplingInterval: number | undefined;
if (samplingIntervalParam !== undefined) {
samplingInterval = Number.parseFloat(samplingIntervalParam as string);
if (!Number.isInteger(samplingInterval) || samplingInterval < 0) {
res.status(400).json({
error: `Invalid 'sampling_interval' query parameter "${samplingIntervalParam}"`,
});
return;
}
}
const cpuProfiler = initCpuProfiling(samplingInterval);
existingSession = { instance: cpuProfiler, response: res };
await cpuProfiler.start();
const profilerRunningLogger = setInterval(() => {
if (existingSession) {
logger.error(`CPU profiler has been enabled for a long time`);
} else {
clearInterval(profilerRunningLogger);
}
}, 10_000).unref();
res.end('CPU profiler started');
})
);
app.get('/profile/native/cpu/start', (req, res) => {
if (neonProfilerRunning) {
res.status(500).end('error: profiler already started');
return;
}
neonProfilerRunning = true;
try {
const startResponse = startProfiler();
console.log(startResponse);
res.end(startResponse);
} catch (error) {
console.error(error);
res.status(500).end(error);
}
});
app.get('/profile/native/cpu/stop', (req, res) => {
if (!neonProfilerRunning) {
res.status(500).end('error: no profiler running');
return;
}
neonProfilerRunning = false;
let profilerResults: Buffer;
try {
profilerResults = stopProfiler();
} catch (error: any) {
console.error(error);
res.status(500).end(error);
return;
}
const fileName = `profile-${Date.now()}.svg`;
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
res.setHeader('Content-Type', 'image/svg+xml');
res.status(200).send(profilerResults);
});
app.get(
'/profile/cpu/stop',
asyncHandler(async (req, res) => {
if (!existingSession) {
res.status(409).json({ error: 'No profile session in progress' });
return;
}
if (existingSession.instance.sessionType !== 'cpu') {
res.status(409).json({ error: 'No CPU profile session in progress' });
return;
}
try {
const elapsedSeconds = existingSession.instance.stopwatch.getElapsedSeconds();
const timestampSeconds = Math.round(Date.now() / 1000);
const filename = `cpu_${timestampSeconds}_${elapsedSeconds}-seconds.cpuprofile`;
const result = await (
existingSession.instance as ProfilerInstance<inspector.Profiler.Profile>
).stop();
const resultString = JSON.stringify(result);
logger.info(
`[CpuProfiler] Completed, total profile report JSON string length: ${resultString.length}`
);
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
res.setHeader('Content-Type', 'application/json; charset=utf-8');
// await new Promise<void>(resolve => res.end(resultString, () => resolve()));
res.end(resultString);
} finally {
const session = existingSession;
existingSession = undefined;
await session?.instance.dispose().catch();
}
})
);
app.get(
'/profile/heap_snapshot',
asyncHandler(async (req, res) => {
if (existingSession) {
res.status(409).json({ error: 'Profile session already in progress' });
return;
}
const filename = `heap_${Math.round(Date.now() / 1000)}.heapsnapshot`;
const tmpFile = path.join(os.tmpdir(), filename);
const fileWriteStream = fs.createWriteStream(tmpFile);
const heapProfiler = initHeapSnapshot(fileWriteStream);
existingSession = { instance: heapProfiler, response: res };
try {
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.flushHeaders();
// Taking a heap snapshot (with current implementation) is a one-shot process ran to get the
// applications current heap memory usage, rather than something done over time. So start and
// stop without waiting.
await heapProfiler.start();
const result = await heapProfiler.stop();
logger.info(
`[HeapProfiler] Completed, total snapshot byte size: ${result.totalSnapshotByteSize}`
);
await pipelineAsync(fs.createReadStream(tmpFile), res);
} finally {
const session = existingSession;
existingSession = undefined;
await session?.instance.dispose().catch();
try {
fileWriteStream.destroy();
} catch (_) {}
try {
logger.info(`[HeapProfiler] Cleaning up tmp file ${tmpFile}`);
fs.unlinkSync(tmpFile);
} catch (_) {}
}
})
);
app.get(
'/profile/cancel',
asyncHandler(async (req, res) => {
if (!existingSession) {
res.status(409).json({ error: 'No existing profile session is exists to cancel' });
return;
}
const session = existingSession;
await session.instance.stop().catch();
await session.instance.dispose().catch();
session.response.destroy();
existingSession = undefined;
await Promise.resolve();
res.json({ ok: 'existing profile session stopped' });
})
);
const server = createServer(app);
const serverSockets = new Set<Socket>();
server.on('connection', socket => {
serverSockets.add(socket);
socket.once('close', () => {
serverSockets.delete(socket);
});
});
await new Promise<void>((resolve, reject) => {
try {
server.once('error', error => {
reject(error);
});
server.listen(serverPort, '0.0.0.0', () => {
resolve();
});
} catch (error) {
reject(error);
}
});
const addr = server.address();
if (addr === null) {
throw new Error('server missing address');
}
const addrStr = typeof addr === 'string' ? addr : `${addr.address}:${addr.port}`;
logger.info(`Started profiler server on: http://${addrStr}`);
const closeServer = async () => {
const closePromise = new Promise<void>((resolve, reject) => {
if (!server.listening) {
// Server already closed (can happen when server is shared between cluster workers)
return resolve();
}
server.close(error => (error ? reject(error) : resolve()));
});
for (const socket of serverSockets) {
socket.destroy();
}
await closePromise;
};
return { server, address: addrStr, close: closeServer };
}