Skip to content

fix(profiling): Use anchored clock for CPU and memory samples (JAVA-578) - #6591

Open
runningcode wants to merge 6 commits into
mainfrom
no/java-578-profiler-sample-ticks
Open

runningcode wants to merge 6 commits into
mainfrom
no/java-578-profiler-sample-ticks

Conversation

@runningcode

@runningcode runningcode commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

📜 Description

When the device clock changes during an Android profile, CPU and memory samples end up in the wrong place. Each sample is sent with two times, and each one broke in a different case. For example, a profile starts at 12:00:00, the clock is set back one hour 2s in, and the profile ends at 4s:

Sample Taken Before: elapsed_since_start_ns Before: timestamp After
A at 1s, before the change 1h 0m 1s ❌ 12:00:01 1s, 12:00:01
B at 3s, after the change 3s 11:00:03 ❌ 3s, 12:00:03

Transaction profiles place samples by elapsed_since_start_ns, and continuous profiles place them by timestamp, so both fields need to be right.

Samples now carry only a tick from options.getMonotonicTicker(). Each profiler is given that ticker and an EpochClock, reads its start tick from the ticker, and creates an AnchoredClock when it starts. Both times come from the sample's tick:

elapsed_since_start_ns = sampleTick - startTick
timestamp              = anchoredClock.at(sampleTick)

Without a clock change, both stay within a millisecond of the old values.

When reviewing, keep in mind a lot of the code is just injecting the needed clocks around. Maybe a factory would be a better way to avoid that. Feel free to suggest it!

💡 Motivation and Context

The ticket names AndroidProfiler, but PerfettoContinuousProfiler has the same bug, so this fixes both. Frame measurements still read the wall clock at each frame and are out of scope.

💚 How did you test it?

New tests in AndroidProfilerTest and ChunkMeasurementCollectorTest use fake clocks to replay the example above, setting the clock back an hour after the profile starts.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🤖 Generated with Claude Code

@linear-code

linear-code Bot commented Nov 12, 2026

Copy link
Copy Markdown

JAVA-578

@sentry

sentry Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.67.0 (1) release

⚙️ sentry-android Build Distribution Settings

@runningcode
runningcode force-pushed the no/java-578-profiler-sample-ticks branch from c45c666 to f4b465c Compare November 23, 2026 10:20
@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: high

For commit 83ab3c9.

Main concerns: public contract, reach, host performance.

Rules that block low also fired:

  • R4: a public API record changed: sentry-android-core/api/sentry-android-core.api, sentry/api/sentry.api

This label is a hint for reviewers, not an approval.

runningcode and others added 4 commits December 4, 2026 14:43
…(JAVA-578)

AndroidProfiler and PerfettoContinuousProfiler's chunk collector placed
each CPU and memory sample on the profile timeline by projecting its
wall-clock timestamp through one wall-to-elapsedRealtime offset taken
when the profile ended. A wall-clock step during the profile shifted
every earlier sample relative to the trace.

PerformanceCollectionData now carries a tick from
options.getMonotonicTicker(), read when the sample is taken. On Android
that is SystemClock.elapsedRealtimeNanos(), the clock both profilers
read their start from, so the offset is a plain subtraction.

Fixes JAVA-578

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

start() already returns the start tick in ProfileStartData, so the test
no longer depends on a private field name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A sample's absolute timestamp was still read from the wall clock when it was taken, so a clock change during a profile moved it. The frontend places continuous profile measurements by that timestamp, not by elapsed_since_start_ns.

Samples now carry only their monotonic tick. Each profiler reads the wall clock once when it starts and computes both the offset and the timestamp of every sample from that reading and the sample's tick. The PerformanceCollectionData constructor keeps its single long argument, now the tick.

Refs JAVA-578

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@runningcode
runningcode force-pushed the no/java-578-profiler-sample-ticks branch from f4b465c to 408a981 Compare December 4, 2026 13:50
@github-actions github-actions Bot removed the risk: high PR risk score: high label Dec 4, 2026
@runningcode runningcode changed the title fix(profiling): Place CPU and memory samples by their monotonic tick (JAVA-578) fix(profiling): Time CPU and memory samples by their monotonic tick (JAVA-578) Dec 4, 2026
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Dec 4, 2026
The profilers now get their EpochClock and MonotonicTicker through their constructors and project sample timestamps with AnchoredClock, instead of reading System.currentTimeMillis() and SystemClock directly. The sample ticks come from options.getMonotonicTicker(), and MonotonicTicker forbids comparing ticks from different tickers, so the profile start now comes from the same ticker.

AnchoredClock.at(tick) is now public, because samples are stamped on the collector thread and projected later. App start profiling has no options yet, so it passes the clocks SentryAndroidOptions returns. AndroidProfiler keeps its old constructor for sentry-react-native.

Refs JAVA-578

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: medium PR risk score: medium risk: high PR risk score: high labels Dec 4, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Dec 4, 2026
@runningcode
runningcode marked this pull request as ready for review December 4, 2026 15:12
@runningcode
runningcode requested review from a team and adinauer as code owners December 4, 2026 15:12
@runningcode
runningcode requested review from 0xadam-brown and romtsn and removed request for a team December 4, 2026 15:12
@github-actions github-actions Bot added risk: high PR risk score: high and removed risk: high PR risk score: high labels Dec 4, 2026
@runningcode runningcode changed the title fix(profiling): Time CPU and memory samples by their monotonic tick (JAVA-578) fix(profiling): Use anchored clock for CPU and memory samples (JAVA-578) Dec 4, 2026

@0xadam-brown 0xadam-brown left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice! 💯

Before merge

My clanker noted that nothing tests that DefaultCompositePerformanceCollector:124 stamps samples with the options ticker. That one line is what the fix depends on, and changing it back would still pass every test. DefaultCompositePerformanceCollectorTest already uses a TestMonotonicTicker. Looks like adding a tickNanos assertion there would do the trick.

W/r/t the factory

My vote would be to inject one Supplier instead of EpochClock + MonotonicTicker, and add anchorTickNanos() to AnchoredClock. That'd give us i) a single constructor param on each of the six constructors, ii) one read at the start of the tick instead of two, and iii) the same ticker everywhere by construction (rather than having to comment in multiple places). But up to you...

Clanker-recommended follow-up

Frame timestamps are now the only series still on the wall clock, so after a clock change they no longer line up with CPU and memory. In ChunkMeasurementCollector the frame's tick is already computed, so it's roughly clock.at(frameEndElapsedRealtimeNanos) using a local captured in start(). AndroidProfiler would also need the clock created before startCollection.

(Fine as a separate PR, assuming you decide to do it.)

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

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Profiler samples are re-anchored once at profile end

2 participants