diff --git a/packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp b/packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp index 9a9bb7a1811db6..1ad57f34625d67 100644 --- a/packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp +++ b/packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp @@ -11,9 +11,7 @@ #include #include - -#include -#include +#include namespace facebook::react { @@ -27,7 +25,7 @@ std::string JSExecutor::getSyntheticBundlePath( } double JSExecutor::performanceNow() { - return chronoToDOMHighResTimeStamp(std::chrono::steady_clock::now()); + return ReactPerfLogger::performanceNow(); } jsinspector_modern::RuntimeTargetDelegate& diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp index 6268e06bf17f28..aac21dd8752873 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp @@ -16,11 +16,8 @@ #include #include #include +#include -#if __has_include() -#include -#define HAS_FUSEBOX -#endif #include "NativePerformance.h" #ifdef RN_DISABLE_OSS_PLUGIN_HEADER @@ -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( @@ -148,29 +137,13 @@ void NativePerformance::measure( std::optional 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( diff --git a/packages/react-native/ReactCommon/reactperflogger/reactperflogger/ReactPerfLogger.cpp b/packages/react-native/ReactCommon/reactperflogger/reactperflogger/ReactPerfLogger.cpp new file mode 100644 index 00000000000000..6f1b8d0aa23835 --- /dev/null +++ b/packages/react-native/ReactCommon/reactperflogger/reactperflogger/ReactPerfLogger.cpp @@ -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 +#if __has_include() +#include +#define HAS_FUSEBOX +#endif +#include + +#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 diff --git a/packages/react-native/ReactCommon/reactperflogger/reactperflogger/ReactPerfLogger.h b/packages/react-native/ReactCommon/reactperflogger/reactperflogger/ReactPerfLogger.h new file mode 100644 index 00000000000000..9e4e27b54a5274 --- /dev/null +++ b/packages/react-native/ReactCommon/reactperflogger/reactperflogger/ReactPerfLogger.h @@ -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 +#include + +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