Skip to content

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

Draft
tomekzaw wants to merge 1 commit into
react:mainfrom
tomekzaw:android-trace-section-atrace
Draft

tomekzaw wants to merge 1 commit into
react:mainfrom
tomekzaw:android-trace-section-atrace

Conversation

@tomekzaw

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:

  • The functions are resolved once with dlsym, so no target gains a link dependency on libandroid.
  • 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.
  • Overhead with tracing off. 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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant