Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser][MT] pre-load threads early in dotnet.js #98637

Merged
merged 7 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/mono/browser/build/BrowserWasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@

<PropertyGroup>
<!-- TODO: set this from some user-facing property? -1 means use the default baked into dotnet.native.js -->
<_WasmPThreadPoolSize Condition="'$(_WasmPThreadPoolSize)' == ''">-1</_WasmPThreadPoolSize>
<_WasmPThreadPoolInitialSize Condition="'$(_WasmPThreadPoolInitialSize)' == ''">-1</_WasmPThreadPoolInitialSize>
<_WasmPThreadPoolUnusedSize Condition="'$(_WasmPThreadPoolUnusedSize)' == ''">-1</_WasmPThreadPoolUnusedSize>
</PropertyGroup>

<ItemGroup>
Expand All @@ -148,7 +149,8 @@
NativeAssets="@(WasmNativeAsset)"
DebugLevel="$(WasmDebugLevel)"
IncludeThreadsWorker="$(WasmEnableThreads)"
PThreadPoolSize="$(_WasmPThreadPoolSize)"
PThreadPoolInitialSize="$(_WasmPThreadPoolInitialSize)"
PThreadPoolUnusedSize="$(_WasmPThreadPoolUnusedSize)"
UseWebcil="$(WasmEnableWebcil)"
WasmIncludeFullIcuData="$(WasmIncludeFullIcuData)"
WasmIcuDataFileName="$(WasmIcuDataFileName)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { threads_c_functions as cwraps } from "../../cwraps";
import { INTERNAL, mono_assert } from "../../globals";
import { mono_log_info, mono_log_debug, mono_log_warn } from "../../logging";
import { withStackAlloc, getI32 } from "../../memory";
import { waitForThread } from "../../pthreads/browser";
import { waitForThread } from "../../pthreads";
import { isDiagnosticMessage, makeDiagnosticServerControlCommand } from "../shared/controller-commands";
import monoDiagnosticsMock from "consts:monoDiagnosticsMock";
import { PThreadPtr, Thread } from "../../types/internal";
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/diagnostics/mock/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { FilterPredicate, MockEnvironment } from "./types";
import Serializer from "../server_pthread/ipc-protocol/base-serializer";
import { CommandSetId, EventPipeCommandId, ProcessCommandId } from "../server_pthread/ipc-protocol/types";
import { assertNever } from "../../types/internal";
import { pthread_self } from "../../pthreads/worker";
import { pthread_self } from "../../pthreads";
import { createPromiseController, mono_assert } from "../../globals";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import monoDiagnosticsMock from "consts:monoDiagnosticsMock";

import { PromiseAndController, assertNever } from "../../types/internal";
import { pthread_self } from "../../pthreads/worker";
import { pthread_self } from "../../pthreads";
import { createPromiseController, mono_assert } from "../../globals";
import { threads_c_functions as cwraps } from "../../cwraps";
import { EventPipeSessionIDImpl } from "../shared/types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { isMonoThreadMessage } from "../../pthreads/shared";
import { isMonoThreadMessage } from "../../pthreads";
import type { MonoThreadMessage } from "../../types/internal";

// Messages from the main thread to the diagnostic server thread
Expand Down
10 changes: 7 additions & 3 deletions src/mono/browser/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,15 @@ type MonoConfig = {
/**
* initial number of workers to add to the emscripten pthread pool
*/
pthreadPoolSize?: number;
pthreadPoolInitialSize?: number;
/**
* initial number of unused workers keep in the emscripten pthread pool after startup
* number of unused workers kept in the emscripten pthread pool after startup
*/
pthreadPoolReady?: number;
pthreadPoolUnusedSize?: number;
/**
* Delay in milliseconds before starting the finalizer thread
*/
finalizerThreadStartDelayMs?: number;
/**
* 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
7 changes: 4 additions & 3 deletions src/mono/browser/runtime/exports-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js }
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_eventloop_has_unsettled_interop_promises } from "./pthreads/shared/eventloop";
import { mono_wasm_pthread_on_pthread_attached, mono_wasm_pthread_on_pthread_unregistered, mono_wasm_pthread_on_pthread_registered, mono_wasm_pthread_set_name } from "./pthreads/worker";
import { mono_wasm_eventloop_has_unsettled_interop_promises } from "./pthreads";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_wasm_diagnostic_server_on_server_thread_created } from "./diagnostics/server_pthread";
Expand All @@ -22,13 +21,15 @@ import { mono_wasm_profiler_leave, mono_wasm_profiler_enter } from "./profiler";
import { mono_wasm_change_case, mono_wasm_change_case_invariant } from "./hybrid-globalization/change-case";
import { mono_wasm_compare_string, mono_wasm_ends_with, mono_wasm_starts_with, mono_wasm_index_of } from "./hybrid-globalization/collations";
import { mono_wasm_get_calendar_info } from "./hybrid-globalization/calendar";
import { mono_wasm_install_js_worker_interop, mono_wasm_uninstall_js_worker_interop } from "./pthreads/shared";

import { mono_wasm_get_culture_info } from "./hybrid-globalization/culture-info";
import { mono_wasm_get_first_day_of_week, mono_wasm_get_first_week_of_year } from "./hybrid-globalization/locales";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

import { mono_wasm_pthread_on_pthread_attached, mono_wasm_pthread_on_pthread_unregistered, mono_wasm_pthread_on_pthread_registered, mono_wasm_pthread_set_name } from "./pthreads";
import { mono_wasm_install_js_worker_interop, mono_wasm_uninstall_js_worker_interop } from "./pthreads";

// the JS methods would be visible to EMCC linker and become imports of the WASM module

export const mono_wasm_threads_imports = !WasmEnableThreads ? [] : [
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/exports-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { mono_wasm_get_func_id_to_name_mappings } from "./logging";
import { monoStringToStringUnsafe } from "./strings";
import { mono_wasm_bind_cs_function } from "./invoke-cs";

import { dumpThreads, thread_available } from "./pthreads/browser";
import { dumpThreads, thread_available } from "./pthreads";

export function export_internal(): any {
return {
Expand Down
6 changes: 1 addition & 5 deletions src/mono/browser/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { mono_wasm_stringify_as_error_with_stack } from "./logging";
import { instantiate_asset, instantiate_symbols_asset, instantiate_segmentation_rules_asset } from "./assets";
import { jiterpreter_dump_stats } from "./jiterpreter";
import { forceDisposeProxies } from "./gc-handles";
import { dumpThreads } from "./pthreads/browser";
import { dumpThreads } from "./pthreads";

export let runtimeList: RuntimeList;

Expand Down Expand Up @@ -70,10 +70,6 @@ function initializeExports(globalObjects: GlobalObjects): RuntimeAPI {
runtimeList = globalThisAny.getDotnetRuntime.__list;
}

if (BuildConfiguration === "Debug") {
globalThisAny.INTERNAL = globals.internal;
}

return exportedRuntimeAPI;
}

Expand Down
5 changes: 3 additions & 2 deletions src/mono/browser/runtime/interp-pgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ export async function getCacheKey(prefix: string): Promise<string | null> {
delete inputs.interopCleanupOnExit;
delete inputs.dumpThreadsOnNonZeroExit;
delete inputs.logExitCode;
delete inputs.pthreadPoolSize;
delete inputs.pthreadPoolReady;
delete inputs.pthreadPoolInitialSize;
delete inputs.pthreadPoolUnusedSize;
delete inputs.finalizerThreadStartDelayMs;
delete inputs.asyncFlushOnExit;
delete inputs.remoteSources;
delete inputs.ignorePdbLoadErrors;
Expand Down
8 changes: 5 additions & 3 deletions src/mono/browser/runtime/loader/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ let parallel_count = 0;
const assetsToLoad: AssetEntryInternal[] = [];
const singleAssets: Map<string, AssetEntryInternal> = new Map();

const worker_empty_prefix = " - ";
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved

const jsRuntimeModulesAssetTypes: {
[k: string]: boolean
} = {
Expand Down Expand Up @@ -738,7 +740,7 @@ export async function streamingCompileWasm() {
export function preloadWorkers() {
if (!WasmEnableThreads) return;
const jsModuleWorker = resolve_single_asset_path("js-module-threads");
for (let i = 0; i < loaderHelpers.config.pthreadPoolSize!; i++) {
for (let i = 0; i < loaderHelpers.config.pthreadPoolInitialSize!; i++) {
const workerNumber = loaderHelpers.workerNextNumber++;
const worker: Partial<PThreadWorker> = new Worker(jsModuleWorker.resolvedUrl!, {
name: "dotnet-worker-" + workerNumber.toString().padStart(3, "0"),
Expand All @@ -748,9 +750,9 @@ export function preloadWorkers() {
pthreadId: PThreadPtrNull,
reuseCount: 0,
updateCount: 0,
threadPrefix: " - ",
threadPrefix: worker_empty_prefix,
threadName: "emscripten-pool",
} as any;
loaderHelpers.loadingWorkers.push(worker as any);
}
}
}
11 changes: 7 additions & 4 deletions src/mono/browser/runtime/loader/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,14 @@ export function normalizeConfig() {
}

// ActiveIssue https://github.com/dotnet/runtime/issues/75602
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
if (WasmEnableThreads && !Number.isInteger(config.pthreadPoolSize)) {
config.pthreadPoolSize = 7;
if (WasmEnableThreads && !Number.isInteger(config.pthreadPoolInitialSize)) {
config.pthreadPoolInitialSize = 7;
}
if (WasmEnableThreads && !Number.isInteger(config.pthreadPoolReady)) {
config.pthreadPoolReady = 3;
if (WasmEnableThreads && !Number.isInteger(config.pthreadPoolUnusedSize)) {
config.pthreadPoolUnusedSize = 3;
}
if (WasmEnableThreads && !Number.isInteger(config.finalizerThreadStartDelayMs)) {
config.finalizerThreadStartDelayMs = 200;
}

// this is how long the Mono GC will try to wait for all threads to be suspended before it gives up and aborts the process
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/managed-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { marshal_array_to_cs, marshal_array_to_cs_impl, marshal_bool_to_cs, mars
import { marshal_int32_to_js, end_marshal_task_to_js, marshal_string_to_js, begin_marshal_task_to_js, marshal_exception_to_js } from "./marshal-to-js";
import { do_not_force_dispose } from "./gc-handles";
import { assert_c_interop, assert_js_interop } from "./invoke-js";
import { mono_wasm_main_thread_ptr } from "./pthreads/shared";
import { mono_wasm_main_thread_ptr } from "./pthreads";
import { _zero_region } from "./memory";
import { stringToUTF8Ptr } from "./strings";

Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/marshal-to-cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { _zero_region, forceThreadMemoryViewRefresh, localHeapViewF64, localHeap
import { stringToMonoStringRoot, stringToUTF16 } from "./strings";
import { JSMarshalerArgument, JSMarshalerArguments, JSMarshalerType, MarshalerToCs, MarshalerToJs, BoundMarshalerToCs, MarshalerType } from "./types/internal";
import { TypedArray } from "./types/emscripten";
import { addUnsettledPromise, settleUnsettledPromise } from "./pthreads/shared/eventloop";
import { addUnsettledPromise, settleUnsettledPromise } from "./pthreads";
import { mono_log_debug } from "./logging";
import { complete_task } from "./managed-exports";
import { gc_locked } from "./gc-lock";
Expand Down
4 changes: 2 additions & 2 deletions src/mono/browser/runtime/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import type { EmscriptenReplacements } from "./types/internal";
import type { TypedArray } from "./types/emscripten";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_WORKER, INTERNAL, Module, loaderHelpers, runtimeHelpers } from "./globals";
import { replaceEmscriptenPThreadUI } from "./pthreads/browser/replacements";
import { replaceEmscriptenPThreadWorker } from "./pthreads/worker/replacements";
import { replaceEmscriptenPThreadWorker } from "./pthreads";
import { replaceEmscriptenPThreadUI } from "./pthreads";

const dummyPerformance = {
now: function () {
Expand Down
4 changes: 2 additions & 2 deletions src/mono/browser/runtime/pthreads/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ On the other hand, pthreads in native code have a peer relationship: any two thr

## Main thread API

In the main thread, `pthreads/browser` provides a `getThread` function that returns a `{ pthread_ptr: pthread_ptr, worker: Worker, port: MessagePort }` object that can be used to communicate with the worker thread.
In the main thread, `pthreads/ui-thread` provides a `getThread` function that returns a `{ pthread_ptr: pthread_ptr, worker: Worker, port: MessagePort }` object that can be used to communicate with the worker thread.

## Worker thread API

In the worker threads, `pthread/worker` provides `currentWorkerThreadEvents` which is an [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) that fires `'dotnet:pthread:created'` and `'dotnet:pthread:attached'` events when a pthread is started on the worker, and when that pthread attaches to the Mono runtime. A good place to add event listeners is in `mono_wasm_pthread_worker_init` in `startup.ts`.
In the worker threads, `pthread/worker-*` provides `currentWorkerThreadEvents` which is an [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) that fires `'dotnet:pthread:created'` and `'dotnet:pthread:attached'` events when a pthread is started on the worker, and when that pthread attaches to the Mono runtime. A good place to add event listeners is in `mono_wasm_pthread_worker_init` in `startup.ts`.
The events have a `portToMain` property which is a dotnet-specific `MessagePort` for posting messages to the main thread and for listening for messages from the main thread.

## Implementation
Expand Down
140 changes: 0 additions & 140 deletions src/mono/browser/runtime/pthreads/browser/replacements.ts

This file was deleted.

Loading
Loading