diff --git a/packages/react-native/ReactCommon/cxxreact/TraceSection.h b/packages/react-native/ReactCommon/cxxreact/TraceSection.h index c0d7fba280b6..2c9ff9ddb5f4 100644 --- a/packages/react-native/ReactCommon/cxxreact/TraceSection.h +++ b/packages/react-native/ReactCommon/cxxreact/TraceSection.h @@ -34,6 +34,10 @@ #include #endif +#if defined(__ANDROID__) && __ANDROID_API__ >= 23 && !defined(WITH_PERFETTO) && !defined(WITH_FBSYSTRACE) +#include +#endif + #if defined(__APPLE__) // This is required so that OS_LOG_TARGET_HAS_10_15_FEATURES will be set. #include @@ -95,6 +99,34 @@ struct ConcreteTraceSection { fbsystrace::FbSystraceSection m_section; }; using TraceSectionUnwrapped = ConcreteTraceSection; +#elif defined(__ANDROID__) && __ANDROID_API__ >= 23 +// Route TraceSection to the platform ATrace API so +// that C++ sections show up in Perfetto/systrace captures of OSS builds. +struct ATraceSection { + public: + template + explicit ATraceSection(const char *name, [[maybe_unused]] ConvertsToStringPiece &&...args) + { + if (ATrace_isEnabled()) { + ATrace_beginSection(name); + active_ = true; + } + } + + ~ATraceSection() + { + if (active_) { + ATrace_endSection(); + } + } + + ATraceSection(const ATraceSection &) = delete; + ATraceSection &operator=(const ATraceSection &) = delete; + + private: + bool active_{false}; +}; +using TraceSectionUnwrapped = ATraceSection; #else struct DummyTraceSection { public: