Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Nov 9, 2023
1 parent 28dcc44 commit 4346bb3
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
alwaysRun: ${{ parameters.isWasmOnlyBuild }}

# NOTE - Since threading is experimental, we don't want to block mainline work
shouldContinueOnError: true
# TODO put back shouldContinueOnError: true
scenarios:
- WasmTestOnBrowser
#- WasmTestOnNodeJS - this is not supported yet, https://github.com/dotnet/runtime/issues/85592
Expand Down
2 changes: 1 addition & 1 deletion eng/testing/tests.browser.targets
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
<!-- snapshots are not yet supported with threads -->
<WasmXHarnessMonoArgs Condition="'$(MonoWasmBuildVariant)' == 'multithread'">$(WasmXHarnessMonoArgs) --no-memory-snapshot</WasmXHarnessMonoArgs>
<!-- help unit test with PlatformDetection.IsThreadingSupported via IsBrowserThreadingSupported env variable -->
<WasmXHarnessMonoArgs Condition="'$(MonoWasmBuildVariant)' == 'multithread'">$(WasmXHarnessMonoArgs) --setenv=IsBrowserThreadingSupported=true</WasmXHarnessMonoArgs>
<WasmXHarnessMonoArgs Condition="'$(MonoWasmBuildVariant)' == 'multithread'">$(WasmXHarnessMonoArgs) --multithreaded --setenv=IsBrowserThreadingSupported=true</WasmXHarnessMonoArgs>
</PropertyGroup>

<PropertyGroup Condition="'$(RunScriptCommand)' == ''">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ internal static (ConsoleLogger Logger, ConsoleSink Sink, ConsoleSink ErrorSink,
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public void ConsoleLoggerOptions_TimeStampFormat_IsReloaded()
{
// Arrange
Expand All @@ -88,7 +87,6 @@ public void ConsoleLoggerOptions_TimeStampFormat_IsReloaded()
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[MemberData(nameof(FormatterNames))]
public void InvalidLogLevel_Throws(string formatterName)
{
Expand All @@ -103,7 +101,6 @@ public void InvalidLogLevel_Throws(string formatterName)
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[MemberData(nameof(FormatterNamesAndLevels))]
public void NoMessageOrException_Noop(string formatterName, LogLevel level)
{
Expand All @@ -123,7 +120,6 @@ public void NoMessageOrException_Noop(string formatterName, LogLevel level)
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[MemberData(nameof(FormatterNamesAndLevels))]
public void Log_LogsCorrectTimestamp(string formatterName, LogLevel level)
{
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Microsoft.Extensions.Logging.Console.Test
public class SimpleConsoleFormatterTests : ConsoleFormatterTests
{
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[InlineData(LoggerColorBehavior.Default)]
[InlineData(LoggerColorBehavior.Enabled)]
[InlineData(LoggerColorBehavior.Disabled)]
Expand Down Expand Up @@ -55,7 +54,6 @@ public void Log_WritingScopes_LogsWithCorrectColorsWhenColorEnabled(LoggerColorB
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public void Log_NoLogScope_DoesNotWriteAnyScopeContentToOutput()
{
// Arrange
Expand Down Expand Up @@ -111,7 +109,6 @@ public void Log_SingleLine_LogsWhenMessageIsNotProvided()
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public void Log_SingleLine_LogsWhenBothMessageAndExceptionProvided()
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<EnableDefaultItems>true</EnableDefaultItems>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
<_WasmPThreadPoolSize Condition="'$(MonoWasmBuildVariant)' == 'multithread'">64</_WasmPThreadPoolSize>
<WasmXHarnessMonoArgs>--setenv=XHARNESS_LOG_TEST_START=true --no-memory-snapshot</WasmXHarnessMonoArgs>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\src\LogLevelAttribute.cs"
Link="tests\DI.Common\Common\src\LogLevelAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<DefineConstants Condition="'$(TargetPlatformIdentifier)' == 'unix' or '$(TargetPlatformIdentifier)' == 'browser'">$(DefineConstants);Unix</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
<WasmXHarnessMonoArgs>--setenv=XHARNESS_LOG_TEST_START=true --no-memory-snapshot</WasmXHarnessMonoArgs>
</PropertyGroup>

<ItemGroup>
<Compile Include="System\ApplicationIdTests.cs" />
<Compile Include="System\Convert.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@ public void Shared_IsSingleton()
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public void Shared_ParallelUsage()
{
using var barrier = new Barrier(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,6 @@ public static void TestParallelForPaths_Exceptions()
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91582", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void TestParallelScheduler()
{
ParallelOptions parallelOptions = new ParallelOptions();
Expand Down Expand Up @@ -993,7 +992,6 @@ public static void TestParallelScheduler()
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void TestInvokeDOPAndCancel()
{
ParallelOptions parallelOptions = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ private static void OrderablePartitionerForEachPLRTest(

// Perform tests on various combinations of Stop()/Break()
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91579", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void SimultaneousStopBreakTests()
{
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ static void oneMoveNext(int length, bool isOrderable)
/// </summary>
/// <param name="length"></param>
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void IterationsWithDependency()
{
static void iterationsWithDependency(int length, int dependencyIndex)
Expand Down Expand Up @@ -167,7 +166,6 @@ public static void PFEDisposeEnum()
/// Exception is expected and the enumerators are disposed
/// </summary>
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91581", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void ExceptionOnMoveNext()
{
static void exceptionOnMoveNext(int length, int indexToThrow, bool isOrderable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace System.Threading.Tasks.Tests
public static class RangePartitionerTests
{
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void RunPartitionerStaticTest_SingleChunking()
{
CountdownEvent cde = new CountdownEvent(2);
Expand Down
4 changes: 0 additions & 4 deletions src/libraries/System.Threading/tests/SemaphoreSlimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public static void RunSemaphoreSlimTest1_Wait_NegativeCases()
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void RunSemaphoreSlimTest1_WaitAsync()
{
// Infinite timeout
Expand All @@ -91,7 +90,6 @@ public static void RunSemaphoreSlimTest1_WaitAsync()
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
public static void RunSemaphoreSlimTest1_WaitAsync_NegativeCases()
{
// Invalid timeout
Expand Down Expand Up @@ -464,7 +462,6 @@ private static void RunSemaphoreSlimTest7_AvailableWaitHandle_Helper(int initial
/// <param name="finalCount">The final semaphore count</param>
/// <returns>True if the test succeeded, false otherwise</returns>
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[InlineData(5, 1000, 50, 50, 50, 0, 5, 1000)]
[InlineData(0, 1000, 50, 25, 25, 25, 0, 500)]
[InlineData(0, 1000, 50, 0, 0, 50, 0, 100)]
Expand Down Expand Up @@ -531,7 +528,6 @@ public static void RunSemaphoreSlimTest8_ConcWaitAndRelease(int initial, int max
/// <param name="finalCount">The final semaphore count</param>
/// <returns>True if the test succeeded, false otherwise</returns>
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))]
[InlineData(5, 1000, 50, 50, 50, 0, 5, 500)]
[InlineData(0, 1000, 50, 25, 25, 25, 0, 500)]
[InlineData(0, 1000, 50, 0, 0, 50, 0, 100)]
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<RunScriptOutputPath>$([MSBuild]::NormalizePath('$(WasmAppDir)', '$(RunScriptOutputName)'))</RunScriptOutputPath>

<!-- so that SharedArrayBuffer is enabled -->
<_ServeHeaders Condition="'$(WasmEnableThreads)' == 'true'">$(_ServeHeaders) -h Cross-Origin-Embedder-Policy:require-corp -h Cross-Origin-Opener-Policy:same-origin</_ServeHeaders>
<_ServeHeaders>$(_ServeHeaders) -h Cross-Origin-Embedder-Policy:require-corp -h Cross-Origin-Opener-Policy:same-origin</_ServeHeaders>

<!-- For streaming instantiation of WASM module and browser cache -->
<_ServeMimeTypes>$(_ServeMimeTypes) --mime .wasm=application/wasm</_ServeMimeTypes>
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function displayMeaning(meaning) {
try {
const { setModuleImports } = await dotnet
.withElementOnExit()
.withSidecar(true)
.create();

setModuleImports("main.js", {
Expand Down
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ type MonoConfig = {
* If true, the snapshot of runtime's memory will be stored in the browser and used for faster startup next time. Default is false.
*/
startupMemoryCache?: boolean;
/**
* If true, the runtime will be initialized in separate web worker. Default is false. It requires COOP headers.
*/
sidecar?: boolean;
/**
* If true, a list of the methods optimized by the interpreter will be saved and used for faster startup
* on future runs of the application
Expand Down
33 changes: 27 additions & 6 deletions src/mono/wasm/runtime/loader/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { init_globalization } from "./icu";
import { setupPreloadChannelToMainThread } from "./worker";
import { importLibraryInitializers, invokeLibraryInitializers } from "./libraryInitializers";
import { initCacheToUseIfEnabled } from "./assetsCache";
import { SideCarMessageCreate, SideCarMessageType, dispatchToSideCar } from "./sidecar";


export class HostBuilder implements DotnetHostBuilder {
Expand Down Expand Up @@ -364,6 +365,18 @@ export class HostBuilder implements DotnetHostBuilder {
}
}

withSidecar(value: boolean): DotnetHostBuilder {
try {
deep_merge_config(monoConfig, {
sidecar: value
});
return this;
} catch (err) {
mono_exit(1, err);
throw err;
}
}

async create(): Promise<RuntimeAPI> {
try {
if (!this.instance) {
Expand Down Expand Up @@ -392,13 +405,21 @@ export class HostBuilder implements DotnetHostBuilder {
}

export async function createApi(): Promise<RuntimeAPI> {
if (ENVIRONMENT_IS_WEB && (loaderHelpers.config! as MonoConfigInternal).forwardConsoleLogsToWS && typeof globalThis.WebSocket != "undefined") {
setup_proxy_console("main", globalThis.console, globalThis.location.origin);
if (!monoConfig.sidecar) {
if (ENVIRONMENT_IS_WEB && (loaderHelpers.config! as MonoConfigInternal).forwardConsoleLogsToWS && typeof globalThis.WebSocket != "undefined") {
setup_proxy_console("main", globalThis.console, globalThis.location.origin);
}
mono_assert(emscriptenModule, "Null moduleConfig");
mono_assert(loaderHelpers.config, "Null moduleConfig.config");
await createEmscripten(emscriptenModule);
return globalObjectsRoot.api;
} else {
const createMessage: SideCarMessageCreate = {
type: SideCarMessageType.Create,
config: loaderHelpers.config!
};
return dispatchToSideCar<RuntimeAPI>(createMessage);
}
mono_assert(emscriptenModule, "Null moduleConfig");
mono_assert(loaderHelpers.config, "Null moduleConfig.config");
await createEmscripten(emscriptenModule);
return globalObjectsRoot.api;
}

export async function createEmscripten(moduleFactory: DotnetModuleConfig | ((api: RuntimeAPI) => DotnetModuleConfig)): Promise<RuntimeAPI | EmscriptenModuleInternal> {
Expand Down
Loading

0 comments on commit 4346bb3

Please sign in to comment.