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] wasm memory snapshot into browser cache #82049

Merged
merged 56 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
ea02d94
wip
pavelsavara Mar 3, 2023
c1aa6c3
wip
pavelsavara Mar 3, 2023
bf9ef4d
wip
pavelsavara Mar 3, 2023
f883a72
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 3, 2023
3fe9aba
wip
pavelsavara Mar 3, 2023
f4225aa
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 3, 2023
d2e5b49
fix
pavelsavara Mar 3, 2023
c409f51
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 6, 2023
e695c42
feedback
pavelsavara Mar 6, 2023
1189015
lint
pavelsavara Mar 6, 2023
b92c82e
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
7ccc8e6
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
ec762ac
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
91c0211
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
20d548b
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 6, 2023
8e6865c
more linear memory usage and recovery from failure of snapshot use
pavelsavara Mar 6, 2023
7bf7881
feedback
pavelsavara Mar 6, 2023
aec9b53
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 8, 2023
5651f37
start runtime twice for unit tests, so that the actual tests is runni…
pavelsavara Mar 8, 2023
a16f667
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 8, 2023
31b46ff
wip
pavelsavara Mar 8, 2023
43a73e1
continue loading non-snapshot assets
pavelsavara Mar 8, 2023
f49354a
disable adding methods to __indirect_function_table before we take me…
pavelsavara Mar 8, 2023
53781ab
rename startupMemoryCache
pavelsavara Mar 8, 2023
60b2c5a
try to fix threads
pavelsavara Mar 8, 2023
33b6e3e
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 8, 2023
977b46c
whitespace
pavelsavara Mar 8, 2023
933594b
fix
pavelsavara Mar 9, 2023
c68c9e8
fix storing memory
pavelsavara Mar 9, 2023
fa3e331
assert
pavelsavara Mar 9, 2023
c7f913b
- make snapshot hash more resilient
pavelsavara Mar 9, 2023
9cb1b72
fix
pavelsavara Mar 9, 2023
d1bbb9c
- renamed memorySnapshotSkippedOrDone
pavelsavara Mar 9, 2023
ee4ba76
- rename loadedMemorySnapshot
pavelsavara Mar 9, 2023
83f0032
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 9, 2023
85ac81d
fix
pavelsavara Mar 9, 2023
b7790f2
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 10, 2023
3168c42
- internal config exitAfterSnapshot
pavelsavara Mar 10, 2023
4a25447
measure
pavelsavara Mar 10, 2023
5067cf7
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 11, 2023
1761786
fix
pavelsavara Mar 11, 2023
94e931a
fix
pavelsavara Mar 13, 2023
2a7508e
optimize timezone detection
pavelsavara Mar 13, 2023
9759ad8
feedback
pavelsavara Mar 13, 2023
1f4e4c6
fix
pavelsavara Mar 13, 2023
e2a2cd4
- enable snapshot in blazor
pavelsavara Mar 13, 2023
eaaeaa5
store cache key
pavelsavara Mar 13, 2023
bc86ffe
feedback and cleanup
pavelsavara Mar 13, 2023
59a7a56
fix debugging
pavelsavara Mar 13, 2023
6d9f7ee
fix
pavelsavara Mar 14, 2023
3e961d1
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 14, 2023
f617787
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 14, 2023
cfc5acf
Update src/mono/wasm/memory-snapshot.md
pavelsavara Mar 14, 2023
c78ef57
Update src/mono/wasm/runtime/assets.ts
pavelsavara Mar 14, 2023
7309e43
Update src/mono/wasm/runtime/assets.ts
pavelsavara Mar 14, 2023
5f8aeef
Merge branch 'main' into browser_memory_snapshot3
pavelsavara Mar 14, 2023
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
2 changes: 0 additions & 2 deletions src/mono/sample/wasm/browser-advanced/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<script type='module' src="./dotnet.js"></script>
<link rel="preload" href="./mono-config.json" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
<link rel="prefetch" href="./managed/System.Private.CoreLib.webcil" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
2 changes: 0 additions & 2 deletions src/mono/sample/wasm/browser-bench/appstart-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
<script type='module' src="./dotnet.js"></script>
<link rel="preload" href="./mono-config.json" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./managed/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
<PropertyGroup>
<_WasmAppIncludeThreadsWorker Condition="'$(WasmEnableThreads)' == 'true' or '$(WasmEnablePerfTracing)' == 'true'">true</_WasmAppIncludeThreadsWorker>
<!-- TODO: set this from some user-facing property? -1 means use the default baked into dotnet.js -->
<_WasmPThreadPoolSize Condition="'$(_WasmPThreadPoolSize)' == '' and ('$(WasmEnableThreads)' == 'true' or '$(WasmEnablePerfTracing)' == 'true')">-1</_WasmPThreadPoolSize>
<_WasmPThreadPoolSize Condition="'$(_WasmPThreadPoolSize)' == ''">-1</_WasmPThreadPoolSize>
</PropertyGroup>

<ItemGroup>
Expand Down
27 changes: 27 additions & 0 deletions src/mono/wasm/memory-snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Memory snapshot of the Mono runtime #
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved

We take snapshot of WASM memory after first cold start at run time​.
We store it on the client side in the browser cache.
For subsequent runs with the same configuration and same assets, we use the snapshot .
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
instead of downloading everything again and doing the runtime startup again.
These subsequent starts are significantly faster.

### Implementation details

- the consistency of inputs (configuration and assets) with the snapshot is done by calculating SHA256 of the inputs.
- the DLLs and other downloaded assets each have SHA256 which is used to validate 'integrity' by the browser.
- the mono-config has field `assetsHash` which is summary SHA256 of all the assets.
- the configuration could be changed programmatically and so we calculate the hash at the runtime, just before taking snapshot.
- the snapshot is taken just after we initialize the Mono runtime.
- before cwraps are initialized (they would be initialized again on subsequent start).
- after loading all the DLLs into memory.
- after loading ICU and timezone data.
- after applying environment variables and other runtime options.
- before any worker threads initialization.
- before any JavaScript interop initialization.
- before any Managed code executes.
- therefore we do not expect to store any application state in the snapshot.

### How to opt out
You can turn this feature of by calling `withStartupMemoryCache (false)` on [dotnet API](https://github.com/dotnet/runtime/blob/main/src/mono/wasm/runtime/dotnet.d.ts).

46 changes: 43 additions & 3 deletions src/mono/wasm/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { mono_wasm_load_bytes_into_heap } from "./memory";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
import { abort_startup, beforeOnRuntimeInitialized, memorySnapshotSkippedOrDone } from "./startup";
import { AssetBehaviours, AssetEntry, AssetEntryInternal, LoadingResource, mono_assert, ResourceRequest } from "./types";
import { InstantiateWasmSuccessCallback, VoidPtr } from "./types/emscripten";

Expand Down Expand Up @@ -38,6 +38,19 @@ const skipBufferByAssetTypes: {
"dotnetwasm": true,
};

const skipInSnapshotByAssetTypes: {
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
[k: string]: boolean
} = {
"resource": true,
"assembly": true,
"pdb": true,
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
"heap": true,
"icu": true,
"js-module-threads": true,
"dotnetwasm": true,
};


// these assets are instantiated differently than the main flow
const skipInstantiateByAssetTypes: {
[k: string]: boolean
Expand All @@ -63,8 +76,10 @@ export async function mono_download_assets(): Promise<void> {
runtimeHelpers.maxParallelDownloads = runtimeHelpers.config.maxParallelDownloads || runtimeHelpers.maxParallelDownloads;
runtimeHelpers.enableDownloadRetry = runtimeHelpers.config.enableDownloadRetry || runtimeHelpers.enableDownloadRetry;
try {
const beforeSnapshotAssets: AssetEntryInternal[] = [];
const afterSnapshotAssets: AssetEntryInternal[] = [];
const promises_of_assets: Promise<AssetEntryInternal>[] = [];
// start fetching and instantiating all assets in parallel

for (const a of runtimeHelpers.config.assets!) {
const asset: AssetEntryInternal = a;
mono_assert(typeof asset === "object", "asset must be object");
Expand All @@ -73,14 +88,38 @@ export async function mono_download_assets(): Promise<void> {
mono_assert(!asset.resolvedUrl || typeof asset.resolvedUrl === "string", "asset resolvedUrl could be string");
mono_assert(!asset.hash || typeof asset.hash === "string", "asset resolvedUrl could be string");
mono_assert(!asset.pendingDownload || typeof asset.pendingDownload === "object", "asset pendingDownload could be object");
if (skipInSnapshotByAssetTypes[asset.behavior]) {
afterSnapshotAssets.push(asset);
} else {
beforeSnapshotAssets.push(asset);
}
}

const countAndStartDownload = (asset: AssetEntryInternal) => {
if (!skipInstantiateByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset)) {
expected_instantiated_assets_count++;
}
if (!skipDownloadsByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset)) {
expected_downloaded_assets_count++;
promises_of_assets.push(start_asset_download(asset));
}
};

// start fetching and assets in parallel, only assets which are not part of memory snapshot
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
for (const asset of beforeSnapshotAssets) {
countAndStartDownload(asset);
}

// continue after we know if memory snapshot is available or not
await memorySnapshotSkippedOrDone.promise;

// start fetching and assets in parallel, only if memory snapshot is not available
if (!runtimeHelpers.loadedMemorySnapshot) {
for (const asset of afterSnapshotAssets) {
countAndStartDownload(asset);
}
}

allDownloadsQueued.promise_control.resolve();

const promises_of_asset_instantiation: Promise<void>[] = [];
Expand All @@ -96,8 +135,9 @@ export async function mono_download_assets(): Promise<void> {
asset.pendingDownload = null as any; // GC
asset.buffer = null as any; // GC

// wait till after onRuntimeInitialized and after memory snapshot is loaded or skipped
await memorySnapshotSkippedOrDone.promise;
await beforeOnRuntimeInitialized.promise;
// this is after onRuntimeInitialized
_instantiate_asset(asset, url, data);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/diagnostics/server_pthread/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class DiagnosticServerImpl implements DiagnosticServer {
}

async stopEventPipe(ws: WebSocket | MockRemoteSocket, sessionID: EventPipeSessionIDImpl): Promise<void> {
console.info("MONO_WASM: stopEventPipe", sessionID);
console.debug("MONO_WASM: stopEventPipe", sessionID);
cwraps.mono_wasm_event_pipe_session_disable(sessionID);
// we might send OK before the session is actually stopped since the websocket is async
// but the client end should be robust to that.
Expand Down Expand Up @@ -266,7 +266,7 @@ class DiagnosticServerImpl implements DiagnosticServer {

resumeRuntime(): void {
if (!this.runtimeResumed) {
console.info("MONO_WASM: resuming runtime startup");
console.debug("MONO_WASM: resuming runtime startup");
cwraps.mono_wasm_diagnostic_server_post_resume_runtime();
this.runtimeResumed = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/mono/wasm/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface DotnetHostBuilder {
withDebugging(level: number): DotnetHostBuilder;
withMainAssembly(mainAssemblyName: string): DotnetHostBuilder;
withApplicationArgumentsFromQuery(): DotnetHostBuilder;
withStartupMemoryCache(value: boolean): DotnetHostBuilder;
create(): Promise<RuntimeAPI>;
run(): Promise<number>;
}
Expand Down Expand Up @@ -128,6 +129,10 @@ type MonoConfig = {
* initial number of workers to add to the emscripten pthread pool
*/
pthreadPoolSize?: number;
/**
* If true, the snapshot of runtime's memory will be stored in the browser and used for faster startup next time. Default is true.
*/
startupMemoryCache?: boolean;
/**
* hash of assets
*/
Expand Down
7 changes: 4 additions & 3 deletions src/mono/wasm/runtime/jiterpreter-jit-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { mono_assert, MonoType, MonoMethod } from "./types";
import { NativePointer, Int32Ptr, VoidPtr } from "./types/emscripten";
import { Module } from "./imports";
import { Module, runtimeHelpers } from "./imports";
import {
getU8, getI32_unaligned, getU32_unaligned, setU32_unchecked
} from "./memory";
Expand Down Expand Up @@ -269,8 +269,9 @@ export function mono_jiterp_do_jit_call_indirect (
}
};

let failed = !getIsWasmEhSupported();
if (!failed) {
let failed = false;
const enabled = !runtimeHelpers.storeMemorySnapshotPending && getIsWasmEhSupported();
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
if (enabled) {
// Wasm EH is supported which means doJitCallModule was loaded and compiled.
// Now that we have jit_call_cb, we can instantiate it.
try {
Expand Down
7 changes: 4 additions & 3 deletions src/mono/wasm/runtime/jiterpreter-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { mono_assert } from "./types";
import { NativePointer, ManagedPointer, VoidPtr } from "./types/emscripten";
import { Module } from "./imports";
import { Module, runtimeHelpers } from "./imports";
import { WasmOpcode } from "./jiterpreter-opcodes";
import cwraps from "./cwraps";

Expand Down Expand Up @@ -1201,8 +1201,9 @@ export function getWasmFunctionTable () {
}

export function addWasmFunctionPointer (f: Function) {
if (!f)
throw new Error("Attempting to set null function into table");
mono_assert(f,"Attempting to set null function into table");
mono_assert(!runtimeHelpers.storeMemorySnapshotPending, "Attempting to set function into table during creation of memory snapshot");

const table = getWasmFunctionTable();
if (wasmFunctionIndicesFree <= 0) {
wasmNextFunctionIndex = table.length;
Expand Down
4 changes: 3 additions & 1 deletion src/mono/wasm/runtime/jiterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { mono_assert, MonoMethod } from "./types";
import { NativePointer } from "./types/emscripten";
import { Module } from "./imports";
import { Module, runtimeHelpers } from "./imports";
import {
getU16, getU32_unaligned
} from "./memory";
Expand Down Expand Up @@ -762,6 +762,8 @@ function generate_wasm (
// independently jitting traces will not stomp on each other and all threads
// have a globally consistent view of which function pointer maps to each trace.
rejected = false;
mono_assert(!runtimeHelpers.storeMemorySnapshotPending, "Attempting to set function into table during creation of memory snapshot");

const idx =
trapTraceErrors
? Module.addFunction(
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export async function init_polyfills_async(): Promise<void> {
}
}
}
runtimeHelpers.subtle = globalThis.crypto?.subtle;
}

const dummyPerformance = {
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const enum MeasuredBlock {
preRunWorker = "mono.preRunWorker",
onRuntimeInitialized = "mono.onRuntimeInitialized",
postRun = "mono.postRun",
memorySnapshot = "mono.memorySnapshot",
loadRuntime = "mono.loadRuntime",
bindingsInit = "mono.bindingsInit",
bindJsFunction = "mono.bindJsFunction:",
Expand Down
14 changes: 14 additions & 0 deletions src/mono/wasm/runtime/run-outer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface DotnetHostBuilder {
withDebugging(level: number): DotnetHostBuilder
withMainAssembly(mainAssemblyName: string): DotnetHostBuilder
withApplicationArgumentsFromQuery(): DotnetHostBuilder
withStartupMemoryCache(value: boolean): DotnetHostBuilder
create(): Promise<RuntimeAPI>
run(): Promise<number>
}
Expand Down Expand Up @@ -137,6 +138,19 @@ class HostBuilder implements DotnetHostBuilder {
}
}

withStartupMemoryCache(value: boolean): DotnetHostBuilder {
try {
const configInternal: MonoConfigInternal = {
startupMemoryCache: value
};
Object.assign(this.moduleConfig.config!, configInternal);
return this;
} catch (err) {
mono_exit(1, err);
throw err;
}
}

withConfig(config: MonoConfig): DotnetHostBuilder {
try {
const providedConfig = { ...config };
Expand Down
4 changes: 3 additions & 1 deletion src/mono/wasm/runtime/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ function set_exit_code_and_quit_now(exit_code: number, reason?: any): void {
Module.err(JSON.stringify(reason));
}
}
else {
else if (!reason) {
reason = new runtimeHelpers.ExitStatus(exit_code);
} else {
exit_code = reason.status;
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
}
}
logErrorOnExit(exit_code, reason);
Expand Down
Loading