Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
- rename loadedMemorySnapshot
 - drop mono_wasm_load_runtime_done
 - reorder startup
  • Loading branch information
pavelsavara committed Mar 9, 2023
commit ee4ba766241a7e1a4a99d25a32b58710bb8a268e
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function mono_download_assets(): Promise<void> {
await memorySnapshotSkippedOrDone.promise;

// start fetching and assets in parallel, only if memory snapshot is not available
if (!runtimeHelpers.loadMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshot) {
for (const asset of afterSnapshotAssets) {
countAndStartDownload(asset);
}
Expand All @@ -135,8 +135,10 @@ 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
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function set_emscripten_entrypoint(
const initialRuntimeHelpers: Partial<RuntimeHelpers> =
{
javaScriptExports: {} as any,
mono_wasm_load_runtime_done: false,
mono_wasm_bindings_is_ready: false,
maxParallelDownloads: 16,
enableDownloadRetry: true,
Expand Down
127 changes: 60 additions & 67 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,23 @@ async function onRuntimeInitializedAsync(userOnRuntimeInitialized: () => void) {
beforeOnRuntimeInitialized.promise_control.resolve();
try {
await wait_for_all_assets();
// load runtime
await mono_wasm_before_user_runtime_initialized();

// load runtime and apply environment settings (if necessary)
await mono_wasm_before_memory_snapshot();

if (MonoWasmThreads) {
await instantiateWasmPThreadWorkerPool();
await mono_wasm_init_diagnostics();
}

bindings_init();
if (!runtimeHelpers.mono_wasm_runtime_is_ready) mono_wasm_runtime_ready();
if (!runtimeHelpers.mono_wasm_symbols_are_ready) readSymbolMapFile("dotnet.js.symbols");

setTimeout(() => {
// when there are free CPU cycles
string_decoder.init_fields();
});

// call user code
try {
Expand Down Expand Up @@ -265,6 +280,11 @@ async function postRunAsync(userpostRun: (() => void)[]) {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: postRunAsync");
try {
const mark = startMeasure();

// create /usr/share folder which is SpecialFolder.CommonApplicationData
Module["FS_createPath"]("/", "usr", true, true);
Module["FS_createPath"]("/", "usr/share", true, true);

// all user Module.postRun callbacks
userpostRun.map(fn => fn());
endMeasure(mark, MeasuredBlock.postRun);
Expand Down Expand Up @@ -343,35 +363,37 @@ async function mono_wasm_pre_init_full(): Promise<void> {
Module.removeRunDependency("mono_wasm_pre_init_full");
}

async function mono_wasm_before_user_runtime_initialized(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_before_user_runtime_initialized");
async function mono_wasm_before_memory_snapshot() {
if (runtimeHelpers.loadedMemorySnapshot) {
// all things below are loaded from the snapshot
return;
}

try {
await _apply_configuration_from_args();
mono_wasm_globalization_init();
for (const k in config.environmentVariables) {
const v = config.environmentVariables![k];
if (typeof (v) === "string")
mono_wasm_setenv(k, v);
else
throw new Error(`Expected environment variable '${k}' to be a string but it was ${typeof v}: '${v}'`);
}

if (!config.startupMemoryCache || !runtimeHelpers.loadMemorySnapshot) {
if (!runtimeHelpers.mono_wasm_load_runtime_done) mono_wasm_load_runtime("unused", config.debugLevel);
}
if (config.startupMemoryCache && !runtimeHelpers.loadMemorySnapshot) {
await storeMemorySnapshot(Module.HEAP8.buffer);
runtimeHelpers.storeMemorySnapshotPending = false;
}
if (MonoWasmThreads) {
await instantiateWasmPThreadWorkerPool();
await mono_wasm_init_diagnostics();
}
bindings_init();
if (!runtimeHelpers.mono_wasm_runtime_is_ready) mono_wasm_runtime_ready();
if (!runtimeHelpers.mono_wasm_symbols_are_ready) readSymbolMapFile("dotnet.js.symbols");
if (config.runtimeOptions)
mono_wasm_set_runtime_options(config.runtimeOptions);

setTimeout(() => {
// when there are free CPU cycles
string_decoder.init_fields();
});
} catch (err: any) {
_print_error("MONO_WASM: Error in mono_wasm_before_user_runtime_initialized", err);
throw err;
if (config.aotProfilerOptions)
mono_wasm_init_aot_profiler(config.aotProfilerOptions);

if (config.browserProfilerOptions)
mono_wasm_init_browser_profiler(config.browserProfilerOptions);

mono_wasm_globalization_init();

mono_wasm_load_runtime("unused", config.debugLevel);

// we didn't have snapshot yet and the feature is enabled. Take snapshot now.
if (config.startupMemoryCache) {
await storeMemorySnapshot(Module.HEAP8.buffer);
runtimeHelpers.storeMemorySnapshotPending = false;
}
}

Expand Down Expand Up @@ -476,10 +498,10 @@ async function instantiate_wasm_module(

if (config.startupMemoryCache && config.assetsHash) {
memorySize = await getMemorySnapshotSize();
runtimeHelpers.loadMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadMemorySnapshot;
runtimeHelpers.loadedMemorySnapshot = !!memorySize;
runtimeHelpers.storeMemorySnapshotPending = !runtimeHelpers.loadedMemorySnapshot;
}
if (!runtimeHelpers.loadMemorySnapshot) {
if (!runtimeHelpers.loadedMemorySnapshot) {
// we should start downloading DLLs etc as they are not in the snapshot
memorySnapshotSkippedOrDone.promise_control.resolve();
}
Expand All @@ -494,7 +516,7 @@ async function instantiate_wasm_module(
assetToLoad.buffer = null as any; // GC
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: instantiate_wasm_module done");

if (runtimeHelpers.loadMemorySnapshot) {
if (runtimeHelpers.loadedMemorySnapshot) {
try {
const wasmMemory = (Module.asm?.memory || Module.wasmMemory)!;

Expand All @@ -509,7 +531,7 @@ async function instantiate_wasm_module(
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: Loaded memory from cache");
} catch (err) {
console.warn("MONO_WASM: failed to load memory snapshot", err);
runtimeHelpers.loadMemorySnapshot = false;
runtimeHelpers.loadedMemorySnapshot = false;
}
// now we know if the loading of memory succeeded or not, we can start loading the rest of the assets
memorySnapshotSkippedOrDone.promise_control.resolve();
Expand All @@ -523,46 +545,17 @@ async function instantiate_wasm_module(
Module.removeRunDependency("instantiate_wasm_module");
}

async function _apply_configuration_from_args() {
// create /usr/share folder which is SpecialFolder.CommonApplicationData
Module["FS_createPath"]("/", "usr", true, true);
Module["FS_createPath"]("/", "usr/share", true, true);

for (const k in config.environmentVariables) {
const v = config.environmentVariables![k];
if (typeof (v) === "string")
mono_wasm_setenv(k, v);
else
throw new Error(`Expected environment variable '${k}' to be a string but it was ${typeof v}: '${v}'`);
}

if (config.runtimeOptions)
mono_wasm_set_runtime_options(config.runtimeOptions);

if (config.aotProfilerOptions)
mono_wasm_init_aot_profiler(config.aotProfilerOptions);

if (config.browserProfilerOptions)
mono_wasm_init_browser_profiler(config.browserProfilerOptions);
}

export function mono_wasm_load_runtime(unused?: string, debugLevel?: number): void {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_load_runtime");
if (runtimeHelpers.mono_wasm_load_runtime_done) {
return;
}
runtimeHelpers.mono_wasm_load_runtime_done = true;
try {
const mark = startMeasure();
if (!runtimeHelpers.loadMemorySnapshot) {
if (debugLevel == undefined) {
debugLevel = 0;
if (config && config.debugLevel) {
debugLevel = 0 + debugLevel;
}
if (debugLevel == undefined) {
debugLevel = 0;
if (config && config.debugLevel) {
debugLevel = 0 + debugLevel;
}
cwraps.mono_wasm_load_runtime(unused || "unused", debugLevel);
}
cwraps.mono_wasm_load_runtime(unused || "unused", debugLevel);
endMeasure(mark, MeasuredBlock.loadRuntime);

if (!config.startupMemoryCache) bindings_init();
Expand Down
3 changes: 1 addition & 2 deletions src/mono/wasm/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ export type RuntimeHelpers = {
runtime_interop_exports_class: MonoClass;

_i52_error_scratch_buffer: Int32Ptr;
mono_wasm_load_runtime_done: boolean;
mono_wasm_runtime_is_ready: boolean;
mono_wasm_bindings_is_ready: boolean;
mono_wasm_symbols_are_ready: boolean;
Expand All @@ -233,7 +232,7 @@ export type RuntimeHelpers = {
locateFile: (path: string, prefix?: string) => string,
javaScriptExports: JavaScriptExports,
loadedFiles: string[],
loadMemorySnapshot: boolean,
loadedMemorySnapshot: boolean,
storeMemorySnapshotPending: boolean,
subtle: SubtleCrypto | null,
preferredIcuAsset: string | null,
Expand Down