forked from Blendman974/faceclaw
Add a performance tracing system (FrameTimings) and fix a big that would cause 6-second stalls
This commit is contained in:
1 parent
971b04f677
commit
630b511d32
11 files changed
+786
-72
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import * as frameTimings from "./frame-timings";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
@@ -32,6 +33,7 @@ export type RawInputEvent =
|
||||
eventType: number;
|
||||
eventSource: number;
|
||||
systemExitReasonCode: number;
|
||||
frameId: number;
|
||||
}
|
||||
| {
|
||||
kind: "text-click";
|
||||
@@ -39,6 +41,7 @@ export type RawInputEvent =
|
||||
eventType: number;
|
||||
eventSource: number;
|
||||
systemExitReasonCode: number;
|
||||
frameId: number;
|
||||
}
|
||||
| {
|
||||
kind: "sys-event";
|
||||
@@ -46,6 +49,7 @@ export type RawInputEvent =
|
||||
eventType: number;
|
||||
eventSource: number;
|
||||
systemExitReasonCode: number;
|
||||
frameId: number;
|
||||
};
|
||||
|
||||
function toJavaByteArray(bytes: Uint8Array): number[] {
|
||||
@@ -67,6 +71,10 @@ export class FaceclawCommunicatorBridge {
|
||||
private readonly listenerProxy: any;
|
||||
private javaCallQueue: Promise<void> = Promise.resolve();
|
||||
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
|
||||
// Recent frame-finished outcomes from the Java side, so waitForFrameFinished
|
||||
// does not race against finishes that land before the wait starts.
|
||||
private readonly finishedFrameOutcomes = new Map<number, string>();
|
||||
private readonly frameFinishedWaiters = new Map<number, Set<(outcome: string) => void>>();
|
||||
private readonly logListeners = new Set<(line: string) => void>();
|
||||
private readonly stateListeners = new Set<(state: CommunicatorState) => void>();
|
||||
private readonly ringListeners = new Set<(event: RawInputEvent) => void>();
|
||||
@@ -101,6 +109,7 @@ export class FaceclawCommunicatorBridge {
|
||||
eventType: number,
|
||||
eventSource: number,
|
||||
systemExitReasonCode: number,
|
||||
frameId: number,
|
||||
) => {
|
||||
const event = {
|
||||
kind: String(kind) as RawInputEvent["kind"],
|
||||
@@ -108,7 +117,9 @@ export class FaceclawCommunicatorBridge {
|
||||
eventType: Number(eventType),
|
||||
eventSource: Number(eventSource),
|
||||
systemExitReasonCode: Number(systemExitReasonCode),
|
||||
frameId: Number(frameId),
|
||||
};
|
||||
frameTimings.logFrame(event.frameId, "input event received on JS side");
|
||||
this.emitAsync(this.ringListeners, event);
|
||||
},
|
||||
onBatteryState: (headsetBattery: number, headsetCharging: number) => {
|
||||
@@ -134,6 +145,9 @@ export class FaceclawCommunicatorBridge {
|
||||
}
|
||||
this.emitAsync(this.frameMetricsListeners, metrics);
|
||||
},
|
||||
onFrameFinished: (frameId: number, outcome: string) => {
|
||||
this.recordFrameFinished(Number(frameId), String(outcome));
|
||||
},
|
||||
});
|
||||
this.communicator.setListener(this.listenerProxy);
|
||||
}
|
||||
@@ -201,6 +215,62 @@ export class FaceclawCommunicatorBridge {
|
||||
return this.communicator;
|
||||
}
|
||||
|
||||
private recordFrameFinished(frameId: number, outcome: string): void {
|
||||
if (!Number.isFinite(frameId) || frameId <= 0) return;
|
||||
this.finishedFrameOutcomes.set(frameId, outcome);
|
||||
while (this.finishedFrameOutcomes.size > 128) {
|
||||
const oldest = this.finishedFrameOutcomes.keys().next().value;
|
||||
if (oldest === undefined) break;
|
||||
this.finishedFrameOutcomes.delete(oldest);
|
||||
}
|
||||
const waiters = this.frameFinishedWaiters.get(frameId);
|
||||
if (waiters) {
|
||||
this.frameFinishedWaiters.delete(frameId);
|
||||
for (const waiter of waiters) {
|
||||
setTimeout(() => waiter(outcome), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve with the frame's outcome once the Java side finishes it (sent,
|
||||
* discarded, or timed out), or with null after timeoutMs. Used as transmit
|
||||
* backpressure: unlike waiting for frame metrics, this also resolves when a
|
||||
* frame is discarded (e.g. deduped as unchanged), so no-op renders do not
|
||||
* stall the render loop.
|
||||
*/
|
||||
waitForFrameFinished(frameId: number, timeoutMs: number): Promise<string | null> {
|
||||
const known = this.finishedFrameOutcomes.get(frameId);
|
||||
if (known !== undefined) return Promise.resolve(known);
|
||||
if (frameId <= 0) return Promise.resolve(null);
|
||||
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
|
||||
return new Promise((resolve) => {
|
||||
let settled = false;
|
||||
const onFinished = (outcome: string) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timeoutHandle);
|
||||
resolve(outcome);
|
||||
};
|
||||
let waiters = this.frameFinishedWaiters.get(frameId);
|
||||
if (!waiters) {
|
||||
waiters = new Set();
|
||||
this.frameFinishedWaiters.set(frameId, waiters);
|
||||
}
|
||||
waiters.add(onFinished);
|
||||
const timeoutHandle = setTimeout(() => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
const pending = this.frameFinishedWaiters.get(frameId);
|
||||
if (pending) {
|
||||
pending.delete(onFinished);
|
||||
if (pending.size === 0) this.frameFinishedWaiters.delete(frameId);
|
||||
}
|
||||
resolve(null);
|
||||
}, delayMs);
|
||||
});
|
||||
}
|
||||
|
||||
waitForNextFrameMetrics(timeoutMs: number): Promise<FrameMetrics | null> {
|
||||
const delayMs = Math.max(1, Math.round(nonNegativeNumber(timeoutMs)));
|
||||
return new Promise((resolve) => {
|
||||
@@ -229,15 +299,17 @@ export class FaceclawCommunicatorBridge {
|
||||
await this.enqueueJavaCall(() => this.communicator.setG2ScreenOn(Boolean(screenOn)));
|
||||
}
|
||||
|
||||
async submitDashboardImage(image8bpp: Uint8Array, width: number, height: number, fingerprint: string, paintMs = -1): Promise<void> {
|
||||
async submitDashboardImage(image8bpp: Uint8Array, width: number, height: number, fingerprint: string, paintMs = -1, frameId = 0): Promise<void> {
|
||||
const snapshot = new Uint8Array(image8bpp);
|
||||
await this.enqueueJavaCall(() => {
|
||||
const javaBytes = frameTimings.span(frameId, "js-to-java-copy", () => toJavaByteArray(snapshot));
|
||||
this.communicator.submitDashboardImage(
|
||||
toJavaByteArray(snapshot),
|
||||
javaBytes,
|
||||
Math.round(width),
|
||||
Math.round(height),
|
||||
fingerprint,
|
||||
Math.round(nonNegativeNumber(paintMs)),
|
||||
Math.round(nonNegativeNumber(frameId)),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* TS-side wrapper for the Java FrameTimings singleton (per-frame latency
|
||||
* instrumentation; see FrameTimings.java). Frame ID 0 means "no frame" and is
|
||||
* ignored by every method, so callers never need to null-check.
|
||||
*
|
||||
* Because the JS side is single-threaded, this module also tracks a "current
|
||||
* frame" so that leaf drawing/IO code can record spans without threading a
|
||||
* frame ID through every call signature.
|
||||
*/
|
||||
|
||||
declare const com: any;
|
||||
declare const global: any;
|
||||
|
||||
let javaInstance: any = null;
|
||||
let javaUnavailable = false;
|
||||
// Fallback IDs when the Java class is unavailable (e.g. unit tests): negative
|
||||
// so they can never collide with real Java-issued frame IDs.
|
||||
let fallbackNextFrameId = -1;
|
||||
let currentFrameId = 0;
|
||||
|
||||
function getJava(): any {
|
||||
if (javaInstance === null && !javaUnavailable) {
|
||||
try {
|
||||
if (typeof com !== "undefined" && global.isAndroid) {
|
||||
javaInstance = com.faceclaw.app.FrameTimings.getInstance();
|
||||
} else {
|
||||
javaUnavailable = true;
|
||||
}
|
||||
} catch {
|
||||
javaUnavailable = true;
|
||||
}
|
||||
}
|
||||
return javaInstance;
|
||||
}
|
||||
|
||||
export function startFrame(reason: string): number {
|
||||
const java = getJava();
|
||||
if (!java) return fallbackNextFrameId--;
|
||||
return Number(java.startFrame(reason));
|
||||
}
|
||||
|
||||
export function logFrame(frameId: number, message: string): void {
|
||||
if (frameId <= 0) return;
|
||||
getJava()?.log(frameId, message);
|
||||
}
|
||||
|
||||
export function spanStart(frameId: number, name: string): void {
|
||||
if (frameId <= 0) return;
|
||||
getJava()?.spanStart(frameId, name);
|
||||
}
|
||||
|
||||
export function spanEnd(frameId: number, name: string): void {
|
||||
if (frameId <= 0) return;
|
||||
getJava()?.spanEnd(frameId, name);
|
||||
}
|
||||
|
||||
export function finishFrame(frameId: number, outcome: string): void {
|
||||
if (frameId <= 0) return;
|
||||
getJava()?.finishFrame(frameId, outcome);
|
||||
}
|
||||
|
||||
export function span<T>(frameId: number, name: string, fn: () => T): T {
|
||||
spanStart(frameId, name);
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
spanEnd(frameId, name);
|
||||
}
|
||||
}
|
||||
|
||||
/** Set the current frame while running fn, for leaf code using the *Current helpers. */
|
||||
export function runWithFrame<T>(frameId: number, fn: () => T): T {
|
||||
const previous = currentFrameId;
|
||||
currentFrameId = frameId;
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
currentFrameId = previous;
|
||||
}
|
||||
}
|
||||
|
||||
export function currentFrame(): number {
|
||||
return currentFrameId;
|
||||
}
|
||||
|
||||
export function logCurrent(message: string): void {
|
||||
logFrame(currentFrameId, message);
|
||||
}
|
||||
|
||||
export function spanCurrent<T>(name: string, fn: () => T): T {
|
||||
return span(currentFrameId, name, fn);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { logCurrent, spanCurrent } from "./frame-timings";
|
||||
import { toUint8Array } from "../util/array-util";
|
||||
|
||||
declare const com: any;
|
||||
@@ -39,13 +40,16 @@ export function readActiveNotificationIcons(maxIcons: number): GrayImage[] {
|
||||
|
||||
const now = Date.now();
|
||||
if (now - cachedAtMs < ICON_CACHE_MS) {
|
||||
logCurrent("notification icons served from cache");
|
||||
return cachedIcons.map(icon => icon.clone());
|
||||
}
|
||||
|
||||
const bytes = toUint8Array(
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getActiveNotificationIconGrays(
|
||||
ICON_SIZE,
|
||||
maxIcons,
|
||||
const bytes = spanCurrent("fetch-notification-icons", () =>
|
||||
toUint8Array(
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getActiveNotificationIconGrays(
|
||||
ICON_SIZE,
|
||||
maxIcons,
|
||||
),
|
||||
),
|
||||
);
|
||||
const iconByteLength = ICON_SIZE * ICON_SIZE;
|
||||
@@ -65,9 +69,11 @@ export function readActiveNotificationIcons(maxIcons: number): GrayImage[] {
|
||||
export function readActiveNotifications(maxNotifications = 50): AndroidNotification[] {
|
||||
if (!global.isAndroid || maxNotifications <= 0) return [];
|
||||
try {
|
||||
const json = String(
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getActiveNotificationsJson(
|
||||
Math.max(0, Math.round(maxNotifications)),
|
||||
const json = spanCurrent("fetch-notifications-json", () =>
|
||||
String(
|
||||
com.faceclaw.app.FaceclawMediaNotificationListenerService.getActiveNotificationsJson(
|
||||
Math.max(0, Math.round(maxNotifications)),
|
||||
),
|
||||
),
|
||||
);
|
||||
const parsed = JSON.parse(json);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { spanCurrent } from "./frame-timings";
|
||||
import { toUint8Array } from "~/util/array-util";
|
||||
|
||||
declare const com: any;
|
||||
@@ -21,8 +22,10 @@ export function readSystemStatusIcons(): GrayImage[] {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return [];
|
||||
|
||||
const bytes = toUint8Array(
|
||||
com.faceclaw.app.FaceclawSystemStatusIconProvider.getSystemStatusIconGrays(context, ICON_SIZE),
|
||||
const bytes = spanCurrent("fetch-system-status-icons", () =>
|
||||
toUint8Array(
|
||||
com.faceclaw.app.FaceclawSystemStatusIconProvider.getSystemStatusIconGrays(context, ICON_SIZE),
|
||||
),
|
||||
);
|
||||
const iconByteLength = ICON_SIZE * ICON_SIZE;
|
||||
const iconCount = Math.floor(bytes.length / iconByteLength);
|
||||
|
||||
Reference in new issue
Block a user