`;
console.error(e);
}
this.loaded = true;
this.drawChart();
}
renderTree() {
// Group by leading path component (everything before the first "/").
const groups = new Map();
for (const d of this.defs) {
if (!d.sample_count) continue;
const slash = d.name.indexOf("/");
const group = slash > 0 ? d.name.substring(0, slash) : "";
if (!groups.has(group)) groups.set(group, []);
groups.get(group).push(d);
}
if (groups.size === 0) {
this.treeEl.innerHTML = `
No counters in this trace.
`;
return;
}
const groupNames = Array.from(groups.keys()).sort((a, b) => a.localeCompare(b));
const parts = [];
for (const g of groupNames) {
const list = groups.get(g);
parts.push(`
${escapeHtml(g || "(ungrouped)")}
`);
for (const d of list) {
parts.push(
``
);
}
}
this.treeEl.innerHTML = parts.join("");
// Set names via DOM (XSS safe).
const rows = this.treeEl.querySelectorAll(".csv-stat-row");
let idx = 0;
for (const g of groupNames) {
for (const d of groups.get(g)) {
if (idx < rows.length) {
const slash = d.name.indexOf("/");
const display = slash > 0 ? d.name.substring(slash + 1) : d.name;
rows[idx].querySelector(".csv-stat-name").textContent = display;
rows[idx].title = d.name;
}
++idx;
}
}
// Wire checkboxes.
for (const cb of this.treeEl.querySelectorAll("input[type=checkbox]")) {
cb.addEventListener("change", () => {
const id = Number(cb.dataset.counterId);
if (cb.checked) {
this.selected.add(id);
if (!this.colorById.has(id)) {
this.colorById.set(id, LINE_COLORS[this.colorIndex++ % LINE_COLORS.length]);
}
this.fetchSeries(id);
} else {
this.selected.delete(id);
this.drawChart();
}
});
}
}
async fetchSeries(id) {
if (this.seriesData.has(id)) {
this.drawChart();
return;
}
try {
const result = await getCounterSeries(id);
const samples = (result.samples || []).map(([t, v]) => ({ timeUs: t, value: v }));
this.seriesData.set(id, samples);
this.drawChart();
} catch (e) {
console.error(`Failed to fetch counter series for ${id}: ${e.message}`);
}
}
resizeCanvas() {
const rect = this.canvas.getBoundingClientRect();
this.width = Math.floor(rect.width);
this.height = Math.floor(rect.height);
const bw = Math.floor(rect.width * this.dpr);
const bh = Math.floor(rect.height * this.dpr);
if (this.canvas.width !== bw || this.canvas.height !== bh) {
this.canvas.width = bw;
this.canvas.height = bh;
}
this.ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0);
}
drawChart() {
this.resizeCanvas();
const ctx = this.ctx;
const W = this.width;
const H = this.height;
const bg = getComputedStyle(document.body).getPropertyValue("--bg0") || "#0d1117";
const fg2 = getComputedStyle(document.body).getPropertyValue("--fg2") || "#8b949e";
const border = getComputedStyle(document.body).getPropertyValue("--border") || "#30363d";
ctx.fillStyle = bg;
ctx.fillRect(0, 0, W, H);
if (this.selected.size === 0) {
ctx.fillStyle = fg2;
ctx.font = "12px -apple-system, Segoe UI, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Select counters from the tree to chart them", W / 2, H / 2);
return;
}
const PAD_L = 70, PAD_R = 12, PAD_T = 12, PAD_B = 28;
const chartW = W - PAD_L - PAD_R;
const chartH = H - PAD_T - PAD_B;
if (chartW <= 0 || chartH <= 0) return;
const startUs = this.viewStartUs;
const endUs = this.viewEndUs;
const rangeUs = Math.max(1, endUs - startUs);
let minVal = Infinity, maxVal = -Infinity;
for (const id of this.selected) {
const samples = this.seriesData.get(id);
if (!samples) continue;
for (const s of samples) {
if (s.timeUs < startUs || s.timeUs > endUs) continue;
if (s.value < minVal) minVal = s.value;
if (s.value > maxVal) maxVal = s.value;
}
}
if (!isFinite(minVal)) { minVal = 0; maxVal = 1; }
if (minVal === maxVal) { minVal -= 0.5; maxVal += 0.5; }
const valRange = maxVal - minVal;
const valPad = valRange * 0.05;
minVal -= valPad;
maxVal += valPad;
const xAt = (us) => PAD_L + (us - startUs) / rangeUs * chartW;
const yAt = (v) => PAD_T + (1 - (v - minVal) / (maxVal - minVal)) * chartH;
ctx.strokeStyle = border;
ctx.lineWidth = 0.5;
for (let i = 0; i <= 4; i++) {
const y = PAD_T + chartH * i / 4;
ctx.beginPath(); ctx.moveTo(PAD_L, y); ctx.lineTo(PAD_L + chartW, y); ctx.stroke();
}
// Format Y-axis labels using the display hint of the first selected counter.
const firstSelected = this.selected.values().next().value;
const firstDef = this.defById.get(firstSelected);
ctx.fillStyle = fg2;
ctx.font = "10px -apple-system, Segoe UI, sans-serif";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
for (let i = 0; i <= 4; i++) {
const v = minVal + (maxVal - minVal) * (1 - i / 4);
const y = PAD_T + chartH * i / 4;
ctx.fillText(formatCounterValue(v, firstDef), PAD_L - 4, y);
}
ctx.textAlign = "center";
ctx.textBaseline = "top";
const tickCount = Math.max(2, Math.min(8, Math.floor(chartW / 80)));
for (let i = 0; i <= tickCount; i++) {
const us = startUs + rangeUs * i / tickCount;
const x = xAt(us);
ctx.fillText(formatTime(us), x, PAD_T + chartH + 4);
}
// Draw step-style lines (counters change at discrete events).
for (const id of this.selected) {
const samples = this.seriesData.get(id);
if (!samples || samples.length === 0) continue;
const color = this.colorById.get(id) || "#fff";
ctx.strokeStyle = color;
ctx.lineWidth = 1.5;
ctx.beginPath();
let started = false;
let prevY = 0;
for (const s of samples) {
if (s.timeUs < startUs || s.timeUs > endUs) continue;
const x = xAt(s.timeUs);
const y = yAt(s.value);
if (!started) { ctx.moveTo(x, y); started = true; }
else { ctx.lineTo(x, prevY); ctx.lineTo(x, y); }
prevY = y;
}
ctx.stroke();
}
ctx.strokeStyle = border;
ctx.lineWidth = 1;
ctx.strokeRect(PAD_L, PAD_T, chartW, chartH);
ctx.font = "10px -apple-system, Segoe UI, sans-serif";
ctx.textAlign = "left";
ctx.textBaseline = "top";
let legendX = PAD_L + 6;
for (const id of this.selected) {
const def = this.defById.get(id);
const name = def ? def.name : `counter ${id}`;
const color = this.colorById.get(id) || "#fff";
ctx.fillStyle = color;
ctx.fillRect(legendX, PAD_T + 4, 10, 10);
ctx.fillStyle = "#ccc";
ctx.fillText(name, legendX + 14, PAD_T + 4);
legendX += ctx.measureText(name).width + 24;
}
this._chartLayout = { PAD_L, PAD_R, PAD_T, PAD_B, chartW, chartH, startUs, endUs, rangeUs, minVal, maxVal, xAt, yAt, firstDef };
}
onChartHover(e) {
if (!this._chartLayout || this.selected.size === 0) {
this.tooltipEl.hidden = true;
return;
}
const rect = this.canvas.getBoundingClientRect();
const mx = e.clientX - rect.left;
const my = e.clientY - rect.top;
const { PAD_L, PAD_T, chartW, chartH, startUs, rangeUs } = this._chartLayout;
if (mx < PAD_L || mx > PAD_L + chartW || my < PAD_T || my > PAD_T + chartH) {
this.tooltipEl.hidden = true;
return;
}
const cursorUs = startUs + (mx - PAD_L) / chartW * rangeUs;
const lines = [];
for (const id of this.selected) {
const samples = this.seriesData.get(id);
if (!samples || samples.length === 0) continue;
let best = null, bestDist = Infinity;
for (const s of samples) {
const d = Math.abs(s.timeUs - cursorUs);
if (d < bestDist) { bestDist = d; best = s; }
}
if (best) {
const def = this.defById.get(id);
const name = def ? def.name : `counter ${id}`;
const color = this.colorById.get(id) || "#fff";
lines.push(`${escapeHtml(name)}: ${formatCounterValue(best.value, def)}`);
}
}
if (lines.length === 0) { this.tooltipEl.hidden = true; return; }
this.tooltipEl.innerHTML = `