Merge branch 'main' of https://github.com/jimrandomh/faceclaw into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-11-01 11:00:38 -08:00
commit fcea0b369a
218 files changed
+14438 -2259

No files matched your search

+10 -3
View File
@@ -14,6 +14,9 @@ import {
const MESSAGE_SIZES = [250, 500, 1000, 2000, 3800];
const WINDOW_SIZES = [1, 2, 3, 4, 6];
const LINK_MODES = ["Current", "Request HIGH", "Request 2M", "HIGH + 2M"];
// CoreBluetooth has no connection-priority or PHY request API (iOS's
// prepareBenchmarkLink is a no-op), so iOS always runs on the current link.
const LINK_REQUESTS_SUPPORTED = !global.isIOS;
const DURATION_MS = 15_000;
// After painting the "running" screen, wait for that frame to reach the
// glasses before the benchmark blocks normal image sends — otherwise the
@@ -40,7 +43,7 @@ type ConfigRow = "size" | "window" | "link" | "start";
export class BandwidthBenchmarkLayer implements Layer {
private phase: Phase = "config";
private readonly menu = new Menu<ConfigRow>({
items: ["size", "window", "link", "start"],
items: LINK_REQUESTS_SUPPORTED ? ["size", "window", "link", "start"] : ["size", "window", "start"],
wrap: false,
rowGap: 1,
highlight: { radius: 4 },
@@ -134,7 +137,8 @@ export class BandwidthBenchmarkLayer implements Layer {
font,
LIST_X,
HEADER_HEIGHT + 30,
`${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}, ${LINK_MODES[this.linkModeIndex]}`,
`${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}`
+ (LINK_REQUESTS_SUPPORTED ? `, ${LINK_MODES[this.linkModeIndex]}` : ""),
150,
);
image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110);
@@ -161,8 +165,11 @@ export class BandwidthBenchmarkLayer implements Layer {
`Throughput: ${(payloadRate / 1024).toFixed(2)} KB/s payload (${(wireRate / 1024).toFixed(2)} KB/s wire)`,
`Messages: ${r.messagesAcked}/${r.messagesSent} acked, ${r.timeouts} timeouts`,
`Elapsed: ${seconds.toFixed(1)} s` + (r.aborted ? " (aborted early)" : ""),
`Link request: ${LINK_MODES[r.linkMode] ?? "Current"}`,
];
if (LINK_REQUESTS_SUPPORTED) {
const phy = r.leftPhy || r.rightPhy ? `; PHY L ${r.leftPhy ?? "?"}, R ${r.rightPhy ?? "?"}` : "";
lines.push(`Link request: ${LINK_MODES[r.linkMode] ?? "Current"}${phy}`);
}
for (const line of lines) {
image.drawText(font, LIST_X, y, line, 190);
y += 20;
-10
View File
@@ -51,24 +51,18 @@ function debugTestsMenu(openInputEvents: OpenPage, openMicrophones: OpenPage): M
},
{
label: "Buzzer demo",
disabled: global.isIOS,
description: global.isIOS ? "Glasses sound playback is not available on iOS yet." : undefined,
onSelect: (ctx) => {
ctx.stack.push(new BuzzerDemoLayer());
},
},
{
label: "Accelerometer demo",
disabled: global.isIOS,
description: global.isIOS ? "Not available on iOS yet." : undefined,
onSelect: (ctx) => {
ctx.stack.push(new AccelerometerDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
},
},
{
label: "Light sensor",
disabled: global.isIOS,
description: global.isIOS ? "Not available on iOS yet." : undefined,
onSelect: (ctx) => {
ctx.stack.push(new LightSensorDemoLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
},
@@ -83,8 +77,6 @@ function debugTestsMenu(openInputEvents: OpenPage, openMicrophones: OpenPage): M
},
{
label: "Microphones",
disabled: global.isIOS,
description: global.isIOS ? "Not available on iOS yet." : undefined,
onSelect: openMicrophones,
},
{
@@ -131,8 +123,6 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc
},
{
label: "Show resource usage",
disabled: global.isIOS,
description: global.isIOS ? "Not available on iOS yet." : undefined,
onSelect: (ctx) => {
ctx.stack.push(new ResourceUsageLayer(DEVELOPER_WINDOW_ID, ctx.actions.requestRender));
},
+51 -67
View File
@@ -4,25 +4,23 @@ import { clamp } from "../../util/numeric-util";
import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
declare const com: any;
import {
MEMORY_SERIES,
formatMb,
sampleResourceUsage,
type ResourceSample,
} from "../../native/resource-usage";
const SAMPLE_INTERVAL_MS = 1_000;
const HISTORY_MS = 60_000;
const MAX_SAMPLES = Math.ceil(HISTORY_MS / SAMPLE_INTERVAL_MS) + 2;
/** Line brightness per memory series, headline first. */
const MEMORY_SHADES = [240, 155, 80];
type ResourceSample = {
atMs: number;
rssMb: number;
nativeMb: number;
javaMb: number;
javaCommittedMb: number;
cpuPercent: number;
threads: number;
};
type ChartSample = ResourceSample & { cpuPercent: number };
type Series = {
value: (sample: ResourceSample) => number;
value: (sample: ChartSample) => number;
shade: number;
};
@@ -30,9 +28,10 @@ type Series = {
* One-minute process monitor. Sampling deliberately continues while another
* window is foreground so switching back reveals the resource cost of that
* workload. Repaints remain visibility-gated to avoid creating hidden frames.
* Which memory figures are charted is up to the platform (MEMORY_SERIES).
*/
export class ResourceUsageLayer implements Layer {
private readonly samples: ResourceSample[] = [];
private readonly samples: ChartSample[] = [];
private timer: ReturnType<typeof setInterval> | null = null;
private removed = false;
private previousElapsedMs = 0;
@@ -68,34 +67,34 @@ export class ResourceUsageLayer implements Layer {
return image;
}
const cpuText = `CPU ${formatPercent(latest.cpuPercent)} ${latest.threads} threads`;
image.drawText(font, width - 16 - font.measureText(cpuText), 5, cpuText, 200);
image.drawText(font, 16, 21, this.sampleError || latest.status, this.sampleError ? 170 : 140);
const memorySeries = MEMORY_SERIES.map((label, index) => ({
label,
shade: MEMORY_SHADES[index] ?? 80,
value: (sample: ChartSample) => sample.memoryMb[index] ?? 0,
}));
const memoryPeak = Math.max(...this.samples.flatMap((sample) => sample.memoryMb), 64);
const memoryScale = roundUp(memoryPeak * 1.08, 64);
const legendLeft = drawLegend(
image,
font,
width,
48,
memorySeries.map((series) => [`${series.label} ${formatMb(series.value(latest))}`, series.shade] as const),
);
const scaleLabel = `Memory 0–${formatMb(memoryScale)}`;
const shortScaleLabel = `0–${formatMb(memoryScale)}`;
image.drawText(
font,
16,
21,
`RSS ${formatMb(latest.rssMb)} native ${formatMb(latest.nativeMb)} Java ${formatMb(latest.javaMb)}/${formatMb(latest.javaCommittedMb)}`,
190,
48,
16 + font.measureText(scaleLabel) + 8 <= legendLeft ? scaleLabel : shortScaleLabel,
140,
);
image.drawText(
font,
width - 16 - font.measureText(`CPU ${formatPercent(latest.cpuPercent)} threads ${latest.threads}`),
47 - font.lineHeight,
`CPU ${formatPercent(latest.cpuPercent)} threads ${latest.threads}`,
155,
);
const memoryPeak = Math.max(...this.samples.map((sample) => sample.rssMb), 64);
const memoryScale = roundUp(memoryPeak * 1.08, 64);
image.drawText(font, 16, 48, `Memory 0–${formatMb(memoryScale)}`, 140);
drawLegend(image, font, width, 48, [
["RSS", 240],
["native", 155],
["Java", 80],
]);
drawChart(image, this.samples, latest.atMs, { x: 16, y: 64, width: width - 32, height: 70 }, memoryScale, [
{ value: (sample) => sample.rssMb, shade: 240 },
{ value: (sample) => sample.nativeMb, shade: 155 },
{ value: (sample) => sample.javaMb, shade: 80 },
]);
drawChart(image, this.samples, latest.atMs, { x: 16, y: 64, width: width - 32, height: 70 }, memoryScale, memorySeries);
const cpuPeak = Math.max(...this.samples.map((sample) => sample.cpuPercent), 100);
const cpuScale = roundUp(cpuPeak * 1.08, 25);
@@ -119,26 +118,16 @@ export class ResourceUsageLayer implements Layer {
private takeSample(): void {
if (this.removed) return;
try {
const raw = com.faceclaw.app.FaceclawResourceUsage.sample();
const atMs = Number(raw[0]);
const cpuMs = Number(raw[1]);
const elapsedDelta = atMs - this.previousElapsedMs;
const cpuDelta = cpuMs - this.previousCpuMs;
const sample = sampleResourceUsage();
const elapsedDelta = sample.atMs - this.previousElapsedMs;
const cpuDelta = sample.cpuMs - this.previousCpuMs;
const cpuPercent = this.previousElapsedMs > 0 && elapsedDelta > 0
? Math.max(0, cpuDelta / elapsedDelta * 100)
: 0;
this.previousElapsedMs = atMs;
this.previousCpuMs = cpuMs;
this.samples.push({
atMs,
cpuPercent,
rssMb: kbToMb(Number(raw[2])),
nativeMb: kbToMb(Number(raw[3])),
javaMb: kbToMb(Number(raw[4])),
javaCommittedMb: kbToMb(Number(raw[5])),
threads: Number(raw[6]),
});
const cutoff = atMs - HISTORY_MS;
this.previousElapsedMs = sample.atMs;
this.previousCpuMs = sample.cpuMs;
this.samples.push({ ...sample, cpuPercent });
const cutoff = sample.atMs - HISTORY_MS;
while (this.samples.length > 1 && this.samples[0]!.atMs < cutoff) this.samples.shift();
while (this.samples.length > MAX_SAMPLES) this.samples.shift();
this.sampleError = "";
@@ -153,7 +142,7 @@ export class ResourceUsageLayer implements Layer {
function drawChart(
image: GrayImage,
samples: readonly ResourceSample[],
samples: readonly ChartSample[],
nowMs: number,
bounds: { x: number; y: number; width: number; height: number },
scale: number,
@@ -163,7 +152,8 @@ function drawChart(
const midY = bounds.y + Math.round((bounds.height - 1) / 2);
image.drawLine(bounds.x + 1, midY, bounds.x + bounds.width - 2, midY, 24);
const startMs = nowMs - HISTORY_MS;
for (const item of series) {
// Back to front, so the headline series is drawn on top where lines cross.
for (const item of [...series].reverse()) {
let previous: { x: number; y: number } | null = null;
for (const sample of samples) {
if (sample.atMs < startMs) continue;
@@ -177,36 +167,30 @@ function drawChart(
}
}
/** Right-aligned legend of line swatches and labels; returns its left edge. */
function drawLegend(
image: GrayImage,
font: ReturnType<typeof getDefaultSmallFont>,
width: number,
y: number,
entries: ReadonlyArray<readonly [string, number]>,
): void {
): number {
const totalWidth = entries.reduce((sum, [label]) => sum + font.measureText(label) + 14, 0);
let x = width - 16 - totalWidth;
const left = width - 16 - totalWidth;
let x = left;
for (const [label, shade] of entries) {
image.drawLine(x, y + 6, x + 8, y + 6, shade);
x += 12;
image.drawText(font, x, y, label, shade);
x += font.measureText(label) + 2;
}
}
function kbToMb(kb: number): number {
return Math.max(0, kb) / 1024;
return left;
}
function roundUp(value: number, step: number): number {
return Math.max(step, Math.ceil(value / step) * step);
}
function formatMb(value: number): string {
if (value >= 1024) return `${(value / 1024).toFixed(1)}G`;
return `${Math.round(value)}M`;
}
function formatPercent(value: number): string {
return `${value < 10 ? value.toFixed(1) : Math.round(value)}%`;
}