Repository navigation
feat(anr): [Non-Fatal ANR 2] Add ThreadHangSignal #6723
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
Open
markushi
wants to merge
20
commits into
markushi/feat/non-fatal-anr-1-hang-recorder
from
markushi/feat/non-fatal-anr-2-thread-hang-signal
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6d03de3
feat(anr): Add ThreadHangSignal
markushi 16f09dc
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 7bd435c
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 2717cf8
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi f68fc3a
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 0e8b2c7
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 81e5f0c
ref(anr): Adapt to the renamed hang types
markushi 0a96f43
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 1a11455
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 1cd4179
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi fb26345
ref(anr): Read the probe time from the metronome
markushi febf121
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 9febe3e
ref(anr): Unsubscribe instead of closing the hang manager
markushi e2e2da8
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 2ab7198
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi fc143d6
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi f5f6458
style(anr): Format the HangManager constants
markushi 47ab474
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi 8f6f5c0
fix(anr): Do not read an unset probe time as a probe run
markushi f3878a8
chore(anr): Merge non-fatal-anr-1 into non-fatal-anr-2
markushi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
sentry-android-core/src/main/java/io/sentry/android/core/hang/ThreadHangSignal.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package io.sentry.android.core.hang; | ||
|
|
||
| import io.sentry.IDebugManager; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Reports {@link HangSignal.State#SUSPICIOUS} when a probe posted to the watched thread did not run | ||
| * for more than the suspicious threshold, and {@link HangSignal.State#IDLE} when it ran. The | ||
| * watched thread is the thread that the {@link ThreadPoster} posts to. | ||
| * | ||
| * <p>Only the probe runs on the watched thread. Everything else runs on the thread that calls the | ||
| * ticks of the {@link Metronome}. All times come from {@link Metronome#nowMs()}. | ||
| * | ||
| * <p>A hang starts at the last run of the probe. The watched thread can block at any time up to one | ||
| * tick after that run, so the duration of a hang can be up to one polling interval too long. | ||
| */ | ||
| @ApiStatus.Internal | ||
| public final class ThreadHangSignal implements HangSignal, Metronome.TickListener { | ||
|
|
||
| private final @NotNull Metronome metronome; | ||
| private final @NotNull IDebugManager debugManager; | ||
| private final @NotNull ThreadPoster poster; | ||
| private final long suspiciousThresholdMs; | ||
|
|
||
| // The origin of Metronome.nowMs() is arbitrary, so 0 cannot mean "never ran". | ||
| private volatile long probeRunMs = Long.MIN_VALUE; | ||
|
|
||
| private final @NotNull Runnable probe; | ||
|
|
||
| private @Nullable HangSignalListener listener; | ||
| private long probeFloorMs; | ||
| private boolean suspicious; | ||
| private long suspiciousSinceMs; | ||
|
|
||
| public ThreadHangSignal( | ||
| final @NotNull Metronome metronome, | ||
| final @NotNull IDebugManager debugManager, | ||
| final @NotNull ThreadPoster poster, | ||
| final long suspiciousThresholdMs) { | ||
| this.metronome = metronome; | ||
| this.debugManager = debugManager; | ||
| this.poster = poster; | ||
| this.suspiciousThresholdMs = suspiciousThresholdMs; | ||
| this.probe = () -> probeRunMs = metronome.nowMs(); | ||
| } | ||
|
|
||
| @Override | ||
| public void start(final @NotNull HangSignalListener listener) { | ||
| this.listener = listener; | ||
| suspicious = false; | ||
| // The probe may not have run since the last stop, so its time would read as a hang. | ||
| probeFloorMs = metronome.nowMs(); | ||
| metronome.addTickListener(this); | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| metronome.removeTickListener(this); | ||
| poster.remove(probe); | ||
| listener = null; | ||
| suspicious = false; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| stop(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMetronomeTick(final long nowMs) { | ||
| check(nowMs); | ||
| poster.remove(probe); | ||
| poster.post(probe); | ||
| } | ||
|
|
||
| private void check(final long nowMs) { | ||
| final @Nullable HangSignalListener currentListener = listener; | ||
| if (currentListener == null) { | ||
| return; | ||
| } | ||
|
|
||
| // A breakpoint is not a hang. | ||
| if (debugManager.isDebuggerConnected()) { | ||
| probeFloorMs = nowMs; | ||
| if (suspicious) { | ||
| suspicious = false; | ||
| currentListener.onSignalCanceled(this); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| final long lastProbeMs = Math.max(probeRunMs, probeFloorMs); | ||
| if (suspicious) { | ||
| if (lastProbeMs > suspiciousSinceMs) { | ||
| suspicious = false; | ||
| currentListener.onSignalChanged(this, State.IDLE, nowMs - lastProbeMs); | ||
| } | ||
| } else if (nowMs - lastProbeMs > suspiciousThresholdMs) { | ||
| suspicious = true; | ||
| suspiciousSinceMs = lastProbeMs; | ||
| currentListener.onSignalChanged(this, State.SUSPICIOUS, nowMs - lastProbeMs); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.