forked from Blendman974/faceclaw
Port the Developer app's resource monitor to iOS
This commit is contained in:
1 parent
c5f80a0b00
commit
f47d82b871
13 files changed
+344
-74
No files matched your search
@@ -114,8 +114,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));
|
||||
},
|
||||
|
||||
@@ -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)}%`;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/** Platform-neutral process resource sample, shared by resource-usage.ts and resource-usage.ios.ts. */
|
||||
|
||||
export type ResourceSample = {
|
||||
/** Monotonic clock (counting deep sleep), ms. */
|
||||
atMs: number;
|
||||
/** Cumulative CPU time of every thread in the process, ms. */
|
||||
cpuMs: number;
|
||||
threads: number;
|
||||
/** One value per entry of the platform's MEMORY_SERIES, MiB. */
|
||||
memoryMb: number[];
|
||||
/** Platform-specific details for the latest sample (peaks, limits, thermal state). */
|
||||
status: string;
|
||||
};
|
||||
|
||||
export function kbToMb(kb: number): number {
|
||||
return Number.isFinite(kb) ? Math.max(0, kb) / 1024 : 0;
|
||||
}
|
||||
|
||||
export function formatMb(value: number): string {
|
||||
if (value >= 1024) return `${(value / 1024).toFixed(1)}G`;
|
||||
return `${Math.round(value)}M`;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { formatMb, kbToMb, type ResourceSample } from "./resource-usage-types";
|
||||
|
||||
export * from "./resource-usage-types";
|
||||
|
||||
declare const FaceclawResourceUsage: any;
|
||||
|
||||
/**
|
||||
* Memory series charted by the resource monitor, headline first: the physical
|
||||
* footprint (what jetsam enforces its limit against, and what Xcode's memory
|
||||
* gauge shows), resident memory, and bytes in use across the malloc zones.
|
||||
*/
|
||||
export const MEMORY_SERIES: readonly string[] = ["footprint", "resident", "malloc"];
|
||||
|
||||
const THERMAL_STATES = ["nominal", "fair", "serious", "critical"];
|
||||
|
||||
export function sampleResourceUsage(): ResourceSample {
|
||||
const raw = JSON.parse(String(FaceclawResourceUsage.sample())) as Record<string, number | boolean | undefined>;
|
||||
const kb = (key: string) => kbToMb(Number(raw[key] ?? 0));
|
||||
const footprintMb = kb("footprintKb");
|
||||
const peakMb = kb("footprintPeakKb");
|
||||
const availableMb = kb("availableKb");
|
||||
const thermal = THERMAL_STATES[Number(raw.thermalState)];
|
||||
return {
|
||||
atMs: Number(raw.atMs),
|
||||
cpuMs: Number(raw.cpuMs ?? 0),
|
||||
threads: Number(raw.threads ?? 0),
|
||||
memoryMb: [footprintMb, kb("residentKb"), kb("mallocKb")],
|
||||
status: [
|
||||
// Footprint plus the remaining headroom is the jetsam limit. No headroom
|
||||
// means no limit applies (the simulator) or it was just exceeded.
|
||||
availableMb > 0 ? `limit ${formatMb(footprintMb + availableMb)}` : "",
|
||||
peakMb > 0 ? `peak ${formatMb(peakMb)}` : "",
|
||||
thermal ? `thermal ${thermal}` : "",
|
||||
raw.lowPower === true ? "Low Power Mode" : "",
|
||||
].filter(Boolean).join(" · "),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { formatMb, kbToMb, type ResourceSample } from "./resource-usage-types";
|
||||
|
||||
export * from "./resource-usage-types";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* Memory series charted by the resource monitor, headline first: RSS from
|
||||
* /proc/self/status (what Android records at exit), the native malloc heap,
|
||||
* and the used Java heap.
|
||||
*/
|
||||
export const MEMORY_SERIES: readonly string[] = ["RSS", "native", "Java"];
|
||||
|
||||
export function sampleResourceUsage(): ResourceSample {
|
||||
const raw = com.faceclaw.app.FaceclawResourceUsage.sample();
|
||||
const peakRssMb = kbToMb(Number(raw[7]));
|
||||
const javaCommittedMb = kbToMb(Number(raw[5]));
|
||||
return {
|
||||
atMs: Number(raw[0]),
|
||||
cpuMs: Number(raw[1]),
|
||||
memoryMb: [kbToMb(Number(raw[2])), kbToMb(Number(raw[3])), kbToMb(Number(raw[4]))],
|
||||
threads: Number(raw[6]),
|
||||
status: [
|
||||
peakRssMb > 0 ? `peak RSS ${formatMb(peakRssMb)}` : "",
|
||||
`Java heap ${formatMb(javaCommittedMb)} committed`,
|
||||
].filter(Boolean).join(" · "),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user