Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Xamarin.Android.Build.Tasks] Don't package empty AOT-d DSOs (#6477)
Fixes: #3932 Profiled AOT tends to produce a number of "empty" `.so` shared libraries because the profile names only a handful of types from a handful of assemblies and the AOT compiler in Mono currently doesn't support not outputting the `.so` in such cases. This has the unfortunate effect that an application may have a large number of such empty `.so` libraries and each of them will have to be opened and loaded before Mono can determine that there's no code in them and the JIT has to compile the requested types and methods. Loading every `.so` takes a non-zero amount of time, especially if the shared libraries aren't unpacked to the filesystem. I've seen times between 10-30ms to load a single shared library, which can add up to a non-trivial amount of time wasted loading empty libraries. This commit adds code to examine and ignore such `.so` files which don't include any executable code. The checks are crafted so that only AOT-d libraries are considered for rejection. The check is kinda/sorta like: o=`llvm-readelf -a path/to/lib.so` if echo $o | grep mono_aot_file_info && : lib has 'mono_aot_file_info' symbol echo $o | grep FUNC ; then : lib contains executable code echo "AOT'd file contains code" fi If the `mono_aot_file_info` symbol isn't present, the `.so` is always included. Time savings are around 3ms for a Classic Xamarin.Android app and around 10ms for a sample MAUI app on the Pixel 3 XL phone. In the latter case 49 shared libraries out of 120 total are rejected and not packaged, contributing to (small, each empty DSO is between 7 and 25k) space savings in the `.apk` in addition to slightly better startup performance. Update the `<ApkDiffCheckRegression/>` so that the size difference threshold for reporting issues with `.apk` diffs is 48KB, not 50KB, as a 50KB threshold meant that no changes were reported for `Xamarin.Forms_Performance_Integration-Signed-Release-Profiled-Aot.apkdesc`. Reducing the threshold to 48KB allowed us to see the result of this optimization.
- Loading branch information
Showing
11 changed files
with
758 additions
and
656 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
build-tools/xaprepare/xaprepare/ThirdPartyNotices/ELFSharp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace Xamarin.Android.Prepare | ||
{ | ||
[TPN] | ||
class KonradKuczynski_ELFSharp_TPN : ThirdPartyNotice | ||
{ | ||
static readonly Uri url = new Uri ("https://elfsharp.it/"); | ||
|
||
public override string LicenseFile => String.Empty; | ||
public override string Name => "KonradKuczynski/ELFSharp"; | ||
public override Uri SourceUrl => url; | ||
public override string LicenseText => @" | ||
Copyright (c) 2011 Konrad Kruczyński and other contributors | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
""Software""), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
This software uses ELF machine constants from the LLVM projects, whose license | ||
is provided below: | ||
============================================================================== | ||
LLVM Release License | ||
============================================================================== | ||
University of Illinois/NCSA | ||
Open Source License | ||
Copyright (c) 2003-2010 University of Illinois at Urbana-Champaign. | ||
All rights reserved. | ||
Developed by: | ||
LLVM Team | ||
University of Illinois at Urbana-Champaign | ||
http://llvm.org | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the ""Software""), to deal with | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
* Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimers. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimers in the | ||
documentation and/or other materials provided with the distribution. | ||
* Neither the names of the LLVM Team, University of Illinois at | ||
Urbana-Champaign, nor the names of its contributors may be used to | ||
endorse or promote products derived from this Software without specific | ||
prior written permission. | ||
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE | ||
SOFTWARE. | ||
"; | ||
public override bool Include (bool includeExternalDeps, bool includeBuildDeps) => includeExternalDeps; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.IO; | ||
|
||
using ELFSharp; | ||
using ELFSharp.ELF; | ||
using ELFSharp.ELF.Sections; | ||
using Microsoft.Android.Build.Tasks; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Xamarin.Android.Tasks | ||
{ | ||
static class ELFHelper | ||
{ | ||
public static bool IsEmptyAOTLibrary (TaskLoggingHelper log, string path) | ||
{ | ||
if (String.IsNullOrEmpty (path) || !File.Exists (path)) { | ||
return false; | ||
} | ||
|
||
try { | ||
return IsEmptyAOTLibrary (log, path, ELFReader.Load (path)); | ||
} catch (Exception ex) { | ||
log.LogWarning ($"Attempt to check whether '{path}' is a valid ELF file failed with exception, ignoring AOT check for the file."); | ||
log.LogWarningFromException (ex, showStackTrace: true); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
static bool IsEmptyAOTLibrary (TaskLoggingHelper log, string path, IELF elf) | ||
{ | ||
ISymbolTable? symtab = GetSymbolTable (elf, ".dynsym"); | ||
if (symtab == null) { | ||
// We can't be sure what the DSO is, play safe | ||
return false; | ||
} | ||
|
||
bool mono_aot_file_info_found = false; | ||
foreach (var entry in symtab.Entries) { | ||
if (String.Compare ("mono_aot_file_info", entry.Name, StringComparison.Ordinal) == 0 && entry.Type == SymbolType.Object) { | ||
mono_aot_file_info_found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!mono_aot_file_info_found) { | ||
// Not a MonoVM AOT assembly | ||
return false; | ||
} | ||
|
||
symtab = GetSymbolTable (elf, ".symtab"); | ||
if (symtab == null) { | ||
// The DSO is stripped, we can't tell if there are any functions defined (.text will be present anyway) | ||
// We perhaps **can** take a look at the .text section size, but it's not a solid check... | ||
log.LogDebugMessage ($"{path} is an AOT assembly but without symbol table (stripped?). Including it in the archive."); | ||
return false; | ||
} | ||
|
||
foreach (var entry in symtab.Entries) { | ||
if (entry.Type == SymbolType.Function) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static ISymbolTable? GetSymbolTable (IELF elf, string sectionName) | ||
{ | ||
ISection? section = GetSection (elf, sectionName); | ||
if (section == null) { | ||
return null; | ||
} | ||
|
||
var symtab = section as ISymbolTable; | ||
if (symtab == null) { | ||
return null; | ||
} | ||
|
||
return symtab; | ||
} | ||
|
||
static ISection? GetSection (IELF elf, string sectionName) | ||
{ | ||
if (!elf.TryGetSection (sectionName, out ISection section)) { | ||
return null; | ||
} | ||
|
||
return section; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.