dial9-viewer 0.3.0

CLI trace viewer and S3 browser for dial9-tokio-telemetry
Documentation
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
#!/usr/bin/env node
"use strict";

const fs = require("fs");
const path = require("path");
const { EVENT_TYPES, parseTrace } = require("./trace_parser.js");
const {
  buildWorkerSpans,
  attachCpuSamples,
  buildActiveTaskTimeline,
  computeSchedulingDelays,
  filterPointsOfInterest,
  buildFlamegraphTree,
  flattenFlamegraph,
  buildFgData,
} = require("./trace_analysis.js");

async function main() {
  const tracePath = process.argv[2] || path.join(__dirname, "demo-trace.bin");

  if (!fs.existsSync(tracePath)) {
    console.error(`Trace file not found: ${tracePath}`);
    process.exit(1);
  }

  function fail(msg) {
    console.log(` ${msg}`);
    process.exit(1);
  }

  function pass(msg) {
    console.log(` ${msg}`);
  }

  const trace = await parseTrace(fs.readFileSync(tracePath));
  const evts = trace.events;

  const wSet = new Set();
  evts.forEach((e) => {
    if (
      e.eventType !== EVENT_TYPES.QueueSample &&
      e.eventType !== EVENT_TYPES.WakeEvent
    )
      wSet.add(e.workerId);
  });
  const workerIds = [...wSet].sort((a, b) => a - b);

  let minTs = evts[0].timestamp;
  let maxTs = evts[evts.length - 1].timestamp;
  for (const e of evts) {
    if (e.timestamp < minTs) minTs = e.timestamp;
    if (e.timestamp > maxTs) maxTs = e.timestamp;
  }

  // ── buildWorkerSpans ──

  const { workerSpans, perWorker, queueSamples, workerQueueSamples, maxLocalQueue, wakesByTask, wakesByWorker } = buildWorkerSpans(
    evts,
    workerIds,
    maxTs
  );

  function testPollsHaveValidRange() {
    for (const w of workerIds) {
      for (const p of workerSpans[w].polls) {
        if (p.start > p.end)
          fail(`Worker ${w}: poll start > end (${p.start} > ${p.end})`);
      }
    }
    pass("All polls have start <= end");
  }

  function testNoOverlappingPolls() {
    for (const w of workerIds) {
      const polls = workerSpans[w].polls;
      for (let i = 1; i < polls.length; i++) {
        if (polls[i].start < polls[i - 1].end)
          fail(`Worker ${w}: overlapping polls at index ${i}`);
      }
    }
    pass("No overlapping polls on same worker");
  }

  function testActiveRatiosInRange() {
    for (const w of workerIds) {
      for (const a of workerSpans[w].actives) {
        if (a.ratio < 0 || a.ratio > 1)
          fail(`Worker ${w}: active ratio ${a.ratio} out of [0, 1]`);
      }
    }
    pass("Active period ratios in [0, 1]");
  }

  function testParksHaveValidRange() {
    for (const w of workerIds) {
      for (const p of workerSpans[w].parks) {
        if (p.start > p.end) fail(`Worker ${w}: park start > end`);
      }
    }
    pass("All parks have start <= end");
  }

  function testQueueSamplesExist() {
    if (queueSamples.length === 0) fail("No queue samples");
    pass(`${queueSamples.length} queue samples`);
  }

  // ── attachCpuSamples ──

  const cpuResult = attachCpuSamples(trace.cpuSamples, workerSpans);

  function testAttachedSamplesWithinPollBounds() {
    for (const w of workerIds) {
      for (const p of workerSpans[w].polls) {
        if (p.cpuSamples) {
          for (const s of p.cpuSamples) {
            if (s.timestamp < p.start || s.timestamp > p.end)
              fail(
                `Worker ${w}: cpu sample at ${s.timestamp} outside poll [${p.start}, ${p.end}]`
              );
          }
        }
        if (p.schedSamples) {
          for (const s of p.schedSamples) {
            if (s.timestamp < p.start || s.timestamp > p.end)
              fail(
                `Worker ${w}: sched sample at ${s.timestamp} outside poll [${p.start}, ${p.end}]`
              );
          }
        }
      }
    }
    pass("All attached samples fall within poll bounds");
  }

  function testCpuResultCounts() {
    if (
      cpuResult.pollsWithCpuSamples < 0 ||
      cpuResult.pollsWithSchedSamples < 0
    )
      fail("Negative sample counts");
    pass(
      `${cpuResult.pollsWithCpuSamples} polls with cpu samples, ${cpuResult.pollsWithSchedSamples} with sched samples`
    );
  }

  // ── extractLocalQueueSamples (via buildWorkerSpans) ──

  function testLocalQueueNonNegative() {
    for (const w of workerIds) {
      for (const s of workerQueueSamples[w]) {
        if (s.local < 0) fail(`Worker ${w}: negative local queue ${s.local}`);
      }
    }
    pass("All local queue depths non-negative");
  }

  function testMaxLocalQueue() {
    if (maxLocalQueue < 1) fail(`maxLocalQueue ${maxLocalQueue} < 1`);
    pass(`maxLocalQueue = ${maxLocalQueue}`);
  }

  // ── buildActiveTaskTimeline ──

  const { activeTaskSamples, taskFirstPoll } = buildActiveTaskTimeline(
    trace.taskSpawnTimes,
    trace.taskTerminateTimes
  );

  function testTimelineSorted() {
    for (let i = 1; i < activeTaskSamples.length; i++) {
      if (activeTaskSamples[i].t < activeTaskSamples[i - 1].t)
        fail(`Timeline not sorted at index ${i}`);
    }
    pass("Timeline sorted by timestamp");
  }

  function testCountNonNegative() {
    for (const s of activeTaskSamples) {
      if (s.count < 0) fail(`Negative task count ${s.count}`);
    }
    pass("Task counts non-negative");
  }

  // ── indexWakeEvents (via buildWorkerSpans) ──

  function testWakesByTaskSorted() {
    for (const arr of Object.values(wakesByTask)) {
      for (let i = 1; i < arr.length; i++) {
        if (arr[i].timestamp < arr[i - 1].timestamp)
          fail("wakesByTask not sorted");
      }
    }
    pass("wakesByTask arrays sorted by timestamp");
  }

  function testWakesByWorkerSorted() {
    for (const arr of Object.values(wakesByWorker)) {
      for (let i = 1; i < arr.length; i++) {
        if (arr[i].timestamp < arr[i - 1].timestamp)
          fail("wakesByWorker not sorted");
      }
    }
    pass("wakesByWorker arrays sorted by timestamp");
  }

  function testWakeCountsConsistent() {
    let taskTotal = 0;
    for (const arr of Object.values(wakesByTask)) taskTotal += arr.length;
    let workerTotal = 0;
    for (const arr of Object.values(wakesByWorker)) workerTotal += arr.length;
    if (taskTotal !== workerTotal)
      fail(
        `wakesByTask total ${taskTotal} != wakesByWorker total ${workerTotal}`
      );
    pass(`${taskTotal} wake events indexed consistently`);
  }

  // ── computeSchedulingDelays ──

  const schedDelays = computeSchedulingDelays(
    workerSpans,
    workerIds,
    wakesByTask
  );

  function testDelaysPositive() {
    for (const sd of schedDelays) {
      if (sd.delay <= 0) fail(`Non-positive delay: ${sd.delay}`);
    }
    pass("All delays positive");
  }

  function testDelaysBounded() {
    for (const sd of schedDelays) {
      if (sd.delay >= 1e9) fail(`Delay >= 1s: ${sd.delay}`);
    }
    pass("All delays < 1s");
  }

  function testWakeBeforePoll() {
    for (const sd of schedDelays) {
      if (sd.wakeTime >= sd.pollTime)
        fail(`wakeTime ${sd.wakeTime} >= pollTime ${sd.pollTime}`);
    }
    pass("wakeTime < pollTime for all delays");
  }

  function testDelaysSorted() {
    for (let i = 1; i < schedDelays.length; i++) {
      if (schedDelays[i].wakeTime < schedDelays[i - 1].wakeTime)
        fail("schedDelays not sorted by wakeTime");
    }
    pass("schedDelays sorted by wakeTime");
  }

  // ── filterPointsOfInterest ──

  function testLongPollFilter() {
    const pois = filterPointsOfInterest(
      "long-poll",
      workerSpans,
      workerIds,
      schedDelays,
      { hasSchedWait: trace.hasSchedWait }
    );
    if (pois.length === 0) fail("No long-poll points of interest found");
    for (const p of pois) {
      if (p.type !== "long-poll") fail(`Wrong type: ${p.type}`);
      if (p.value <= 1) fail(`long-poll value ${p.value} <= 1ms`);
    }
    pass(`long-poll filter: ${pois.length} results, all > 1ms`);
  }

  function testCpuSampledFilter() {
    const pois = filterPointsOfInterest(
      "cpu-sampled",
      workerSpans,
      workerIds,
      schedDelays,
      { hasSchedWait: trace.hasSchedWait }
    );
    if (pois.length === 0) fail("No cpu-sampled points of interest found");
    for (const p of pois) {
      if (p.type !== "cpu-sampled") fail(`Wrong type: ${p.type}`);
      if (p.value <= 0) fail(`cpu-sampled value ${p.value} <= 0`);
    }
    pass(`cpu-sampled filter: ${pois.length} results, all with samples`);
  }

  function testWakeDelayFilter() {
    const pois = filterPointsOfInterest(
      "wake-delay",
      workerSpans,
      workerIds,
      schedDelays,
      { hasSchedWait: trace.hasSchedWait }
    );
    if (pois.length === 0) fail("No wake-delay points of interest found");
    for (const p of pois) {
      if (p.type !== "wake-delay") fail(`Wrong type: ${p.type}`);
      if (p.value <= 100) fail(`wake-delay value ${p.value} <= 100µs`);
    }
    pass(`wake-delay filter: ${pois.length} results, all > 100µs`);
  }

  function testSortByWorst() {
    const pois = filterPointsOfInterest(
      "long-poll",
      workerSpans,
      workerIds,
      schedDelays,
      { hasSchedWait: trace.hasSchedWait, sortByWorst: true }
    );
    for (let i = 1; i < pois.length; i++) {
      if (pois[i].value > pois[i - 1].value) fail("sortByWorst not descending");
    }
    pass("sortByWorst produces descending order");
  }

  // ── buildFlamegraphTree / flattenFlamegraph ──

  function testFlamegraphTree() {
    const cpuSamples = trace.cpuSamples.filter((s) => s.source !== 1);
    if (cpuSamples.length === 0) fail("No CPU samples found");

    const root = buildFlamegraphTree(cpuSamples, trace.callframeSymbols);
    if (root.count !== cpuSamples.length)
      fail(`Root count ${root.count} != sample count ${cpuSamples.length}`);
    pass(`Root count matches sample count (${root.count})`);
  }

  function testFlattenFlamegraph() {
    const cpuSamples = trace.cpuSamples.filter((s) => s.source !== 1);
    if (cpuSamples.length === 0) fail("No CPU samples found");

    const root = buildFlamegraphTree(cpuSamples, trace.callframeSymbols);
    const { nodes, maxDepth } = flattenFlamegraph(root, cpuSamples.length);
    for (const n of nodes) {
      if (n.x < 0 || n.x >= 1) fail(`Node x=${n.x} out of [0, 1)`);
      if (n.w <= 0) fail(`Node w=${n.w} <= 0`);
    }
    if (maxDepth < 0) fail(`maxDepth ${maxDepth} < 0`);
    pass(`${nodes.length} flamegraph nodes, maxDepth=${maxDepth}`);
  }

  function testBuildFgData() {
    const cpuSamples = trace.cpuSamples.filter((s) => s.source !== 1);
    if (cpuSamples.length === 0) fail("No CPU samples found");

    const data = buildFgData(cpuSamples, trace.callframeSymbols);
    if (!data) fail("buildFgData returned null for non-empty samples");
    if (data.totalSamples !== cpuSamples.length)
      fail(`totalSamples ${data.totalSamples} != ${cpuSamples.length}`);
    pass(
      `buildFgData: ${data.nodes.length} nodes, ${data.totalSamples} samples`
    );
  }

  function testBuildFgDataEmpty() {
    const data = buildFgData([], trace.callframeSymbols);
    if (data !== null) fail("buildFgData should return null for empty samples");
    pass("buildFgData returns null for empty samples");
  }

  // ── Regression: open PollStart at trace end must not create phantom poll (#194) ──

  function testOpenPollStartDiscarded() {
    // Simulate a rotated segment where PollStart is the last event (no PollEnd).
    const syntheticEvents = [
      { eventType: EVENT_TYPES.PollStart, timestamp: 1000, workerId: 0, taskId: 1, spawnLocId: null, spawnLoc: null, localQueue: 0 },
      { eventType: EVENT_TYPES.PollEnd,   timestamp: 2000, workerId: 0 },
      // This PollStart has no matching PollEnd — file rotated
      { eventType: EVENT_TYPES.PollStart, timestamp: 3000, workerId: 0, taskId: 2, spawnLocId: null, spawnLoc: null, localQueue: 0 },
    ];
    const syntheticMaxTs = 1_000_000; // 1ms later — would create a huge phantom poll
    const result = buildWorkerSpans(syntheticEvents, [0], syntheticMaxTs);
    const polls = result.workerSpans[0].polls;
    if (polls.length !== 1) fail(`Expected 1 poll, got ${polls.length}  open PollStart was not discarded`);
    if (polls[0].start !== 1000 || polls[0].end !== 2000) fail(`Unexpected poll range`);
    pass("Open PollStart at trace end is discarded (no phantom long poll)");
  }

  // ── Run all tests ──

  console.log("\nbuildWorkerSpans:");
  testOpenPollStartDiscarded();
  testPollsHaveValidRange();
  testNoOverlappingPolls();
  testActiveRatiosInRange();
  testParksHaveValidRange();
  testQueueSamplesExist();

  console.log("\nattachCpuSamples:");
  testAttachedSamplesWithinPollBounds();
  testCpuResultCounts();

  console.log("\nextractLocalQueueSamples:");
  testLocalQueueNonNegative();
  testMaxLocalQueue();

  console.log("\nbuildActiveTaskTimeline:");
  testTimelineSorted();
  testCountNonNegative();

  console.log("\nindexWakeEvents:");
  testWakesByTaskSorted();
  testWakesByWorkerSorted();
  testWakeCountsConsistent();

  console.log("\ncomputeSchedulingDelays:");
  testDelaysPositive();
  testDelaysBounded();
  testWakeBeforePoll();
  testDelaysSorted();

  console.log("\nfilterPointsOfInterest:");
  testLongPollFilter();
  testCpuSampledFilter();
  testWakeDelayFilter();
  testSortByWorst();

  console.log("\nflamegraph:");
  testFlamegraphTree();
  testFlattenFlamegraph();
  testBuildFgData();
  testBuildFgDataEmpty();

  console.log("\n✓ All analysis checks passed!");
}

main().catch((e) => {
  console.error(e);
  process.exit(1);
});