Skip to content

Commit

Permalink
[WIP] new Profiled AOT support
Browse files Browse the repository at this point in the history
Context: dotnet/runtime#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:

    <PropertyGroup>
      <AndroidUseDefaultAotProfile>false</AndroidUseDefaultAotProfile>
    </PropertyGroup>
    <ItemGroup>
      <AndroidMibcProfile Include="android.mibc" />
    </ItemGroup>

~~ 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]
  • Loading branch information
jonathanpeppers committed Jul 28, 2022
1 parent c3f00cd commit 6e98ccd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)">
<Output PropertyName="_Triple" TaskParameter="Triple" />
<Output PropertyName="_ToolPrefix" TaskParameter="ToolPrefix" />
Expand All @@ -84,6 +85,7 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).
<_MonoAOTAssemblies Update="@(_MonoAOTAssemblies)" ProcessArguments="$(AndroidExtraAotOptions)" />
</ItemGroup>
<PropertyGroup>
<_PgoBinaryPath Condition=" '$(_PgoBinaryPath)' == '' ">$(USERPROFILE)\.dotnet\tools\dotnet-pgo</_PgoBinaryPath>
<_MonoAOTCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier', '$(RuntimeIdentifier)'))</_MonoAOTCompilerPath>
<_LLVMPath Condition=" '$(EnableLLVM)' == 'true' ">$([System.IO.Path]::GetDirectoryName ('$(_MonoAOTCompilerPath)'))</_LLVMPath>
</PropertyGroup>
Expand All @@ -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)">
<Output TaskParameter="CompiledAssemblies" ItemName="_MonoAOTCompiledAssemblies" />
Expand Down
13 changes: 11 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/GetAotAssemblies.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +15,8 @@ public class GetAotAssemblies : GetAotArguments
{
public override string TaskPrefix => "GAOT";

public ITaskItem [] MibcProfiles { get; set; } = Array.Empty<ITaskItem> ();

public override Task RunTaskAsync ()
{
NdkTools ndk = NdkTools.Create (AndroidNdkDirectory, logErrors: EnableLLVM, log: Log);
Expand Down Expand Up @@ -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));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/profiled-aot/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<PropertyGroup>
<Configuration>Release</Configuration>
<AndroidNeedsInternetPermission>true</AndroidNeedsInternetPermission>
<AndroidEnableAotProfiler>true</AndroidEnableAotProfiler>
<!-- <AndroidEnableAotProfiler>true</AndroidEnableAotProfiler> -->
<AndroidEnableProfiler>true</AndroidEnableProfiler>
<RunAOTCompilation>false</RunAOTCompilation>
</PropertyGroup>

Expand Down
10 changes: 3 additions & 7 deletions src/profiled-aot/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)CommonMethods.cs" />
<AndroidAotProfile Include="custom.aprof" />
<PackageReference Include="Mono.AotProfiler.Android" Version="7.0.0-preview1" />
<!-- <AndroidAotProfile Include="custom.aprof" />
<PackageReference Include="Mono.AotProfiler.Android" Version="7.0.0-preview1" /> -->
</ItemGroup>
<PropertyGroup>
<RecordDependsOn>
Clean;
_ClearSystemProperties;
BuildAndStartAotProfiling;
_Sleep;
FinishAotProfiling;
_StripAppMethods;
_SaveMethodNames;
Run;
</RecordDependsOn>
</PropertyGroup>
<Target Name="Record" DependsOnTargets="$(RecordDependsOn)">
Expand Down

0 comments on commit 6e98ccd

Please sign in to comment.