Skip to content
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

Enable lazy loading of AOTd assemblies #6940

Merged
merged 4 commits into from
Apr 25, 2022
Merged

Conversation

grendello
Copy link
Contributor

@grendello grendello commented Apr 19, 2022

Fixes: #6935
Context: dotnet/runtime#67024

Add support for enabling lazy loading of AOTd assemblies and their
associated shared libraries, when targetting .NET6+.

The feature is enabled by default when AOT is enabled for Release
builds and can be disabled by setting the new
$(AndroidAotEnableLazyLoad) MSBuild property to false.

Performance gains are obvious, but their scale depends on where we look.
Results of running a Hello World MAUI app on Pixel 6 Pro:

Displayed time:

Before After Δ Notes
490.600 475.000 -3.18% ✓ preload enabled; 32-bit build
372.700 364.800 -2.12% ✓ preload enabled; 64-bit build
476.100 467.100 -1.89% ✓ preload disabled; 32-bit build; no compression
483.900 476.900 -1.45% ✓ preload disabled; 32-bit build
366.400 361.600 -1.31% ✓ preload disabled; 64-bit build; no compression
470.500 468.800 -0.36% ✓ preload enabled; 32-bit build; no compression
363.500 363.800 +0.08% ✗ preload enabled; 64-bit build; no compression
367.800 368.200 +0.11% ✗ preload disabled; 64-bit build

Just the JNIEnv.Initialize call during native init:

Before After Δ Notes
6.242 5.225 -16.29% ✓ preload enabled; 64-bit build; no compression
6.175 5.200 -15.79% ✓ preload enabled; 64-bit build
8.846 7.566 -14.46% ✓ preload disabled; 32-bit build; no compression
6.221 5.367 -13.73% ✓ preload disabled; 64-bit build
8.731 7.747 -11.27% ✓ preload enabled; 32-bit build; no compression
6.110 5.443 -10.90% ✓ preload disabled; 64-bit build; no compression
8.896 7.955 -10.57% ✓ preload disabled; 32-bit build
8.602 7.787 -9.48% ✓ preload enabled; 32-bit build

Total time taken by the native init (before handing control over to the managed
land):

Before After Δ Notes
38.545 19.769 -48.71% ✓ preload enabled; 64-bit build
39.047 20.395 -47.77% ✓ preload disabled; 64-bit build
32.218 16.960 -47.36% ✓ preload enabled; 64-bit build; no compression
32.211 17.615 -45.31% ✓ preload disabled; 64-bit build; no compression
47.988 26.566 -44.64% ✓ preload disabled; 32-bit build
47.726 27.156 -43.10% ✓ preload enabled; 32-bit build
41.426 24.058 -41.93% ✓ preload enabled; 32-bit build; no compression
40.949 24.302 -40.65% ✓ preload disabled; 32-bit build; no compression

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you able to time the difference?

@grendello
Copy link
Contributor Author

Were you able to time the difference?

Yep, I will time it in a few, want to fix the test failures first

@grendello
Copy link
Contributor Author

Unfortunately, enabling the feature for 64-bit targets results in this Mono runtime abort:

04-19 17:39:10.059 23263 23263 E osoft.hellomau: * Assertion at /__w/1/s/src/mono/mono/mini/tramp-arm64.c:53, condition `((ins >> 24) & 0x1f) == 0x10' not met
--------- beginning of crash
04-19 17:39:10.059 23263 23263 F libc    : Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 23263 (osoft.hellomaui), pid 23263 (osoft.hellomaui)
04-19 17:39:10.067  3642  3688 W earchbox:searc: Profile data size exceeds 500000 bytes. It has 517865 bytes.
04-19 17:39:10.087 23283 23283 I crash_dump64: obtaining output fd from tombstoned, type: kDebuggerdTombstoneProto
04-19 17:39:10.088   695   695 I tombstoned: received crash request for pid 23263
04-19 17:39:10.088 23283 23283 I crash_dump64: performing dump of process 23263 (target tid = 23263)
04-19 17:39:10.093 23283 23283 E DEBUG   : failed to read /proc/uptime: Permission denied
04-19 17:39:10.156 23283 23283 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
04-19 17:39:10.156 23283 23283 F DEBUG   : Build fingerprint: 'google/raven/raven:12/SP2A.220405.004/8233519:user/release-keys'
04-19 17:39:10.156 23283 23283 F DEBUG   : Revision: 'MP1.0'
04-19 17:39:10.156 23283 23283 F DEBUG   : ABI: 'arm64'
04-19 17:39:10.156 23283 23283 F DEBUG   : Timestamp: 2022-04-19 17:39:10.093100770+0200
04-19 17:39:10.156 23283 23283 F DEBUG   : Process uptime: 0s
04-19 17:39:10.156 23283 23283 F DEBUG   : Cmdline: com.microsoft.hellomaui
04-19 17:39:10.156 23283 23283 F DEBUG   : pid: 23263, tid: 23263, name: osoft.hellomaui  >>> com.microsoft.hellomaui <<<
04-19 17:39:10.156 23283 23283 F DEBUG   : uid: 10468
04-19 17:39:10.156 23283 23283 F DEBUG   : tagged_addr_ctrl: 0000000000000001
04-19 17:39:10.156 23283 23283 F DEBUG   : signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
04-19 17:39:10.156 23283 23283 F DEBUG   :     x0  0000000000000000  x1  0000000000005adf  x2  0000000000000006  x3  0000007fe32429b0
04-19 17:39:10.156 23283 23283 F DEBUG   :     x4  6e6d6e6c2e627172  x5  6e6d6e6c2e627172  x6  6e6d6e6c2e627172  x7  7f7f7f7f7f7f7f7f
04-19 17:39:10.156 23283 23283 F DEBUG   :     x8  00000000000000f0  x9  0000007ee0f4b0b0  x10 0000000000000000  x11 ffffff80fffffbdf
04-19 17:39:10.156 23283 23283 F DEBUG   :     x12 0000000000000001  x13 000000000000006f  x14 0000007fe3241850  x15 00000006bfd1824f
04-19 17:39:10.156 23283 23283 F DEBUG   :     x16 0000007ee0fe8050  x17 0000007ee0fc5bd0  x18 0000007efaa7c000  x19 0000000000005adf
04-19 17:39:10.156 23283 23283 F DEBUG   :     x20 0000000000005adf  x21 00000000ffffffff  x22 0000007bc4498634  x23 0000007ee64d08c8
04-19 17:39:10.156 23283 23283 F DEBUG   :     x24 b400007dc7446c60  x25 0000007bc44652b2  x26 0000007bc448fbc0  x27 0000007bc6594000
04-19 17:39:10.156 23283 23283 F DEBUG   :     x28 0000007ef9e06000  x29 0000007fe3242a30
04-19 17:39:10.156 23283 23283 F DEBUG   :     lr  0000007ee0f7872c  sp  0000007fe3242990  pc  0000007ee0f7875c  pst 0000000000001000
04-19 17:39:10.156 23283 23283 F DEBUG   : backtrace:
04-19 17:39:10.156 23283 23283 F DEBUG   :       #00 pc 000000000004f75c  /apex/com.android.runtime/lib64/bionic/libc.so (abort+168) (BuildId: 53a228529316d67f22e241dd17ea9b9e)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #01 pc 0000000000029a44  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmono-android.release.so (xamarin::android::internal::MonodroidRuntime::mono_log_handler(char const*, char const*, char const*, int, void*)+144) (BuildId: 42930c597d00d00f3b1499d0eec0f9344851ec1b)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #02 pc 0000000000264074  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (BuildId: 251a05655a142d99e874b57ce251b592262dcda3)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #03 pc 00000000002641a0  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (BuildId: 251a05655a142d99e874b57ce251b592262dcda3)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #04 pc 00000000002641e4  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (BuildId: 251a05655a142d99e874b57ce251b592262dcda3)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #05 pc 0000000000223908  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (BuildId: 251a05655a142d99e874b57ce251b592262dcda3)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #06 pc 00000000001d92c0  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (BuildId: 251a05655a142d99e874b57ce251b592262dcda3)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #07 pc 00000000001d956c  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (BuildId: 251a05655a142d99e874b57ce251b592262dcda3)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #08 pc 00000000001eb558  /data/app/~~xLKRytPGIfZB8DXZv6jhZQ==/com.microsoft.hellomaui-0sdsM71KJIG0ObMb3Ky2sg==/split_config.arm64_v8a.apk!libmonosgen-2.0.so (BuildId: 251a05655a142d99e874b57ce251b592262dcda3)
04-19 17:39:10.156 23283 23283 F DEBUG   :       #09 pc 0000000000004ef8  <anonymous:7ee717d000>

@jonathanpeppers
Copy link
Member

@lambdageek @vargaz ideas about the issue above? Should we just file an issue on dotnet/runtime?

@grendello
Copy link
Contributor Author

@jonathanpeppers I already talked to @vargaz, we'll continue tomorrow :)

@grendello
Copy link
Contributor Author

@grendello
Copy link
Contributor Author

Please don't merge until #6946 is in and I can rebase this PR on it

Fixes: dotnet#6935
Context: dotnet/runtime#67024

Add support for enabling lazy loading of AOTd assemblies and their
associated shared libraries, when targetting .NET6+.

The feature is enabled by default when AOT is enabled for `Release`
builds and can be disabled by setting the new
`$(AndroidAotEnableLazyLoad)` MSBuild property to `false`.
@jonpryor
Copy link
Member

jonpryor commented Apr 25, 2022

Fixes: https://github.com/xamarin/xamarin-android/issues/6935

Context: https://github.com/dotnet/runtime/pull/67024

Add support for enabling lazy loading of AOTd assemblies and their
associated shared libraries, when targeting .NET6+.

The feature is enabled by default when AOT is enabled for `Release`
builds and can be disabled by setting the new
`$(AndroidAotEnableLazyLoad)` MSBuild property to `false`.

Performance gains are obvious, but their scale depends on where we look.
Results of running a Hello World MAUI app on Pixel 6 Pro, with:

See PR for full timing details.

With:

  * `$(RuntimeIdentifier)`=android-arm64
  * `$(AndroidEnablePreloadAssemblies)`=False
  * `$(AndroidUseAssemblyStore)`=True, `$(AndroidEnableAssemblyCompression)`=True

| Scenario                              |    Before |     After |         Δ |
| ------------------------------------- | --------: | --------: | --------: |
| `ActivityTaskManager: Displayed` time |   367.800 |   368.200 |  +0.11% ✗ |
| `JNIEnv.Initialize()` time            |     6.221 |     5.367 | -13.73% ✓ |
| Total native init time                |    39.047 |    20.395 | -47.77% ✓ |

With:

  * `$(RuntimeIdentifier)`=android-arm
  * `$(AndroidEnablePreloadAssemblies)`=True
  * `$(AndroidUseAssemblyStore)`=True, `$(AndroidEnableAssemblyCompression)`=True

| Scenario                              |    Before |     After |         Δ |
| ------------------------------------- | --------: | --------: | --------: |
| `ActivityTaskManager: Displayed` time |   490.600 |   475.000 |  -3.18% ✓ |
| `JNIEnv.Initialize()` time            |     8.602 |     7.787 |  -9.48% ✓ |
| Total native init time                |    47.726 |    27.156 | -43.10% ✓ |

@jonpryor jonpryor merged commit 6dc426f into dotnet:main Apr 25, 2022
@grendello grendello deleted the aot-lazy-load branch April 25, 2022 20:19
jonathanpeppers pushed a commit to jonathanpeppers/xamarin-android that referenced this pull request Apr 25, 2022
Backports: dotnet#6940
Fixes: dotnet#6935

Context: dotnet/runtime#67024

Add support for enabling lazy loading of AOTd assemblies and their
associated shared libraries, when targeting .NET6+.

The feature is enabled by default when AOT is enabled for `Release`
builds and can be disabled by setting the new
`$(AndroidAotEnableLazyLoad)` MSBuild property to `false`.

Performance gains are obvious, but their scale depends on where we look.
Results of running a Hello World MAUI app on Pixel 6 Pro, with:

See PR for full timing details.

With:

  * `$(RuntimeIdentifier)`=android-arm64
  * `$(AndroidEnablePreloadAssemblies)`=False
  * `$(AndroidUseAssemblyStore)`=True, `$(AndroidEnableAssemblyCompression)`=True

| Scenario                              |    Before |     After |         Δ |
| ------------------------------------- | --------: | --------: | --------: |
| `ActivityTaskManager: Displayed` time |   367.800 |   368.200 |  +0.11% ✗ |
| `JNIEnv.Initialize()` time            |     6.221 |     5.367 | -13.73% ✓ |
| Total native init time                |    39.047 |    20.395 | -47.77% ✓ |

With:

  * `$(RuntimeIdentifier)`=android-arm
  * `$(AndroidEnablePreloadAssemblies)`=True
  * `$(AndroidUseAssemblyStore)`=True, `$(AndroidEnableAssemblyCompression)`=True

| Scenario                              |    Before |     After |         Δ |
| ------------------------------------- | --------: | --------: | --------: |
| `ActivityTaskManager: Displayed` time |   490.600 |   475.000 |  -3.18% ✓ |
| `JNIEnv.Initialize()` time            |     8.602 |     7.787 |  -9.48% ✓ |
| Total native init time                |    47.726 |    27.156 | -43.10% ✓ |
jonathanpeppers added a commit that referenced this pull request Apr 26, 2022
Backports: #6940
Fixes: #6935

Context: dotnet/runtime#67024

Add support for enabling lazy loading of AOTd assemblies and their
associated shared libraries, when targeting .NET6+.

The feature is enabled by default when AOT is enabled for `Release`
builds and can be disabled by setting the new
`$(AndroidAotEnableLazyLoad)` MSBuild property to `false`.

Performance gains are obvious, but their scale depends on where we look.
Results of running a Hello World MAUI app on Pixel 6 Pro, with:

See PR for full timing details.

With:

  * `$(RuntimeIdentifier)`=android-arm64
  * `$(AndroidEnablePreloadAssemblies)`=False
  * `$(AndroidUseAssemblyStore)`=True, `$(AndroidEnableAssemblyCompression)`=True

| Scenario                              |    Before |     After |         Δ |
| ------------------------------------- | --------: | --------: | --------: |
| `ActivityTaskManager: Displayed` time |   367.800 |   368.200 |  +0.11% ✗ |
| `JNIEnv.Initialize()` time            |     6.221 |     5.367 | -13.73% ✓ |
| Total native init time                |    39.047 |    20.395 | -47.77% ✓ |

With:

  * `$(RuntimeIdentifier)`=android-arm
  * `$(AndroidEnablePreloadAssemblies)`=True
  * `$(AndroidUseAssemblyStore)`=True, `$(AndroidEnableAssemblyCompression)`=True

| Scenario                              |    Before |     After |         Δ |
| ------------------------------------- | --------: | --------: | --------: |
| `ActivityTaskManager: Displayed` time |   490.600 |   475.000 |  -3.18% ✓ |
| `JNIEnv.Initialize()` time            |     8.602 |     7.787 |  -9.48% ✓ |
| Total native init time                |    47.726 |    27.156 | -43.10% ✓ |

Co-authored-by: Marek Habersack <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Jan 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add .NET 6 support for --aot-lazy-assembly-load
3 participants