From ad0639d15d9027de8ec23cfb6dcbc70059b85f04 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Sat, 2 Apr 2022 08:29:29 +0000 Subject: [PATCH 1/2] [wasm] EmccCompile: Add support for compiling sequentially This adds support for two cases: 1. Compiling an assembly in parallel fails due to getting oomkill'ed. In this case, it will get compiled again but not in parallel with anything else. 2. User specified assemblies that should always be compiled sequentially. This would be useful for cases where some assemblies fail to compile because `emcc` runs out of memory. New: ``` - $(WasmRetrySequentiallyIfNeeded) - Retry assembly compilation to .o files sequentially, if needed. One such case could be if the compiler got killed due to OOM. Defaults to true. - @(WasmAssemblyToAOTSequentially) - Filenames of assemblies to always compile (to .o) sequentially ``` --- src/mono/wasm/build/WasmApp.Native.targets | 5 ++ src/mono/wasm/build/WasmApp.targets | 4 ++ src/tasks/WasmAppBuilder/EmccCompile.cs | 59 +++++++++++++++++++--- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 501cb7e156808f..34e77f08a48668 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -158,6 +158,7 @@ $(WasmBuildNative) false + true <_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h <_WasmRuntimeICallTablePath>$(_WasmIntermediateOutputPath)runtime-icall-table.h @@ -325,11 +326,13 @@ <_WasmSourceFileToCompile Remove="@(_WasmSourceFileToCompile)" /> <_WasmSourceFileToCompile Include="@(_WasmRuntimePackSrcFile)" Dependencies="%(_WasmRuntimePackSrcFile.Dependencies);$(_EmccDefaultFlagsRsp);$(_EmccCompileRsp)" /> + @@ -344,6 +347,7 @@ <_BitCodeFile Dependencies="%(_BitCodeFile.Dependencies);$(_EmccDefaultFlagsRsp);$(_EmccCompileBitcodeRsp)" /> + <_WasmSourceFileToCompileSequentially Include="@(WasmAssemblyToAOTSequentially -> '%(Identity).bc')" /> @@ -352,6 +356,7 @@ Arguments=""@$(_EmccDefaultFlagsRsp)" "@$(_EmccCompileBitcodeRsp)"" EnvironmentVariables="@(EmscriptenEnvVars)" DisableParallelCompile="$(DisableParallelEmccCompile)" + SourceFileNamesToCompileSequentially="@(_WasmSourceFileToCompileSequentially)" OutputMessageImportance="$(_EmccCompileOutputMessageImportance)"> diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 070195319d6b53..411f066e680e90 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -66,8 +66,12 @@ Defaults to false. - $(WasmAotProfilePath) - Path to an AOT profile file. - $(WasmExceptionHandling) - Enable support for the WASM Exception Handling feature. + - $(WasmRetrySequentiallyIfNeeded) - Retry assembly compilation to .o files sequentially, if needed. + One such case could be if the compiler got killed due to OOM. + Defaults to true. Public items: + - @(WasmAssemblyToAOTSequentially) - Filenames of assemblies to always compile (to .o) sequentially - @(WasmExtraFilesToDeploy) - Files to copy to $(WasmAppDir). (relative path can be set via %(TargetPath) metadata) - @(WasmFilesToIncludeInFileSystem) - Files to include in the vfs diff --git a/src/tasks/WasmAppBuilder/EmccCompile.cs b/src/tasks/WasmAppBuilder/EmccCompile.cs index 4ff6ab1a4866e2..607273be3b4087 100644 --- a/src/tasks/WasmAppBuilder/EmccCompile.cs +++ b/src/tasks/WasmAppBuilder/EmccCompile.cs @@ -35,6 +35,8 @@ public class EmccCompile : Microsoft.Build.Utilities.Task public string Arguments { get; set; } = string.Empty; public string? WorkingDirectory { get; set; } public string OutputMessageImportance{ get; set; } = "Low"; + public bool RetrySequentiallyIfNeeded { get; set; } = true; + public string[] SourceFileNamesToCompileSequentially { get; set; } = Array.Empty(); [Output] public ITaskItem[]? OutputFiles { get; private set; } @@ -80,6 +82,7 @@ private bool ExecuteActual() _totalFiles = SourceFiles.Length; IDictionary envVarsDict = GetEnvironmentVariablesDict(); ConcurrentBag outputItems = new(); + ConcurrentBag<(string srcFile, string objFile, bool retryAllowed, string? firstOutput)> filesToCompileSequentially = new(); try { List<(string, string)> filesToCompile = new(); @@ -96,15 +99,21 @@ private bool ExecuteActual() { Log.LogMessage(MessageImportance.Low, $"Skipping {srcFile} because {reason}."); outputItems.Add(CreateOutputItemFor(srcFile, objFile)); + continue; } - else + + if (SourceFileNamesToCompileSequentially.Contains(Path.GetFileName(srcFile))) { - Log.LogMessage(MessageImportance.Low, $"Compiling {srcFile} because {reason}."); - filesToCompile.Add((srcFile, objFile)); + Log.LogMessage(MessageImportance.Low, $"Compiling {srcFile} because {reason}. It will be compiled at the end, sequentially."); + filesToCompileSequentially.Add((srcFile, objFile, retryAllowed: false, null)); + continue; } + + Log.LogMessage(MessageImportance.Low, $"Compiling {srcFile} because {reason}."); + filesToCompile.Add((srcFile, objFile)); } - _numCompiled = SourceFiles.Length - filesToCompile.Count; + _numCompiled = SourceFiles.Length - (filesToCompile.Count + filesToCompileSequentially.Count); if (_numCompiled == _totalFiles) { // nothing to do! @@ -160,12 +169,23 @@ all assigned to that one partition. new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism }, (toCompile, state) => { - if (!ProcessSourceFile(toCompile.Item1, toCompile.Item2)) + if (!ProcessSourceFile(toCompile.Item1, toCompile.Item2, retryAllowed: allowedParallelism > 1)) state.Stop(); }); if (!result.IsCompleted && !Log.HasLoggedErrors) + { Log.LogError("Unknown failure occured while compiling. Check logs to get more details."); + } + else if (!Log.HasLoggedErrors && !filesToCompileSequentially.IsEmpty) + { + Log.LogMessage(MessageImportance.High, "Compiling sequentially ..."); + foreach ((string srcFile, string objFile, bool retryAllowed, string? firstOutput) in filesToCompileSequentially) + { + if (!ProcessSourceFile(srcFile, objFile, retryAllowed, firstOutput)) + break; + } + } if (!Log.HasLoggedErrors) { @@ -183,7 +203,7 @@ all assigned to that one partition. OutputFiles = outputItems.ToArray(); return !Log.HasLoggedErrors; - bool ProcessSourceFile(string srcFile, string objFile) + bool ProcessSourceFile(string srcFile, string objFile, bool retryAllowed, string? firstAttemptOutput = null) { string tmpObjFile = Path.GetTempFileName(); try @@ -209,6 +229,21 @@ bool ProcessSourceFile(string srcFile, string objFile) var elapsedSecs = (endTime - startTime).TotalSeconds; if (exitCode != 0) { + if (firstAttemptOutput is not null) + { + Log.LogError($"Failed to compile {srcFile} -> {objFile} again. [took {elapsedSecs:F}s]"); + Log.LogError($"Output from the first attempt: {Environment.NewLine}{firstAttemptOutput}"); + Log.LogError($"Output from second first attempt: {Environment.NewLine}{output}"); + + return false; + } + else if (retryAllowed && ShouldRetrySequentially(output, out string? reason)) + { + Log.LogMessage(MessageImportance.High, $"Failed to compile {srcFile} due {reason}. It will be retried sequentially, at the end. ouptut: {output}"); + filesToCompileSequentially.Add((srcFile, objFile, retryAllowed: false, output ?? string.Empty)); + return true; + } + Log.LogError($"Failed to compile {srcFile} -> {objFile}{Environment.NewLine}{output} [took {elapsedSecs:F}s]"); return false; } @@ -292,6 +327,18 @@ bool IsNewerThanOutput(string inFile, string outFile, out string reason) } } + private static bool ShouldRetrySequentially(string output, [NotNullWhen(true)] out string? reason) + { + reason = null; + if (output.Contains("(received SIGKILL (-9))")) + { + reason = "to possibly running out of memory"; + return true; + } + + return false; + } + private IDictionary GetEnvironmentVariablesDict() { Dictionary envVarsDict = new(); From 4f2944adda8e7d89c447527a6e8442dfa47b0870 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Mon, 4 Apr 2022 21:43:25 +0000 Subject: [PATCH 2/2] Remove the retry mechanism --- src/mono/wasm/build/WasmApp.Native.targets | 2 -- src/mono/wasm/build/WasmApp.targets | 3 -- src/tasks/WasmAppBuilder/EmccCompile.cs | 40 ++++------------------ 3 files changed, 6 insertions(+), 39 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 34e77f08a48668..c0d5579fd5760b 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -158,7 +158,6 @@ $(WasmBuildNative) false - true <_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h <_WasmRuntimeICallTablePath>$(_WasmIntermediateOutputPath)runtime-icall-table.h @@ -332,7 +331,6 @@ Arguments='"@$(_EmccDefaultFlagsRsp)" "@$(_EmccCompileRsp)"' EnvironmentVariables="@(EmscriptenEnvVars)" DisableParallelCompile="$(DisableParallelEmccCompile)" - RetrySequentiallyIfNeeded="$(WasmRetryCompileIfNeeded)" OutputMessageImportance="$(_EmccCompileOutputMessageImportance)"> diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 411f066e680e90..dfd8a45761b12b 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -66,9 +66,6 @@ Defaults to false. - $(WasmAotProfilePath) - Path to an AOT profile file. - $(WasmExceptionHandling) - Enable support for the WASM Exception Handling feature. - - $(WasmRetrySequentiallyIfNeeded) - Retry assembly compilation to .o files sequentially, if needed. - One such case could be if the compiler got killed due to OOM. - Defaults to true. Public items: - @(WasmAssemblyToAOTSequentially) - Filenames of assemblies to always compile (to .o) sequentially diff --git a/src/tasks/WasmAppBuilder/EmccCompile.cs b/src/tasks/WasmAppBuilder/EmccCompile.cs index 607273be3b4087..ca16d28ceeff8e 100644 --- a/src/tasks/WasmAppBuilder/EmccCompile.cs +++ b/src/tasks/WasmAppBuilder/EmccCompile.cs @@ -35,7 +35,6 @@ public class EmccCompile : Microsoft.Build.Utilities.Task public string Arguments { get; set; } = string.Empty; public string? WorkingDirectory { get; set; } public string OutputMessageImportance{ get; set; } = "Low"; - public bool RetrySequentiallyIfNeeded { get; set; } = true; public string[] SourceFileNamesToCompileSequentially { get; set; } = Array.Empty(); [Output] @@ -82,7 +81,7 @@ private bool ExecuteActual() _totalFiles = SourceFiles.Length; IDictionary envVarsDict = GetEnvironmentVariablesDict(); ConcurrentBag outputItems = new(); - ConcurrentBag<(string srcFile, string objFile, bool retryAllowed, string? firstOutput)> filesToCompileSequentially = new(); + ConcurrentBag<(string srcFile, string objFile)> filesToCompileSequentially = new(); try { List<(string, string)> filesToCompile = new(); @@ -105,7 +104,7 @@ private bool ExecuteActual() if (SourceFileNamesToCompileSequentially.Contains(Path.GetFileName(srcFile))) { Log.LogMessage(MessageImportance.Low, $"Compiling {srcFile} because {reason}. It will be compiled at the end, sequentially."); - filesToCompileSequentially.Add((srcFile, objFile, retryAllowed: false, null)); + filesToCompileSequentially.Add((srcFile, objFile)); continue; } @@ -169,7 +168,7 @@ all assigned to that one partition. new ParallelOptions { MaxDegreeOfParallelism = allowedParallelism }, (toCompile, state) => { - if (!ProcessSourceFile(toCompile.Item1, toCompile.Item2, retryAllowed: allowedParallelism > 1)) + if (!ProcessSourceFile(toCompile.Item1, toCompile.Item2)) state.Stop(); }); @@ -180,9 +179,9 @@ all assigned to that one partition. else if (!Log.HasLoggedErrors && !filesToCompileSequentially.IsEmpty) { Log.LogMessage(MessageImportance.High, "Compiling sequentially ..."); - foreach ((string srcFile, string objFile, bool retryAllowed, string? firstOutput) in filesToCompileSequentially) + foreach ((string srcFile, string objFile) in filesToCompileSequentially) { - if (!ProcessSourceFile(srcFile, objFile, retryAllowed, firstOutput)) + if (!ProcessSourceFile(srcFile, objFile)) break; } } @@ -203,7 +202,7 @@ all assigned to that one partition. OutputFiles = outputItems.ToArray(); return !Log.HasLoggedErrors; - bool ProcessSourceFile(string srcFile, string objFile, bool retryAllowed, string? firstAttemptOutput = null) + bool ProcessSourceFile(string srcFile, string objFile) { string tmpObjFile = Path.GetTempFileName(); try @@ -229,21 +228,6 @@ bool ProcessSourceFile(string srcFile, string objFile, bool retryAllowed, string var elapsedSecs = (endTime - startTime).TotalSeconds; if (exitCode != 0) { - if (firstAttemptOutput is not null) - { - Log.LogError($"Failed to compile {srcFile} -> {objFile} again. [took {elapsedSecs:F}s]"); - Log.LogError($"Output from the first attempt: {Environment.NewLine}{firstAttemptOutput}"); - Log.LogError($"Output from second first attempt: {Environment.NewLine}{output}"); - - return false; - } - else if (retryAllowed && ShouldRetrySequentially(output, out string? reason)) - { - Log.LogMessage(MessageImportance.High, $"Failed to compile {srcFile} due {reason}. It will be retried sequentially, at the end. ouptut: {output}"); - filesToCompileSequentially.Add((srcFile, objFile, retryAllowed: false, output ?? string.Empty)); - return true; - } - Log.LogError($"Failed to compile {srcFile} -> {objFile}{Environment.NewLine}{output} [took {elapsedSecs:F}s]"); return false; } @@ -327,18 +311,6 @@ bool IsNewerThanOutput(string inFile, string outFile, out string reason) } } - private static bool ShouldRetrySequentially(string output, [NotNullWhen(true)] out string? reason) - { - reason = null; - if (output.Contains("(received SIGKILL (-9))")) - { - reason = "to possibly running out of memory"; - return true; - } - - return false; - } - private IDictionary GetEnvironmentVariablesDict() { Dictionary envVarsDict = new();