Skip to content

Commit

Permalink
Report JavaScript thread (facebook#49395)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#49395

# Changelog: [Internal]

Adding a new method to `RuntimeTarget` that will register it for Tracing.

In our case, it will schedule a callback on JS executor that will register JavaScript thread with `PerformanceTracer`.

Reviewed By: huntie

Differential Revision: D69530984
  • Loading branch information
hoxyq authored and facebook-github-bot committed Feb 27, 2025
1 parent 71a7842 commit 13fc4d2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ void InstanceAgent::maybeSendPendingConsoleMessages() {

void InstanceAgent::startTracing() {
if (runtimeAgent_) {
runtimeAgent_->registerForTracing();
runtimeAgent_->enableSamplingProfiler();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
#include "SessionState.h"

#include <jsinspector-modern/InspectorInterfaces.h>
#include <jsinspector-modern/InstanceTarget.h>
#include <jsinspector-modern/RuntimeAgent.h>
#include <jsinspector-modern/tracing/InstanceTracingProfile.h>

#include <functional>

namespace facebook::react::jsinspector_modern {

class InstanceTarget;

/**
* An Agent that handles requests from the Chrome DevTools Protocol for the
* given InstanceTarget.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ RuntimeAgent::~RuntimeAgent() {
sessionState_.lastRuntimeAgentExportedState = getExportedState();
}

void RuntimeAgent::registerForTracing() {
targetController_.registerForTracing();
}

void RuntimeAgent::enableSamplingProfiler() {
targetController_.enableSamplingProfiler();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class RuntimeAgent final {
*/
ExportedState getExportedState();

/**
* Registers the corresponding RuntimeTarget for Tracing: might enable some
* capabilities that will be later used in Tracing Profile.
*/
void registerForTracing();

/**
* Start sampling profiler for the corresponding RuntimeTarget.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "SessionState.h"

#include <jsinspector-modern/RuntimeTarget.h>
#include <jsinspector-modern/tracing/PerformanceTracer.h>

using namespace facebook::jsi;

Expand Down Expand Up @@ -159,6 +160,10 @@ void RuntimeTargetController::notifyDebuggerSessionDestroyed() {
target_.emitDebuggerSessionDestroyed();
}

void RuntimeTargetController::registerForTracing() {
target_.registerForTracing();
}

void RuntimeTargetController::enableSamplingProfiler() {
target_.enableSamplingProfiler();
}
Expand All @@ -172,6 +177,12 @@ RuntimeTargetController::collectSamplingProfile() {
return target_.collectSamplingProfile();
}

void RuntimeTarget::registerForTracing() {
jsExecutor_([](auto& /*runtime*/) {
PerformanceTracer::getInstance().reportJavaScriptThread();
});
}

void RuntimeTarget::enableSamplingProfiler() {
delegate_.enableSamplingProfiler();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ class RuntimeTargetController {
*/
void notifyDebuggerSessionDestroyed();

/**
* Registers the corresponding RuntimeTarget for Tracing: might enable some
* capabilities that will be later used in Tracing Profile.
*/
void registerForTracing();

/**
* Start sampling profiler for the corresponding RuntimeTarget.
*/
Expand Down Expand Up @@ -202,6 +208,12 @@ class JSINSPECTOR_EXPORT RuntimeTarget
FrontendChannel channel,
SessionState& sessionState);

/**
* Registers this Runtime for Tracing: might enable some
* capabilities that will be later used in Tracing Profile.
*/
void registerForTracing();

/**
* Start sampling profiler for a particular JavaScript runtime.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ void PerformanceTracer::reportProcess(uint64_t id, const std::string& name) {
});
}

void PerformanceTracer::reportJavaScriptThread() {
reportThread(oscompat::getCurrentThreadId(), "JavaScript");
}

void PerformanceTracer::reportThread(uint64_t id, const std::string& name) {
if (!tracing_) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class PerformanceTracer {
*/
void reportThread(uint64_t id, const std::string& name);

/**
* Should only be called from the JavaScript thread, will buffer metadata
* Trace Event.
*/
void reportJavaScriptThread();

private:
PerformanceTracer();
PerformanceTracer(const PerformanceTracer&) = delete;
Expand Down

0 comments on commit 13fc4d2

Please sign in to comment.