Emit C++ TraceSection markers through ATrace on Android (#59065)

Summary:
`TraceSection` (`ReactCommon/cxxreact/TraceSection.h`) is the C++ tracing primitive used throughout the renderer: `ShadowTree::commit`, `UIManager::createNode`, the Yoga measure callbacks, `FabricMountingManager::executeMount` and others. On Android it only does something when built with `WITH_PERFETTO` or `WITH_FBSYSTRACE`, and the OSS build sets neither, so it compiles to `DummyTraceSection`.

**Current behavior.** Perfetto and systrace captures of OSS Android apps show only the Java/Kotlin sections. The C++ half of the renderer (commit, layout, text measurement, building mount instructions) is invisible.

**With this change.** A new Android branch calls the NDK's `ATrace_beginSection` and `ATrace_endSection`:

- It includes `<android/trace.h>` and calls the NDK functions directly. They exist from API 23, and React Native's `minSdk` is 24. `libreactnative` already links `libandroid`, and `libhermestooling` gets it through `libreactnative`, so no build changes are needed.
- Each section checks `ATrace_isEnabled()` first, so when nobody is tracing it costs one call.
- `WITH_PERFETTO` and `WITH_FBSYSTRACE` keep precedence.
- Section arguments (key/value pairs) are ignored for now.

**Why.** Without this, profiling React Native on Android with standard tools can't show where commit and layout time goes. In a capture of RNTester mounting a 100-post feed, this branch shows 7084 `UIManager::createNode` sections and 1632 Yoga measure-callback sections, among others; on `main` none of them appear.

## Changelog:

[ANDROID] [ADDED] - C++ `TraceSection` markers now appear in Perfetto and systrace captures

Pull Request resolved: https://github.com/react/react-native/pull/59065

Test Plan:
- RNTester `assembleDebug` and `assembleRelease` (`-PreactNativeArchitectures=arm64-v8a`) and `yarn format-check-cpp` pass.
- Recorded Perfetto traces (`atrace_apps: "com.facebook.react.uiapp"`) of the same RNTester scenario on `main` and on this branch. The C++ sections appear only on this branch.
- Rebuilt after switching to direct calls: `libreactnative.so` imports the three `ATrace_*` functions from `libandroid.so`, and a Perfetto capture of an RNTester release launch on a physical device shows the C++ sections (`UIManager::createNode`, `ShadowTree::commit`, `FabricMountingManager::executeMount`, ...).
- **Overhead with tracing off** (measured on the first revision, which looked the functions up with `dlsym`; direct calls do the same work minus the null checks). Emulator (Pixel 10 Pro AVD, API 37), release builds, current `main` vs. this branch, 6 runs, medians:

| scenario | total CPU, main | total CPU, this PR | change |
|---|--:|--:|--:|
| mount 100-post feed | 128.8 ms | 130.0 ms | +1% |
| mount 1000 labels | 117.7 ms | 117.1 ms | −1% |

Update phases were within emulator noise, with mixed signs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed By: javache

Differential Revision: D125730780

Pulled By: Abbondanzo

fbshipit-source-id: bba4bc9a4b191f7dd1acc67925df51d379f97745
This commit is contained in:
Tomek Zawadzki authored and meta-codesync[bot] committed 2026-10-19 11:49:25 -07:00
1 parent 65b3e647f5
commit 7728787c25
1 file changed
+32
@@ -34,6 +34,10 @@
#include <reactperflogger/ReactPerfettoCategories.h>
#endif
#if defined(__ANDROID__) && __ANDROID_API__ >= 23 && !defined(WITH_PERFETTO) && !defined(WITH_FBSYSTRACE)
#include <android/trace.h>
#endif
#if defined(__APPLE__)
// This is required so that OS_LOG_TARGET_HAS_10_15_FEATURES will be set.
#include <os/trace_base.h>
@@ -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 <typename... ConvertsToStringPiece>
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: