Skip to content

Commit

Permalink
Encapsulate TracingAgent (facebook#46986)
Browse files Browse the repository at this point in the history
Summary:

# Changelog: [Internal]

Encapsulate integration with `FuseboxTracer.cpp` into `TracingAgent.cpp`, which will be responsible for handling `Tracing` domain CDP events.

Closer to the top of the stack, more changes will be added to it.

Differential Revision: D64249695
  • Loading branch information
hoxyq authored and facebook-github-bot committed Oct 14, 2024
1 parent 4dd47ee commit c90247d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 40 deletions.
46 changes: 6 additions & 40 deletions packages/react-native/ReactCommon/jsinspector-modern/HostAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

#include <chrono>

#include <reactperflogger/fusebox/FuseboxTracer.h>

using namespace std::chrono;
using namespace std::literals::string_view_literals;

Expand All @@ -36,7 +34,8 @@ HostAgent::HostAgent(
targetController_(targetController),
hostMetadata_(std::move(hostMetadata)),
sessionState_(sessionState),
networkIOAgent_(NetworkIOAgent(frontendChannel, executor)) {}
networkIOAgent_(NetworkIOAgent(frontendChannel, executor)),
tracingAgent_(TracingAgent(frontendChannel)) {}

void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
bool shouldSendOKResponse = false;
Expand Down Expand Up @@ -149,50 +148,17 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {

shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
} else if (req.method == "Tracing.start") {
// @cdp Tracing.start support is experimental.
if (FuseboxTracer::getFuseboxTracer().startTracing()) {
shouldSendOKResponse = true;
} else {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session already started"));
return;
}
isFinishedHandlingRequest = true;
} else if (req.method == "Tracing.end") {
// @cdp Tracing.end support is experimental.
bool firstChunk = true;
auto id = req.id;
bool wasStopped = FuseboxTracer::getFuseboxTracer().stopTracing(
[this, firstChunk, id](const folly::dynamic& eventsChunk) {
if (firstChunk) {
frontendChannel_(cdp::jsonResult(id));
}
frontendChannel_(cdp::jsonNotification(
"Tracing.dataCollected",
folly::dynamic::object("value", eventsChunk)));
});
if (!wasStopped) {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session not started"));
return;
}
frontendChannel_(cdp::jsonNotification(
"Tracing.tracingComplete",
folly::dynamic::object("dataLossOccurred", false)));
shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
}

if (!isFinishedHandlingRequest &&
networkIOAgent_.handleRequest(req, targetController_.getDelegate())) {
return;
}

if (!isFinishedHandlingRequest && tracingAgent_.handleRequest(req)) {
return;
}

if (!isFinishedHandlingRequest && instanceAgent_ &&
instanceAgent_->handleRequest(req)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "HostTarget.h"
#include "NetworkIOAgent.h"
#include "SessionState.h"
#include "TracingAgent.h"

#include <jsinspector-modern/InspectorInterfaces.h>
#include <jsinspector-modern/InstanceAgent.h>
Expand Down Expand Up @@ -110,6 +111,8 @@ class HostAgent final {
SessionState& sessionState_;

NetworkIOAgent networkIOAgent_;

TracingAgent tracingAgent_;
};

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "TracingAgent.h"

#include <reactperflogger/fusebox/FuseboxTracer.h>

namespace facebook::react::jsinspector_modern {

bool TracingAgent::handleRequest(const cdp::PreparsedRequest& req) {
if (req.method == "Tracing.start") {
// @cdp Tracing.start support is experimental.
if (FuseboxTracer::getFuseboxTracer().startTracing()) {
frontendChannel_(cdp::jsonResult(req.id));
} else {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session already started"));
}

return true;
} else if (req.method == "Tracing.end") {
// @cdp Tracing.end support is experimental.
bool firstChunk = true;
auto id = req.id;
bool wasStopped = FuseboxTracer::getFuseboxTracer().stopTracing(
[this, firstChunk, id](const folly::dynamic& eventsChunk) {
if (firstChunk) {
frontendChannel_(cdp::jsonResult(id));
}
frontendChannel_(cdp::jsonNotification(
"Tracing.dataCollected",
folly::dynamic::object("value", eventsChunk)));
});

if (!wasStopped) {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session not started"));
} else {
frontendChannel_(cdp::jsonNotification(
"Tracing.tracingComplete",
folly::dynamic::object("dataLossOccurred", false)));
}

frontendChannel_(cdp::jsonResult(req.id));
return true;
}

return false;
}

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once