-
-
Notifications
You must be signed in to change notification settings - Fork 469
feat(android): Parse memory and GC info from ANR thread dumps #5428
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
7
commits into
main
Choose a base branch
from
feat/anr-thread-dump-memory-info
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
46f32f2
feat(android): Parse memory and GC info from ANR thread dumps
markushi 9cb9fdf
changelog
markushi d19b7d0
Feed Art stats into a new top level ArtContext
markushi 089d25e
Update Changelog
markushi 7edda13
Update changelog
markushi af52824
Address PR comments
markushi c8f560d
Parse all available timing units
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
149 changes: 149 additions & 0 deletions
149
...droid-core/src/main/java/io/sentry/android/core/internal/threaddump/ArtContextParser.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,149 @@ | ||
| package io.sentry.android.core.internal.threaddump; | ||
|
|
||
| import io.sentry.protocol.ArtContext; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Parses ART runtime memory and GC metrics from ANR thread dump lines. | ||
| * | ||
| * @see <a href="https://android.googlesource.com/platform/art/+/master/runtime/gc/heap.cc#1282">ART | ||
| * Heap::DumpGcCountRateHistogram</a> | ||
| */ | ||
| final class ArtContextParser { | ||
|
|
||
| private static final long KB = 1024; | ||
| private static final long MB = 1024 * KB; | ||
| private static final long GB = 1024 * MB; | ||
|
|
||
| private static final String FREE_MEMORY_PREFIX = "Free memory "; | ||
| private static final String FREE_MEMORY_UNTIL_GC_PREFIX = "Free memory until GC "; | ||
| private static final String FREE_MEMORY_UNTIL_OOME_PREFIX = "Free memory until OOME "; | ||
| private static final String TOTAL_MEMORY_PREFIX = "Total memory "; | ||
| private static final String MAX_MEMORY_PREFIX = "Max memory "; | ||
| private static final String TOTAL_TIME_WAITING_FOR_GC_PREFIX = | ||
| "Total time waiting for GC to complete: "; | ||
| private static final String TOTAL_GC_COUNT_PREFIX = "Total GC count: "; | ||
| private static final String TOTAL_GC_TIME_PREFIX = "Total GC time: "; | ||
| private static final String TOTAL_BLOCKING_GC_COUNT_PREFIX = "Total blocking GC count: "; | ||
| private static final String TOTAL_BLOCKING_GC_TIME_PREFIX = "Total blocking GC time: "; | ||
| private static final String TOTAL_PRE_OOME_GC_COUNT_PREFIX = "Total pre-OOME GC count: "; | ||
|
|
||
| private @Nullable ArtContext artContext; | ||
|
|
||
| @Nullable | ||
| ArtContext getArtContext() { | ||
| return artContext; | ||
| } | ||
|
|
||
| void parseLine(final @NotNull String text) { | ||
|
Member
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. Do we have a source URL for the substrings we parse against that we could link to in a Javadoc? (eg, here?) That'd let folks know where to look if things change + where they can double-check semantics. |
||
| if (text.startsWith(FREE_MEMORY_UNTIL_OOME_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setFreeMemoryUntilOome( | ||
| parsePrettySize(text.substring(FREE_MEMORY_UNTIL_OOME_PREFIX.length()))); | ||
| } else if (text.startsWith(FREE_MEMORY_UNTIL_GC_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setFreeMemoryUntilGc( | ||
| parsePrettySize(text.substring(FREE_MEMORY_UNTIL_GC_PREFIX.length()))); | ||
| } else if (text.startsWith(FREE_MEMORY_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setFreeMemory(parsePrettySize(text.substring(FREE_MEMORY_PREFIX.length()))); | ||
| } else if (text.startsWith(TOTAL_MEMORY_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setTotalMemory(parsePrettySize(text.substring(TOTAL_MEMORY_PREFIX.length()))); | ||
| } else if (text.startsWith(MAX_MEMORY_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setMaxMemory(parsePrettySize(text.substring(MAX_MEMORY_PREFIX.length()))); | ||
| } else if (text.startsWith(TOTAL_TIME_WAITING_FOR_GC_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setGcWaitingTime(parseTimeMs(text.substring(TOTAL_TIME_WAITING_FOR_GC_PREFIX.length()))); | ||
| } else if (text.startsWith(TOTAL_GC_TIME_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setGcTotalTime(parseTimeMs(text.substring(TOTAL_GC_TIME_PREFIX.length()))); | ||
| } else if (text.startsWith(TOTAL_GC_COUNT_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setGcTotalCount(parseLongOrNull(text.substring(TOTAL_GC_COUNT_PREFIX.length()))); | ||
| } else if (text.startsWith(TOTAL_BLOCKING_GC_TIME_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setGcBlockingTime(parseTimeMs(text.substring(TOTAL_BLOCKING_GC_TIME_PREFIX.length()))); | ||
| } else if (text.startsWith(TOTAL_BLOCKING_GC_COUNT_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setGcBlockingCount( | ||
| parseLongOrNull(text.substring(TOTAL_BLOCKING_GC_COUNT_PREFIX.length()))); | ||
| } else if (text.startsWith(TOTAL_PRE_OOME_GC_COUNT_PREFIX)) { | ||
| getOrCreateArtContext() | ||
| .setGcPreOomeCount( | ||
| parseLongOrNull(text.substring(TOTAL_PRE_OOME_GC_COUNT_PREFIX.length()))); | ||
| } | ||
| } | ||
|
|
||
| private @NotNull ArtContext getOrCreateArtContext() { | ||
| if (artContext == null) { | ||
| artContext = new ArtContext(); | ||
| } | ||
| return artContext; | ||
| } | ||
|
|
||
| /** | ||
| * Matches Android's PrettySize output: number followed by unit with no space, e.g. "3107KB". | ||
| * | ||
| * <p>Counterpart to | ||
| * https://cs.android.com/android/platform/superproject/+/android-latest-release:art/libartbase/base/utils.cc;l=232-251;drc=d0d3deb269b1e14de2ec2707815e38bc95de570c | ||
| */ | ||
| private @Nullable Long parsePrettySize(final @NotNull String sizeString) { | ||
| final String trimmed = sizeString.trim(); | ||
| try { | ||
| if (trimmed.endsWith("GB")) { | ||
| return Long.parseLong(trimmed.substring(0, trimmed.length() - 2)) * GB; | ||
| } else if (trimmed.endsWith("MB")) { | ||
| return Long.parseLong(trimmed.substring(0, trimmed.length() - 2)) * MB; | ||
| } else if (trimmed.endsWith("KB")) { | ||
| return Long.parseLong(trimmed.substring(0, trimmed.length() - 2)) * KB; | ||
| } else if (trimmed.endsWith("B")) { | ||
| return Long.parseLong(trimmed.substring(0, trimmed.length() - 1)); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Parses ART's PrettyDuration output and converts to milliseconds. Handles "s", "ms", "us", "ns" | ||
| * suffixes and the bare "0" special case. | ||
| * | ||
| * @see <a | ||
| * href="https://cs.android.com/android/platform/superproject/+/android-latest-release:art/libartbase/base/time_utils.cc;l=95-133;drc=16e1409f339b1318fe1cdce8462f089b3b0475e8">ART | ||
| * PrettyDuration / FormatDuration</a> | ||
| */ | ||
| private static @Nullable Double parseTimeMs(final @NotNull String timeString) { | ||
| final String trimmed = timeString.trim(); | ||
| try { | ||
| if (trimmed.equals("0")) { | ||
| return 0.0; | ||
| } | ||
| // Double.parseDouble is locale-independent (always uses '.' as decimal separator), | ||
| // which matches the ART runtime output format. | ||
| if (trimmed.endsWith("ms")) { | ||
| return Double.parseDouble(trimmed.substring(0, trimmed.length() - 2)); | ||
| } else if (trimmed.endsWith("ns")) { | ||
| return Double.parseDouble(trimmed.substring(0, trimmed.length() - 2)) / 1_000_000.0; | ||
| } else if (trimmed.endsWith("us")) { | ||
| return Double.parseDouble(trimmed.substring(0, trimmed.length() - 2)) / 1_000.0; | ||
| } else if (trimmed.endsWith("s")) { | ||
| return Double.parseDouble(trimmed.substring(0, trimmed.length() - 1)) * 1_000.0; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static @Nullable Long parseLongOrNull(final @NotNull String value) { | ||
| try { | ||
| return Long.parseLong(value.trim()); | ||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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.
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.
Do we have a sense for how frequently these strings change, and how stable they are across OEMs? Just wondering how much we need to monitor the presence of unparseable / null values here vs our ability to set it and forget it.
(Doesn't have to hold up this PR, regardless.)