-
-
Notifications
You must be signed in to change notification settings - Fork 464
Add Continuous Profiling support (v8) #3710
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f0d8942
added IContinuousProfiler and implementations
stefanosiano ec061f9
updated IHub to IScopes
stefanosiano cb3c9d3
Merge branch 'refs/heads/8.x.x' into feat/continuous-profiling-part1
stefanosiano 4365bc3
Merge branch '8.x.x' into feat/continuous-profiling-part1
stefanosiano 209b03d
wrapped continuous profiler close future in a try catch block
stefanosiano da77271
Merge branch '8.x.x' into feat/continuous-profiling-part1
stefanosiano a9235ae
Change payload for Continuous Profiling v8 (p2) (#3711)
stefanosiano 6c36c4b
Merge branch '8.x.x' into feat/continuous-profiling-part1
stefanosiano b0e8333
Add rate limit for Continuous Profiling v8 (p6) (#3926)
stefanosiano e70d583
added SentryOptions.continuousProfilesSampleRate (#4013)
stefanosiano 87bdd6d
Wrap up continuous profiling (#4069)
stefanosiano fcfa576
Change continuous profiling to session sample rate (#4180)
stefanosiano 1440de9
Rename continuous profiling APIs (#4182)
stefanosiano 27d91aa
Add continuous profiling ProfileLifecycle (#4202)
stefanosiano 3d0fb3c
Add Continuous Profiling isStartProfilerOnAppStart option (#4226)
stefanosiano 1206307
Merge branch 'refs/heads/main' into feat/continuous-profiling-part1
stefanosiano c7164d7
merged main and updated tests
stefanosiano 1dfdeb8
Merge branch 'main' into feat/continuous-profiling-part1
stefanosiano d68733d
Merge branch 'main' into feat/continuous-profiling-part1
stefanosiano f467dbe
merged main and updated tests
stefanosiano 9954e1c
merged main and updated tests
stefanosiano 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
170 changes: 170 additions & 0 deletions
170
sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.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,170 @@ | ||
| package io.sentry.android.core; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
|
||
| import android.annotation.SuppressLint; | ||
| import android.os.Build; | ||
| import io.sentry.IContinuousProfiler; | ||
| import io.sentry.ILogger; | ||
| import io.sentry.IScopes; | ||
| import io.sentry.ISentryExecutorService; | ||
| import io.sentry.SentryLevel; | ||
| import io.sentry.android.core.internal.util.SentryFrameMetricsCollector; | ||
| import java.util.concurrent.Future; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.jetbrains.annotations.VisibleForTesting; | ||
|
|
||
| @ApiStatus.Internal | ||
| public class AndroidContinuousProfiler implements IContinuousProfiler { | ||
| private static final long MAX_CHUNK_DURATION_MILLIS = 10000; | ||
|
|
||
| private final @NotNull ILogger logger; | ||
| private final @Nullable String profilingTracesDirPath; | ||
| private final boolean isProfilingEnabled; | ||
| private final int profilingTracesHz; | ||
| private final @NotNull ISentryExecutorService executorService; | ||
| private final @NotNull BuildInfoProvider buildInfoProvider; | ||
| private boolean isInitialized = false; | ||
| private final @NotNull SentryFrameMetricsCollector frameMetricsCollector; | ||
| private @Nullable AndroidProfiler profiler = null; | ||
| private boolean isRunning = false; | ||
| private @Nullable IScopes scopes; | ||
| private @Nullable Future<?> closeFuture; | ||
|
|
||
| public AndroidContinuousProfiler( | ||
| final @NotNull BuildInfoProvider buildInfoProvider, | ||
| final @NotNull SentryFrameMetricsCollector frameMetricsCollector, | ||
| final @NotNull ILogger logger, | ||
| final @Nullable String profilingTracesDirPath, | ||
| final boolean isProfilingEnabled, | ||
| final int profilingTracesHz, | ||
| final @NotNull ISentryExecutorService executorService) { | ||
| this.logger = logger; | ||
| this.frameMetricsCollector = frameMetricsCollector; | ||
| this.buildInfoProvider = buildInfoProvider; | ||
| this.profilingTracesDirPath = profilingTracesDirPath; | ||
| this.isProfilingEnabled = isProfilingEnabled; | ||
| this.profilingTracesHz = profilingTracesHz; | ||
| this.executorService = executorService; | ||
| } | ||
|
|
||
| private void init() { | ||
| // We initialize it only once | ||
| if (isInitialized) { | ||
| return; | ||
| } | ||
| isInitialized = true; | ||
| if (!isProfilingEnabled) { | ||
| logger.log(SentryLevel.INFO, "Profiling is disabled in options."); | ||
| return; | ||
| } | ||
| if (profilingTracesDirPath == null) { | ||
| logger.log( | ||
| SentryLevel.WARNING, | ||
| "Disabling profiling because no profiling traces dir path is defined in options."); | ||
| return; | ||
| } | ||
| if (profilingTracesHz <= 0) { | ||
| logger.log( | ||
| SentryLevel.WARNING, | ||
| "Disabling profiling because trace rate is set to %d", | ||
| profilingTracesHz); | ||
| return; | ||
| } | ||
|
|
||
| profiler = | ||
| new AndroidProfiler( | ||
| profilingTracesDirPath, | ||
| (int) SECONDS.toMicros(1) / profilingTracesHz, | ||
| frameMetricsCollector, | ||
| null, | ||
| logger, | ||
| buildInfoProvider); | ||
| } | ||
|
|
||
| public synchronized void setScopes(final @NotNull IScopes scopes) { | ||
| this.scopes = scopes; | ||
| } | ||
|
|
||
| public synchronized void start() { | ||
| // Debug.startMethodTracingSampling() is only available since Lollipop, but Android Profiler | ||
| // causes crashes on api 21 -> https://github.com/getsentry/sentry-java/issues/3392 | ||
| if (buildInfoProvider.getSdkInfoVersion() < Build.VERSION_CODES.LOLLIPOP_MR1) return; | ||
|
|
||
| // Let's initialize trace folder and profiling interval | ||
| init(); | ||
| // init() didn't create profiler, should never happen | ||
| if (profiler == null) { | ||
| return; | ||
| } | ||
|
|
||
stefanosiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final AndroidProfiler.ProfileStartData startData = profiler.start(); | ||
| // check if profiling started | ||
| if (startData == null) { | ||
| return; | ||
| } | ||
| isRunning = true; | ||
|
|
||
| closeFuture = executorService.schedule(() -> stop(true), MAX_CHUNK_DURATION_MILLIS); | ||
stefanosiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public synchronized void stop() { | ||
| stop(false); | ||
| } | ||
|
|
||
| @SuppressLint("NewApi") | ||
| private synchronized void stop(final boolean restartProfiler) { | ||
| if (closeFuture != null) { | ||
| closeFuture.cancel(true); | ||
| } | ||
| // check if profiler was created and it's running | ||
| if (profiler == null || !isRunning) { | ||
| return; | ||
| } | ||
|
|
||
| // onTransactionStart() is only available since Lollipop_MR1 | ||
| // and SystemClock.elapsedRealtimeNanos() since Jelly Bean | ||
| if (buildInfoProvider.getSdkInfoVersion() < Build.VERSION_CODES.LOLLIPOP_MR1) { | ||
romtsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| // todo add PerformanceCollectionData | ||
| final AndroidProfiler.ProfileEndData endData = profiler.endAndCollect(false, null); | ||
|
|
||
| // check if profiler end successfully | ||
| if (endData == null) { | ||
| return; | ||
| } | ||
|
|
||
| isRunning = false; | ||
|
|
||
| // todo schedule capture profile chunk envelope | ||
|
|
||
| if (restartProfiler) { | ||
| logger.log(SentryLevel.DEBUG, "Profile chunk finished. Starting a new one."); | ||
| start(); | ||
| } else { | ||
| logger.log(SentryLevel.DEBUG, "Profile chunk finished."); | ||
| } | ||
| } | ||
|
|
||
| public synchronized void close() { | ||
| if (closeFuture != null) { | ||
| closeFuture.cancel(true); | ||
| } | ||
stefanosiano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| stop(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isRunning() { | ||
| return isRunning; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| @Nullable | ||
| Future<?> getCloseFuture() { | ||
| return closeFuture; | ||
| } | ||
| } | ||
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
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.