Emit stashed trace to an active Fusebox client (#53771)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53771

# Changelog: [Internal]

Instead of opening DevTools every time we emit a background trace, we are going to check if there is an active session with Fusebox client and will send it to the first one registered.

Reviewed By: huntie

Differential Revision: D82321146

fbshipit-source-id: 46b4d090ae9a6f8b4fc98181b303ff552c561eb8
This commit is contained in:
Ruslan Lesiutin authored and Facebook GitHub Bot committed 2025-09-15 12:45:53 -07:00
1 parent 1b6d3c9ce1
commit 01b4749f6a
10 files changed
+142 -24

No files matched your search

@@ -387,10 +387,14 @@ public abstract class DevSupportManagerBase(
TracingState.ENABLEDINBACKGROUNDMODE ->
DevOptionHandler {
UiThreadUtil.runOnUiThread {
if (reactInstanceDevHelper is PerfMonitorDevHelper)
reactInstanceDevHelper.inspectorTarget?.pauseAndAnalyzeBackgroundTrace()
if (reactInstanceDevHelper is PerfMonitorDevHelper) {
reactInstanceDevHelper.inspectorTarget?.let {
if (it.pauseAndAnalyzeBackgroundTrace()) {
openDebugger(DebuggerFrontendPanelName.PERFORMANCE.toString())
}
}
}
}
openDebugger(DebuggerFrontendPanelName.PERFORMANCE.toString())
}
TracingState.DISABLED ->
DevOptionHandler {
@@ -17,8 +17,11 @@ internal interface PerfMonitorInspectorTargetBinding {
/** Get the current CDP or background performance tracing state. */
public fun getTracingState(): TracingState
/** Attempt to pause the current background performance trace, and open in DevTools. */
public fun pauseAndAnalyzeBackgroundTrace()
/**
* Attempt to pause the current background performance trace, and open in DevTools. Returns true
* if there is an active session that can display the trace, false otherwise.
*/
public fun pauseAndAnalyzeBackgroundTrace(): Boolean
/** Attempt to start a new background performance trace. */
public fun resumeBackgroundTrace()
@@ -68,8 +68,11 @@ internal class PerfMonitorOverlayManager(
private fun handleRecordingButtonPress() {
when (tracingState) {
TracingState.ENABLEDINBACKGROUNDMODE -> {
devHelper.inspectorTarget?.pauseAndAnalyzeBackgroundTrace()
onRequestOpenDevTools()
devHelper.inspectorTarget?.let {
if (!it.pauseAndAnalyzeBackgroundTrace()) {
onRequestOpenDevTools()
}
}
}
TracingState.DISABLED -> {
devHelper.inspectorTarget?.resumeBackgroundTrace()
@@ -37,7 +37,7 @@ internal class ReactHostInspectorTarget(reactHostImpl: ReactHostImpl) :
external fun startBackgroundTrace(): Boolean
external fun stopAndStashBackgroundTrace()
external fun stopAndMaybeEmitBackgroundTrace(): Boolean
external fun stopAndDiscardBackgroundTrace()
@@ -51,11 +51,13 @@ internal class ReactHostInspectorTarget(reactHostImpl: ReactHostImpl) :
perfMonitorListeners.add(listener)
}
override fun pauseAndAnalyzeBackgroundTrace() {
stopAndStashBackgroundTrace()
override fun pauseAndAnalyzeBackgroundTrace(): Boolean {
val emitted = stopAndMaybeEmitBackgroundTrace()
perfMonitorListeners.forEach { listener ->
listener.onRecordingStateChanged(TracingState.DISABLED)
}
return emitted
}
override fun resumeBackgroundTrace() {
@@ -157,9 +157,16 @@ tracing::TraceRecordingState JReactHostInspectorTarget::stopTracing() {
}
}
void JReactHostInspectorTarget::stopAndStashBackgroundTrace() {
jboolean JReactHostInspectorTarget::stopAndMaybeEmitBackgroundTrace() {
auto capturedTrace = inspectorTarget_->stopTracing();
if (inspectorTarget_->hasActiveSessionWithFuseboxClient()) {
inspectorTarget_->emitTraceRecordingForFirstFuseboxClient(
std::move(capturedTrace));
return jboolean(true);
}
stashTraceRecordingState(std::move(capturedTrace));
return jboolean(false);
}
void JReactHostInspectorTarget::stopAndDiscardBackgroundTrace() {
@@ -188,8 +195,8 @@ void JReactHostInspectorTarget::registerNatives() {
"startBackgroundTrace",
JReactHostInspectorTarget::startBackgroundTrace),
makeNativeMethod(
"stopAndStashBackgroundTrace",
JReactHostInspectorTarget::stopAndStashBackgroundTrace),
"stopAndMaybeEmitBackgroundTrace",
JReactHostInspectorTarget::stopAndMaybeEmitBackgroundTrace),
makeNativeMethod(
"stopAndDiscardBackgroundTrace",
JReactHostInspectorTarget::stopAndDiscardBackgroundTrace),
@@ -82,17 +82,6 @@ class JReactHostInspectorTarget
static void registerNatives();
void sendDebuggerResumeCommand();
/**
* Starts a background trace recording for this HostTarget.
*
* \return false if already tracing, true otherwise.
*/
bool startBackgroundTrace();
/**
* Stops previously started trace recording and stashes the captured trace,
* which will be emitted the next time CDP session is created.
*/
void stopAndStashBackgroundTrace();
/**
* Get the state of the background trace: running, stopped, or disabled
* Background tracing will be disabled if there is no metro connection or if
@@ -101,6 +90,20 @@ class JReactHostInspectorTarget
* \return the background trace state
*/
jint tracingState();
/**
* Starts a background trace recording for this HostTarget.
*
* \return false if already tracing, true otherwise.
*/
bool startBackgroundTrace();
/**
* Stops previously started trace recording and:
* - If there is an active CDP session with Fusebox client enabled, emits the
* trace and returns true.
* - Otherwise, stashes the captured trace, that will be emitted when the CDP
* session is initialized. Returns false.
*/
jboolean stopAndMaybeEmitBackgroundTrace();
/**
* Stops previously started trace recording and discards the captured trace.
*/
@@ -340,6 +340,18 @@ class HostAgent::Impl final {
}
}
bool hasFuseboxClientConnected() const {
return fuseboxClientType_ == FuseboxClientType::Fusebox;
}
void emitExternalTraceRecording(
tracing::TraceRecordingState traceRecording) const {
assert(
hasFuseboxClientConnected() &&
"Attempted to emit a trace recording to a non-Fusebox client");
tracingAgent_.emitExternalTraceRecording(std::move(traceRecording));
}
private:
enum class FuseboxClientType { Unknown, Fusebox, NonFusebox };
@@ -440,6 +452,11 @@ class HostAgent::Impl final {
void handleRequest(const cdp::PreparsedRequest& req) {}
void setCurrentInstanceAgent(std::shared_ptr<InstanceAgent> agent) {}
bool hasFuseboxClientConnected() const {
return false;
}
void emitExternalTraceRecording(tracing::TraceRecordingState traceRecording) {
}
};
#endif // REACT_NATIVE_DEBUGGER_ENABLED
@@ -469,6 +486,15 @@ void HostAgent::setCurrentInstanceAgent(
impl_->setCurrentInstanceAgent(std::move(instanceAgent));
}
bool HostAgent::hasFuseboxClientConnected() const {
return impl_->hasFuseboxClientConnected();
}
void HostAgent::emitExternalTraceRecording(
tracing::TraceRecordingState traceRecording) const {
impl_->emitExternalTraceRecording(std::move(traceRecording));
}
#pragma mark - Tracing
HostTracingAgent::HostTracingAgent(tracing::TraceRecordingState& state)
@@ -66,6 +66,20 @@ class HostAgent final {
*/
void setCurrentInstanceAgent(std::shared_ptr<InstanceAgent> agent);
/**
* Returns whether this HostAgent is part of the session that has an active
* Fusebox client connecte, i.e. with Chrome DevTools Frontend fork for React
* Native.
*/
bool hasFuseboxClientConnected() const;
/**
* Emits the trace recording that was captured externally, not via the
* CDP-initiated request.
*/
void emitExternalTraceRecording(
tracing::TraceRecordingState traceRecording) const;
private:
// We use the private implementation idiom to ensure this class has the same
// layout regardless of whether REACT_NATIVE_DEBUGGER_ENABLED is defined. The
@@ -101,6 +101,19 @@ class HostTargetSession {
}
}
/**
* Returns whether the ReactNativeApplication CDP domain is enabled.
*
* Chrome DevTools Frontend enables this domain as a client.
*/
bool hasFuseboxClient() const {
return hostAgent_.hasFuseboxClientConnected();
}
void emitTraceRecording(tracing::TraceRecordingState traceRecording) const {
hostAgent_.emitExternalTraceRecording(std::move(traceRecording));
}
private:
// Owned by this instance, but shared (weakly) with the frontend channel
std::shared_ptr<RAIIRemoteConnection> remote_;
@@ -344,4 +357,32 @@ folly::dynamic createHostMetadataPayload(const HostTargetMetadata& metadata) {
return result;
}
bool HostTarget::hasActiveSessionWithFuseboxClient() const {
bool hasActiveFuseboxSession = false;
sessions_.forEach([&](HostTargetSession& session) {
hasActiveFuseboxSession |= session.hasFuseboxClient();
});
return hasActiveFuseboxSession;
}
void HostTarget::emitTraceRecordingForFirstFuseboxClient(
tracing::TraceRecordingState traceRecording) const {
bool emitted = false;
sessions_.forEach([&](HostTargetSession& session) {
if (emitted) {
/**
* TraceRecordingState object is not copiable for performance reasons,
* because it could contain large Runtime sampling profile object.
*
* This approach would not work with multi-client debugger setup.
*/
return;
}
if (session.hasFuseboxClient()) {
session.emitTraceRecording(std::move(traceRecording));
emitted = true;
}
});
}
} // namespace facebook::react::jsinspector_modern
@@ -297,6 +297,21 @@ class JSINSPECTOR_EXPORT HostTarget
*/
tracing::TracingState tracingState() const;
/**
* Returns whether there is an active session with the Fusebox client, i.e.
* with Chrome DevTools Frontend fork for React Native.
*/
bool hasActiveSessionWithFuseboxClient() const;
/**
* Emits the trace recording for the first active session with the Fusebox
* client.
*
* @see \c hasActiveFrontendSession
*/
void emitTraceRecordingForFirstFuseboxClient(
tracing::TraceRecordingState traceRecording) const;
private:
/**
* Constructs a new HostTarget.