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
@@ -101,6 +101,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private String desiredFingerprint = "";
|
||||
private byte[] desiredBmp = new byte[0];
|
||||
private int desiredPaintMs;
|
||||
private int desiredFrameId;
|
||||
|
||||
private final ArrayDeque<OutboundMessage> pendingMessages = new ArrayDeque<>();
|
||||
private final ArrayDeque<OutboundMessage> inFlightMessages = new ArrayDeque<>();
|
||||
@@ -109,6 +110,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
|
||||
public FaceclawBleCommunicator(Context context, String rightAddress, String leftAddress, String ringAddress) {
|
||||
this.appContext = context.getApplicationContext();
|
||||
FrameTimings.getInstance().init(appContext);
|
||||
this.powerManager = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE);
|
||||
this.bleManager = new FaceclawBleManager(appContext);
|
||||
this.bleManager.setListener(this);
|
||||
@@ -229,17 +231,27 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
int width,
|
||||
int height,
|
||||
String fingerprint,
|
||||
int paintMs
|
||||
int paintMs,
|
||||
int frameId
|
||||
) {
|
||||
Log.i(TAG, "Received an updated frame ");
|
||||
// Convert the full-screen 8bpp grayscale buffer into the BMP wire format here so
|
||||
// that all framing concerns live on the Java side.
|
||||
FrameTimings.getInstance().spanStart(frameId, "bmp-convert");
|
||||
byte[] bmp = image8bpp == null ? new byte[0] : BmpUtil.build4bppBmp(image8bpp, width, height);
|
||||
FrameTimings.getInstance().spanEnd(frameId, "bmp-convert");
|
||||
int supersededFrameId;
|
||||
synchronized (desiredTilesLock) {
|
||||
supersededFrameId = desiredFrameId;
|
||||
desiredBmp = bmp;
|
||||
desiredFingerprint = fingerprint == null ? "" : fingerprint;
|
||||
desiredPaintMs = paintMs;
|
||||
desiredFrameId = frameId;
|
||||
}
|
||||
if (supersededFrameId != 0 && supersededFrameId != frameId) {
|
||||
finishFrame(supersededFrameId, "discarded: superseded by frame#" + frameId + " before send");
|
||||
}
|
||||
FrameTimings.getInstance().log(frameId, "image submitted as desired frame");
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
@@ -452,7 +464,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
if (event != null) {
|
||||
emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode);
|
||||
int frameId = FrameTimings.getInstance().startFrame(
|
||||
"input:" + event.kind + " type=" + event.eventType + " src=" + event.eventSource);
|
||||
FrameTimings.getInstance().log(frameId, "input event decoded from BLE notification");
|
||||
emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode, frameId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,7 +485,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
lastConnectionOrInputAtMs = arrivalMs;
|
||||
}
|
||||
logLine("direct ring " + decoded.label + " " + decoded.detail + " raw=" + hex(data));
|
||||
emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode);
|
||||
int frameId = FrameTimings.getInstance().startFrame("input:ring:" + decoded.label);
|
||||
FrameTimings.getInstance().log(frameId, "input event decoded from direct ring notification");
|
||||
emitRingEvent(event.kind, event.containerName, event.eventType, event.eventSource, event.systemExitReasonCode, frameId);
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
@@ -729,6 +746,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
OutboundMessage messageToPrewrite = null;
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
|
||||
maybeFinishNoChangeDesiredFrame();
|
||||
|
||||
synchronized (lock) {
|
||||
if (!inFlightMessages.isEmpty()) {
|
||||
OutboundMessage oldest = inFlightMessages.peekFirst();
|
||||
@@ -808,6 +827,26 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the desired image already matches what the glasses display, nothing will
|
||||
* ever be enqueued for it, so finish its frame now (otherwise the TS side
|
||||
* would block on it until its backpressure timeout).
|
||||
*/
|
||||
private void maybeFinishNoChangeDesiredFrame() {
|
||||
int frameIdToFinish = 0;
|
||||
synchronized (lock) {
|
||||
synchronized (desiredTilesLock) {
|
||||
if (desiredFrameId != 0 && !displayedFingerprint.isEmpty() && desiredFingerprint.equals(displayedFingerprint)) {
|
||||
frameIdToFinish = desiredFrameId;
|
||||
desiredFrameId = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (frameIdToFinish != 0) {
|
||||
finishFrame(frameIdToFinish, "discarded: no change from displayed image");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean shouldBlockPrewriteForHeartbeatLocked(long now) {
|
||||
if (shutdownRequested || !warmedUp || !fixedLayoutCreated) {
|
||||
return false;
|
||||
@@ -1041,14 +1080,18 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (message.imageUpdateId <= 0) {
|
||||
return;
|
||||
}
|
||||
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId);
|
||||
int frameId = stats == null ? 0 : stats.frameId;
|
||||
if (message.imageMessageNumber == 1) {
|
||||
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(message.imageUpdateId);
|
||||
if (stats != null && stats.firstWriteStartedAtMs <= 0) {
|
||||
stats.firstWriteStartedAtMs = message.writeStartedAtMs > 0 ? message.writeStartedAtMs : message.sentAtMs;
|
||||
}
|
||||
FrameTimings.getInstance().log(frameId, "first bluetooth packet sent");
|
||||
logImageUpdateLandmarkLocked("first bluetooth message sent", message, message.sentAtMs);
|
||||
}
|
||||
if (message.imageMessageNumber == message.imageMessageCount) {
|
||||
FrameTimings.getInstance().log(frameId,
|
||||
"last bluetooth packet sent (message " + message.imageMessageNumber + "/" + message.imageMessageCount + ")");
|
||||
logImageUpdateLandmarkLocked("last bluetooth message sent", message, message.sentAtMs);
|
||||
}
|
||||
}
|
||||
@@ -1062,9 +1105,23 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (stats != null && stats.firstWriteStartedAtMs > 0) {
|
||||
emitFrameMetrics(stats.paintMs, (int) Math.max(0, ackedAtMs - stats.firstWriteStartedAtMs), stats.tileCount);
|
||||
}
|
||||
if (stats != null) {
|
||||
finishFrame(stats.frameId, "sent");
|
||||
}
|
||||
logImageUpdateLandmarkLocked("last bluetooth message acked", message, ackedAtMs);
|
||||
}
|
||||
|
||||
/** Remove the stats entry for an image update that will not complete, finishing its frame. */
|
||||
private void discardImageUpdateStatsLocked(int imageUpdateId, String reason) {
|
||||
if (imageUpdateId <= 0) {
|
||||
return;
|
||||
}
|
||||
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.remove(imageUpdateId);
|
||||
if (stats != null) {
|
||||
finishFrame(stats.frameId, "discarded: " + reason);
|
||||
}
|
||||
}
|
||||
|
||||
private void logImageUpdateLandmarkLocked(String event, OutboundMessage message, long elapsedMs) {
|
||||
logLine("image update#" + message.imageUpdateId + " " + event
|
||||
+ " at " + timestamp(elapsedMs)
|
||||
@@ -1155,30 +1212,38 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
String fingerprint = getDesiredFingerprint();
|
||||
byte[] bmp;
|
||||
int paintMs;
|
||||
int frameId;
|
||||
synchronized (desiredTilesLock) {
|
||||
bmp = desiredBmp;
|
||||
paintMs = desiredPaintMs;
|
||||
frameId = desiredFrameId;
|
||||
desiredFrameId = 0;
|
||||
}
|
||||
if (bmp == null) {
|
||||
bmp = new byte[0];
|
||||
}
|
||||
if (Arrays.equals(bmp, displayedBmp)) {
|
||||
displayedFingerprint = fingerprint;
|
||||
finishFrame(frameId, "discarded: image content identical to displayed");
|
||||
return;
|
||||
}
|
||||
|
||||
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
|
||||
BleImageOptimizer.TileImagePlan plan =
|
||||
new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId());
|
||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
||||
FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan");
|
||||
|
||||
int updateId = nextImageUpdateId++;
|
||||
int messageCount = plan.fragments.size();
|
||||
imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1));
|
||||
imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId));
|
||||
for (int i = 0; i < plan.fragments.size(); i++) {
|
||||
BleProtocol.ImageFragment fragment = plan.fragments.get(i);
|
||||
enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, i + 1, messageCount, true);
|
||||
}
|
||||
|
||||
FrameTimings.getInstance().log(frameId, "queued image update#" + updateId
|
||||
+ " messages=" + messageCount + " payload=" + plan.payload.length + "B");
|
||||
logLine("queue image update#" + updateId + " fingerprint=" + fingerprint
|
||||
+ " messages=" + messageCount);
|
||||
}
|
||||
@@ -1221,7 +1286,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
};
|
||||
message.onTimeout = () -> {
|
||||
imageUpdateStats.remove(message.imageUpdateId);
|
||||
discardImageUpdateStatsLocked(message.imageUpdateId, "image ack timeout (will retry)");
|
||||
clearMessagesOfKindLocked("image");
|
||||
displayedFingerprint = "";
|
||||
displayedBmp = new byte[0];
|
||||
@@ -1325,7 +1390,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
pendingIterator.remove();
|
||||
discardPrewriteIfMatchesLocked(message);
|
||||
if ("image".equals(kind)) {
|
||||
imageUpdateStats.remove(message.imageUpdateId);
|
||||
discardImageUpdateStatsLocked(message.imageUpdateId, "pending messages cleared (" + kind + ")");
|
||||
}
|
||||
magicPool.release(message.sid, message.magic, message.label, "cleared pending " + kind);
|
||||
}
|
||||
@@ -1336,7 +1401,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (kind.equals(message.kind)) {
|
||||
inFlightIterator.remove();
|
||||
if ("image".equals(kind)) {
|
||||
imageUpdateStats.remove(message.imageUpdateId);
|
||||
discardImageUpdateStatsLocked(message.imageUpdateId, "inflight messages cleared (" + kind + ")");
|
||||
}
|
||||
magicPool.release(message.sid, message.magic, message.label, "cleared inflight " + kind);
|
||||
}
|
||||
@@ -1352,6 +1417,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
while (!pendingMessages.isEmpty()) {
|
||||
var message = pendingMessages.removeFirst();
|
||||
discardPrewriteIfMatchesLocked(message);
|
||||
discardImageUpdateStatsLocked(message.imageUpdateId, "pending messages cleared: " + reason);
|
||||
magicPool.release(message.sid, message.magic, message.label, "cleared pending: " + reason);
|
||||
}
|
||||
}
|
||||
@@ -1359,6 +1425,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private void clearInFlightMessagesLocked(String reason) {
|
||||
while (!inFlightMessages.isEmpty()) {
|
||||
var message = inFlightMessages.removeFirst();
|
||||
discardImageUpdateStatsLocked(message.imageUpdateId, "inflight messages cleared: " + reason);
|
||||
magicPool.release(message.sid, message.magic, message.label, "cleared inflight: " + reason);
|
||||
}
|
||||
}
|
||||
@@ -1457,17 +1524,39 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return false;
|
||||
}
|
||||
|
||||
private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode) {
|
||||
private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId) {
|
||||
final FaceclawBleCommunicatorListener current = listener;
|
||||
if (current == null) {
|
||||
FrameTimings.getInstance().finishFrame(frameId, "discarded: no listener attached");
|
||||
return;
|
||||
}
|
||||
final String containerNameSnapshot = containerName == null ? "" : containerName;
|
||||
mainHandler.post(() -> {
|
||||
FrameTimings.getInstance().log(frameId, "dispatching input event on main thread");
|
||||
try {
|
||||
current.onRingEvent(kind, containerNameSnapshot, eventType, eventSource, systemExitReasonCode);
|
||||
current.onRingEvent(kind, containerNameSnapshot, eventType, eventSource, systemExitReasonCode, frameId);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onRingEvent failed", t);
|
||||
FrameTimings.getInstance().finishFrame(frameId, "discarded: listener onRingEvent failed");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Finish a frame owned by the communicator and tell the TS side, which may be awaiting it. */
|
||||
private void finishFrame(int frameId, String outcome) {
|
||||
if (frameId <= 0) {
|
||||
return;
|
||||
}
|
||||
FrameTimings.getInstance().finishFrame(frameId, outcome);
|
||||
final FaceclawBleCommunicatorListener current = listener;
|
||||
if (current == null) {
|
||||
return;
|
||||
}
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
current.onFrameFinished(frameId, outcome);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "listener onFrameFinished failed", t);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+2
-1
@@ -3,8 +3,9 @@ package com.faceclaw.app;
|
||||
public interface FaceclawBleCommunicatorListener {
|
||||
void onLog(String line);
|
||||
void onStateChange(String phase, String status);
|
||||
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode);
|
||||
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId);
|
||||
void onBatteryState(int headsetBattery, int headsetCharging);
|
||||
void onEvenAppConflict(String message);
|
||||
void onFrameMetrics(int paintMs, int transmitMs, int tileCount);
|
||||
void onFrameFinished(int frameId, String outcome);
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
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.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Per-frame timing instrumentation, shared between the Java BLE layer and the
|
||||
* Typescript UI layer. A "frame" starts when we receive an input event (or a
|
||||
* timer fires and we decide to redraw), and finishes when the resulting screen
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* adb pull /sdcard/Android/data/<pkg>/files/frame-timings.txt
|
||||
*/
|
||||
public final class FrameTimings {
|
||||
private static final String TAG = "FrameTimings";
|
||||
private static final FrameTimings INSTANCE = new FrameTimings();
|
||||
|
||||
/** Frames still open after this long are finished as "timeout". */
|
||||
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 SENT_DURATIONS_WINDOW = 512;
|
||||
private static final int MAX_LINES_PER_FRAME = 200;
|
||||
private static final String EXPORT_FILE_NAME = "frame-timings.txt";
|
||||
|
||||
public static FrameTimings getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
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<>();
|
||||
long finishedAtMs;
|
||||
String outcome; // null while open; "sent", "discarded: ...", "timeout: ..."
|
||||
|
||||
Frame(int id, String reason, long startedAtMs, long startedWallClockMs) {
|
||||
this.id = id;
|
||||
this.reason = reason;
|
||||
this.startedAtMs = startedAtMs;
|
||||
this.startedWallClockMs = startedWallClockMs;
|
||||
}
|
||||
|
||||
long durationMs() {
|
||||
return finishedAtMs - startedAtMs;
|
||||
}
|
||||
|
||||
boolean wasSent() {
|
||||
return outcome != null && outcome.startsWith("sent");
|
||||
}
|
||||
}
|
||||
|
||||
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<>();
|
||||
private int nextFrameId = 1;
|
||||
private long framesStarted;
|
||||
private long framesSent;
|
||||
private long framesDiscarded;
|
||||
private long framesTimedOut;
|
||||
private boolean exportDirty;
|
||||
private File exportDir;
|
||||
private Thread exportThread;
|
||||
|
||||
private FrameTimings() {
|
||||
}
|
||||
|
||||
/** Idempotent; enables filesystem export. Safe to call from any constructor path. */
|
||||
public void init(Context context) {
|
||||
File dir = context.getApplicationContext().getExternalFilesDir(null);
|
||||
if (dir == null) {
|
||||
dir = context.getApplicationContext().getFilesDir();
|
||||
}
|
||||
synchronized (lock) {
|
||||
exportDir = dir;
|
||||
if (exportThread == null) {
|
||||
exportThread = new Thread(this::exportLoop, "FrameTimingsExport");
|
||||
exportThread.setDaemon(true);
|
||||
exportThread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Begin a frame; reason is a short label like "input:sys-event:0". Returns the frame ID. */
|
||||
public int startFrame(String reason) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
Frame frame;
|
||||
synchronized (lock) {
|
||||
frame = new Frame(nextFrameId++, reason == null ? "" : reason, now, System.currentTimeMillis());
|
||||
activeFrames.put(frame.id, frame);
|
||||
framesStarted++;
|
||||
}
|
||||
return frame.id;
|
||||
}
|
||||
|
||||
public void log(int frameId, String message) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
addLineLocked(frame, now, message);
|
||||
}
|
||||
}
|
||||
|
||||
public void spanStart(int frameId, String name) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
frame.openSpans.put(name, now);
|
||||
addLineLocked(frame, now, "span " + name + " start");
|
||||
}
|
||||
}
|
||||
|
||||
public void spanEnd(int frameId, String name) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
Long startedAt = frame.openSpans.remove(name);
|
||||
if (startedAt == null) {
|
||||
addLineLocked(frame, now, "span " + name + " end (start not recorded)");
|
||||
} else {
|
||||
addLineLocked(frame, now, "span " + name + " end (" + (now - startedAt) + "ms)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish a frame. Outcomes: "sent" (counted in latency percentiles), anything
|
||||
* starting with "discarded", or "timeout". First finish wins; later calls for
|
||||
* the same frame are ignored, so racing completion paths are safe.
|
||||
*/
|
||||
public void finishFrame(int frameId, String outcome) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
Frame finished;
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.remove(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
finishFrameLocked(frame, now, outcome == null ? "discarded: no outcome given" : outcome);
|
||||
finished = frame;
|
||||
}
|
||||
Log.i(TAG, "frame#" + finished.id + " [" + finished.reason + "] -> " + finished.outcome
|
||||
+ " in " + finished.durationMs() + "ms");
|
||||
}
|
||||
|
||||
/** One-line stats summary, e.g. for showing in the phone UI. */
|
||||
public String statsSummary() {
|
||||
synchronized (lock) {
|
||||
long[] percentiles = sentPercentilesLocked();
|
||||
return "frames started=" + framesStarted + " sent=" + framesSent
|
||||
+ " discarded=" + framesDiscarded + " timeout=" + framesTimedOut
|
||||
+ (percentiles == null
|
||||
? ""
|
||||
: " | sent-latency p50=" + percentiles[0] + "ms p90=" + percentiles[1]
|
||||
+ "ms p99=" + percentiles[2] + "ms max=" + percentiles[3] + "ms");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
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");
|
||||
}
|
||||
return;
|
||||
}
|
||||
frame.lines.add(String.format(Locale.US, "%+6dms [%s] %s",
|
||||
now - frame.startedAtMs, 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.openSpans.clear();
|
||||
addLineLocked(frame, now, "finished: " + outcome);
|
||||
|
||||
if (frame.wasSent()) {
|
||||
framesSent++;
|
||||
sentDurations.addLast(frame.durationMs());
|
||||
while (sentDurations.size() > SENT_DURATIONS_WINDOW) {
|
||||
sentDurations.removeFirst();
|
||||
}
|
||||
insertSlowestLocked(frame);
|
||||
} else if (outcome.startsWith("timeout")) {
|
||||
framesTimedOut++;
|
||||
} else {
|
||||
framesDiscarded++;
|
||||
}
|
||||
|
||||
recentFrames.addLast(frame);
|
||||
while (recentFrames.size() > RECENT_FRAMES_KEPT) {
|
||||
recentFrames.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);
|
||||
}
|
||||
}
|
||||
|
||||
/** {p50, p90, p99, max} over the rolling sent-duration window, or null if empty. */
|
||||
private long[] sentPercentilesLocked() {
|
||||
if (sentDurations.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<Long> sorted = new ArrayList<>(sentDurations);
|
||||
Collections.sort(sorted);
|
||||
return new long[] {
|
||||
percentileOfSorted(sorted, 50),
|
||||
percentileOfSorted(sorted, 90),
|
||||
percentileOfSorted(sorted, 99),
|
||||
sorted.get(sorted.size() - 1),
|
||||
};
|
||||
}
|
||||
|
||||
private static long percentileOfSorted(List<Long> sorted, int percentile) {
|
||||
int index = (int) Math.ceil(percentile / 100.0 * sorted.size()) - 1;
|
||||
return sorted.get(Math.max(0, Math.min(sorted.size() - 1, index)));
|
||||
}
|
||||
|
||||
private void sweepTimedOutFrames() {
|
||||
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();
|
||||
finishFrameLocked(frame, now, "timeout: never reported finished");
|
||||
timedOut.add(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Frame frame : timedOut) {
|
||||
Log.w(TAG, "frame#" + frame.id + " [" + frame.reason + "] timed out after "
|
||||
+ frame.durationMs() + "ms without being finished");
|
||||
}
|
||||
}
|
||||
|
||||
private void exportLoop() {
|
||||
long lastExportAtMs = 0;
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(SWEEP_INTERVAL_MS);
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
sweepTimedOutFrames();
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
boolean shouldExport;
|
||||
synchronized (lock) {
|
||||
shouldExport = exportDirty && exportDir != null && now - lastExportAtMs >= EXPORT_INTERVAL_MS;
|
||||
}
|
||||
if (shouldExport) {
|
||||
lastExportAtMs = now;
|
||||
exportToFile();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "export loop error", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void exportToFile() {
|
||||
String content;
|
||||
File dir;
|
||||
synchronized (lock) {
|
||||
dir = exportDir;
|
||||
if (dir == null) {
|
||||
return;
|
||||
}
|
||||
content = buildExportLocked();
|
||||
exportDirty = false;
|
||||
}
|
||||
File target = new File(dir, EXPORT_FILE_NAME);
|
||||
File temp = new File(dir, EXPORT_FILE_NAME + ".tmp");
|
||||
try {
|
||||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(temp), StandardCharsets.UTF_8)) {
|
||||
writer.write(content);
|
||||
}
|
||||
if (!temp.renameTo(target)) {
|
||||
Log.w(TAG, "failed to rename " + temp + " to " + target);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to export frame timings", t);
|
||||
}
|
||||
}
|
||||
|
||||
private String buildExportLocked() {
|
||||
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("=== slowest sent frames ===\n");
|
||||
for (Frame frame : slowestSentFrames) {
|
||||
appendFrameLocked(out, frame, wallClockFormat);
|
||||
}
|
||||
|
||||
out.append("=== recent frames (oldest first) ===\n");
|
||||
for (Frame frame : recentFrames) {
|
||||
appendFrameLocked(out, frame, wallClockFormat);
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -129,11 +129,13 @@ public final class BleImageOptimizer {
|
||||
public static final class ImageUpdateStats {
|
||||
final int paintMs;
|
||||
final int tileCount;
|
||||
final int frameId;
|
||||
long firstWriteStartedAtMs;
|
||||
|
||||
ImageUpdateStats(int paintMs, int tileCount) {
|
||||
ImageUpdateStats(int paintMs, int tileCount, int frameId) {
|
||||
this.paintMs = Math.max(0, paintMs);
|
||||
this.tileCount = tileCount;
|
||||
this.frameId = frameId;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user