-
-
Notifications
You must be signed in to change notification settings - Fork 464
feat(anr): Profile main thread when ANR and report ANR profiles to Sentry #4899
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
6
commits into
main
Choose a base branch
from
markushi/feat/anr-profiling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,685
−34
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b286ad5
Profile main thread when ANR and report ANR profiles to sentry
markushi a62b5e8
docs(changelog): Add ANR profiling integration entry
markushi ae66f73
Merge branch 'main' into markushi/feat/anr-profiling
markushi f226d84
Fix api dump file
markushi 7d423a4
Address PR feedback
markushi 5824f8f
Merge branch 'markushi/feat/anr-profiling' of github.com:getsentry/se…
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,19 @@ | |
| import io.sentry.ILogger; | ||
| import io.sentry.IScopes; | ||
| import io.sentry.Integration; | ||
| import io.sentry.ProfileChunk; | ||
| import io.sentry.ProfileContext; | ||
| import io.sentry.SentryEvent; | ||
| import io.sentry.SentryExceptionFactory; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.SentryOptions; | ||
| import io.sentry.SentryStackTraceFactory; | ||
| import io.sentry.android.core.anr.AggregatedStackTrace; | ||
| import io.sentry.android.core.anr.AnrCulpritIdentifier; | ||
| import io.sentry.android.core.anr.AnrException; | ||
| import io.sentry.android.core.anr.AnrProfile; | ||
| import io.sentry.android.core.anr.AnrProfileManager; | ||
| import io.sentry.android.core.anr.StackTraceConverter; | ||
| import io.sentry.android.core.cache.AndroidEnvelopeCache; | ||
| import io.sentry.android.core.internal.threaddump.Lines; | ||
| import io.sentry.android.core.internal.threaddump.ThreadDumpParser; | ||
|
|
@@ -28,6 +38,7 @@ | |
| import io.sentry.protocol.Message; | ||
| import io.sentry.protocol.SentryId; | ||
| import io.sentry.protocol.SentryThread; | ||
| import io.sentry.protocol.profiling.SentryProfile; | ||
| import io.sentry.transport.CurrentDateProvider; | ||
| import io.sentry.transport.ICurrentDateProvider; | ||
| import io.sentry.util.HintUtils; | ||
|
|
@@ -41,6 +52,7 @@ | |
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
|
|
@@ -284,6 +296,8 @@ private void reportAsSentryEvent( | |
| } | ||
| } | ||
|
|
||
| applyAnrProfile(isBackground, anrTimestamp, event); | ||
|
|
||
| final @NotNull SentryId sentryId = scopes.captureEvent(event, hint); | ||
| final boolean isEventDropped = sentryId.equals(SentryId.EMPTY_ID); | ||
| if (!isEventDropped) { | ||
|
|
@@ -299,6 +313,65 @@ private void reportAsSentryEvent( | |
| } | ||
| } | ||
|
|
||
| private void applyAnrProfile( | ||
| final boolean isBackground, final long anrTimestamp, final @NotNull SentryEvent event) { | ||
|
|
||
| // as of now AnrProfilingIntegration only generates profiles in foreground | ||
| if (isBackground) { | ||
| return; | ||
| } | ||
|
|
||
| @Nullable AnrProfile anrProfile = null; | ||
| try (final AnrProfileManager provider = new AnrProfileManager(options)) { | ||
| anrProfile = provider.load(); | ||
| } catch (Throwable t) { | ||
| options.getLogger().log(SentryLevel.INFO, "Could not retrieve ANR profile"); | ||
| } | ||
|
|
||
| if (anrProfile != null) { | ||
| options.getLogger().log(SentryLevel.INFO, "ANR profile found"); | ||
| if (anrTimestamp >= anrProfile.startTimeMs && anrTimestamp <= anrProfile.endtimeMs) { | ||
| final SentryProfile profile = StackTraceConverter.convert(anrProfile); | ||
| final ProfileChunk chunk = | ||
| new ProfileChunk( | ||
| new SentryId(), | ||
| new SentryId(), | ||
| null, | ||
| new HashMap<>(0), | ||
| anrTimestamp / 1000.0d, | ||
| ProfileChunk.PLATFORM_JAVA, | ||
| options); | ||
| chunk.setSentryProfile(profile); | ||
|
|
||
| options.getLogger().log(SentryLevel.DEBUG, ""); | ||
|
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. |
||
| scopes.captureProfileChunk(chunk); | ||
|
|
||
| final @Nullable AggregatedStackTrace culprit = | ||
| AnrCulpritIdentifier.identify(anrProfile.stacks); | ||
| if (culprit != null) { | ||
| // TODO Consider setting a static fingerprint to reduce noise | ||
| // if culprit quality is low (e.g. when culprit frame is pollNative()) | ||
| final @NotNull StackTraceElement[] stack = culprit.getStack(); | ||
| if (stack.length > 0) { | ||
| final StackTraceElement stackTraceElement = culprit.getStack()[0]; | ||
| final String message = | ||
| stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName(); | ||
| final AnrException exception = new AnrException(message); | ||
| exception.setStackTrace(stack); | ||
|
|
||
| // TODO should this be re-used from somewhere else? | ||
| final SentryExceptionFactory factory = | ||
| new SentryExceptionFactory(new SentryStackTraceFactory(options)); | ||
| event.setExceptions(factory.getSentryExceptions(exception)); | ||
| event.getContexts().setProfile(new ProfileContext(chunk.getProfilerId())); | ||
| } | ||
| } | ||
| } else { | ||
| options.getLogger().log(SentryLevel.DEBUG, "ANR profile found, but doesn't match"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private @NotNull ParseResult parseThreadDump( | ||
| final @NotNull ApplicationExitInfo exitInfo, final boolean isBackground) { | ||
| final byte[] dump; | ||
|
|
||
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
54 changes: 54 additions & 0 deletions
54
sentry-android-core/src/main/java/io/sentry/android/core/anr/AggregatedStackTrace.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,54 @@ | ||
| package io.sentry.android.core.anr; | ||
|
|
||
| import java.util.Arrays; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
|
|
||
| @ApiStatus.Internal | ||
| public class AggregatedStackTrace { | ||
| // the number of frames of the stacktrace | ||
| final int depth; | ||
|
|
||
| // the quality of the stack trace, higher means better | ||
| final int quality; | ||
|
|
||
| private final StackTraceElement[] stack; | ||
|
|
||
| // 0 is the most detailed frame in the stacktrace | ||
| private final int stackStartIdx; | ||
| private final int stackEndIdx; | ||
|
|
||
| // the total number of times this exact stacktrace was captured | ||
| int count; | ||
|
|
||
| // first time the stacktrace occured | ||
| private long startTimeMs; | ||
|
|
||
| // last time the stacktrace occured | ||
| private long endTimeMs; | ||
|
|
||
| public AggregatedStackTrace( | ||
| final StackTraceElement[] stack, | ||
| final int stackStartIdx, | ||
| final int stackEndIdx, | ||
| final long timestampMs, | ||
| final int quality) { | ||
| this.stack = stack; | ||
| this.stackStartIdx = stackStartIdx; | ||
| this.stackEndIdx = stackEndIdx; | ||
| this.depth = stackEndIdx - stackStartIdx + 1; | ||
| this.startTimeMs = timestampMs; | ||
| this.endTimeMs = timestampMs; | ||
| this.count = 1; | ||
| this.quality = quality; | ||
| } | ||
|
|
||
| public void add(long timestampMs) { | ||
| this.startTimeMs = Math.min(startTimeMs, timestampMs); | ||
| this.endTimeMs = Math.max(endTimeMs, timestampMs); | ||
| this.count++; | ||
| } | ||
|
|
||
| public StackTraceElement[] getStack() { | ||
| return Arrays.copyOfRange(stack, stackStartIdx, stackEndIdx + 1); | ||
| } | ||
| } |
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.
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.
Bug: Concurrent File Access Causes Data Corruption
Creating a new
AnrProfileManagerinstance inapplyAnrProfilewhileAnrProfilingIntegrationmay still be running creates a race condition. Both instances open file handles to the sameanr_profilefile, potentially corrupting data asAnrProfilingIntegrationwrites whileAnrV2Integrationreads and closes the queue file, leading to concurrent access issues with the underlying QueueFile.