Skip to content

Commit 8dddff5

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Refactor logging of Fabric commit statistics
Summary: This is a refactor of the logging of Fabric commit statistics to simplify the way we track performance points. we'll later refactor and iterate on the API to integrate fabric perf point into developer tools changelog: [internal] internal Reviewed By: ShikaSD Differential Revision: D34006700 fbshipit-source-id: 93a01accd90dfacc8b44edd158033b442a843284
1 parent 6ab5bb6 commit 8dddff5

File tree

2 files changed

+138
-17
lines changed

2 files changed

+138
-17
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.fabric;
9+
10+
import static com.facebook.react.bridge.ReactMarkerConstants.*;
11+
12+
import androidx.annotation.Nullable;
13+
import com.facebook.common.logging.FLog;
14+
import com.facebook.react.bridge.ReactMarker;
15+
import com.facebook.react.bridge.ReactMarkerConstants;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
public class ConsoleReactFabricPerfLogger implements ReactMarker.FabricMarkerListener {
20+
21+
private final Map<Integer, FabricCommitPoint> mFabricCommitMarkers = new HashMap<>();
22+
23+
private static class FabricCommitPoint {
24+
public long commitStart;
25+
public long commitEnd;
26+
public long finishTransactionStart;
27+
public long finishTransactionEnd;
28+
public long diffStart;
29+
public long diffEnd;
30+
public long updateUIMainThreadEnd;
31+
public long layoutStart;
32+
public long layoutEnd;
33+
public long batchExecutionStart;
34+
public long batchExecutionEnd;
35+
public long updateUIMainThreadStart;
36+
}
37+
38+
@Override
39+
public void logFabricMarker(
40+
ReactMarkerConstants name, @Nullable String tag, int instanceKey, long timestamp) {
41+
42+
if (isFabricCommitMarker(name)) {
43+
FabricCommitPoint commitPoint = mFabricCommitMarkers.get(instanceKey);
44+
if (commitPoint == null) {
45+
commitPoint = new FabricCommitPoint();
46+
mFabricCommitMarkers.put(instanceKey, commitPoint);
47+
}
48+
updateFabricCommitPoint(name, commitPoint, timestamp);
49+
50+
if (name == ReactMarkerConstants.FABRIC_BATCH_EXECUTION_END) {
51+
FLog.i(
52+
FabricUIManager.TAG,
53+
"Statistic of Fabric commit #: "
54+
+ instanceKey
55+
+ "\n - Total commit time: "
56+
+ (commitPoint.finishTransactionEnd - commitPoint.commitStart)
57+
+ " ms.\n - Layout: "
58+
+ (commitPoint.layoutEnd - commitPoint.layoutStart)
59+
+ " ms.\n - Diffing: "
60+
+ (commitPoint.diffEnd - commitPoint.diffStart)
61+
+ " ms.\n"
62+
+ " - FinishTransaction (Diffing + Processing + Serialization of MutationInstructions): "
63+
+ (commitPoint.finishTransactionEnd - commitPoint.finishTransactionStart)
64+
+ " ms.\n"
65+
+ " - Mounting: "
66+
+ (commitPoint.batchExecutionEnd - commitPoint.batchExecutionStart)
67+
+ " ms.");
68+
mFabricCommitMarkers.remove(instanceKey);
69+
}
70+
}
71+
}
72+
73+
private static boolean isFabricCommitMarker(ReactMarkerConstants name) {
74+
return name == FABRIC_COMMIT_START
75+
|| name == FABRIC_COMMIT_END
76+
|| name == FABRIC_FINISH_TRANSACTION_START
77+
|| name == FABRIC_FINISH_TRANSACTION_END
78+
|| name == FABRIC_DIFF_START
79+
|| name == FABRIC_DIFF_END
80+
|| name == FABRIC_LAYOUT_START
81+
|| name == FABRIC_LAYOUT_END
82+
|| name == FABRIC_BATCH_EXECUTION_START
83+
|| name == FABRIC_BATCH_EXECUTION_END
84+
|| name == FABRIC_UPDATE_UI_MAIN_THREAD_START
85+
|| name == FABRIC_UPDATE_UI_MAIN_THREAD_END;
86+
}
87+
88+
private static void updateFabricCommitPoint(
89+
ReactMarkerConstants name, FabricCommitPoint commitPoint, long timestamp) {
90+
switch (name) {
91+
case FABRIC_COMMIT_START:
92+
commitPoint.commitStart = timestamp;
93+
break;
94+
case FABRIC_COMMIT_END:
95+
commitPoint.commitEnd = timestamp;
96+
break;
97+
case FABRIC_FINISH_TRANSACTION_START:
98+
commitPoint.finishTransactionStart = timestamp;
99+
break;
100+
case FABRIC_FINISH_TRANSACTION_END:
101+
commitPoint.finishTransactionEnd = timestamp;
102+
break;
103+
case FABRIC_DIFF_START:
104+
commitPoint.diffStart = timestamp;
105+
break;
106+
case FABRIC_DIFF_END:
107+
commitPoint.diffEnd = timestamp;
108+
break;
109+
case FABRIC_LAYOUT_START:
110+
commitPoint.layoutStart = timestamp;
111+
break;
112+
case FABRIC_LAYOUT_END:
113+
commitPoint.layoutEnd = timestamp;
114+
break;
115+
case FABRIC_BATCH_EXECUTION_START:
116+
commitPoint.batchExecutionStart = timestamp;
117+
break;
118+
case FABRIC_BATCH_EXECUTION_END:
119+
commitPoint.batchExecutionEnd = timestamp;
120+
break;
121+
case FABRIC_UPDATE_UI_MAIN_THREAD_START:
122+
commitPoint.updateUIMainThreadStart = timestamp;
123+
break;
124+
case FABRIC_UPDATE_UI_MAIN_THREAD_END:
125+
commitPoint.updateUIMainThreadEnd = timestamp;
126+
break;
127+
}
128+
}
129+
}

ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public class FabricUIManager implements UIManager, LifecycleEventListener {
105105
ReactFeatureFlags.enableFabricLogs
106106
|| PrinterHolder.getPrinter()
107107
.shouldDisplayLogMessage(ReactDebugOverlayTags.FABRIC_UI_MANAGER);
108+
public ConsoleReactFabricPerfLogger mConsoleReactFabricPerfLogger;
108109

109110
static {
110111
FabricSoLoader.staticInit();
@@ -364,6 +365,10 @@ public void stopSurface(final int surfaceID) {
364365
public void initialize() {
365366
mEventDispatcher.registerEventEmitter(FABRIC, new FabricEventEmitter(this));
366367
mEventDispatcher.addBatchEventDispatchedListener(mEventBeatManager);
368+
if (ENABLE_FABRIC_LOGS) {
369+
mConsoleReactFabricPerfLogger = new ConsoleReactFabricPerfLogger();
370+
ReactMarker.addFabricListener(mConsoleReactFabricPerfLogger);
371+
}
367372
}
368373

369374
// This is called on the JS thread (see CatalystInstanceImpl).
@@ -373,6 +378,10 @@ public void initialize() {
373378
public void onCatalystInstanceDestroy() {
374379
FLog.i(TAG, "FabricUIManager.onCatalystInstanceDestroy");
375380

381+
if (mConsoleReactFabricPerfLogger != null) {
382+
ReactMarker.removeFabricListener(mConsoleReactFabricPerfLogger);
383+
}
384+
376385
if (mDestroyed) {
377386
ReactSoftExceptionLogger.logSoftException(
378387
FabricUIManager.TAG, new IllegalStateException("Cannot double-destroy FabricUIManager"));
@@ -767,23 +776,6 @@ private void scheduleMountItem(
767776
ReactMarker.logFabricMarker(
768777
ReactMarkerConstants.FABRIC_LAYOUT_END, null, commitNumber, layoutEndTime);
769778
ReactMarker.logFabricMarker(ReactMarkerConstants.FABRIC_COMMIT_END, null, commitNumber);
770-
771-
if (ENABLE_FABRIC_LOGS) {
772-
FLog.i(
773-
TAG,
774-
"Statistic of Fabric commit #: "
775-
+ commitNumber
776-
+ "\n - Total commit time: "
777-
+ (finishTransactionEndTime - commitStartTime)
778-
+ " ms.\n - Layout: "
779-
+ mLayoutTime
780-
+ " ms.\n - Diffing: "
781-
+ (diffEndTime - diffStartTime)
782-
+ " ms.\n"
783-
+ " - FinishTransaction (Diffing + Processing + Serialization of MountingInstructions): "
784-
+ mFinishTransactionCPPTime
785-
+ " ms.");
786-
}
787779
}
788780
}
789781

0 commit comments

Comments
 (0)