Propagate console.timeStamp entries to Perfetto (#52542)

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

Changelog: [internal]

This adds support to propagate performance entries logged to `console.timeStamp` to Perfetto, if enabled. It also modifies the Perfetto integration to support track groups, in addition to track names.

Reviewed By: hoxyq

Differential Revision: D78092596

fbshipit-source-id: 76b0fe2738d856fd75ee941de223d6a6a73d8e1c
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2025-07-11 08:41:01 -07:00
1 parent 0021dafc51
commit 6f6945e64f
5 files changed
+67 -15

No files matched your search

+1 -1
View File
@@ -166,7 +166,7 @@ let reactJsInspector = RNTarget(
name: .reactJsInspector,
path: "ReactCommon/jsinspector-modern",
excludedPaths: ["tracing", "network", "tests"],
dependencies: [.reactNativeDependencies, .reactFeatureFlags, .jsi, .reactJsInspectorTracing, .reactJsInspectorNetwork, .reactRuntimeExecutor],
dependencies: [.reactNativeDependencies, .reactFeatureFlags, .jsi, .reactJsInspectorTracing, .reactJsInspectorNetwork, .reactRuntimeExecutor, .reactPerfLogger],
defines: [
CXXSetting.define("REACT_NATIVE_DEBUGGER_ENABLED", to: "1", .when(configuration: BuildConfiguration.debug)),
CXXSetting.define("REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY", to: "1", .when(configuration: BuildConfiguration.debug)),
@@ -25,6 +25,7 @@ target_link_libraries(jsinspector
jsinspector_tracing
react_featureflags
runtimeexecutor
reactperflogger
)
target_compile_reactnative_options(jsinspector PRIVATE)
target_compile_options(jsinspector PRIVATE
@@ -8,6 +8,7 @@
#include <jsinspector-modern/RuntimeTarget.h>
#include <jsinspector-modern/tracing/PerformanceTracer.h>
#include <reactperflogger/ReactPerfettoLogger.h>
#include <concepts>
#include <deque>
#include <string>
@@ -19,6 +20,20 @@ namespace facebook::react::jsinspector_modern {
namespace {
inline std::optional<HighResTimeStamp> getTimeStampForPerfetto(
const std::optional<tracing::ConsoleTimeStampEntry>& consoleTimeStampEntry,
const HighResTimeStamp now) {
if (consoleTimeStampEntry) {
if (std::holds_alternative<HighResTimeStamp>(*consoleTimeStampEntry)) {
return std::get<HighResTimeStamp>(*consoleTimeStampEntry);
} else {
return std::nullopt;
}
} else {
return now;
}
}
struct ConsoleState {
/**
* https://console.spec.whatwg.org/#counting
@@ -376,7 +391,8 @@ void consoleTimeStamp(
const jsi::Value* arguments,
size_t argumentsCount) {
auto& performanceTracer = tracing::PerformanceTracer::getInstance();
if (!performanceTracer.isTracing() || argumentsCount == 0) {
if ((!performanceTracer.isTracing() && !ReactPerfettoLogger::isTracing()) ||
argumentsCount == 0) {
// If not tracing, just early return to avoid the cost of parsing.
return;
}
@@ -444,8 +460,22 @@ void consoleTimeStamp(
}
}
performanceTracer.reportTimeStamp(
label, start, end, trackName, trackGroup, color);
if (performanceTracer.isTracing()) {
performanceTracer.reportTimeStamp(
label, start, end, trackName, trackGroup, color);
}
if (ReactPerfettoLogger::isTracing()) {
std::optional<HighResTimeStamp> perfettoStart =
getTimeStampForPerfetto(start, now);
std::optional<HighResTimeStamp> perfettoEnd =
getTimeStampForPerfetto(end, now);
if (perfettoStart && perfettoEnd) {
ReactPerfettoLogger::measure(
label, *perfettoStart, *perfettoEnd, trackName, trackGroup);
}
}
}
/*
@@ -22,10 +22,26 @@ const std::string PERFETTO_DEFAULT_TRACK_NAME = "# Web Performance";
const std::string PERFETTO_TRACK_NAME_PREFIX = "# Web Performance: ";
std::string toPerfettoTrackName(
const std::optional<std::string_view>& trackName) {
return trackName.has_value()
? PERFETTO_TRACK_NAME_PREFIX + std::string(trackName.value())
: PERFETTO_DEFAULT_TRACK_NAME;
const std::optional<std::string_view>& trackName,
const std::optional<std::string_view>& trackGroup) {
std::string perfettoTrackName = PERFETTO_DEFAULT_TRACK_NAME;
if (trackName || trackGroup) {
perfettoTrackName += ": ";
if (trackGroup) {
perfettoTrackName += *trackGroup;
if (trackName) {
perfettoTrackName += " | ";
}
}
if (trackName) {
perfettoTrackName += *trackName;
}
}
return perfettoTrackName;
}
#elif defined(WITH_FBSYSTRACE)
int64_t getDeltaNanos(HighResTimeStamp jsTime) {
@@ -50,10 +66,12 @@ int64_t getDeltaNanos(HighResTimeStamp jsTime) {
const std::string_view& eventName,
HighResTimeStamp startTime,
HighResTimeStamp endTime,
const std::optional<std::string_view>& trackName) {
const std::optional<std::string_view>& trackName,
const std::optional<std::string_view>& trackGroup) {
#if defined(WITH_PERFETTO)
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
auto track = getPerfettoWebPerfTrackAsync(toPerfettoTrackName(trackName));
auto track = getPerfettoWebPerfTrackAsync(
toPerfettoTrackName(trackName, trackGroup));
TRACE_EVENT_BEGIN(
"react-native",
perfetto::DynamicString(eventName.data(), eventName.size()),
@@ -75,13 +93,14 @@ int64_t getDeltaNanos(HighResTimeStamp jsTime) {
/* static */ void ReactPerfettoLogger::mark(
const std::string_view& eventName,
HighResTimeStamp startTime,
const std::optional<std::string_view>& trackName) {
const std::optional<std::string_view>& trackName,
const std::optional<std::string_view>& trackGroup) {
#if defined(WITH_PERFETTO)
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
TRACE_EVENT_INSTANT(
"react-native",
perfetto::DynamicString(eventName.data(), eventName.size()),
getPerfettoWebPerfTrackSync(toPerfettoTrackName(trackName)),
getPerfettoWebPerfTrackSync(toPerfettoTrackName(trackName, trackGroup)),
highResTimeStampToPerfettoTraceTime(startTime));
}
#elif defined(WITH_FBSYSTRACE)
@@ -11,7 +11,7 @@
#include <reactperflogger/ReactPerfettoCategories.h>
#include <optional>
#include <string>
#include <string_view>
namespace facebook::react {
@@ -26,13 +26,15 @@ class ReactPerfettoLogger {
static void mark(
const std::string_view& eventName,
HighResTimeStamp startTime,
const std::optional<std::string_view>& trackName);
const std::optional<std::string_view>& trackName = std::nullopt,
const std::optional<std::string_view>& trackGroup = std::nullopt);
static void measure(
const std::string_view& eventName,
HighResTimeStamp startTime,
HighResTimeStamp endTime,
const std::optional<std::string_view>& trackName);
const std::optional<std::string_view>& trackName = std::nullopt,
const std::optional<std::string_view>& trackGroup = std::nullopt);
};
} // namespace facebook::react