From a5fb483af1dea64130ab3da6224ad9c0034a0874 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Wed, 11 Jan 2023 18:57:40 +0100 Subject: [PATCH 01/15] make initial size small again --- src/mono/wasm/build/WasmApp.Native.targets | 2 +- src/mono/wasm/build/WasmApp.targets | 2 +- src/mono/wasm/wasm.proj | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 6f9524e7f7a30e..9e48058f90981c 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -188,7 +188,7 @@ <_EmccLinkRsp>$(_WasmIntermediateOutputPath)emcc-link.rsp $(EmccTotalMemory) - 536870912 + 16777216 diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index aa9832ada35215..2313c7ab914652 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -51,7 +51,7 @@ - $(EmccFlags) - Emcc flags used for both compiling native files, and linking - $(EmccExtraLDFlags) - Extra emcc flags for linking - $(EmccExtraCFlags) - Extra emcc flags for compiling native files - - $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 536870912 + - $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 16777216 Corresponds to `INITIAL_MEMORY` arg for emcc. (previously named EmccTotalMemory, which is still kept as an alias) diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index b049dcfbabb055..cfc3f40b78c380 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -158,7 +158,7 @@ <_EmccExportedRuntimeMethods>"[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]" <_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',') - 536870912 + 16777216 <_EmccLinkFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" /> From 87c28ce6738f89d46d80cf6c808ef581263ffec1 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Wed, 11 Jan 2023 19:13:47 +0100 Subject: [PATCH 02/15] bench --- .../wasm/browser-bench/Wasm.Browser.Bench.Sample.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/mono/sample/wasm/browser-bench/Wasm.Browser.Bench.Sample.csproj b/src/mono/sample/wasm/browser-bench/Wasm.Browser.Bench.Sample.csproj index 1f4f8e02e424a5..0ee75e187a8f3c 100644 --- a/src/mono/sample/wasm/browser-bench/Wasm.Browser.Bench.Sample.csproj +++ b/src/mono/sample/wasm/browser-bench/Wasm.Browser.Bench.Sample.csproj @@ -9,10 +9,6 @@ $(EnableAOTAndTrimming) $(EnableAOTAndTrimming) $(EnableAOTAndTrimming) - - 83886080 From 4da3be8a8c76ef8a675853705fbc9c3dfdaada30 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Mon, 16 Jan 2023 20:59:00 +0100 Subject: [PATCH 03/15] make EmccInitialHeapSize dynamic based on size of the assemblies --- src/mono/wasm/build/WasmApp.Native.targets | 5 +++ src/mono/wasm/build/WasmApp.targets | 7 ++- .../WasmAppBuilder/AssembliesTotalSize.cs | 43 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/tasks/WasmAppBuilder/AssembliesTotalSize.cs diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 9e48058f90981c..877e8a60a9e5c8 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -149,6 +149,10 @@ + + + + <_MonoAotCrossCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','browser-wasm')) <_EmccDefaultFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-default.rsp')) @@ -188,6 +192,7 @@ <_EmccLinkRsp>$(_WasmIntermediateOutputPath)emcc-link.rsp $(EmccTotalMemory) + $(_WasmAssembliesTotalSize) 16777216 diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 14a5d7c8210059..500c988c88275b 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -1,6 +1,7 @@ + <_EmccLDSFlags Include="-s ERROR_ON_UNDEFINED_SYMBOLS=0" Condition="'$(WasmBuildingForNestedPublish)' != 'true'" /> @@ -377,7 +369,41 @@ - + + + <_AOTObjectFile Include="%(_BitcodeFile.ObjectFile)" /> + + + + + + + + + + + + + + + $(EmccTotalMemory) + $(_WasmCalculatedInitialHeapSize) + 16777216 + + + <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" /> + + + + <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-wasm.a <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a diff --git a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs index 7b85cfd04a0542..baee0fab23e720 100644 --- a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs +++ b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs @@ -10,19 +10,24 @@ using Microsoft.Build.Framework; using Microsoft.Build.Utilities; -/// estimate the total memory needed for the assemblies. +/// estimate the total memory needed for the assemblies and AOT data segments. public class WasmCalculateInitialHeapSize : Task { [Required] [NotNull] public string[]? Assemblies { get; set; } + [Required] + public string[]? AOTDataSegments { get; set; } + [Output] - public long? TotalSize { get; private set; } + public long? InitialHeapSize { get; private set; } public override bool Execute () { long totalDllSize=0; + long totalDataSize=0; + foreach (var asm in Assemblies) { var info = new FileInfo(asm); @@ -34,12 +39,22 @@ public override bool Execute () totalDllSize += info.Length; } + // during non-AOT builds, AOTDataSegments is null + if (AOTDataSegments != null) + { + foreach (var segment in AOTDataSegments) + { + totalDataSize += long.Parse(segment); + } + } + // this is arbitrary guess about memory overhead of the runtime, after the assemblies are loaded const double extraMemoryRatio = 1.2; - long memorySize = (long) (totalDllSize * extraMemoryRatio); + // plus size of data segments generated by AOT + long memorySize = totalDataSize + (long) (totalDllSize * extraMemoryRatio); // round it up to 64KB page size for wasm - TotalSize = (memorySize + 0x10000) & 0xFFFF0000; + InitialHeapSize = (memorySize + 0x10000) & 0xFFFF0000; return true; } From 292feef3bdf4b1df05956db0a9b877279987cef8 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Wed, 18 Jan 2023 12:08:00 +0100 Subject: [PATCH 09/15] whitespace --- .../WasmAppBuilder/WasmCalculateInitialHeapSize.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs index baee0fab23e720..45ed1de9356fb9 100644 --- a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs +++ b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs @@ -23,10 +23,10 @@ public class WasmCalculateInitialHeapSize : Task [Output] public long? InitialHeapSize { get; private set; } - public override bool Execute () + public override bool Execute() { - long totalDllSize=0; - long totalDataSize=0; + long totalDllSize = 0; + long totalDataSize = 0; foreach (var asm in Assemblies) { @@ -40,7 +40,7 @@ public override bool Execute () } // during non-AOT builds, AOTDataSegments is null - if (AOTDataSegments != null) + if (AOTDataSegments != null) { foreach (var segment in AOTDataSegments) { @@ -51,7 +51,7 @@ public override bool Execute () // this is arbitrary guess about memory overhead of the runtime, after the assemblies are loaded const double extraMemoryRatio = 1.2; // plus size of data segments generated by AOT - long memorySize = totalDataSize + (long) (totalDllSize * extraMemoryRatio); + long memorySize = totalDataSize + (long)(totalDllSize * extraMemoryRatio); // round it up to 64KB page size for wasm InitialHeapSize = (memorySize + 0x10000) & 0xFFFF0000; From c688a1a5fb1c4d62cd6feb7e196f9c9c0292d693 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Thu, 19 Jan 2023 13:49:23 +0100 Subject: [PATCH 10/15] feedback --- src/mono/wasm/build/WasmApp.Native.targets | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 8dfb598c95963c..3e3a0d0c5c80db 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -187,6 +187,7 @@ <_EmccCompileBitcodeRsp>$(_WasmIntermediateOutputPath)emcc-compile-bc.rsp <_EmccLinkRsp>$(_WasmIntermediateOutputPath)emcc-link.rsp + $(EmccTotalMemory) @@ -369,7 +370,9 @@ - + <_AOTObjectFile Include="%(_BitcodeFile.ObjectFile)" /> @@ -394,13 +397,9 @@ - $(EmccTotalMemory) $(_WasmCalculatedInitialHeapSize) 16777216 - - <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" /> - @@ -414,6 +413,8 @@ + <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" /> + <_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" /> <_WasmNativeFileForLinking Include="%(_WasmSourceFileToCompile.ObjectFile)" /> From f92e9ff34b1f27140867fe2824617f884693ecec Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Tue, 24 Jan 2023 17:11:57 +0100 Subject: [PATCH 11/15] Update src/mono/wasm/build/WasmApp.targets Co-authored-by: Ankit Jain --- src/mono/wasm/build/WasmApp.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 8b15984c86d2a6..992f29ff559cd5 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -52,7 +52,7 @@ - $(EmccFlags) - Emcc flags used for both compiling native files, and linking - $(EmccExtraLDFlags) - Extra emcc flags for linking - $(EmccExtraCFlags) - Extra emcc flags for compiling native files - - $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 16777216 or size of the DLLs. + - $(EmccInitialHeapSize) - Initial heap size specified with `emcc`. Default value: 16777216 or size of the DLLs, whichever is larger. Corresponds to `INITIAL_MEMORY` arg for emcc. (previously named EmccTotalMemory, which is still kept as an alias) From f47fac1feb360fc07bea5ab53963035c93e73c25 Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Tue, 24 Jan 2023 17:12:48 +0100 Subject: [PATCH 12/15] Update src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs index 45ed1de9356fb9..9f472629a8024a 100644 --- a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs +++ b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs @@ -10,6 +10,8 @@ using Microsoft.Build.Framework; using Microsoft.Build.Utilities; +namespace Microsoft.WebAssembly.Build.Tasks; + /// estimate the total memory needed for the assemblies and AOT data segments. public class WasmCalculateInitialHeapSize : Task { From 28fd57c4483de76d444e632e9f3d7bd4f5964bb3 Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Tue, 24 Jan 2023 17:13:16 +0100 Subject: [PATCH 13/15] Update src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs index 9f472629a8024a..3f8e30744231f3 100644 --- a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs +++ b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs @@ -16,8 +16,7 @@ namespace Microsoft.WebAssembly.Build.Tasks; public class WasmCalculateInitialHeapSize : Task { [Required] - [NotNull] - public string[]? Assemblies { get; set; } + public string[] Assemblies { get; set; } = Array.Empty(); [Required] public string[]? AOTDataSegments { get; set; } From 358f8d5515cce04c93e7da821d66a25cd0f9d13b Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Tue, 24 Jan 2023 17:13:39 +0100 Subject: [PATCH 14/15] Update src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs index 3f8e30744231f3..b586f7b1be847e 100644 --- a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs +++ b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs @@ -19,7 +19,7 @@ public class WasmCalculateInitialHeapSize : Task public string[] Assemblies { get; set; } = Array.Empty(); [Required] - public string[]? AOTDataSegments { get; set; } + public string[] AOTDataSegments { get; set; } = Array.Empty(); [Output] public long? InitialHeapSize { get; private set; } From c7caa7348436387b6a10c407af979caeaeeb65f1 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 24 Jan 2023 18:00:43 +0100 Subject: [PATCH 15/15] feedback --- src/mono/wasm/build/WasmApp.Native.targets | 8 +++---- .../WasmCalculateInitialHeapSize.cs | 21 ++++++++----------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 3e3a0d0c5c80db..57e69e842b623b 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -370,7 +370,7 @@ - @@ -387,13 +387,13 @@ - + AOTDataSegmentSizes="@(_AOTDataSegmentSize)"> @@ -402,7 +402,7 @@ - + <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-wasm.a <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a diff --git a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs index b586f7b1be847e..d54bd65b765953 100644 --- a/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs +++ b/src/tasks/WasmAppBuilder/WasmCalculateInitialHeapSize.cs @@ -2,11 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Linq; -using System.Reflection; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; @@ -18,18 +14,17 @@ public class WasmCalculateInitialHeapSize : Task [Required] public string[] Assemblies { get; set; } = Array.Empty(); - [Required] - public string[] AOTDataSegments { get; set; } = Array.Empty(); + public string[] AOTDataSegmentSizes { get; set; } = Array.Empty(); [Output] - public long? InitialHeapSize { get; private set; } + public long InitialHeapSize { get; private set; } public override bool Execute() { long totalDllSize = 0; long totalDataSize = 0; - foreach (var asm in Assemblies) + foreach (string asm in Assemblies) { var info = new FileInfo(asm); if (!info.Exists) @@ -40,13 +35,15 @@ public override bool Execute() totalDllSize += info.Length; } - // during non-AOT builds, AOTDataSegments is null - if (AOTDataSegments != null) + // during non-AOT builds, AOTDataSegmentSizes is empty + foreach (string segment in AOTDataSegmentSizes) { - foreach (var segment in AOTDataSegments) + if (!long.TryParse(segment, out long segmentSize)) { - totalDataSize += long.Parse(segment); + Log.LogError($"Could not parse AOT Data segment size '{segment}"); + return false; } + totalDataSize += segmentSize; } // this is arbitrary guess about memory overhead of the runtime, after the assemblies are loaded