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
@@ -0,0 +1,68 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.os.Debug;
|
||||
import android.os.Process;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/** Lightweight process counters for the on-glasses resource-usage monitor. */
|
||||
public final class FaceclawResourceUsage {
|
||||
private FaceclawResourceUsage() {}
|
||||
|
||||
/**
|
||||
* Returns elapsedMs, processCpuMs, rssKb, nativeHeapKb, javaHeapKb,
|
||||
* javaHeapCommittedKb, and threadCount. Reading /proc/self/status avoids
|
||||
* the considerably heavier full Debug.MemoryInfo/PSS collection once per
|
||||
* second while still reporting the RSS that Android records at exit.
|
||||
*/
|
||||
public static long[] sample() {
|
||||
long[] status = readProcessStatus();
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
long javaUsedBytes = runtime.totalMemory() - runtime.freeMemory();
|
||||
return new long[] {
|
||||
SystemClock.elapsedRealtime(),
|
||||
Process.getElapsedCpuTime(),
|
||||
status[0],
|
||||
Debug.getNativeHeapAllocatedSize() / 1024L,
|
||||
javaUsedBytes / 1024L,
|
||||
runtime.totalMemory() / 1024L,
|
||||
status[1],
|
||||
};
|
||||
}
|
||||
|
||||
/** Returns {VmRSS in KiB, thread count}; unavailable values are zero. */
|
||||
private static long[] readProcessStatus() {
|
||||
long rssKb = 0;
|
||||
long threadCount = 0;
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader("/proc/self/status"))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (line.startsWith("VmRSS:")) {
|
||||
rssKb = firstNumber(line);
|
||||
} else if (line.startsWith("Threads:")) {
|
||||
threadCount = firstNumber(line);
|
||||
}
|
||||
if (rssKb > 0 && threadCount > 0) break;
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
// The graph remains useful with the runtime heap counters alone.
|
||||
}
|
||||
return new long[] { rssKb, threadCount };
|
||||
}
|
||||
|
||||
private static long firstNumber(String line) {
|
||||
int start = 0;
|
||||
while (start < line.length() && !Character.isDigit(line.charAt(start))) start++;
|
||||
int end = start;
|
||||
while (end < line.length() && Character.isDigit(line.charAt(end))) end++;
|
||||
if (start == end) return 0;
|
||||
try {
|
||||
return Long.parseLong(line.substring(start, end));
|
||||
} catch (NumberFormatException ignored) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ImageSource } from "@nativescript/core";
|
||||
import { Application, ImageSource } from "@nativescript/core";
|
||||
import { EvenAIStatus, EvenAIStatusName, EventSourceType, EventSourceTypeName, OsEventTypeList, OsEventTypeName } from "./events";
|
||||
import { loadDeviceAddresses } from "./device-addresses";
|
||||
import { ensureBlePermissions, ensureVoicePermissions } from "./android-permissions";
|
||||
@@ -1703,6 +1703,15 @@ class DashboardController {
|
||||
*/
|
||||
private updateCompositePreview(): void {
|
||||
if (!this.communicator) return;
|
||||
// The connected foreground service intentionally keeps this controller
|
||||
// alive after the phone UI is backgrounded. Do not keep constructing
|
||||
// 640x480 Android Bitmaps for a window that cannot display them: besides
|
||||
// being wasted work, paused NativeScript views can retain queued image
|
||||
// updates long enough to put severe pressure on the native heap.
|
||||
if (global.isAndroid) {
|
||||
const activity = Application.android.foregroundActivity;
|
||||
if (!activity || !activity.hasWindowFocus()) return;
|
||||
}
|
||||
const now = Date.now();
|
||||
if (
|
||||
this.lastConnectedPreviewUpdateAtMs > 0 &&
|
||||
|
||||
+5
-1
@@ -24,6 +24,8 @@ export type MenuLayout = {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
/** Whether to draw the rounded outline around the menu. Default: true. */
|
||||
showBorder?: boolean;
|
||||
/** Smallest box to draw even when items don't fill it. Default: top half of the screen. */
|
||||
minHeight?: number;
|
||||
/** Height cap before the menu starts scrolling. Default: the full screen. */
|
||||
@@ -215,7 +217,9 @@ export class MenuLayer implements Layer {
|
||||
// Fill 1, not 0: identical after 4bpp quantization, but 0 is the
|
||||
// transparent color key when a menu paints on the shell surface.
|
||||
image.fillRoundedRect(x, y, width, height, 1);
|
||||
image.drawRoundedRect(x, y, width, height, 72);
|
||||
if (this.layout.showBorder !== false) {
|
||||
image.drawRoundedRect(x, y, width, height, 72);
|
||||
}
|
||||
if (this.title) {
|
||||
image.drawText(font, x + 12, y + 8, this.title, 220);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user