Skip to content

Commit 853b367

Browse files
radekdoulikjonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] Add targets for AOT profiling (#3964)
Fixes: #3792 Context: #3740 Context: #3963 Add the following targets to simplify Profiled AOT usage: * `BuildAndStartAotProfiling` * `FinishAotProfiling` The `BuildAndStartAotProfiling` target builds an Application `.csproj` with the AOT profiler embedded into the `.apk`, installs the `.apk`, sets the AOT profiler socket port to `$(AndroidAotProfilerPort)`, and starts the *launch* Activity on the target device. `$(AndroidAotProfilerPort)` defaults to port 9999. The `FinishAotProfiling` target attaches to the `$(AndroidAotProfilerPort)` port, collects the AOT profiler data from the target device, and writes the collected data into `$(AndroidAotCustomProfilePath)`. `$(AndroidAotCustomProfilePath)` defaults to `custom.aprof`. The use of these two targets allows easily launching an Activity, waiting for a period of time -- so that process startup can complete, or so that some set of application behaviors can be included into the profiled data -- and then collecting the profile.
1 parent 83cd391 commit 853b367

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

Documentation/guides/BuildProcess.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ The following build targets are defined for Xamarin.Android projects:
119119
`Resource.designer.cs` file. This target is usually called by the
120120
IDE when new resources are added to the project.
121121

122+
- **BuildAndStartAotProfiling** – Builds the package with
123+
embedded AOT profiler, sets the AOT profiler socket port to
124+
`$(AndroidAotProfilerPort)` and starts the *launch* activity.
125+
126+
Added in Xamarin.Android v10.3.
127+
128+
- **FinishAotProfiling** – Collects the AOT profiler data from
129+
the device or the emulator through sockets port
130+
`$(AndroidAotProfilerPort)` and writes them to
131+
`$(AndroidAotCustomProfilePath)`.
132+
133+
The default values for port and custom profile are `9999` and
134+
`custom.aprof`.
135+
136+
The `aprofutil` call may be extended with
137+
`$(AProfUtilExtraOptions)`, to pass additional options.
138+
139+
This is equivalent to:
140+
141+
aprofutil $(AProfUtilExtraOptions) -s -v -f -p $(AndroidAotProfilerPort) -o "$(AndroidAotCustomProfilePath)"
142+
143+
Added in Xamarin.Android v10.3.
144+
122145
<a name="Build_Extension_Points" />
123146

124147
## Build Extension Points

Documentation/release-notes/3964.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Add new AOT profiling targets
2+
3+
The new `BuildAndStartAotProfiling` target builds the package with
4+
embedded AOT profiler, sets AOT profiler socket port to
5+
`$(AndroidAotProfilerPort)` and starts the *launch* activity.
6+
7+
The new `FinishAotProfiling` target collects the AOT profiler data
8+
from the device or the emulator through sockets port
9+
`$(AndroidAotProfilerPort)` and writes them to
10+
`$(AndroidAotCustomProfilePath)`.
11+
12+
The default values for port and custom profile are `9999` and
13+
`custom.aprof`.
14+
15+
The `aprofutil` call may be extended with
16+
`$(AProfUtilExtraOptions)`, to pass additional options.
17+
18+
This is equivalent to:
19+
20+
aprofutil $(AProfUtilExtraOptions) -s -v -f -p $(AndroidAotProfilerPort) -o "$(AndroidAotCustomProfilePath)"
21+

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Application.targets

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,30 @@ Copyright (C) 2019 Microsoft Corporation. All rights reserved.
3535
</GetAndroidPackageName>
3636
<Exec Command="&quot;$(AdbToolPath)adb&quot; $(AdbTarget) shell am force-stop &quot;$(_AndroidPackage)&quot;" />
3737
</Target>
38+
<PropertyGroup>
39+
<AndroidAotCustomProfilePath Condition=" '$(AndroidAotCustomProfilePath)' == '' ">custom.aprof</AndroidAotCustomProfilePath>
40+
<AndroidAotProfilerPort Condition=" '$(AndroidAotProfilerPort)' == '' ">9999</AndroidAotProfilerPort>
41+
<_BeginAotProfilingDependsOnTargets>
42+
_SetupAotProfiling;
43+
Build;
44+
Install;
45+
_SetAotProfilingPropsOnDevice;
46+
StartAndroidActivity;
47+
</_BeginAotProfilingDependsOnTargets>
48+
</PropertyGroup>
49+
<Target Name="_SetupAotProfiling">
50+
<PropertyGroup>
51+
<AndroidEmbedProfilers>aot</AndroidEmbedProfilers>
52+
</PropertyGroup>
53+
</Target>
54+
<Target Name="_SetAotProfilingPropsOnDevice">
55+
<Exec Command="&quot;$(AdbToolPath)adb&quot; $(AdbTarget) shell setprop debug.mono.profile aot:port=$(AndroidAotProfilerPort)" />
56+
</Target>
57+
<Target Name="BuildAndStartAotProfiling"
58+
DependsOnTargets="$(_BeginAotProfilingDependsOnTargets)">
59+
</Target>
60+
<Target Name="FinishAotProfiling"
61+
DependsOnTargets="_ResolveSdks">
62+
<Exec Command="&quot;$(MonoAndroidBinDirectory)aprofutil&quot; $(AProfUtilExtraOptions) -s -v -f -p $(AndroidAotProfilerPort) -o &quot;$(AndroidAotCustomProfilePath)&quot;" />
63+
</Target>
3864
</Project>

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ because xbuild doesn't support framework reference assemblies.
11711171
<_PropertyCacheItems Include="BundleAssemblies=$(BundleAssemblies)" />
11721172
<_PropertyCacheItems Include="AotAssemblies=$(AotAssemblies)" />
11731173
<_PropertyCacheItems Include="AndroidAotMode=$(AndroidAotMode)" />
1174+
<_PropertyCacheItems Include="AndroidEmbedProfilers=$(AndroidEmbedProfilers)" />
11741175
<_PropertyCacheItems Include="AndroidEnableProfiledAot=$(AndroidEnableProfiledAot)" />
11751176
<_PropertyCacheItems Include="ExplicitCrunch=$(AndroidExplicitCrunch)" />
11761177
<_PropertyCacheItems Include="AndroidDexTool=$(AndroidDexTool)" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using NUnit.Framework;
2+
using System.IO;
3+
using Xamarin.ProjectTools;
4+
5+
namespace Xamarin.Android.Build.Tests
6+
{
7+
public class AotProfileTests : DeviceTest
8+
{
9+
10+
[Test]
11+
public void BuildBasicApplicationAndAotProfileIt ()
12+
{
13+
if (!HasDevices)
14+
Assert.Ignore ("Skipping test. No devices available.");
15+
16+
var proj = new XamarinAndroidApplicationProject () { IsRelease = true };
17+
proj.SetProperty (KnownProperties.AndroidSupportedAbis, "armeabi-v7a;x86");
18+
var projDirectory = Path.Combine ("temp", TestName);
19+
using (var b = CreateApkBuilder (projDirectory)) {
20+
Assert.IsTrue (b.RunTarget (proj, "BuildAndStartAotProfiling"), "Run of BuildAndStartAotProfiling should have succeeded.");
21+
System.Threading.Thread.Sleep (5000);
22+
b.BuildLogFile = "build2.log";
23+
Assert.IsTrue (b.RunTarget (proj, "FinishAotProfiling", doNotCleanupOnUpdate: true), "Run of FinishAotProfiling should have succeeded.");
24+
var customProfile = Path.Combine (Root, projDirectory, "custom.aprof");
25+
FileAssert.Exists (customProfile);
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)