forked from Blendman974/faceclaw
Performance optimizations
This commit is contained in:
1 parent
e49c699062
commit
f6035ea9ec
18 files changed
+715
-158
No files matched your search
@@ -182,6 +182,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// into desiredPacked; the texture-cache planner may replay them as
|
||||
// on-glasses cached draws.
|
||||
private SurfaceCompositor.ScreenDraw[] desiredDraws = new SurfaceCompositor.ScreenDraw[0];
|
||||
// (frame, reason) of the last "waiting to send" line, so a frame that
|
||||
// stalls for seconds records one line per state change (see
|
||||
// noteImageStallLocked). Send-loop thread only.
|
||||
private int stallFrameId;
|
||||
private String stallReason = "";
|
||||
// Highest compositor sequence stored as the desired frame; composites that
|
||||
// lost a store race to a newer one are discarded (their content is already
|
||||
// included in the newer composite).
|
||||
@@ -646,10 +651,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
* frame landed while briefly hidden would stay blank until its next repaint.
|
||||
*/
|
||||
public void setSurfaceVisible(String id, boolean visible) {
|
||||
// Its own frame: this recomposite is a real screen update with real
|
||||
// latency, and without one it would show up in other frames' logs only
|
||||
// as an anonymous "superseded by frame#0".
|
||||
int frameId = FrameTimings.getInstance().startFrame(
|
||||
"compositor:visible " + id + "=" + visible);
|
||||
compositor.setSurfaceVisible(id, visible);
|
||||
SurfaceCompositor.Composite composite = compositor.composite();
|
||||
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||
storeDesiredComposite(composite, packed, 0, 0);
|
||||
storeDesiredComposite(composite, packed, 0, frameId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -658,10 +668,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
* unblanking restores the previous screen content without repaints.
|
||||
*/
|
||||
public void setScreenBlanked(boolean blanked) {
|
||||
int frameId = FrameTimings.getInstance().startFrame(
|
||||
"compositor:" + (blanked ? "blank" : "unblank"));
|
||||
compositor.setBlanked(blanked);
|
||||
SurfaceCompositor.Composite composite = compositor.composite();
|
||||
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||
storeDesiredComposite(composite, packed, 0, 0);
|
||||
storeDesiredComposite(composite, packed, 0, frameId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -722,6 +734,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
java.nio.ByteBuffer glyphs
|
||||
) {
|
||||
Log.i(TAG, "Received an updated frame for surface " + surfaceId);
|
||||
FrameTimings.getInstance().log(frameId, "surface " + surfaceId + " updated rect="
|
||||
+ rectWidth + "x" + rectHeight + "+" + rectX + "+" + rectY
|
||||
+ (glyphs == null ? " (no glyph draws)" : ""));
|
||||
FrameTimings.getInstance().spanStart(frameId, "composite");
|
||||
SurfaceCompositor.Composite composite = compositor.applyAndComposite(
|
||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs);
|
||||
@@ -1702,10 +1717,19 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// A frame ready to send right now: don't inject a fresh
|
||||
// heartbeat in front of it (the image's own ack resets the
|
||||
// firmware heartbeat timer, so the heartbeat is redundant).
|
||||
// "Ready" covers both a fresh composite still to be planned
|
||||
// and a planned image already queued but not yet written --
|
||||
// enqueueing sets lastEnqueuedFingerprint, so testing only
|
||||
// the desired fingerprint left the queued-but-unwritten
|
||||
// window unprotected and a heartbeat that came due there
|
||||
// cost the frame a full ack round trip (measured 124ms on
|
||||
// frame#232 of the 2026-08-20 02:27 capture).
|
||||
boolean imageWaiting = !shutdownRequested && fixedLayoutCreated && warmedUp
|
||||
&& !hasPendingOrInflightKindLocked("heartbeat")
|
||||
&& now >= imageRetryAfterMs
|
||||
&& !getDesiredFingerprint().equals(lastEnqueuedFingerprint);
|
||||
&& (hasPendingImageLocked()
|
||||
|| !getDesiredFingerprint().equals(lastEnqueuedFingerprint));
|
||||
noteImageStallLocked(now, windowHasRoom);
|
||||
if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) {
|
||||
return ConnectionOptions.IDLE_SLEEP_MS;
|
||||
}
|
||||
@@ -2249,7 +2273,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
+ " baked=" + tex.bakedCandidates
|
||||
+ (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "")
|
||||
+ " cache=" + textureCache.usedBytes() + "B"
|
||||
+ " payload=" + tex.payload.length + "B";
|
||||
+ " payload=" + tex.payload.length + "B"
|
||||
+ " (rects " + tex.rectsMs + "ms, match " + tex.matchMs
|
||||
+ "ms, cache " + tex.cacheMs + "ms, punch " + tex.punchMs
|
||||
+ "ms, encode " + tex.encodeMs + "ms)";
|
||||
FrameTimings.getInstance().log(frameId, texLog);
|
||||
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
|
||||
return;
|
||||
@@ -2511,6 +2538,117 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
: "Glasses charging.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Record, into the frame that is waiting, why it did not go out on this
|
||||
* pass of the send loop. Without this the export shows a bare multi-second
|
||||
* jump between "image submitted as desired frame" and the first BLE
|
||||
* packet, with no hint whether we were blocked on warmup, a heartbeat, the
|
||||
* BLE window, or another message queued ahead. Deduped on (frame, reason),
|
||||
* so a frame stalled for seconds gets one line per state change rather
|
||||
* than one per loop pass.
|
||||
*/
|
||||
private void noteImageStallLocked(long now, boolean windowHasRoom) {
|
||||
int frameId;
|
||||
synchronized (desiredTilesLock) {
|
||||
frameId = desiredFrameId;
|
||||
}
|
||||
String reason;
|
||||
if (frameId != 0 && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) {
|
||||
reason = describeEnqueueBlockerLocked(now, windowHasRoom);
|
||||
} else {
|
||||
// Nothing waiting to be planned; an already-planned image may still
|
||||
// be queued behind other traffic.
|
||||
OutboundMessage queuedImage = firstPendingImageLocked();
|
||||
frameId = queuedImage == null ? 0 : imageUpdateFrameIdLocked(queuedImage.imageUpdateId);
|
||||
reason = queuedImage == null ? null : describeWriteBlockerLocked(now, windowHasRoom, queuedImage);
|
||||
}
|
||||
if (frameId == 0 || reason == null) {
|
||||
stallFrameId = 0;
|
||||
stallReason = "";
|
||||
return;
|
||||
}
|
||||
if (frameId == stallFrameId && reason.equals(stallReason)) {
|
||||
return;
|
||||
}
|
||||
stallFrameId = frameId;
|
||||
stallReason = reason;
|
||||
FrameTimings.getInstance().log(frameId, "waiting to send: " + reason);
|
||||
}
|
||||
|
||||
/** Why the desired composite has not been turned into wire messages yet, or null. */
|
||||
private String describeEnqueueBlockerLocked(long now, boolean windowHasRoom) {
|
||||
if (shutdownRequested) {
|
||||
return "shutdown requested";
|
||||
}
|
||||
if (!sessionReady) {
|
||||
return "BLE session not ready";
|
||||
}
|
||||
if (!fixedLayoutCreated) {
|
||||
return "display layout not created yet";
|
||||
}
|
||||
if (!warmedUp) {
|
||||
return "waiting for the warmup frame";
|
||||
}
|
||||
if (now < imageRetryAfterMs) {
|
||||
return "image retry backoff (" + (imageRetryAfterMs - now) + "ms left)";
|
||||
}
|
||||
if (hasPendingImageLocked()) {
|
||||
return "an earlier image is still queued";
|
||||
}
|
||||
if (!windowHasRoom) {
|
||||
return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)";
|
||||
}
|
||||
if (hasPendingOrInflightKindLocked("heartbeat")) {
|
||||
return "heartbeat in flight";
|
||||
}
|
||||
if (!pendingMessages.isEmpty()) {
|
||||
return pendingMessages.size() + " message(s) queued ahead, next "
|
||||
+ pendingMessages.peekFirst().label;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Why a planned image message has not been written to BLE yet, or null. */
|
||||
private String describeWriteBlockerLocked(long now, boolean windowHasRoom, OutboundMessage queuedImage) {
|
||||
if (!sessionReady) {
|
||||
return "BLE session not ready";
|
||||
}
|
||||
if (!windowHasRoom) {
|
||||
return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)";
|
||||
}
|
||||
if (hasPendingOrInflightKindLocked("heartbeat")) {
|
||||
return "heartbeat in flight";
|
||||
}
|
||||
// handleHeartbeat is a barrier: while one is due it holds back every
|
||||
// other write, so a frame queued at the wrong moment waits a heartbeat
|
||||
// round trip. Reported explicitly because it is otherwise invisible --
|
||||
// heartbeats belong to no frame.
|
||||
long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs;
|
||||
if (warmedUp && fixedLayoutCreated && !shutdownRequested
|
||||
&& heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS) {
|
||||
return "heartbeat due (" + heartbeatElapsedMs + "ms since the last one acked)";
|
||||
}
|
||||
OutboundMessage head = pendingMessages.peekFirst();
|
||||
if (head != null && head != queuedImage) {
|
||||
return "queued behind " + head.label;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private OutboundMessage firstPendingImageLocked() {
|
||||
for (OutboundMessage message : pendingMessages) {
|
||||
if (message.imageUpdateId > 0) {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private int imageUpdateFrameIdLocked(int imageUpdateId) {
|
||||
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(imageUpdateId);
|
||||
return stats == null ? 0 : stats.frameId;
|
||||
}
|
||||
|
||||
private void finishDesiredFrameLocked(String outcome) {
|
||||
int frameIdToFinish;
|
||||
synchronized (desiredTilesLock) {
|
||||
|
||||
@@ -14,8 +14,7 @@ import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -27,14 +26,21 @@ import java.util.Map;
|
||||
* update has been fully transmitted to the glasses, is discarded unsent, or
|
||||
* times out because nobody reported finishing it.
|
||||
*
|
||||
* All public methods are thread-safe and cheap enough to call from the BLE
|
||||
* worker thread, the Android main thread, and the JS thread. Frame IDs are
|
||||
* positive ints; 0 means "no frame" and is silently ignored everywhere, so
|
||||
* callers do not need to null-check.
|
||||
* Frames form a tree. One input event can fan out into several renders (the
|
||||
* focused app's window plus the shell chrome, say), and each of those is its
|
||||
* own frame linked back to the input frame that caused it via
|
||||
* {@link #startFrame(String, int)}. The export nests descendants under their
|
||||
* root and renders every line against the root's clock, so a single block
|
||||
* shows the whole input-to-pixels story. Latency percentiles are measured over
|
||||
* roots: root start to the first descendant that actually reached the glasses.
|
||||
*
|
||||
* Statistics (p50/p90/p99 of event-to-last-ack for sent frames) and full log
|
||||
* lines for recent and slowest frames are periodically exported to
|
||||
* getExternalFilesDir()/frame-timings.txt, retrievable via
|
||||
* All public methods are thread-safe and cheap enough to call from the BLE
|
||||
* worker thread, the Android main thread, the JS thread, and app worker
|
||||
* threads. Frame IDs are positive ints; 0 means "no frame" and is silently
|
||||
* ignored everywhere, so callers do not need to null-check.
|
||||
*
|
||||
* Statistics and full log lines for recent and slowest frames are periodically
|
||||
* exported to getExternalFilesDir()/frame-timings.txt, retrievable via
|
||||
* adb pull /sdcard/Android/data/<pkg>/files/frame-timings.txt
|
||||
*/
|
||||
public final class FrameTimings {
|
||||
@@ -45,31 +51,66 @@ public final class FrameTimings {
|
||||
private static final long FRAME_TIMEOUT_MS = 30_000;
|
||||
private static final long EXPORT_INTERVAL_MS = 15_000;
|
||||
private static final long SWEEP_INTERVAL_MS = 5_000;
|
||||
private static final int RECENT_FRAMES_KEPT = 50;
|
||||
private static final int SLOWEST_FRAMES_KEPT = 20;
|
||||
private static final int RECENT_ROOTS_KEPT = 40;
|
||||
private static final int SLOWEST_ROOTS_KEPT = 20;
|
||||
private static final int SENT_DURATIONS_WINDOW = 512;
|
||||
private static final int MAX_LINES_PER_FRAME = 200;
|
||||
/**
|
||||
* Frames retained for ID lookup. Well above the number any export can
|
||||
* reference, so a late log/span/finish call always finds its frame; frames
|
||||
* older than this are unreachable and get collected.
|
||||
*/
|
||||
private static final int FRAMES_RETAINED = 1024;
|
||||
private static final String EXPORT_FILE_NAME = "frame-timings.txt";
|
||||
|
||||
public static FrameTimings getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private static final class Line {
|
||||
final long atMs; // SystemClock.elapsedRealtime()
|
||||
final String thread;
|
||||
final String message;
|
||||
|
||||
Line(long atMs, String thread, String message) {
|
||||
this.atMs = atMs;
|
||||
this.thread = thread;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Frame {
|
||||
final int id;
|
||||
final String reason;
|
||||
final long startedAtMs; // SystemClock.elapsedRealtime()
|
||||
final long startedWallClockMs;
|
||||
final List<String> lines = new ArrayList<>();
|
||||
final Map<String, Long> openSpans = new HashMap<>();
|
||||
final Frame parent;
|
||||
final List<Frame> children = new ArrayList<>(0);
|
||||
final List<Line> lines = new ArrayList<>();
|
||||
final Map<String, Long> openSpans = new LinkedHashMap<>();
|
||||
String reason;
|
||||
long finishedAtMs;
|
||||
String outcome; // null while open; "sent", "discarded: ...", "timeout: ..."
|
||||
/** Set on the root once some frame in its subtree reached the glasses. */
|
||||
boolean subtreeSentRecorded;
|
||||
|
||||
Frame(int id, String reason, long startedAtMs, long startedWallClockMs) {
|
||||
Frame(int id, String reason, long startedAtMs, long startedWallClockMs, Frame parent) {
|
||||
this.id = id;
|
||||
this.reason = reason;
|
||||
this.startedAtMs = startedAtMs;
|
||||
this.startedWallClockMs = startedWallClockMs;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
Frame root() {
|
||||
Frame frame = this;
|
||||
while (frame.parent != null) {
|
||||
frame = frame.parent;
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
boolean isOpen() {
|
||||
return outcome == null;
|
||||
}
|
||||
|
||||
long durationMs() {
|
||||
@@ -79,13 +120,45 @@ public final class FrameTimings {
|
||||
boolean wasSent() {
|
||||
return outcome != null && outcome.startsWith("sent");
|
||||
}
|
||||
|
||||
/** Last moment anything in this subtree happened, for whole-tree duration. */
|
||||
long subtreeEndAtMs(long nowMs) {
|
||||
long end = isOpen() ? nowMs : finishedAtMs;
|
||||
for (Frame child : children) {
|
||||
end = Math.max(end, child.subtreeEndAtMs(nowMs));
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
boolean subtreeHasOpenFrame() {
|
||||
if (isOpen()) {
|
||||
return true;
|
||||
}
|
||||
for (Frame child : children) {
|
||||
if (child.subtreeHasOpenFrame()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private final Object lock = new Object();
|
||||
private final Map<Integer, Frame> activeFrames = new HashMap<>();
|
||||
private final ArrayDeque<Frame> recentFrames = new ArrayDeque<>();
|
||||
private final List<Frame> slowestSentFrames = new ArrayList<>();
|
||||
private final ArrayDeque<Long> sentDurations = new ArrayDeque<>();
|
||||
/**
|
||||
* Every frame we still retain, keyed by ID: open frames plus enough
|
||||
* history that late calls and the export can still resolve one.
|
||||
*/
|
||||
private final LinkedHashMap<Integer, Frame> framesById =
|
||||
new LinkedHashMap<Integer, Frame>(64, 0.75f, false) {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<Integer, Frame> eldest) {
|
||||
return size() > FRAMES_RETAINED && !eldest.getValue().isOpen();
|
||||
}
|
||||
};
|
||||
private final ArrayDeque<Frame> recentRoots = new ArrayDeque<>();
|
||||
private final List<Frame> slowestRoots = new ArrayList<>();
|
||||
private final ArrayDeque<Long> inputLatencies = new ArrayDeque<>();
|
||||
private final ArrayDeque<Long> renderLatencies = new ArrayDeque<>();
|
||||
private int nextFrameId = 1;
|
||||
private long framesStarted;
|
||||
private long framesSent;
|
||||
@@ -114,22 +187,56 @@ public final class FrameTimings {
|
||||
}
|
||||
}
|
||||
|
||||
/** Begin a frame; reason is a short label like "input:sys-event:0". Returns the frame ID. */
|
||||
/** Begin a root frame; reason is a short label like "input:sys-event type=0". */
|
||||
public int startFrame(String reason) {
|
||||
return startFrame(reason, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin a frame caused by an existing one (parentFrameId; 0 for a root).
|
||||
* The child is nested under its root in the export and its latency counts
|
||||
* against the root, so an input event that fans out into an app render and
|
||||
* a shell-chrome render reads as one timeline instead of three.
|
||||
*/
|
||||
public int startFrame(String reason, int parentFrameId) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
Frame frame;
|
||||
synchronized (lock) {
|
||||
frame = new Frame(nextFrameId++, reason == null ? "" : reason, now, System.currentTimeMillis());
|
||||
activeFrames.put(frame.id, frame);
|
||||
Frame parent = framesById.get(parentFrameId);
|
||||
frame = new Frame(nextFrameId++, reason == null ? "" : reason, now,
|
||||
System.currentTimeMillis(), parent);
|
||||
framesById.put(frame.id, frame);
|
||||
framesStarted++;
|
||||
if (parent != null) {
|
||||
parent.children.add(frame);
|
||||
addLineLocked(parent, now, "spawned frame#" + frame.id + " (" + frame.reason + ")");
|
||||
}
|
||||
}
|
||||
return frame.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to a frame's label, for facts only known after it started (which
|
||||
* app is in the foreground, which window a render is for). Shows up in the
|
||||
* frame's header line, so the export is scannable without reading bodies.
|
||||
*/
|
||||
public void annotate(int frameId, String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
synchronized (lock) {
|
||||
Frame frame = framesById.get(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
frame.reason = frame.reason.isEmpty() ? text : frame.reason + " " + text;
|
||||
}
|
||||
}
|
||||
|
||||
public void log(int frameId, String message) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -140,7 +247,7 @@ public final class FrameTimings {
|
||||
public void spanStart(int frameId, String name) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -152,7 +259,7 @@ public final class FrameTimings {
|
||||
public void spanEnd(int frameId, String name) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -174,7 +281,7 @@ public final class FrameTimings {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
Frame finished;
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.remove(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -188,73 +295,94 @@ public final class FrameTimings {
|
||||
/** One-line stats summary, e.g. for showing in the phone UI. */
|
||||
public String statsSummary() {
|
||||
synchronized (lock) {
|
||||
long[] percentiles = sentPercentilesLocked();
|
||||
long[] input = percentilesLocked(inputLatencies);
|
||||
return "frames started=" + framesStarted + " sent=" + framesSent
|
||||
+ " discarded=" + framesDiscarded + " timeout=" + framesTimedOut
|
||||
+ (percentiles == null
|
||||
+ (input == null
|
||||
? ""
|
||||
: " | sent-latency p50=" + percentiles[0] + "ms p90=" + percentiles[1]
|
||||
+ "ms p99=" + percentiles[2] + "ms max=" + percentiles[3] + "ms");
|
||||
: " | input-to-display p50=" + input[0] + "ms p90=" + input[1]
|
||||
+ "ms p99=" + input[2] + "ms max=" + input[3] + "ms");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** The frame with this ID if it exists and is still open, else null. */
|
||||
private Frame openFrameLocked(int frameId) {
|
||||
if (frameId <= 0) {
|
||||
return null;
|
||||
}
|
||||
Frame frame = framesById.get(frameId);
|
||||
return frame != null && frame.isOpen() ? frame : null;
|
||||
}
|
||||
|
||||
private void addLineLocked(Frame frame, long now, String message) {
|
||||
if (frame.lines.size() >= MAX_LINES_PER_FRAME) {
|
||||
if (frame.lines.size() == MAX_LINES_PER_FRAME) {
|
||||
frame.lines.add("... line cap reached");
|
||||
frame.lines.add(new Line(now, Thread.currentThread().getName(), "... line cap reached"));
|
||||
}
|
||||
return;
|
||||
}
|
||||
frame.lines.add(String.format(Locale.US, "%+6dms [%s] %s",
|
||||
now - frame.startedAtMs, Thread.currentThread().getName(), message));
|
||||
frame.lines.add(new Line(now, Thread.currentThread().getName(), message));
|
||||
}
|
||||
|
||||
private void finishFrameLocked(Frame frame, long now, String outcome) {
|
||||
frame.finishedAtMs = now;
|
||||
frame.outcome = outcome;
|
||||
for (Map.Entry<String, Long> open : frame.openSpans.entrySet()) {
|
||||
frame.lines.add(String.format(Locale.US, "%+6dms span %s never ended (started at +%dms)",
|
||||
now - frame.startedAtMs, open.getKey(), open.getValue() - frame.startedAtMs));
|
||||
frame.lines.add(new Line(now, Thread.currentThread().getName(),
|
||||
"span " + open.getKey() + " never ended (started at +"
|
||||
+ (open.getValue() - frame.startedAtMs) + "ms)"));
|
||||
}
|
||||
frame.openSpans.clear();
|
||||
addLineLocked(frame, now, "finished: " + outcome);
|
||||
|
||||
Frame root = frame.root();
|
||||
if (frame.wasSent()) {
|
||||
framesSent++;
|
||||
sentDurations.addLast(frame.durationMs());
|
||||
while (sentDurations.size() > SENT_DURATIONS_WINDOW) {
|
||||
sentDurations.removeFirst();
|
||||
// The user-visible latency is input (or timer) to the first pixels
|
||||
// that actually reached the glasses, wherever in the tree that was.
|
||||
if (!root.subtreeSentRecorded) {
|
||||
root.subtreeSentRecorded = true;
|
||||
recordLatencyLocked(root, now - root.startedAtMs);
|
||||
insertSlowestLocked(root);
|
||||
}
|
||||
insertSlowestLocked(frame);
|
||||
} else if (outcome.startsWith("timeout")) {
|
||||
framesTimedOut++;
|
||||
} else {
|
||||
framesDiscarded++;
|
||||
}
|
||||
|
||||
recentFrames.addLast(frame);
|
||||
while (recentFrames.size() > RECENT_FRAMES_KEPT) {
|
||||
recentFrames.removeFirst();
|
||||
if (root == frame) {
|
||||
recentRoots.addLast(frame);
|
||||
while (recentRoots.size() > RECENT_ROOTS_KEPT) {
|
||||
recentRoots.removeFirst();
|
||||
}
|
||||
}
|
||||
exportDirty = true;
|
||||
}
|
||||
|
||||
private void insertSlowestLocked(Frame frame) {
|
||||
slowestSentFrames.add(frame);
|
||||
Collections.sort(slowestSentFrames, (a, b) -> Long.compare(b.durationMs(), a.durationMs()));
|
||||
while (slowestSentFrames.size() > SLOWEST_FRAMES_KEPT) {
|
||||
slowestSentFrames.remove(slowestSentFrames.size() - 1);
|
||||
private void recordLatencyLocked(Frame root, long latencyMs) {
|
||||
ArrayDeque<Long> bucket = root.reason.startsWith("input:") ? inputLatencies : renderLatencies;
|
||||
bucket.addLast(latencyMs);
|
||||
while (bucket.size() > SENT_DURATIONS_WINDOW) {
|
||||
bucket.removeFirst();
|
||||
}
|
||||
}
|
||||
|
||||
/** {p50, p90, p99, max} over the rolling sent-duration window, or null if empty. */
|
||||
private long[] sentPercentilesLocked() {
|
||||
if (sentDurations.isEmpty()) {
|
||||
private void insertSlowestLocked(Frame root) {
|
||||
if (!slowestRoots.contains(root)) {
|
||||
slowestRoots.add(root);
|
||||
}
|
||||
// Sorted and trimmed at export time, when every subtree duration is final.
|
||||
}
|
||||
|
||||
/** {p50, p90, p99, max} over a rolling latency window, or null if empty. */
|
||||
private long[] percentilesLocked(ArrayDeque<Long> window) {
|
||||
if (window.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<Long> sorted = new ArrayList<>(sentDurations);
|
||||
List<Long> sorted = new ArrayList<>(window);
|
||||
Collections.sort(sorted);
|
||||
return new long[] {
|
||||
percentileOfSorted(sorted, 50),
|
||||
@@ -273,11 +401,8 @@ public final class FrameTimings {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
List<Frame> timedOut = new ArrayList<>();
|
||||
synchronized (lock) {
|
||||
Iterator<Map.Entry<Integer, Frame>> iterator = activeFrames.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Frame frame = iterator.next().getValue();
|
||||
if (now - frame.startedAtMs >= FRAME_TIMEOUT_MS) {
|
||||
iterator.remove();
|
||||
for (Frame frame : new ArrayList<>(framesById.values())) {
|
||||
if (frame.isOpen() && now - frame.startedAtMs >= FRAME_TIMEOUT_MS) {
|
||||
finishFrameLocked(frame, now, "timeout: never reported finished");
|
||||
timedOut.add(frame);
|
||||
}
|
||||
@@ -340,45 +465,103 @@ public final class FrameTimings {
|
||||
}
|
||||
|
||||
private String buildExportLocked() {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
SimpleDateFormat wallClockFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
|
||||
StringBuilder out = new StringBuilder(64 * 1024);
|
||||
out.append("FrameTimings export at ").append(wallClockFormat.format(new Date())).append('\n');
|
||||
out.append(statsSummaryLocked()).append('\n');
|
||||
out.append('\n');
|
||||
out.append("Frames are trees: an input event's own frame is the root, and the renders it\n");
|
||||
out.append("caused are indented under it with offsets measured from the root's start.\n");
|
||||
out.append('\n');
|
||||
|
||||
out.append("=== slowest sent frames ===\n");
|
||||
for (Frame frame : slowestSentFrames) {
|
||||
appendFrameLocked(out, frame, wallClockFormat);
|
||||
Collections.sort(slowestRoots, (a, b) ->
|
||||
Long.compare(b.subtreeEndAtMs(now) - b.startedAtMs, a.subtreeEndAtMs(now) - a.startedAtMs));
|
||||
while (slowestRoots.size() > SLOWEST_ROOTS_KEPT) {
|
||||
slowestRoots.remove(slowestRoots.size() - 1);
|
||||
}
|
||||
|
||||
out.append("=== slowest frames that reached the glasses ===\n");
|
||||
for (Frame frame : slowestRoots) {
|
||||
appendTreeLocked(out, frame, frame, wallClockFormat, now);
|
||||
}
|
||||
|
||||
out.append("=== recent frames (oldest first) ===\n");
|
||||
for (Frame frame : recentFrames) {
|
||||
appendFrameLocked(out, frame, wallClockFormat);
|
||||
for (Frame frame : recentRoots) {
|
||||
appendTreeLocked(out, frame, frame, wallClockFormat, now);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String statsSummaryLocked() {
|
||||
long[] percentiles = sentPercentilesLocked();
|
||||
return "frames started=" + framesStarted + " sent=" + framesSent
|
||||
+ " discarded=" + framesDiscarded + " timeout=" + framesTimedOut
|
||||
+ " open=" + activeFrames.size()
|
||||
+ (percentiles == null
|
||||
? ""
|
||||
: "\nsent-frame latency (last " + sentDurations.size() + "): p50=" + percentiles[0]
|
||||
+ "ms p90=" + percentiles[1] + "ms p99=" + percentiles[2]
|
||||
+ "ms max=" + percentiles[3] + "ms");
|
||||
long[] input = percentilesLocked(inputLatencies);
|
||||
long[] render = percentilesLocked(renderLatencies);
|
||||
StringBuilder out = new StringBuilder();
|
||||
out.append("frames started=").append(framesStarted).append(" sent=").append(framesSent)
|
||||
.append(" discarded=").append(framesDiscarded).append(" timeout=").append(framesTimedOut)
|
||||
.append(" open=").append(openFrameCountLocked());
|
||||
appendPercentilesLocked(out, "input-to-display latency", inputLatencies.size(), input);
|
||||
appendPercentilesLocked(out, "render-to-display latency", renderLatencies.size(), render);
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private void appendFrameLocked(StringBuilder out, Frame frame, SimpleDateFormat wallClockFormat) {
|
||||
out.append("frame#").append(frame.id)
|
||||
.append(" [").append(frame.reason).append(']')
|
||||
.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs)))
|
||||
.append(" duration ").append(frame.durationMs()).append("ms")
|
||||
.append(" outcome ").append(frame.outcome)
|
||||
.append('\n');
|
||||
for (String line : frame.lines) {
|
||||
out.append(" ").append(line).append('\n');
|
||||
private void appendPercentilesLocked(StringBuilder out, String label, int count, long[] percentiles) {
|
||||
if (percentiles == null) {
|
||||
return;
|
||||
}
|
||||
out.append('\n').append(label).append(" (last ").append(count).append("): p50=")
|
||||
.append(percentiles[0]).append("ms p90=").append(percentiles[1])
|
||||
.append("ms p99=").append(percentiles[2]).append("ms max=").append(percentiles[3]).append("ms");
|
||||
}
|
||||
|
||||
private int openFrameCountLocked() {
|
||||
int open = 0;
|
||||
for (Frame frame : framesById.values()) {
|
||||
if (frame.isOpen()) {
|
||||
open++;
|
||||
}
|
||||
}
|
||||
return open;
|
||||
}
|
||||
|
||||
/** Print a frame and its descendants, all timed against root's start. */
|
||||
private void appendTreeLocked(
|
||||
StringBuilder out, Frame frame, Frame root, SimpleDateFormat wallClockFormat, long now) {
|
||||
int depth = 0;
|
||||
for (Frame walk = frame; walk != root; walk = walk.parent) {
|
||||
depth++;
|
||||
}
|
||||
String indent = repeat(" ", depth);
|
||||
long startOffsetMs = frame.startedAtMs - root.startedAtMs;
|
||||
out.append(indent).append("frame#").append(frame.id)
|
||||
.append(" [").append(frame.reason).append(']');
|
||||
if (frame == root) {
|
||||
out.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs)));
|
||||
long totalMs = frame.subtreeEndAtMs(now) - frame.startedAtMs;
|
||||
out.append(" duration ").append(frame.durationMs()).append("ms");
|
||||
if (!frame.children.isEmpty()) {
|
||||
out.append(" (tree ").append(totalMs).append("ms")
|
||||
.append(frame.subtreeHasOpenFrame() ? ", still open" : "").append(')');
|
||||
}
|
||||
} else {
|
||||
out.append(" started +").append(startOffsetMs).append("ms")
|
||||
.append(" duration ").append(frame.isOpen() ? "open" : (frame.durationMs() + "ms"));
|
||||
}
|
||||
out.append(" outcome ").append(frame.outcome == null ? "(open)" : frame.outcome).append('\n');
|
||||
for (Line line : frame.lines) {
|
||||
out.append(indent).append(String.format(Locale.US, " %+7dms [%s] %s",
|
||||
line.atMs - root.startedAtMs, line.thread, line.message)).append('\n');
|
||||
}
|
||||
for (Frame child : frame.children) {
|
||||
appendTreeLocked(out, child, root, wallClockFormat, now);
|
||||
}
|
||||
}
|
||||
|
||||
private static String repeat(String unit, int times) {
|
||||
StringBuilder out = new StringBuilder(unit.length() * times);
|
||||
for (int i = 0; i < times; i++) {
|
||||
out.append(unit);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
+27
-1
@@ -65,6 +65,19 @@ public final class TexturePlanner {
|
||||
public final int bakedCandidates;
|
||||
public final int uploadBytes;
|
||||
public final boolean fullFrame;
|
||||
/**
|
||||
* Where the planning time went, in ms. This runs on the send thread,
|
||||
* directly in the input-to-display path, so the split is reported in
|
||||
* the frame log: rects = finding the changed region, match = checking
|
||||
* each draw against the composite, cache = residency/uploads plus run
|
||||
* building, punch = clearing replayed draws out of a copy of the
|
||||
* frame, encode = compressing the leftover pixels.
|
||||
*/
|
||||
public int rectsMs;
|
||||
public int matchMs;
|
||||
public int cacheMs;
|
||||
public int punchMs;
|
||||
public int encodeMs;
|
||||
|
||||
Result(byte[] payload, List<byte[]> uploads, int nextFid, int rectCount,
|
||||
int drawnGlyphs, int runCount, int drawnImages, int fwGlyphs, int fwRuns,
|
||||
@@ -121,6 +134,7 @@ public final class TexturePlanner {
|
||||
if (next.length != stride * height) {
|
||||
return null;
|
||||
}
|
||||
long planStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
|
||||
// Rect geometry, matching what the plain paths would send.
|
||||
boolean fullFrame = previous == null || previous.length != next.length;
|
||||
@@ -143,6 +157,8 @@ public final class TexturePlanner {
|
||||
rects = Collections.singletonList(new int[]{0, 0, width, height});
|
||||
}
|
||||
|
||||
long matchStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
|
||||
// Candidates: draws whose written pixels intersect a sent rect, are
|
||||
// representable on the wire, and land correct against the composite.
|
||||
List<Selected> selected = new ArrayList<>();
|
||||
@@ -210,6 +226,7 @@ public final class TexturePlanner {
|
||||
if (selected.isEmpty() && fwSubs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
long cacheStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
|
||||
// Residency: ensure every selected draw is in the cache, retrying once
|
||||
// after a reset when the bump allocator fills. A draw that still fails
|
||||
@@ -244,6 +261,7 @@ public final class TexturePlanner {
|
||||
|
||||
// Punch the drawn pixels out of a copy of the composite, then encode
|
||||
// the rect deltas from the punched content.
|
||||
long punchStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
byte[] punched = next.clone();
|
||||
for (Selected sel : drawn) {
|
||||
punch(punched, stride, width, height, sel);
|
||||
@@ -252,6 +270,7 @@ public final class TexturePlanner {
|
||||
punchFw(punched, stride, width, height, fw);
|
||||
}
|
||||
|
||||
long encodeStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
List<byte[]> subs = new ArrayList<>();
|
||||
int fid = fidStart;
|
||||
if (fullFrame) {
|
||||
@@ -271,9 +290,16 @@ public final class TexturePlanner {
|
||||
|
||||
int uploadBytes = cache.pendingUploadBytes();
|
||||
List<byte[]> uploads = cache.drainUploadPayloads(UPLOAD_PAYLOAD_MAX);
|
||||
return new Result(payload, uploads, fid, rects.size(), drawnGlyphs.size(), runs.size(),
|
||||
Result result = new Result(payload, uploads, fid, rects.size(), drawnGlyphs.size(), runs.size(),
|
||||
imageDrawable.size(), fwGlyphCount, fwSubs.size(),
|
||||
bakedCandidates, uploadBytes, fullFrame);
|
||||
long doneAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
result.rectsMs = (int) (matchStartedAtMs - planStartedAtMs);
|
||||
result.matchMs = (int) (cacheStartedAtMs - matchStartedAtMs);
|
||||
result.cacheMs = (int) (punchStartedAtMs - cacheStartedAtMs);
|
||||
result.punchMs = (int) (encodeStartedAtMs - punchStartedAtMs);
|
||||
result.encodeMs = (int) (doneAtMs - encodeStartedAtMs);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** One punch task for an emitted firmware-font glyph. */
|
||||
|
||||
Reference in new issue
Block a user