From 6e98ccdcc819dab69fb14797249f1e802515c89e Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 28 Jul 2022 15:59:39 -0500 Subject: [PATCH] [WIP] new Profiled AOT support Context: https://github.com/dotnet/runtime/pull/71787 Context: https://dev.azure.com/dnceng/public/_artifacts/feed/7.0.100-preview.7.22377.5-nonshipping/NuGet/dotnet-pgo/overview/7.0.0-preview.7.22375.6 1. Record a `.nettrace` file: adb reverse tcp:9000 tcp:9001 adb shell setprop debug.mono.profile '127.0.0.1:9000,suspend' dotnet-dsrouter client-server -tcps 127.0.0.1:9001 -ipcc /tmp/maui-app --verbose debug dotnet-trace collect --diagnostic-port /tmp/maui-app --providers Microsoft-Windows-DotNETRuntime:0x1F000080018:5 --output android.nettrace 2. Install `dotnet-pgo`: dotnet tool install -g dotnet-pgo --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/7.0.100-preview.7.22377.5-nonshipping/nuget/v3/index.json --version 7.0.0-preview.7.22375.6 3. Convert to `.mibc`: dotnet-pgo create-mibc --trace android.nettrace --output android.mibc --reference "C:\src\xamarin-android\bin\Release\dotnet\packs\Microsoft.NETCore.App.Runtime.Mono.android-arm64\7.0.0-rc.1.22367.4\runtimes\android-arm64\native\System.Private.CoreLib.dll" --reference "C:\src\xamarin-android\bin\Release\dotnet\packs\Microsoft.NETCore.App.Runtime.Mono.android-arm64\7.0.0-rc.1.22367.4\runtimes\android-arm64\lib\net7.0\*.dll" To test this, I made a `dotnet new android` app and added: false ~~ Known Issues ~~ If you name the project `doo`, for example, instead of `android`, you run into: C:\src\xamarin-android\bin\Release\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.0-ci.dotnet-pgo.119\targets\Microsoft.Android.Sdk.Aot.targets(93,5): Precompiling failed for C:\src\doo\obj\Release\net7.0-android\android-arm64\linked\System.Linq.dll. Mono Ahead of Time compiler - compiling assembly C:\src\doo\obj\Release\net7.0-android\android-arm64\linked\System.Linq.dll AOTID 7AC4BDE2-68D0-3D51-483B-A1059F9CC51D * Assertion at D:\a\_work\1\s\src\mono\mono\mini\aot-compiler.c:13309, condition `is_ok (error)' not met, function:add_mibc_group_method_methods, Could not load file or assembly 'android, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. [C:\src\doo\doo.csproj] The main assembly is `doo.dll`, and the app has no `android.dll`. If you name the app `android`, you hit errors for methods in the profile that are not in the app: C:\src\xamarin-android\bin\Release\dotnet\packs\Microsoft.Android.Sdk.Windows\33.0.0-ci.dotnet-pgo.119\targets\Microsoft.Android.Sdk.Aot.targets(93,5): erro r : * Assertion at D:\a\_work\1\s\src\mono\mono\mini\aot-compiler.c:13309, condition `is_ok (error)' not met, function:add_mibc_group_method_methods, Could not resolve type with token 010000ad from typeref (expected class 'CommonMethods' in assembly 'android, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nul l') assembly:android, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null type:CommonMethods member:(null) [C:\src\doo\android.csproj] --- .../targets/Microsoft.Android.Sdk.Aot.targets | 4 ++++ .../Tasks/GetAotAssemblies.cs | 13 +++++++++++-- src/profiled-aot/Directory.Build.props | 3 ++- src/profiled-aot/Directory.Build.targets | 10 +++------- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets index cac685b8837..3e81dcea95e 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets @@ -72,6 +72,7 @@ They run in a context of an inner build with a single $(RuntimeIdentifier). RuntimeIdentifier="$(RuntimeIdentifier)" EnableLLVM="$(EnableLLVM)" Profiles="@(AndroidAotProfile)" + MibcProfiles="@(AndroidMibcProfile)" StripLibraries="$(_AndroidAotStripLibraries)"> @@ -84,6 +85,7 @@ They run in a context of an inner build with a single $(RuntimeIdentifier). <_MonoAOTAssemblies Update="@(_MonoAOTAssemblies)" ProcessArguments="$(AndroidExtraAotOptions)" /> + <_PgoBinaryPath Condition=" '$(_PgoBinaryPath)' == '' ">$(USERPROFILE)\.dotnet\tools\dotnet-pgo <_MonoAOTCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier', '$(RuntimeIdentifier)')) <_LLVMPath Condition=" '$(EnableLLVM)' == 'true' ">$([System.IO.Path]::GetDirectoryName ('$(_MonoAOTCompilerPath)')) @@ -105,6 +107,8 @@ They run in a context of an inner build with a single $(RuntimeIdentifier). LLVMPath="$(_LLVMPath)" LdName="$(_LdName)" LdFlags="$(_LdFlags)" + PgoBinaryPath="$(_PgoBinaryPath)" + NetTracePath="$(_NetTracePath)" WorkingDirectory="$(MSBuildProjectDirectory)" AotArguments="$(AndroidAotAdditionalArguments)"> diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAotAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAotAssemblies.cs index d9c98fa208a..d62098b7612 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GetAotAssemblies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAotAssemblies.cs @@ -1,7 +1,9 @@ +using System; using System.IO; using System.Text; using System.Threading.Tasks; using Microsoft.Android.Build.Tasks; +using Microsoft.Build.Framework; using Xamarin.Android.Tools; namespace Xamarin.Android.Tasks @@ -13,6 +15,8 @@ public class GetAotAssemblies : GetAotArguments { public override string TaskPrefix => "GAOT"; + public ITaskItem [] MibcProfiles { get; set; } = Array.Empty (); + public override Task RunTaskAsync () { NdkTools ndk = NdkTools.Create (AndroidNdkDirectory, logErrors: EnableLLVM, log: Log); @@ -51,9 +55,14 @@ public override Task RunTaskAsync () if (Profiles != null && Profiles.Length > 0) { aotProfiles.Append (",profile-only"); foreach (var p in Profiles) { - var fp = Path.GetFullPath (p.ItemSpec); aotProfiles.Append (",profile="); - aotProfiles.Append (fp); + aotProfiles.Append (Path.GetFullPath (p.ItemSpec)); + } + } else if (MibcProfiles != null && MibcProfiles.Length > 0) { + aotProfiles.Append (",profile-only"); + foreach (var p in MibcProfiles) { + aotProfiles.Append (",mibc-profile="); + aotProfiles.Append (Path.GetFullPath (p.ItemSpec)); } } diff --git a/src/profiled-aot/Directory.Build.props b/src/profiled-aot/Directory.Build.props index e3c07d765f6..c000a8f9fb0 100644 --- a/src/profiled-aot/Directory.Build.props +++ b/src/profiled-aot/Directory.Build.props @@ -2,7 +2,8 @@ Release true - true + + true false diff --git a/src/profiled-aot/Directory.Build.targets b/src/profiled-aot/Directory.Build.targets index 81e53cc0ecf..6c5a474121d 100644 --- a/src/profiled-aot/Directory.Build.targets +++ b/src/profiled-aot/Directory.Build.targets @@ -6,18 +6,14 @@ - - + Clean; _ClearSystemProperties; - BuildAndStartAotProfiling; - _Sleep; - FinishAotProfiling; - _StripAppMethods; - _SaveMethodNames; + Run;