Skip to content

Commit d839e4a

Browse files
Xin Chenfacebook-github-bot
authored andcommitted
Call C++ ReactMarker from android side to log platform specific timing (#38321)
Summary: Pull Request resolved: #38321 This diff adds the native method in `ReactMarker.java` file so that any logs in the android side will call into C++. Given that currently the C++ uses the native implementation from hosting platforms, this allows any call (either from C++ or android) will end up calling the C++ method after logging is done. The motivation of this change is to allow us to collect timing information from hosting platforms, and report back to JS performance startup API. We will have items that are logged before the JNI part is loaded, and those will be cached and sent over in the next diff. Changelog: [Android][Internal] - Implemented native method for Android LogMarker API to report timing values to C++. Reviewed By: mdvacca Differential Revision: D43806115 fbshipit-source-id: 11497bdc0af8d1b0299a797c636bb7af7a6ddda4
1 parent eadbfb8 commit d839e4a

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactMarker.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,15 @@ public static void logMarker(ReactMarkerConstants name, @Nullable String tag, in
140140
for (MarkerListener listener : sListeners) {
141141
listener.logMarker(name, tag, instanceKey);
142142
}
143+
144+
if (ReactBridge.isInitialized()) {
145+
nativeLogMarker(name.name(), SystemClock.uptimeMillis());
146+
}
143147
}
144148

149+
@DoNotStrip
150+
private static native void nativeLogMarker(String markerName, long markerTime);
151+
145152
@DoNotStrip
146153
public static void setAppStartTime(long appStartTime) {
147154
sAppStartTime = appStartTime;

packages/react-native/ReactAndroid/src/main/jni/react/jni/JReactMarker.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void JReactMarker::logPerfMarkerBridgeless(
6363
}
6464

6565
void JReactMarker::logPerfMarkerWithInstanceKey(
66-
const facebook::react::ReactMarker::ReactMarkerId markerId,
66+
const ReactMarker::ReactMarkerId markerId,
6767
const char *tag,
6868
const int instanceKey) {
6969
switch (markerId) {
@@ -109,4 +109,46 @@ double JReactMarker::getAppStartTime() {
109109
return meth(cls);
110110
}
111111

112+
void JReactMarker::nativeLogMarker(
113+
jni::alias_ref<jclass> /* unused */,
114+
std::string markerNameStr,
115+
jlong markerTime) {
116+
// TODO: refactor this to a bidirectional map along with
117+
// logPerfMarkerWithInstanceKey
118+
if (markerNameStr == "RUN_JS_BUNDLE_START") {
119+
ReactMarker::logMarkerDone(
120+
ReactMarker::RUN_JS_BUNDLE_START, (double)markerTime);
121+
} else if (markerNameStr == "RUN_JS_BUNDLE_END") {
122+
ReactMarker::logMarkerDone(
123+
ReactMarker::RUN_JS_BUNDLE_STOP, (double)markerTime);
124+
} else if (markerNameStr == "CREATE_REACT_CONTEXT_END") {
125+
ReactMarker::logMarkerDone(
126+
ReactMarker::CREATE_REACT_CONTEXT_STOP, (double)markerTime);
127+
} else if (markerNameStr == "loadApplicationScript_startStringConvert") {
128+
ReactMarker::logMarkerDone(
129+
ReactMarker::JS_BUNDLE_STRING_CONVERT_START, (double)markerTime);
130+
} else if (markerNameStr == "loadApplicationScript_endStringConvert") {
131+
ReactMarker::logMarkerDone(
132+
ReactMarker::JS_BUNDLE_STRING_CONVERT_STOP, (double)markerTime);
133+
} else if (markerNameStr == "NATIVE_MODULE_SETUP_START") {
134+
ReactMarker::logMarkerDone(
135+
ReactMarker::NATIVE_MODULE_SETUP_START, (double)markerTime);
136+
} else if (markerNameStr == "NATIVE_MODULE_SETUP_END") {
137+
ReactMarker::logMarkerDone(
138+
ReactMarker::NATIVE_MODULE_SETUP_STOP, (double)markerTime);
139+
} else if (markerNameStr == "REGISTER_JS_SEGMENT_START") {
140+
ReactMarker::logMarkerDone(
141+
ReactMarker::REGISTER_JS_SEGMENT_START, (double)markerTime);
142+
} else if (markerNameStr == "REGISTER_JS_SEGMENT_STOP") {
143+
ReactMarker::logMarkerDone(
144+
ReactMarker::REGISTER_JS_SEGMENT_STOP, (double)markerTime);
145+
}
146+
}
147+
148+
void JReactMarker::registerNatives() {
149+
javaClassLocal()->registerNatives({
150+
makeNativeMethod("nativeLogMarker", JReactMarker::nativeLogMarker),
151+
});
152+
}
153+
112154
} // namespace facebook::react

packages/react-native/ReactAndroid/src/main/jni/react/jni/JReactMarker.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class JReactMarker : public facebook::jni::JavaClass<JReactMarker> {
1818
public:
1919
static constexpr auto kJavaDescriptor =
2020
"Lcom/facebook/react/bridge/ReactMarker;";
21+
static void registerNatives();
2122
static void setLogPerfMarkerIfNeeded();
2223

2324
private:
@@ -38,6 +39,10 @@ class JReactMarker : public facebook::jni::JavaClass<JReactMarker> {
3839
const char *tag,
3940
const int instanceKey);
4041
static double getAppStartTime();
42+
static void nativeLogMarker(
43+
jni::alias_ref<jclass> /* unused */,
44+
std::string markerNameStr,
45+
jlong markerTime);
4146
};
4247

4348
} // namespace facebook::react

packages/react-native/ReactAndroid/src/main/jni/react/jni/OnLoad.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "CatalystInstanceImpl.h"
1717
#include "CxxModuleWrapper.h"
1818
#include "JCallback.h"
19+
#include "JReactMarker.h"
1920
#include "JavaScriptExecutorHolder.h"
2021
#include "ProxyExecutor.h"
2122
#include "WritableNativeArray.h"
@@ -85,6 +86,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
8586
NativeMap::registerNatives();
8687
ReadableNativeMap::registerNatives();
8788
WritableNativeMap::registerNatives();
89+
JReactMarker::registerNatives();
8890

8991
#ifdef WITH_INSPECTOR
9092
JInspector::registerNatives();

packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ void logMarker(const ReactMarkerId markerId) {
2929
}
3030

3131
void logTaggedMarker(const ReactMarkerId markerId, const char *tag) {
32-
StartupLogger::getInstance().logStartupEvent(markerId);
3332
logTaggedMarkerImpl(markerId, tag);
3433
}
3534

@@ -38,27 +37,31 @@ void logMarkerBridgeless(const ReactMarkerId markerId) {
3837
}
3938

4039
void logTaggedMarkerBridgeless(const ReactMarkerId markerId, const char *tag) {
41-
StartupLogger::getInstance().logStartupEvent(markerId);
4240
logTaggedMarkerBridgelessImpl(markerId, tag);
4341
}
4442

43+
void logMarkerDone(const ReactMarkerId markerId, double markerTime) {
44+
StartupLogger::getInstance().logStartupEvent(markerId, markerTime);
45+
}
46+
4547
StartupLogger &StartupLogger::getInstance() {
4648
static StartupLogger instance;
4749
return instance;
4850
}
4951

50-
void StartupLogger::logStartupEvent(const ReactMarkerId markerId) {
51-
auto now = JSExecutor::performanceNow();
52+
void StartupLogger::logStartupEvent(
53+
const ReactMarkerId markerId,
54+
double markerTime) {
5255
switch (markerId) {
5356
case ReactMarkerId::RUN_JS_BUNDLE_START:
5457
if (runJSBundleStartTime == 0) {
55-
runJSBundleStartTime = now;
58+
runJSBundleStartTime = markerTime;
5659
}
5760
return;
5861

5962
case ReactMarkerId::RUN_JS_BUNDLE_STOP:
6063
if (runJSBundleEndTime == 0) {
61-
runJSBundleEndTime = now;
64+
runJSBundleEndTime = markerTime;
6265
}
6366
return;
6467

packages/react-native/ReactCommon/cxxreact/ReactMarker.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class StartupLogger {
7171
public:
7272
static StartupLogger &getInstance();
7373

74-
void logStartupEvent(const ReactMarker::ReactMarkerId markerId);
74+
void logStartupEvent(const ReactMarkerId markerName, double markerTime);
7575
double getAppStartTime();
7676
double getRunJSBundleStartTime();
7777
double getRunJSBundleEndTime();
@@ -85,5 +85,12 @@ class StartupLogger {
8585
double runJSBundleEndTime;
8686
};
8787

88+
// When the marker got logged from the platform, it will notify here. This is
89+
// used to collect react markers that are logged in the platform instead of in
90+
// C++.
91+
extern RN_EXPORT void logMarkerDone(
92+
const ReactMarkerId markerId,
93+
double markerTime);
94+
8895
} // namespace ReactMarker
8996
} // namespace facebook::react

0 commit comments

Comments
 (0)