Repository navigation
feat(anr): [Non-Fatal ANR 3] Add ThreadSampleCollector #6724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package io.sentry.android.core.hang; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||
|
|
||
| import io.sentry.SentryLevel; | ||
| import io.sentry.SentryOptions; | ||
| import io.sentry.android.core.anr.AnrProfileManager; | ||
| import io.sentry.android.core.anr.AnrProfileRotationHelper; | ||
| import io.sentry.android.core.anr.AnrStackTrace; | ||
| import io.sentry.time.EpochClock; | ||
| import io.sentry.time.MonotonicTicker; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Samples the stack of a thread on each tick of a hang interval. The samples stay in memory until | ||
| * the interval is {@link #DISK_THRESHOLD_MS} old, then they are written to the ANR profile file, so | ||
| * the fatal ANR path can still read them if the process is killed. | ||
| * | ||
| * <p>Not thread-safe, {@link HangRecorder} calls it only on its recorder thread. | ||
| */ | ||
| @ApiStatus.Internal | ||
| public final class ThreadSampleCollector implements HangDataCollector { | ||
|
|
||
| static final long DISK_THRESHOLD_MS = 700; | ||
|
|
||
| private final @NotNull SentryOptions options; | ||
| private final @NotNull EpochClock epochClock; | ||
| private final @NotNull MonotonicTicker ticker; | ||
| private final @NotNull Thread thread; | ||
| private final @NotNull List<AnrStackTrace> samples = new ArrayList<>(); | ||
| private @Nullable AnrProfileManager profileManager; | ||
| private boolean onDisk; | ||
|
|
||
| public ThreadSampleCollector( | ||
| final @NotNull SentryOptions options, | ||
| final @NotNull EpochClock epochClock, | ||
| final @NotNull MonotonicTicker ticker, | ||
| final @NotNull Thread thread) { | ||
| this.options = options; | ||
| this.epochClock = epochClock; | ||
| this.ticker = ticker; | ||
| this.thread = thread; | ||
| } | ||
|
|
||
| @Override | ||
| public void onRecordingStarted(final @NotNull HangInterval interval) { | ||
| reset(); | ||
| if (profileManager == null) { | ||
| final @Nullable String cacheDirPath = options.getCacheDirPath(); | ||
| if (cacheDirPath != null) { | ||
| final @NotNull File file = | ||
| AnrProfileRotationHelper.getFileForRecording(new File(cacheDirPath)); | ||
| profileManager = new AnrProfileManager(options, file); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onSample(final @NotNull HangInterval interval) { | ||
| if (samples.size() >= AnrProfileManager.MAX_NUM_STACKTRACES) { | ||
| return; | ||
| } | ||
|
|
||
| final long epochMs = NANOSECONDS.toMillis(epochClock.now().epochNanos()); | ||
| final @NotNull AnrStackTrace sample = | ||
| new AnrStackTrace(epochMs, thread.getStackTrace(), thread.getState()); | ||
| samples.add(sample); | ||
|
|
||
| final long nowMs = NANOSECONDS.toMillis(ticker.tickNanos()); | ||
| // Most hangs end before this, so they cause no disk IO. | ||
| if (nowMs - interval.getStartTimeMs() < DISK_THRESHOLD_MS) { | ||
| return; | ||
| } | ||
|
|
||
| final @Nullable AnrProfileManager manager = profileManager; | ||
| if (manager == null) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| if (!onDisk) { | ||
| onDisk = true; | ||
| startDiskRecording(manager); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failed flush skips buffered samplesMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit a19a7c8. Configure here. |
||
| } else { | ||
| manager.add(sample); | ||
| } | ||
| } catch (IOException e) { | ||
| options.getLogger().log(SentryLevel.WARNING, "Failed to write the thread samples", e); | ||
| } | ||
| } | ||
|
|
||
| private void startDiskRecording(final @NotNull AnrProfileManager manager) throws IOException { | ||
| manager.clear(); | ||
| for (final @NotNull AnrStackTrace buffered : samples) { | ||
| manager.add(buffered); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onRecordingStopped( | ||
| final @NotNull HangInterval interval, final @NotNull HangRecord.Builder out) { | ||
| out.setSamples(new ArrayList<>(samples)); | ||
| } | ||
|
|
||
| /** Discards the samples in memory only, the fatal path keeps the file. */ | ||
| @Override | ||
| public void onRecordingCanceled() { | ||
| reset(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| reset(); | ||
| final @Nullable AnrProfileManager manager = profileManager; | ||
| profileManager = null; | ||
| if (manager != null) { | ||
| manager.close(); | ||
| } | ||
| } | ||
|
|
||
| private void reset() { | ||
| samples.clear(); | ||
| onDisk = false; | ||
| } | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sample timestamps use wall clock
Low Severity
Each sample stamps itself with a fresh
epochClock.now()reading. Those timestamps are later sorted and turned into profile sample times, so they are related instants. A wall-clock jump during the hang can reorder stacks or stretch the profile.AnchoredClockis the type for this: one epoch read, then monotonic ticks for the rest.Triggered by learned rule: AnchoredClock for related instants, not EpochClock math
Reviewed by Cursor Bugbot for commit a19a7c8. Configure here.