Skip to content

Commit

Permalink
[One .NET] Profiled AOT support
Browse files Browse the repository at this point in the history
Fixes: dotnet#6053

This is still WIP. Still a few pieces missing on the dotnet/runtime to
enable this:

dotnet/runtime#56989

We will need a `libmono-profiler-aot.so` from dotnet/runtime to be
able to record AOT profiles.

For now, I could:

1. Use the `startup.aotprofile` we're already shipping in "legacy"
   Xamarin.Android.
2. Pass this in when `$(AndroidEnableProfiledAot)` is `true`.

~~ Results ~~

All tests:

 1. Were running on a [Google Pixel 5][0], and
 2. Enabled two architectures, arm64 and x86, and
 3. **AOT time** was average of 10 runs with `-c Release
    -p:RunAOTCompilation=true`, with the`Activity: Displayed` time
 4. **Profiled AOT time** was average of 10 runs with `-c Release
    -p:RunAOTCompilation=true -p:AndroidEnableProfiledAot=true` with
    the `Activity: Displayed` time.

| Test                |      AOT time | Profiled AOT time |  AOT apk size | Profiled AOT apk size |
| ------------------- | ------------: | ----------------: | ------------: | --------------------: |
| [HelloAndroid][1]   |  00:00:00.246 |      00:00:00.288 |    12,151,755 |             9,161,675 |
| [HelloMaui][2]      |  00:00:00.619 |      00:00:01.131 |    43,442,233 |            19,992,633 |

From these results, we see that Profiled AOT is AOT'ing *some* of the
assemblies.

We are not getting the best startup time yet, because some methods are
still using the JIT:

    08-06 14:12:34.985 30817 30817 D Mono    : AOT: FOUND method Android.Runtime.JNIEnv:NewGlobalRef (intptr) [0x7ae3c8c620 - 0x7ae3c8c6a0 0x7ae3c9849c]
    08-06 14:12:34.985 30817 30817 D Mono    : AOT: NOT FOUND: intptr:op_Explicit (intptr).
    08-06 14:12:34.985 30817 30817 D Mono    : AOT: NOT FOUND: intptr:op_Explicit (int).
    08-06 14:12:34.986 30817 30817 D Mono    : AOT: FOUND method System.WeakReference`1:.ctor (T,bool) [0x7ae3d8c580 - 0x7ae3d8c5d0 0x7ae3ddaec1]
    08-06 14:12:34.986 30817 30817 D Mono    : AOT: FOUND method System.WeakReference`1:Create (T,bool) [0x7ae3d8c4c0 - 0x7ae3d8c580 0x7ae3ddaebd]
    08-06 14:12:34.986 30817 30817 D Mono    : AOT: NOT FOUND: System.Runtime.InteropServices.GCHandle:Alloc (object,System.Runtime.InteropServices.GCHandleType).
    08-06 14:12:34.986 30817 30817 D Mono    : AOT: NOT FOUND: System.Runtime.InteropServices.GCHandle:Alloc (object,System.Runtime.InteropServices.GCHandleType).
    08-06 14:12:34.986 30817 30817 D Mono    : AOT: NOT FOUND: System.Runtime.InteropServices.GCHandle:.ctor (object,System.Runtime.InteropServices.GCHandleType).

Overall, seems promising, though.

[0]: store.google.com/us/product/pixel_5_specs?hl=en-US
[1]: dotnet/maui-samples@7144604/HelloAndroid
[2]: dotnet/maui-samples@7144604/HelloMaui
  • Loading branch information
jonathanpeppers committed Aug 12, 2021
1 parent 2866832 commit 2df0344
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 20 deletions.
1 change: 1 addition & 0 deletions build-tools/create-packs/Microsoft.Android.Sdk.proj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ core workload SDK packs imported by WorkloadManifest.targets.
<_PackageFiles Include="$(XamarinAndroidSourcePath)src\Xamarin.Android.Build.Tasks\Microsoft.Android.Sdk\Sdk\**" PackagePath="Sdk" />
<_PackageFiles Include="$(XamarinAndroidSourcePath)src\Microsoft.Android.Sdk.ILLink\PreserveLists\**" PackagePath="PreserveLists" />
<_PackageFiles Include="$(XamarinAndroidSourcePath)src\Xamarin.Android.Build.Tasks\Microsoft.Android.Sdk\targets\**" PackagePath="targets" />
<_PackageFiles Include="$(XamarinAndroidSourcePath)src\Xamarin.Android.Build.Tasks\dotnet.aotprofile" PackagePath="targets" />
<_PackageFiles Include="$(IntermediateOutputPath)UnixFilePermissions.xml" PackagePath="data" Condition=" '$(HostOS)' != 'Windows' " />
<None Include="$(MSBuildThisFileDirectory)SignList.xml" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions build-tools/installers/create-installers.targets
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
<ItemGroup>
<_MSBuildFiles Include="$(MSBuildSrcDir)\android-support-multidex.jar" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\apksigner.jar" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\aprofutil.exe" ExcludeFromAndroidNETSdk="true" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\aprofutil.pdb" ExcludeFromAndroidNETSdk="true" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\aprofutil.exe" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\aprofutil.pdb" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\cil-strip.exe" ExcludeFromAndroidNETSdk="true" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\cil-strip.pdb" ExcludeFromAndroidNETSdk="true" />
<_MSBuildFiles Include="$(MSBuildSrcDir)\class-parse.exe" ExcludeFromAndroidNETSdk="true" />
Expand Down Expand Up @@ -355,7 +355,7 @@
<_MSBuildFilesUnixSignAndHarden Include="$(MSBuildSrcDir)\$(HostOS)\ndk\x86_64-linux-android-strip" />
<_MSBuildFilesUnix Include="$(MSBuildSrcDir)\$(HostOS)\illinkanalyzer" Permission="755" />
<_MSBuildFilesUnix Include="$(MSBuildSrcDir)\$(HostOS)\jit-times" Permission="755" />
<_MSBuildFilesUnix Include="$(MSBuildSrcDir)\$(HostOS)\aprofutil" ExcludeFromAndroidNETSdk="true" />
<_MSBuildFilesUnix Include="$(MSBuildSrcDir)\$(HostOS)\aprofutil" Permission="755" />
<_MSBuildFilesUnixSignAndHarden Include="$(MSBuildSrcDir)\$(HostOS)\mono" ExcludeFromAndroidNETSdk="true" />
<_MSBuildFilesUnix Include="$(MSBuildSrcDir)\$(HostOS)\mono.config" />
<_MSBuildFilesUnix Include="$(MSBuildSrcDir)\$(HostOS)\mono-symbolicate" ExcludeFromAndroidNETSdk="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).
TempDirectory="$([MSBuild]::EnsureTrailingSlash($(_AotOutputDirectory)))%(FileName)"
AotArguments="$(_AotArguments),temp-path=$([System.IO.Path]::GetFullPath(%(_MonoAOTAssemblies.TempDirectory)))"
/>
<_AotProfiles Include="$(MSBuildThisFileDirectory)dotnet.aotprofile" Condition=" '$(AndroidEnableProfiledAot)' == 'true' " />
</ItemGroup>
<MakeDir Directories="$(IntermediateOutputPath)aot\;@(_MonoAOTAssemblies->'%(TempDirectory)')" />
<MonoAOTCompiler
Assemblies="@(_MonoAOTAssemblies)"
CompilerBinaryPath="$(_MonoAOTCompilerPath)"
AotProfilePath="@(_AotProfiles)"
DisableParallelAot="$(_DisableParallelAot)"
LibraryFormat="So"
Mode="$(AndroidAotMode)"
Expand Down
Binary file added src/Xamarin.Android.Build.Tasks/dotnet.aotprofile
Binary file not shown.
19 changes: 2 additions & 17 deletions src/monodroid/jni/monodroid-glue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,6 @@ MonodroidRuntime::set_trace_options (void)
mono_jit_set_trace_options (value.get ());
}

#if defined (NET6)
inline void
MonodroidRuntime::set_profile_options ()
{
Expand All @@ -1582,23 +1581,10 @@ MonodroidRuntime::set_profile_options ()
value.assign (prop_value);
}

#if defined (NET6)
// setenv(3) makes copies of its arguments
setenv ("DOTNET_DiagnosticPorts", value.get (), 1);
}
#else // def NET6
inline void
MonodroidRuntime::set_profile_options ()
{
// We want to avoid dynamic allocation, thus let’s create a buffer that can take both the property value and a
// path without allocation
dynamic_local_string<SENSIBLE_PATH_MAX + PROPERTY_VALUE_BUFFER_LEN> value;
{
dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN> prop_value;
if (androidSystem.monodroid_get_system_property (Debug::DEBUG_MONO_PROFILE_PROPERTY, prop_value) == 0)
return;

value.assign (prop_value.get (), prop_value.length ());
}
#endif // def NET6

constexpr char OUTPUT_ARG[] = "output=";
constexpr size_t OUTPUT_ARG_LEN = sizeof(OUTPUT_ARG) - 1;
Expand Down Expand Up @@ -1673,7 +1659,6 @@ MonodroidRuntime::set_profile_options ()
log_warn (LOG_DEFAULT, "Initializing profiler with options: %s", value.get ());
debug.monodroid_profiler_load (androidSystem.get_runtime_libdir (), value.get (), output_path.get ());
}
#endif // ndef NET6

/*
Disable LLVM signal handlers.
Expand Down

0 comments on commit 2df0344

Please sign in to comment.