Skip to content

Emit C++ TraceSection markers through ATrace on Android - #59065

Open
tomekzaw wants to merge 2 commits into
react:mainfrom
tomekzaw:android-trace-section-atrace
Open

tomekzaw wants to merge 2 commits into
react:mainfrom
tomekzaw:android-trace-section-atrace

Conversation

@tomekzaw

@tomekzaw tomekzaw commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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

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

In OSS Android builds TraceSection compiles to a no-op unless WITH_PERFETTO or
WITH_FBSYSTRACE is defined, which nothing in the build sets. Route it to the
NDK ATrace API instead (resolved with dlsym, gated on ATrace_isEnabled) so that
the existing C++ sections show up in Perfetto and systrace captures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 14, 2026
@facebook-github-tools facebook-github-tools Bot added p: Software Mansion Partner: Software Mansion Partner labels Oct 14, 2026
@tomekzaw
tomekzaw marked this pull request as ready for review October 15, 2026 14:46
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 15, 2026
@meta-codesync

meta-codesync Bot commented Oct 16, 2026

Copy link
Copy Markdown

@Abbondanzo has imported this pull request. If you are a Meta employee, you can view this in D125730780.

@Abbondanzo Abbondanzo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<android/trace.h> should have these

Comment thread packages/react-native/ReactCommon/cxxreact/TraceSection.h Outdated
Comment thread packages/react-native/ReactCommon/cxxreact/TraceSection.h Outdated
Comment thread packages/react-native/ReactCommon/cxxreact/TraceSection.h Outdated
Include the NDK header and call ATrace_isEnabled/beginSection/endSection
directly instead of resolving them with dlsym. libreactnative already
links libandroid, and hermestooling links it through libreactnative.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tomekzaw

Copy link
Copy Markdown
Contributor Author

Thanks! Applied all three suggestions in 5c5202e.

A note on linking: libreactnative already links libandroid, and libhermestooling picks it up through libreactnative, so calling ATrace directly needs no CMake changes. RNTester debug and release builds pass, and the C++ sections still show up in a Perfetto capture.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants