-
Notifications
You must be signed in to change notification settings - Fork 580
[CoreCLR/NativeAOT] Use a no-GC region during Android startup #12782
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
simonrozsival
wants to merge
9
commits into
main
Choose a base branch
from
simonrozsival-coreclr-startup-no-gc
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.
+129
−2
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3edd00a
[CoreCLR] Use no-GC region during Android startup
simonrozsival be35259
[CoreCLR] Add startup no-GC opt-out
simonrozsival a0ee861
Use startup no-GC region with NativeAOT
simonrozsival 149aa6e
Use System.Threading.Lock for startup GC state
simonrozsival 5b09e54
Name startup no-GC option argument
simonrozsival 5c34535
Centralize startup no-GC runtime gating
simonrozsival 5d71d43
Use unmanaged Activity fully-drawn callback
simonrozsival 904163a
Restore standard Activity JNI callback
simonrozsival ab4b439
Keep startup no-GC completion private
simonrozsival 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| using System; | ||
| using System.Threading; | ||
|
|
||
| namespace Android.Runtime; | ||
|
|
||
| sealed class StartupNoGCRegion | ||
| { | ||
| const long Budget = 24 * 1024 * 1024; | ||
| // Bound the process-wide region when an app never reports that startup is fully drawn. | ||
| static readonly TimeSpan DefaultFallbackTimeout = TimeSpan.FromSeconds (10); | ||
| static readonly StartupNoGCRegion instance = new (); | ||
|
|
||
| readonly Lock sync = new (); | ||
| Timer? fallbackTimer; | ||
| State state; | ||
|
|
||
| enum State | ||
| { | ||
| NotStarted, | ||
| Active, | ||
| Ended, | ||
| } | ||
|
|
||
| internal static void Start () => instance.StartRegion (); | ||
|
|
||
| internal static void End () => instance.Finish (); | ||
|
|
||
| void StartRegion () | ||
| { | ||
| if (Microsoft.Android.Runtime.RuntimeFeature.IsMonoRuntime) { | ||
| return; | ||
| } | ||
|
|
||
| lock (sync) { | ||
| if (state != State.NotStarted) { | ||
| return; | ||
| } | ||
|
|
||
| bool started; | ||
| try { | ||
| started = GC.TryStartNoGCRegion (Budget, disallowFullBlockingGC: true); | ||
| } catch (InvalidOperationException) { | ||
| state = State.Ended; | ||
| return; | ||
| } | ||
|
|
||
| if (!started) { | ||
| state = State.Ended; | ||
| return; | ||
| } | ||
|
|
||
| fallbackTimer = new Timer ( | ||
| static value => { | ||
| if (value is StartupNoGCRegion noGCRegion) { | ||
| noGCRegion.Finish (); | ||
| } | ||
| }, | ||
| this, | ||
| DefaultFallbackTimeout, | ||
| Timeout.InfiniteTimeSpan | ||
| ); | ||
| state = State.Active; | ||
| } | ||
| } | ||
|
|
||
| void Finish () | ||
| { | ||
| Timer? timer; | ||
| lock (sync) { | ||
| if (state != State.Active) { | ||
| return; | ||
| } | ||
|
|
||
| state = State.Ended; | ||
| timer = fallbackTimer; | ||
| fallbackTimer = null; | ||
| } | ||
|
|
||
| timer?.Dispose (); | ||
|
|
||
| try { | ||
| GC.EndNoGCRegion (); | ||
|
simonrozsival marked this conversation as resolved.
|
||
| } catch (InvalidOperationException) { | ||
| // The runtime already left the region because its budget was exhausted | ||
| // or a collection was induced. | ||
| } | ||
| } | ||
| } | ||
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.