Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ReactPerfLogger to log to Perfetto and Fusebox #46793

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

#include <folly/Conv.h>
#include <jsinspector-modern/ReactCdp.h>

#include <react/timing/primitives.h>
#include <chrono>
#include <reactperflogger/ReactPerfLogger.h>

namespace facebook::react {

Expand All @@ -27,7 +25,7 @@ std::string JSExecutor::getSyntheticBundlePath(
}

double JSExecutor::performanceNow() {
return chronoToDOMHighResTimeStamp(std::chrono::steady_clock::now());
return ReactPerfLogger::performanceNow();
}

jsinspector_modern::RuntimeTargetDelegate&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
#include <jsi/instrumentation.h>
#include <react/performance/timeline/PerformanceEntryReporter.h>
#include <react/performance/timeline/PerformanceObserver.h>
#include <reactperflogger/ReactPerfLogger.h>

#if __has_include(<reactperflogger/fusebox/FuseboxTracer.h>)
#include <reactperflogger/fusebox/FuseboxTracer.h>
#define HAS_FUSEBOX
#endif
#include "NativePerformance.h"

#ifdef RN_DISABLE_OSS_PLUGIN_HEADER
Expand Down Expand Up @@ -124,18 +121,10 @@ void NativePerformance::mark(
jsi::Runtime& rt,
std::string name,
double startTime) {
PerformanceEntryReporter::getInstance()->reportMark(name, startTime);
auto [trackName, eventName] = parseTrackName(name);
ReactPerfLogger::mark(eventName, startTime, trackName);

#ifdef WITH_PERFETTO
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
auto [trackName, eventName] = parseTrackName(name);
TRACE_EVENT_INSTANT(
"react-native",
perfetto::DynamicString(eventName.data(), eventName.size()),
getPerfettoWebPerfTrackSync(trackName),
performanceNowToPerfettoTraceTime(startTime));
}
#endif
PerformanceEntryReporter::getInstance()->reportMark(name, startTime);
}

void NativePerformance::measure(
Expand All @@ -148,29 +137,13 @@ void NativePerformance::measure(
std::optional<std::string> endMark) {
auto [trackName, eventName] = parseTrackName(name);

#ifdef HAS_FUSEBOX
FuseboxTracer::getFuseboxTracer().addEvent(
eventName, (uint64_t)startTime, (uint64_t)endTime, trackName);
#endif
// TODO T190600850 support startMark/endMark
if (!startMark && !endMark) {
ReactPerfLogger::measure(eventName, startTime, endTime, trackName);
}

PerformanceEntryReporter::getInstance()->reportMeasure(
eventName, startTime, endTime, duration, startMark, endMark);

#ifdef WITH_PERFETTO
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
// TODO T190600850 support startMark/endMark
if (!startMark && !endMark) {
auto track = getPerfettoWebPerfTrackAsync(trackName);
TRACE_EVENT_BEGIN(
"react-native",
perfetto::DynamicString(eventName.data(), eventName.size()),
track,
performanceNowToPerfettoTraceTime(startTime));
TRACE_EVENT_END(
"react-native", track, performanceNowToPerfettoTraceTime(endTime));
}
}
#endif
}

void NativePerformance::clearMarks(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 "ReactPerfLogger.h"

#include <react/timing/primitives.h>
#if __has_include(<reactperflogger/fusebox/FuseboxTracer.h>)
#include <reactperflogger/fusebox/FuseboxTracer.h>
#define HAS_FUSEBOX
#endif
#include <chrono>

#include "ReactPerfetto.h"

namespace facebook::react {

/* static */ void ReactPerfLogger::measure(
const std::string_view& eventName,
double startTime,
double endTime,
const std::string_view& trackName) {
#ifdef HAS_FUSEBOX
FuseboxTracer::getFuseboxTracer().addEvent(
eventName, (uint64_t)startTime, (uint64_t)endTime, trackName);
#endif

#ifdef WITH_PERFETTO
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
auto track = getPerfettoWebPerfTrackAsync(std::string(trackName));
TRACE_EVENT_BEGIN(
"react-native",
perfetto::DynamicString(eventName.data(), eventName.size()),
track,
performanceNowToPerfettoTraceTime(startTime));
TRACE_EVENT_END(
"react-native", track, performanceNowToPerfettoTraceTime(endTime));
}
#endif
}

/* static */ void ReactPerfLogger::mark(
const std::string_view& eventName,
double startTime,
const std::string_view& trackName) {
// TODO(T203046480) Support mark in FuseboxTracer

#ifdef WITH_PERFETTO
if (TRACE_EVENT_CATEGORY_ENABLED("react-native")) {
TRACE_EVENT_INSTANT(
"react-native",
perfetto::DynamicString(eventName.data(), eventName.size()),
getPerfettoWebPerfTrackSync(std::string(trackName)),
performanceNowToPerfettoTraceTime(startTime));
}
#endif
}

/* static */ double ReactPerfLogger::performanceNow() {
return chronoToDOMHighResTimeStamp(std::chrono::steady_clock::now());
}
} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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

#include <reactperflogger/ReactPerfettoCategories.h>
#include <string>

namespace facebook::react {

class ReactPerfLogger {
public:
static void mark(
const std::string_view& eventName,
double startTime,
const std::string_view& trackName);

/**
* This accepts performance events that should go to internal tracing
* frameworks like Perfetto, and should go to DevTools like Fusebox.
*/
static void measure(
const std::string_view& eventName,
double startTime,
double endTime,
const std::string_view& trackName);

static double performanceNow();
};

} // namespace facebook::react