Port the Developer app's resource monitor to iOS

This commit is contained in:
James Babcock committed 2026-10-31 14:45:43 -07:00
1 parent c5f80a0b00
commit f47d82b871
13 files changed
+344 -74

No files matched your search

+22
View File
@@ -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`;
}
+37
View File
@@ -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(" · "),
};
}
+28
View File
@@ -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(" · "),
};
}