forked from Blendman974/faceclaw
Add a resource-usage monitor to debug app; fix excessive memory-usage from screen preview when not focused
This commit is contained in:
1 parent
b5213170e3
commit
9ade3503a0
6 files changed
+331
-7
No files matched your search
@@ -2,6 +2,7 @@ import { MenuLayer } from "../../ui/menu";
|
||||
import { ScreenTestLayer } from "./screen-test";
|
||||
import { BuzzerDemoLayer } from "./buzzer-demo";
|
||||
import { AccelerometerDemoLayer } from "./accelerometer-demo";
|
||||
import { ResourceUsageLayer } from "./resource-usage";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
@@ -19,7 +20,7 @@ export function createDebugTestsAppWindow(options: InProcessAppOptions): InProce
|
||||
"Debug tests",
|
||||
[
|
||||
{
|
||||
label: "Screen test",
|
||||
label: "Dither test",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new ScreenTestLayer());
|
||||
},
|
||||
@@ -36,11 +37,18 @@ export function createDebugTestsAppWindow(options: InProcessAppOptions): InProce
|
||||
ctx.stack.push(new AccelerometerDemoLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Show resource usage",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new ResourceUsageLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
x: 8,
|
||||
y: 8,
|
||||
width: 272,
|
||||
showBorder: false,
|
||||
minHeight: 0,
|
||||
maxHeight: appViewportSize("min").height - 16,
|
||||
},
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
const SAMPLE_INTERVAL_MS = 1_000;
|
||||
const HISTORY_MS = 60_000;
|
||||
const MAX_SAMPLES = Math.ceil(HISTORY_MS / SAMPLE_INTERVAL_MS) + 2;
|
||||
|
||||
type ResourceSample = {
|
||||
atMs: number;
|
||||
rssMb: number;
|
||||
nativeMb: number;
|
||||
javaMb: number;
|
||||
javaCommittedMb: number;
|
||||
cpuPercent: number;
|
||||
threads: number;
|
||||
};
|
||||
|
||||
type Series = {
|
||||
value: (sample: ResourceSample) => number;
|
||||
shade: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export class ResourceUsageLayer implements Layer {
|
||||
private readonly samples: ResourceSample[] = [];
|
||||
private timer: ReturnType<typeof setInterval> | null = null;
|
||||
private removed = false;
|
||||
private previousElapsedMs = 0;
|
||||
private previousCpuMs = 0;
|
||||
private sampleError = "";
|
||||
|
||||
constructor(
|
||||
private readonly windowId: string,
|
||||
private readonly requestRender: () => void,
|
||||
) {
|
||||
this.takeSample();
|
||||
this.timer = setInterval(() => this.takeSample(), SAMPLE_INTERVAL_MS);
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed = true;
|
||||
if (this.timer !== null) {
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const latest = this.samples[this.samples.length - 1];
|
||||
|
||||
image.drawText(font, 16, 5, "Resource usage — last minute", 230);
|
||||
if (!latest) {
|
||||
image.drawText(font, 16, 30, this.sampleError || "Waiting for first sample…", 170);
|
||||
image.drawText(font, 16, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
image.drawText(
|
||||
font,
|
||||
16,
|
||||
21,
|
||||
`RSS ${formatMb(latest.rssMb)} native ${formatMb(latest.nativeMb)} Java ${formatMb(latest.javaMb)}/${formatMb(latest.javaCommittedMb)}`,
|
||||
190,
|
||||
);
|
||||
image.drawText(
|
||||
font,
|
||||
width - 16 - font.measureText(`CPU ${formatPercent(latest.cpuPercent)} threads ${latest.threads}`),
|
||||
37,
|
||||
`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 },
|
||||
]);
|
||||
|
||||
const cpuPeak = Math.max(...this.samples.map((sample) => sample.cpuPercent), 100);
|
||||
const cpuScale = roundUp(cpuPeak * 1.08, 25);
|
||||
image.drawText(font, 16, 144, `CPU 0–${Math.round(cpuScale)}% (one core)`, 140);
|
||||
drawChart(image, this.samples, latest.atMs, { x: 16, y: 160, width: width - 32, height: 60 }, cpuScale, [
|
||||
{ value: (sample) => sample.cpuPercent, shade: 220 },
|
||||
]);
|
||||
|
||||
image.drawText(font, 16, height - 16, `-60s`, 80);
|
||||
const nowLabel = `now ${GESTURE_DOUBLE_CLICK} back`;
|
||||
image.drawText(font, width - 16 - font.measureText(nowLabel), height - 16, nowLabel, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
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 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;
|
||||
while (this.samples.length > 1 && this.samples[0]!.atMs < cutoff) this.samples.shift();
|
||||
while (this.samples.length > MAX_SAMPLES) this.samples.shift();
|
||||
this.sampleError = "";
|
||||
} catch (error) {
|
||||
this.sampleError = `Sampling failed: ${(error as Error)?.message ?? error}`;
|
||||
}
|
||||
if (shell.isWindowVisible(this.windowId)) {
|
||||
this.requestRender();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawChart(
|
||||
image: GrayImage,
|
||||
samples: readonly ResourceSample[],
|
||||
nowMs: number,
|
||||
bounds: { x: number; y: number; width: number; height: number },
|
||||
scale: number,
|
||||
series: readonly Series[],
|
||||
): void {
|
||||
image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 45);
|
||||
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) {
|
||||
let previous: { x: number; y: number } | null = null;
|
||||
for (const sample of samples) {
|
||||
if (sample.atMs < startMs) continue;
|
||||
const x = bounds.x + Math.round(clamp((sample.atMs - startMs) / HISTORY_MS, 0, 1) * (bounds.width - 1));
|
||||
const y = bounds.y + bounds.height - 1
|
||||
- Math.round(clamp(item.value(sample) / Math.max(1, scale), 0, 1) * (bounds.height - 1));
|
||||
if (previous) image.drawLine(previous.x, previous.y, x, y, item.shade);
|
||||
else image.setPixel(x, y, item.shade);
|
||||
previous = { x, y };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawLegend(
|
||||
image: GrayImage,
|
||||
font: ReturnType<typeof getDefaultSmallFont>,
|
||||
width: number,
|
||||
y: number,
|
||||
entries: ReadonlyArray<readonly [string, number]>,
|
||||
): void {
|
||||
const totalWidth = entries.reduce((sum, [label]) => sum + font.measureText(label) + 14, 0);
|
||||
let x = width - 16 - totalWidth;
|
||||
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;
|
||||
}
|
||||
|
||||
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)}%`;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
||||
import { drawListScrollbar } from "../../ui/menu";
|
||||
|
||||
const COLOR_VALUE_COUNT = 256;
|
||||
const COLOR_VALUE_STEP = 1;
|
||||
@@ -10,8 +11,11 @@ const LABEL_Y_OFFSET = 0;
|
||||
const SWATCH_Y_OFFSET = 12;
|
||||
const SWATCH_WIDTH = 24;
|
||||
const SWATCH_HEIGHT = 16;
|
||||
const GRID_ROWS = Math.ceil(COLOR_VALUE_COUNT / GRID_COLUMNS);
|
||||
|
||||
export class ScreenTestLayer implements Layer {
|
||||
private scrollRow = 0;
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
// Sized to the hosting stack (the Debug tests app viewport).
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
@@ -19,13 +23,17 @@ export class ScreenTestLayer implements Layer {
|
||||
const font = getDefaultSmallFont();
|
||||
const cellWidth = Math.floor(width / GRID_COLUMNS);
|
||||
const gridX = Math.floor((width - cellWidth * GRID_COLUMNS) / 2);
|
||||
const visibleRows = Math.max(1, Math.ceil(height / CELL_HEIGHT));
|
||||
const maxScrollRow = Math.max(0, GRID_ROWS - visibleRows);
|
||||
this.scrollRow = Math.max(0, Math.min(maxScrollRow, this.scrollRow));
|
||||
|
||||
for (let value = 0; value < COLOR_VALUE_COUNT; value += COLOR_VALUE_STEP) {
|
||||
const slot = value / COLOR_VALUE_STEP;
|
||||
const col = slot % GRID_COLUMNS;
|
||||
const row = Math.floor(slot / GRID_COLUMNS);
|
||||
const cellX = gridX + col * cellWidth;
|
||||
const cellY = row * CELL_HEIGHT;
|
||||
const cellY = (row - this.scrollRow) * CELL_HEIGHT;
|
||||
if (cellY >= height || cellY + CELL_HEIGHT <= 0) continue;
|
||||
const label = String(value);
|
||||
const labelX = cellX + Math.max(0, Math.floor((cellWidth - font.measureText(label)) / 2));
|
||||
const swatchX = cellX + Math.floor((cellWidth - SWATCH_WIDTH) / 2);
|
||||
@@ -34,12 +42,27 @@ export class ScreenTestLayer implements Layer {
|
||||
fillSwatch(image, swatchX, cellY + SWATCH_Y_OFFSET, SWATCH_WIDTH, SWATCH_HEIGHT, value);
|
||||
}
|
||||
|
||||
if (GRID_ROWS > visibleRows) {
|
||||
drawListScrollbar(image, width - 4, 2, height - 4, this.scrollRow, visibleRows, GRID_ROWS);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.scrollRow = Math.max(0, this.scrollRow - 1);
|
||||
return;
|
||||
case "scroll-down": {
|
||||
const { height } = ctx.stack.getBaseSize();
|
||||
const visibleRows = Math.max(1, Math.ceil(height / CELL_HEIGHT));
|
||||
this.scrollRow = Math.min(Math.max(0, GRID_ROWS - visibleRows), this.scrollRow + 1);
|
||||
return;
|
||||
}
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,4 +93,4 @@ function ditherPattern(x: number, y: number, fillFraction: number): number {
|
||||
];
|
||||
const patternValue = pattern[y % 4][x % 4];
|
||||
return (fillFraction > patternValue) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user