-
-
Notifications
You must be signed in to change notification settings - Fork 444
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
Fix slow and frozen frames tracking #2271
Merged
+286
−39
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
343ada1
Snapshot frames onActivityStarted and on transaction finish then calc…
adinauer 6044b4c
Add changelog
adinauer 2f4462c
Add tests
adinauer f6b0dc7
Add final keyword
adinauer 4f2ca28
Update sentry-android-core/src/main/java/io/sentry/android/core/Activ…
adinauer 8cab3eb
Update sentry-android-core/src/test/java/io/sentry/android/core/Activ…
adinauer 391ea57
Update sentry-android-core/src/test/java/io/sentry/android/core/Activ…
adinauer 5b7f10b
Code Review changes
adinauer 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 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 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import io.sentry.protocol.SentryId; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
@@ -27,6 +28,8 @@ public final class ActivityFramesTracker { | |
|
||
private final @NotNull Map<SentryId, Map<String, @NotNull MeasurementValue>> | ||
activityMeasurements = new ConcurrentHashMap<>(); | ||
private final @NotNull Map<Activity, FrameCounts> frameCountAtStartSnapshots = | ||
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. I like the name 👏 |
||
new WeakHashMap<>(); | ||
|
||
public ActivityFramesTracker(final @NotNull LoadClass loadClass, final @Nullable ILogger logger) { | ||
androidXAvailable = | ||
|
@@ -55,33 +58,31 @@ public synchronized void addActivity(final @NotNull Activity activity) { | |
return; | ||
} | ||
frameMetricsAggregator.add(activity); | ||
snapshotFrameCountsAtStart(activity); | ||
} | ||
|
||
@SuppressWarnings("NullAway") | ||
public synchronized void setMetrics( | ||
final @NotNull Activity activity, final @NotNull SentryId sentryId) { | ||
private void snapshotFrameCountsAtStart(final @NotNull Activity activity) { | ||
FrameCounts frameCounts = calculateCurrentFrameCounts(); | ||
if (frameCounts != null) { | ||
frameCountAtStartSnapshots.put(activity, frameCounts); | ||
} | ||
} | ||
|
||
private @Nullable FrameCounts calculateCurrentFrameCounts() { | ||
if (!isFrameMetricsAggregatorAvailable()) { | ||
return; | ||
return null; | ||
} | ||
|
||
if (frameMetricsAggregator == null) { | ||
return null; | ||
} | ||
final @Nullable SparseIntArray[] framesRates = frameMetricsAggregator.getMetrics(); | ||
|
||
int totalFrames = 0; | ||
int slowFrames = 0; | ||
int frozenFrames = 0; | ||
|
||
SparseIntArray[] framesRates = null; | ||
try { | ||
framesRates = frameMetricsAggregator.remove(activity); | ||
} catch (Throwable ignored) { | ||
// throws IllegalArgumentException when attempting to remove OnFrameMetricsAvailableListener | ||
// that was never added. | ||
// there's no contains method. | ||
// throws NullPointerException when attempting to remove OnFrameMetricsAvailableListener and | ||
// there was no | ||
// Observers, See | ||
// https://android.googlesource.com/platform/frameworks/base/+/140ff5ea8e2d99edc3fbe63a43239e459334c76b | ||
} | ||
|
||
if (framesRates != null) { | ||
if (framesRates != null && framesRates.length > 0) { | ||
final SparseIntArray totalIndexArray = framesRates[FrameMetricsAggregator.TOTAL_INDEX]; | ||
if (totalIndexArray != null) { | ||
for (int i = 0; i < totalIndexArray.size(); i++) { | ||
|
@@ -100,39 +101,98 @@ public synchronized void setMetrics( | |
} | ||
} | ||
|
||
if (totalFrames == 0 && slowFrames == 0 && frozenFrames == 0) { | ||
return new FrameCounts(totalFrames, slowFrames, frozenFrames); | ||
} | ||
|
||
@SuppressWarnings("NullAway") | ||
public synchronized void setMetrics( | ||
final @NotNull Activity activity, final @NotNull SentryId transactionId) { | ||
if (!isFrameMetricsAggregatorAvailable()) { | ||
return; | ||
} | ||
|
||
try { | ||
// NOTE: removing an activity does not reset the frame counts, only reset() does | ||
frameMetricsAggregator.remove(activity); | ||
} catch (Throwable ignored) { | ||
// throws IllegalArgumentException when attempting to remove OnFrameMetricsAvailableListener | ||
// that was never added. | ||
// there's no contains method. | ||
// throws NullPointerException when attempting to remove OnFrameMetricsAvailableListener and | ||
// there was no | ||
// Observers, See | ||
// https://android.googlesource.com/platform/frameworks/base/+/140ff5ea8e2d99edc3fbe63a43239e459334c76b | ||
} | ||
|
||
final @Nullable FrameCounts frameCounts = diffFrameCountsAtEnd(activity); | ||
|
||
if (frameCounts == null | ||
|| (frameCounts.totalFrames == 0 | ||
&& frameCounts.slowFrames == 0 | ||
&& frameCounts.frozenFrames == 0)) { | ||
return; | ||
} | ||
|
||
final MeasurementValue tfValues = new MeasurementValue(totalFrames, NONE_UNIT); | ||
final MeasurementValue sfValues = new MeasurementValue(slowFrames, NONE_UNIT); | ||
final MeasurementValue ffValues = new MeasurementValue(frozenFrames, NONE_UNIT); | ||
final MeasurementValue tfValues = new MeasurementValue(frameCounts.totalFrames, NONE_UNIT); | ||
final MeasurementValue sfValues = new MeasurementValue(frameCounts.slowFrames, NONE_UNIT); | ||
final MeasurementValue ffValues = new MeasurementValue(frameCounts.frozenFrames, NONE_UNIT); | ||
final Map<String, @NotNull MeasurementValue> measurements = new HashMap<>(); | ||
measurements.put("frames_total", tfValues); | ||
measurements.put("frames_slow", sfValues); | ||
measurements.put("frames_frozen", ffValues); | ||
|
||
activityMeasurements.put(sentryId, measurements); | ||
activityMeasurements.put(transactionId, measurements); | ||
} | ||
|
||
private @Nullable FrameCounts diffFrameCountsAtEnd(final @NotNull Activity activity) { | ||
@Nullable final FrameCounts frameCountsAtStart = frameCountAtStartSnapshots.remove(activity); | ||
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. Thanks 🙏 |
||
if (frameCountsAtStart == null) { | ||
return null; | ||
} | ||
|
||
@Nullable final FrameCounts frameCountsAtEnd = calculateCurrentFrameCounts(); | ||
if (frameCountsAtEnd == null) { | ||
return null; | ||
} | ||
|
||
final int diffTotalFrames = frameCountsAtEnd.totalFrames - frameCountsAtStart.totalFrames; | ||
final int diffSlowFrames = frameCountsAtEnd.slowFrames - frameCountsAtStart.slowFrames; | ||
final int diffFrozenFrames = frameCountsAtEnd.frozenFrames - frameCountsAtStart.frozenFrames; | ||
|
||
return new FrameCounts(diffTotalFrames, diffSlowFrames, diffFrozenFrames); | ||
} | ||
|
||
@Nullable | ||
public synchronized Map<String, @NotNull MeasurementValue> takeMetrics( | ||
final @NotNull SentryId sentryId) { | ||
final @NotNull SentryId transactionId) { | ||
if (!isFrameMetricsAggregatorAvailable()) { | ||
return null; | ||
} | ||
|
||
final Map<String, @NotNull MeasurementValue> stringMeasurementValueMap = | ||
activityMeasurements.get(sentryId); | ||
activityMeasurements.remove(sentryId); | ||
activityMeasurements.get(transactionId); | ||
activityMeasurements.remove(transactionId); | ||
return stringMeasurementValueMap; | ||
} | ||
|
||
@SuppressWarnings("NullAway") | ||
public synchronized void stop() { | ||
if (isFrameMetricsAggregatorAvailable()) { | ||
frameMetricsAggregator.stop(); | ||
frameMetricsAggregator.reset(); | ||
} | ||
activityMeasurements.clear(); | ||
} | ||
|
||
private static final class FrameCounts { | ||
private final int totalFrames; | ||
private final int slowFrames; | ||
private final int frozenFrames; | ||
|
||
private FrameCounts(final int totalFrames, final int slowFrames, final int frozenFrames) { | ||
this.totalFrames = totalFrames; | ||
this.slowFrames = slowFrames; | ||
this.frozenFrames = frozenFrames; | ||
} | ||
} | ||
} |
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.
@marandaneto since no public API changes were necessary I assume this fix doesn't require any further action on hybrid SDKs.