Fallback to fbsystrace when Perfetto is not available (#49657)

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

For some use-cases we still want to log using the internal fbsystrace API which allows backdating of events.

This feels a bit weird putting it in `ReactPerfettoLogger`, but maybe we should rename that to something like `TraceLogger` (like we have `TraceSection`).

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D70109680

fbshipit-source-id: 7757b81ec5032e3800446a88198c7da135afdd0e
This commit is contained in:
Pieter De Baets
2025-02-26 07:43:18 -08:00
committed by Facebook GitHub Bot
parent 65242998ca
commit 4a055f36eb
@@ -7,15 +7,19 @@
#include "ReactPerfettoLogger.h"
#ifdef WITH_PERFETTO
#if defined(WITH_PERFETTO)
#include "ReactPerfetto.h"
#elif defined(WITH_FBSYSTRACE)
#include <fbsystrace.h>
#endif
#include <chrono>
namespace facebook::react {
#ifdef WITH_PERFETTO
namespace {
#if defined(WITH_PERFETTO)
const std::string PERFETTO_DEFAULT_TRACK_NAME = "# Web Performance";
const std::string PERFETTO_TRACK_NAME_PREFIX = "# Web Performance: ";
@@ -25,13 +29,21 @@ std::string toPerfettoTrackName(
? PERFETTO_TRACK_NAME_PREFIX + std::string(trackName.value())
: PERFETTO_DEFAULT_TRACK_NAME;
}
} // namespace
#elif defined(WITH_FBSYSTRACE)
int64_t getDeltaNanos(double jsTime) {
auto now = std::chrono::steady_clock::now().time_since_epoch();
return static_cast<int64_t>(jsTime * 1.e6) -
std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
}
#endif
} // namespace
/* static */ bool ReactPerfettoLogger::isTracing() {
#ifdef WITH_PERFETTO
#if defined(WITH_PERFETTO)
return TRACE_EVENT_CATEGORY_ENABLED("react-native");
#elif defined(WITH_FBSYSTRACE)
return fbsystrace_is_tracing(TRACE_TAG_REACT_APPS);
#else
return false;
#endif
@@ -42,7 +54,7 @@ std::string toPerfettoTrackName(
double startTime,
double endTime,
const std::optional<std::string_view>& trackName) {
#ifdef WITH_PERFETTO
#if defined(WITH_PERFETTO)
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
auto track = getPerfettoWebPerfTrackAsync(toPerfettoTrackName(trackName));
TRACE_EVENT_BEGIN(
@@ -53,6 +65,13 @@ std::string toPerfettoTrackName(
TRACE_EVENT_END(
"react-native", track, performanceNowToPerfettoTraceTime(endTime));
}
#elif defined(WITH_FBSYSTRACE)
static int cookie = 0;
fbsystrace_begin_async_section_with_timedelta(
TRACE_TAG_REACT_APPS, eventName.data(), cookie, getDeltaNanos(startTime));
fbsystrace_end_async_section_with_timedelta(
TRACE_TAG_REACT_APPS, eventName.data(), cookie, getDeltaNanos(endTime));
cookie++;
#endif
}
@@ -60,7 +79,7 @@ std::string toPerfettoTrackName(
const std::string_view& eventName,
double startTime,
const std::optional<std::string_view>& trackName) {
#ifdef WITH_PERFETTO
#if defined(WITH_PERFETTO)
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
TRACE_EVENT_INSTANT(
"react-native",