-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtrace_parser.js
More file actions
848 lines (792 loc) · 28.6 KB
/
trace_parser.js
File metadata and controls
848 lines (792 loc) · 28.6 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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// trace_parser.js - Binary trace parser using dial9-trace-format decoder
// Can be used in browser or Node.js
(function (exports) {
"use strict";
const MAX_EVENTS = Infinity; // no cap — use time range filtering for large traces
function getTraceDecoder() {
if (typeof require !== "undefined") {
const path = require("path");
return require(path.resolve(__dirname, "decode.js")).TraceDecoder;
}
// Browser: decode.js must be loaded before this script
if (typeof TraceDecoder !== "undefined") return TraceDecoder;
throw new Error(
"TraceDecoder not found. Load decode.js before trace_parser.js"
);
}
/** Parse a string/bigint/number to a JS number */
function num(v) {
if (typeof v === "number") return v;
if (typeof v === "bigint") return Number(v);
if (typeof v === "string" && v !== "")
if (!isNaN(Number(v))) return Number(v);
throw new Error(`Invalid number: ${v}`);
}
/** Decompress gzip data if detected, otherwise return as-is. */
async function maybeGunzip(buf) {
const b = buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf;
if (b.length < 2 || b[0] !== 0x1f || b[1] !== 0x8b) {
return buf;
}
if (typeof DecompressionStream !== "undefined") {
return await new Response(
new Blob([b]).stream().pipeThrough(new DecompressionStream("gzip"))
).arrayBuffer();
}
// Fallback for older Node.js without DecompressionStream
const zlib = require("zlib");
const decompressed = zlib.gunzipSync(Buffer.from(b));
return decompressed.buffer.slice(
decompressed.byteOffset,
decompressed.byteOffset + decompressed.byteLength
);
}
/**
* @typedef {{
* eventType: number,
* timestamp: number,
* workerId: number,
* localQueue: number,
* globalQueue: number,
* cpuTime: number,
* schedWait: number,
* taskId: number,
* spawnLocId: string|null,
* spawnLoc: string|null,
* wakerTaskId?: number,
* wokenTaskId?: number,
* targetWorker?: number,
* }} TraceEvent
*/
/**
* @typedef {{
* timestamp: number,
* workerId: number,
* tid: number,
* source: number,
* callchain: string[],
* cpu: number|null,
* }} CpuSample
*/
/**
* @typedef {{ symbol: string, location: string|null }} SymbolFrame
*/
/**
* @typedef {{
* magic: "D9TF",
* version: number,
* events: TraceEvent[],
* truncated: boolean,
* hasCpuTime: boolean,
* hasSchedWait: boolean,
* hasTaskTracking: boolean,
* spawnLocations: Map<string, string>,
* taskSpawnLocs: Map<number, string|null>,
* taskSpawnTimes: Map<number, number>,
* taskTerminateTimes: Map<number, number>,
* taskInstrumented: Map<number, boolean>,
* cpuSamples: CpuSample[],
* callframeSymbols: Map<string, SymbolFrame|SymbolFrame[]>,
* threadNames: Map<number, string>,
* runtimeWorkers: Map<string, number[]>,
* }} ParsedTrace
*/
const EVENT_TYPES = {
PollStart: 0,
PollEnd: 1,
WorkerPark: 2,
WorkerUnpark: 3,
QueueSample: 4,
WakeEvent: 9,
};
/**
* Parse dial9 trace data from a buffer, file path, or directory.
*
* - Buffer/ArrayBuffer/Uint8Array: returns Promise<ParsedTrace> (browser compatible).
* - String (file path): returns AsyncIterable yielding one ParsedTrace (Node.js only).
* - String (directory): returns AsyncIterable yielding one ParsedTrace per file,
* with parallel parsing and caching (Node.js only).
*
* In the browser, fetch trace data via the viewer API and pass the ArrayBuffer.
*
* @param {ArrayBuffer|Uint8Array|string} input - Binary data, file path, or directory path
* @param {Object} [options] - Optional parsing options
* @param {number} [options.maxEvents] - Maximum number of events to parse (default: Infinity)
* @param {number} [options.startTime] - Start of time range filter (absolute ns, inclusive)
* @param {number} [options.endTime] - End of time range filter (absolute ns, inclusive)
* @param {function} [options.onParseProgress] - Called with {done, total, file} as files complete
* @param {boolean} [options.cache] - Enable disk caching for directories (default: true)
* @param {boolean} [options.parallel] - Enable parallel parsing for directories (default: true)
* @param {boolean} [options.force] - Ignore cached results and re-parse (default: false)
* @param {number} [options.sample] - Only parse N evenly-spaced files from a directory
* @returns {AsyncIterable<ParsedTrace>}
*/
function parseTrace(input, options) {
if (typeof input === 'string') {
if (typeof require === 'undefined') {
throw new Error(
'File/directory paths require Node.js. In the browser, fetch trace ' +
'data via the viewer API (e.g. /api/trace) and pass the ArrayBuffer ' +
'to parseTrace().'
);
}
const fs = require('fs');
const stat = fs.statSync(input);
if (stat.isDirectory()) {
return parseTraceDir(input, options);
}
// Single file path: async iterable yielding one trace
return wrapSingle(parseTraceBuffer(fs.readFileSync(input), options));
}
// Buffer: return Promise<ParsedTrace> directly (backwards compatible with browser)
return parseTraceBuffer(input, options);
}
/** Wrap a Promise<ParsedTrace> as an async iterable that yields once. */
function wrapSingle(promise) {
return {
[Symbol.asyncIterator]() {
let done = false;
return { async next() {
if (done) return { done: true, value: undefined };
done = true;
return { done: false, value: await promise };
}};
}
};
}
/** @private Parse a binary trace buffer. */
async function parseTraceBuffer(buffer, options) {
buffer = await maybeGunzip(buffer);
const maxEvents = (options && options.maxEvents != null) ? options.maxEvents : MAX_EVENTS;
const startTime = (options && options.startTime != null) ? options.startTime : 0;
const endTime = (options && options.endTime != null) ? options.endTime : Infinity;
const onProgress = (options && options.onParseProgress) || null;
const YIELD_BYTES = 100 * 1024; // yield to browser every 100KB
const TD = getTraceDecoder();
const dec = new TD(
buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer
);
if (!dec.decodeHeader()) throw new Error("Invalid trace header");
const totalBytes = dec.byteLength;
const events = [];
const spawnLocations = new Map();
const taskSpawnLocs = new Map();
const taskSpawnTimes = new Map();
const taskTerminateTimes = new Map();
const taskInstrumented = new Map(); // taskId -> bool (true if spawned via TelemetryHandle::spawn)
const callframeSymbols = new Map();
const cpuSamples = [];
const threadNames = new Map();
const runtimeWorkers = new Map(); // runtime name → [workerId, ...]
const customEvents = []; // unrecognized event types: {name, timestamp, fields}
// { monotonicNs, realtimeNs } anchors used to recover wall clock.
const clockSyncAnchors = [];
// Legacy classifier: epoch ns are ~1e18, monotonic ns are much smaller.
// 2020 is a practical floor that separates those ranges.
const LEGACY_EPOCH_FLOOR_MS = 1_577_836_800_000; // 2020-01-01
let legacySegmentMetaWallNs = null;
// Smallest monotonic ts seen across all event frames.
// Used as the monotonic timestamp for the legacy synthesized anchor.
let minMonoTs = null;
const capped = () => events.length >= maxEvents;
const UNCAPPED_FRAMES = new Set([
"TaskSpawnEvent",
"TaskTerminateEvent",
"CpuSampleEvent",
"SymbolTableEntry",
"SegmentMetadataEvent",
"ClockSyncEvent",
]);
let lastYieldPos = 0;
let frame;
while ((frame = dec.nextFrame()) !== null) {
// Yield to browser periodically so spinner can update
if (onProgress && dec.position - lastYieldPos >= YIELD_BYTES) {
lastYieldPos = dec.position;
onProgress({ bytesRead: dec.position, totalBytes, eventCount: events.length });
await new Promise((r) => setTimeout(r, 0));
}
if (frame.type !== "event") continue;
const v = frame.values;
const ts = num(frame.timestamp_ns);
// Track smallest monotonic ts for legacy anchor synthesis.
// Skip SegmentMetadata (legacy wall clock) and SymbolTableEntry.
if (
ts != null &&
frame.name !== "SegmentMetadataEvent" &&
frame.name !== "SymbolTableEntry" &&
(minMonoTs == null || ts < minMonoTs)
) {
minMonoTs = ts;
}
if (capped() && !UNCAPPED_FRAMES.has(frame.name)) continue;
// Time range filtering: skip events outside the requested range
// (uncapped frames like symbols/metadata are always processed)
const inTimeRange = ts >= startTime && ts <= endTime;
if (!inTimeRange && !UNCAPPED_FRAMES.has(frame.name)) continue;
switch (frame.name) {
case "PollStartEvent": {
const spawnLoc = v.spawn_loc || null;
if (spawnLoc) spawnLocations.set(spawnLoc, spawnLoc);
const taskId = num(v.task_id);
if (taskId && spawnLoc && !taskSpawnLocs.has(taskId)) {
taskSpawnLocs.set(taskId, spawnLoc);
}
events.push({
eventType: 0,
timestamp: ts,
workerId: num(v.worker_id),
localQueue: num(v.local_queue),
globalQueue: 0,
cpuTime: 0,
schedWait: 0,
taskId,
spawnLocId: spawnLoc,
spawnLoc,
});
break;
}
case "PollEndEvent":
events.push({
eventType: 1,
timestamp: ts,
workerId: num(v.worker_id),
globalQueue: 0,
localQueue: 0,
cpuTime: 0,
schedWait: 0,
taskId: 0,
spawnLocId: null,
spawnLoc: null,
});
break;
case "WorkerParkEvent":
events.push({
eventType: 2,
timestamp: ts,
workerId: num(v.worker_id),
localQueue: num(v.local_queue),
cpuTime: num(v.cpu_time_ns),
globalQueue: 0,
schedWait: 0,
taskId: 0,
spawnLocId: null,
spawnLoc: null,
});
break;
case "WorkerUnparkEvent":
events.push({
eventType: 3,
timestamp: ts,
workerId: num(v.worker_id),
localQueue: num(v.local_queue),
cpuTime: num(v.cpu_time_ns),
schedWait: num(v.sched_wait_ns),
globalQueue: 0,
taskId: 0,
spawnLocId: null,
spawnLoc: null,
});
break;
case "QueueSampleEvent":
events.push({
eventType: 4,
timestamp: ts,
globalQueue: num(v.global_queue),
workerId: 0,
localQueue: 0,
cpuTime: 0,
schedWait: 0,
taskId: 0,
spawnLocId: null,
spawnLoc: null,
});
break;
case "TaskSpawnEvent": {
const taskId = num(v.task_id);
const spawnLoc = v.spawn_loc || null;
const instrumented = v.instrumented ?? true;
if (spawnLoc) spawnLocations.set(spawnLoc, spawnLoc);
taskSpawnLocs.set(taskId, spawnLoc);
taskSpawnTimes.set(taskId, ts);
taskInstrumented.set(taskId, !!instrumented);
break;
}
case "TaskTerminateEvent":
taskTerminateTimes.set(num(v.task_id), ts);
break;
case "WakeEventEvent":
events.push({
eventType: 9,
timestamp: ts,
workerId: num(v.target_worker),
wakerTaskId: num(v.waker_task_id),
wokenTaskId: num(v.woken_task_id),
targetWorker: num(v.target_worker),
globalQueue: 0,
localQueue: 0,
cpuTime: 0,
schedWait: 0,
taskId: 0,
spawnLocId: null,
spawnLoc: null,
});
break;
case "CpuSampleEvent": {
const chain = (v.callchain || []).map(
(addr) => "0x" + BigInt(addr).toString(16)
);
// `cpu` is encoded as OptionalVarint: null when the backend could
// not determine the CPU. Varints decode as strings for BigInt safety;
// CPU ids always fit in a Number.
const cpu = v.cpu == null ? null : Number(v.cpu);
cpuSamples.push({
timestamp: ts,
workerId: num(v.worker_id),
tid: num(v.tid),
source: num(v.source),
callchain: chain,
cpu,
});
const tn = v.thread_name;
if (tn) {
threadNames.set(num(v.tid), tn);
}
break;
}
case "ClockSyncEvent": {
const real = num(v.realtime_ns);
if (real > 0) {
clockSyncAnchors.push({ monotonicNs: ts, realtimeNs: real });
}
break;
}
case "SegmentMetadataEvent": {
// If this looks epoch-scale, treat it as legacy wall clock.
if (
legacySegmentMetaWallNs == null &&
ts != null &&
ts / 1e6 >= LEGACY_EPOCH_FLOOR_MS
) {
legacySegmentMetaWallNs = ts;
}
const entries = v.entries || {};
for (const [key, val] of Object.entries(entries)) {
if (key.startsWith("runtime.")) {
const name = key.slice("runtime.".length);
const ids = val
.split(",")
.map(Number)
.filter((n) => !isNaN(n));
if (ids.length > 0) runtimeWorkers.set(name, ids);
}
}
break;
}
case "SymbolTableEntry": {
const addrKey = "0x" + BigInt(v.addr).toString(16);
const depth = Number(v.inline_depth || 0);
const sf = v.source_file || "";
const sl = Number(v.source_line || 0);
const location = sf ? (sl ? `${sf}:${sl}` : sf) : null;
const entry = { symbol: v.symbol_name, location };
if (depth === 0) {
// Outermost frame: store directly (or as first element of array)
const existing = callframeSymbols.get(addrKey);
if (Array.isArray(existing)) {
existing[0] = entry;
} else {
callframeSymbols.set(addrKey, entry);
}
} else {
// Inlined frame: promote to array
let arr = callframeSymbols.get(addrKey);
if (!Array.isArray(arr)) {
arr = [arr || { symbol: addrKey, location: null }];
callframeSymbols.set(addrKey, arr);
}
arr[depth] = entry;
}
break;
}
default: {
// Unrecognized event type: capture as a custom event
if (ts != null) {
customEvents.push({
name: frame.name,
timestamp: ts,
fields: v,
});
}
break;
}
}
}
// Legacy fallback: synthesize an anchor from legacy SegmentMetadata wall
// time + earliest monotonic event timestamp. This is best-effort only.
if (
clockSyncAnchors.length === 0 &&
legacySegmentMetaWallNs != null &&
minMonoTs != null
) {
clockSyncAnchors.push({
monotonicNs: minMonoTs,
realtimeNs: legacySegmentMetaWallNs,
});
}
clockSyncAnchors.sort((a, b) => {
if (a.monotonicNs < b.monotonicNs) return -1;
if (a.monotonicNs > b.monotonicNs) return 1;
return 0;
});
let clockOffsetNs = null;
if (clockSyncAnchors.length > 0) {
const a0 = clockSyncAnchors[0];
clockOffsetNs = a0.realtimeNs - a0.monotonicNs;
}
const hasTimeFilter = startTime > 0 || endTime < Infinity;
return {
magic: "D9TF",
version: dec.version,
events,
truncated: events.length >= maxEvents,
timeFiltered: hasTimeFilter,
filterStartTime: hasTimeFilter ? startTime : null,
filterEndTime: hasTimeFilter ? endTime : null,
hasCpuTime: true,
hasSchedWait: true,
hasTaskTracking: true,
spawnLocations,
taskSpawnLocs,
taskSpawnTimes,
taskInstrumented,
cpuSamples,
callframeSymbols,
threadNames,
taskTerminateTimes,
runtimeWorkers,
customEvents,
clockSyncAnchors,
clockOffsetNs,
};
}
// ── Directory parsing (Node-only) ──
/** Reconstruct Maps from [key, value] arrays produced by parse_worker.js. */
function entriesToMap(arr) {
return new Map(arr);
}
/** Load a cached ParsedTrace from NDJSON, reconstructing Maps. */
async function loadCachedTrace(cachePath) {
const fs = require('fs');
const buf = await fs.promises.readFile(cachePath);
let pos = 0;
function nextLine() {
const nl = buf.indexOf(10, pos);
if (nl === -1) {
if (pos < buf.length) { const s = buf.toString('utf8', pos, buf.length); pos = buf.length; return s; }
return null;
}
const s = buf.toString('utf8', pos, nl);
pos = nl + 1;
return s;
}
let raw = null;
const events = [];
const cpuSamples = [];
const customEvents = [];
let line;
while ((line = nextLine()) !== null) {
if (!line) continue;
const rec = JSON.parse(line);
switch (rec.t) {
case 'm':
raw = rec.d;
if (raw.spawnLocations) raw.spawnLocations = entriesToMap(raw.spawnLocations);
if (raw.taskSpawnLocs) raw.taskSpawnLocs = entriesToMap(raw.taskSpawnLocs);
if (raw.taskSpawnTimes) raw.taskSpawnTimes = entriesToMap(raw.taskSpawnTimes);
if (raw.taskTerminateTimes) raw.taskTerminateTimes = entriesToMap(raw.taskTerminateTimes);
if (raw.callframeSymbols) raw.callframeSymbols = entriesToMap(raw.callframeSymbols);
if (raw.threadNames) raw.threadNames = entriesToMap(raw.threadNames);
if (raw.runtimeWorkers) raw.runtimeWorkers = entriesToMap(raw.runtimeWorkers);
break;
case 'e': events.push(rec.d); break;
case 'c': cpuSamples.push(rec.d); break;
case 'x': customEvents.push(rec.d); break;
}
}
raw.events = events;
raw.cpuSamples = cpuSamples;
raw.customEvents = customEvents;
return raw;
}
/**
* Parse all trace files in a directory with caching and parallelism.
* Workers do parse + analysis. Cache holds pre-computed analysis results.
* Returns {files, [Symbol.asyncIterator]} where each item is {file, analysis}.
* @private
*/
function parseTraceDir(dirPath, options) {
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execFile } = require('child_process');
const opts = options || {};
const useCache = opts.cache !== false;
const force = opts.force === true;
const sampleN = opts.sample != null ? opts.sample : null;
const onProgress = opts.onParseProgress || null;
const TRACE_EXT = /\.(bin|bin\.gz)$/;
let files = fs.readdirSync(dirPath)
.filter(f => TRACE_EXT.test(f))
.sort();
if (files.length === 0) {
throw new Error(`No .bin or .bin.gz files found in ${dirPath}`);
}
if (sampleN != null) {
if (sampleN < 1) throw new Error('sample must be >= 1');
if (sampleN < files.length) {
const step = files.length / sampleN;
const sampled = [];
for (let i = 0; i < sampleN; i++) {
sampled.push(files[Math.floor(i * step)]);
}
files = sampled;
}
}
const cacheDir = path.join(dirPath, '.d9-cache');
if (useCache) {
fs.mkdirSync(cacheDir, { recursive: true });
}
const concurrency = (opts.parallel === false) ? 1 : Math.min(os.cpus().length, 32);
const workerCandidate = path.resolve(__dirname, 'analyze.js');
const workerFallback = path.resolve(__dirname, '..', 'skills', 'analyze.js');
const workerScript = fs.existsSync(workerCandidate) ? workerCandidate : workerFallback;
function cachePathFor(file) {
return path.join(cacheDir, file.replace(TRACE_EXT, '') + '.json');
}
function isCacheValid(file) {
if (!useCache || force) return false;
const cp = cachePathFor(file);
try {
const cacheStat = fs.statSync(cp);
const srcStat = fs.statSync(path.join(dirPath, file));
return cacheStat.mtimeMs > srcStat.mtimeMs;
} catch { return false; }
}
// Ensure file is cached (spawn worker if needed). Returns Promise<boolean> (true = cache hit).
function ensureCached(file) {
if (isCacheValid(file)) return Promise.resolve(true);
const tracePath = path.join(dirPath, file);
const cp = useCache ? cachePathFor(file) : path.join(os.tmpdir(), 'd9-' + process.pid + '-' + file + '.json');
const args = [workerScript, '--parse-worker', tracePath, cp];
return new Promise((resolve, reject) => {
execFile(process.execPath, args, { maxBuffer: 10 * 1024 * 1024 }, (err, stdout, stderr) => {
if (err) reject(new Error(`Failed to process ${file}: ${stderr || err.message}`));
else resolve(false);
});
});
}
// Dispatch all workers with concurrency limiting.
// Workers run independently of the iterator.
if (onProgress) onProgress({ done: 0, total: files.length, file: null });
let workersCompleted = 0;
let cacheHits = 0;
const fileReady = [];
let active = 0;
const waiters = [];
for (let i = 0; i < files.length; i++) {
fileReady.push(new Promise((resolve, reject) => {
function go() {
active++;
ensureCached(files[i]).then((wasCached) => {
workersCompleted++;
if (wasCached) cacheHits++;
active--;
if (onProgress) onProgress({ done: workersCompleted, total: files.length, file: files[i], cached: cacheHits });
resolve();
if (waiters.length > 0) waiters.shift()();
}, reject);
}
if (active < concurrency) go();
else waiters.push(go);
}));
}
return {
files: files,
allCached: Promise.all(fileReady),
[Symbol.asyncIterator]() {
let idx = 0;
return {
async next() {
if (idx >= files.length) return { done: true, value: undefined };
const i = idx++;
await fileReady[i];
const cp = useCache ? cachePathFor(files[i]) : path.join(os.tmpdir(), 'd9-' + process.pid + '-' + files[i] + '.json');
const trace = await loadCachedTrace(cp);
if (!useCache) try { fs.unlinkSync(cp); } catch {}
return { done: false, value: trace };
}
};
}
};
}
// ── Symbol formatting utilities ──
function _stripBoringGenerics(s) {
const boring = /^[A-Z]$|^(Fut|Req|Res|Bs|InnerFuture)$/;
return s.replace(/<([^<>]*)>/g, (match, inner) => {
const params = inner.split(",").map((p) => p.trim());
if (params.every((p) => boring.test(p))) return "";
const kept = params.filter((p) => !boring.test(p));
return kept.length ? `<${kept.join(",")}>` : "";
});
}
function _lastSeg(s) {
return s.split("::").pop();
}
function _shortenPath(s) {
const parts = s.split("::");
let closures = 0;
for (let i = parts.length - 1; i >= 0; i--) {
if (parts[i] === "{{closure}}") closures++;
else break;
}
const meaningful = parts.length - closures;
if (meaningful <= 3) return s;
return parts.slice(meaningful - 3).join("::");
}
/**
* Try to build a docs.rs source link from a location path containing a crate-version segment.
* Matches any path like: .../hyper-0.14.28/src/client/connect/http.rs:474
* Returns URL string or null.
*/
function _docsRsUrl(location) {
if (!location) return null;
const m = location.match(
/\/([a-z][a-z0-9_-]*)-(\d+\.\d+[^/]*)\/(.+?)(?::(\d+))?$/
);
if (!m) return null;
const [, crate_, version, rawPath, line] = m;
const crateSrc = crate_.replace(/-/g, "_");
const path = rawPath.replace(/^src\//, "");
let url = `https://docs.rs/${crate_}/${version}/src/${crateSrc}/${path}.html`;
if (line) url += `#${line}`;
return url;
}
/**
* Extract just the filename from a location string.
* e.g. "/home/user/.cargo/registry/src/.../hyper-0.14.28/src/client/connect/http.rs:474" → "http.rs"
*/
function _fileName(location) {
if (!location) return null;
const m = location.match(/([^/]+\.rs)(?::\d+)?$/);
return m ? m[1] : null;
}
/**
* Format a stack frame for human-readable display.
* Accepts either a resolved frame object or a raw address + callframeSymbols map.
* @param {{symbol: string, location: string|null}|string} frame - Resolved frame or address string
* @param {Map<string, {symbol: string, location: string|null}>} [callframeSymbols] - Required when frame is an address string
* @returns {{text: string, docsUrl: string|null}}
*/
function formatFrame(frame, callframeSymbols) {
if (typeof frame === "string") {
if (!callframeSymbols)
throw new Error(
"formatFrame requires callframeSymbols when given an address string"
);
const entry = callframeSymbols.get(frame);
if (!entry) return { text: frame || "(unknown)", docsUrl: null };
frame = Array.isArray(entry) ? entry[0] : entry;
}
const { symbol: sym, location } = frame;
if (!sym || sym.startsWith("0x"))
return { text: sym || "(unknown)", docsUrl: null };
let result = sym;
const traitImplMatch = result.match(/^<(.+?) as (.+?)>::(.+)$/);
if (traitImplMatch) {
let [, implType, trait_, method] = traitImplMatch;
const shortType = _lastSeg(_stripBoringGenerics(implType));
result =
shortType.length <= 2
? `${_lastSeg(_stripBoringGenerics(trait_))}::${method}`
: `${shortType}::${method}`;
} else if (result.includes("::")) {
result = _shortenPath(_stripBoringGenerics(result));
}
const fileName = _fileName(location);
if (location) {
const m = location.match(/:(\d+)$/);
if (m) result += ` ${fileName || ""}:${m[1]}`;
}
return { text: result, docsUrl: _docsRsUrl(location) };
}
/**
* Resolve a callchain (array of address strings) to frame objects.
* When an address has inlined frames (stored as an array in callframeSymbols),
* they are expanded in place (outermost first, then inlined callees).
* @param {string[]} callchain - Address strings like "0x55cc6d053893"
* @param {Map<string, {symbol: string, location: string|null}|Array>} callframeSymbols
* @returns {{symbol: string, location: string|null}[]}
*/
function symbolizeChain(callchain, callframeSymbols) {
const result = [];
for (const addr of callchain) {
const entry = callframeSymbols.get(addr);
if (!entry) {
result.push({ symbol: addr, location: null });
continue;
}
if (Array.isArray(entry)) {
for (const e of entry) result.push(e);
continue;
}
if (typeof entry === "string") {
result.push({ symbol: entry, location: null });
continue;
}
result.push(entry);
}
return result;
}
/**
* Deduplicate CPU/sched samples by symbolized stack trace.
* @param {Object[]} samples - Array of {callchain, ...} sample objects
* @param {Map} callframeSymbols
* @returns {{count: number, frames: Object[], leaf: string, leafRaw: string}[]}
*/
function deduplicateSamples(samples, callframeSymbols) {
const groups = new Map();
for (const sample of samples) {
const frames = symbolizeChain(sample.callchain, callframeSymbols);
const key = frames.map((f) => f.symbol).join("\0");
if (!groups.has(key)) {
groups.set(key, {
count: 0,
frames,
leaf: frames[0] ? formatFrame(frames[0]).text : "(unknown)",
leafRaw: frames[0] ? frames[0].symbol : "",
});
}
groups.get(key).count++;
}
return [...groups.values()].sort((a, b) => b.count - a.count);
}
// Export for both browser and Node.js
if (typeof module !== "undefined" && module.exports) {
module.exports = {
EVENT_TYPES,
parseTrace,
formatFrame,
symbolizeChain,
deduplicateSamples,
};
} else {
exports.TraceParser = {
EVENT_TYPES,
parseTrace,
formatFrame,
symbolizeChain,
deduplicateSamples,
};
}
})(typeof exports === "undefined" ? this : exports);