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][mono] Heap snapshot system for WASM #105713

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions eng/liveBuilds.targets
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@
Include="$(LibrariesNativeArtifactsPath)src\es6\*.js"
NativeSubDirectory="src\es6"
IsNative="true" />
<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'browser'"
Include="$(RepoRoot)\src\mono\browser\runtime\heapshot\diagnostics-panel.html"
NativeSubDirectory=""
IsNative="true" />

<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'browser'"
Include="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,15 @@
<PlatformManifestFileEntry Include="icudt_hybrid.dat" IsNative="true" />
<PlatformManifestFileEntry Include="segmentation-rules.json" IsNative="true" />
<PlatformManifestFileEntry Include="package.json" IsNative="true" />
<PlatformManifestFileEntry Include="diagnostics-panel.html" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.es6.pre.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.es6.lib.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.es6.extpost.js" IsNative="true" />
<PlatformManifestFileEntry Include="corebindings.c" IsNative="true" />
<PlatformManifestFileEntry Include="runtime.h" IsNative="true" />
<PlatformManifestFileEntry Include="runtime.c" IsNative="true" />
<PlatformManifestFileEntry Include="driver.c" IsNative="true" />
<PlatformManifestFileEntry Include="heapshot.c" IsNative="true" />
<PlatformManifestFileEntry Include="pinvoke.c" IsNative="true" />
<PlatformManifestFileEntry Include="pinvoke.h" IsNative="true" />
<PlatformManifestFileEntry Include="gc-common.h" IsNative="true" />
Expand Down
9 changes: 9 additions & 0 deletions src/mono/browser/browser.proj
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,14 @@
DestinationFolder="$(MonoObjDir)"
SkipUnchangedFiles="true" />

<Copy SourceFiles="runtime/heapshot/diagnostics-panel.html"
DestinationFolder="$(NativeBinDir)"
SkipUnchangedFiles="true" />

<Copy SourceFiles="runtime/runtime.h;
runtime/runtime.c;
runtime/driver.c;
runtime/heapshot.c;
runtime/pinvoke.c;
runtime/corebindings.c;"
DestinationFolder="$(NativeBinDir)src"
Expand Down Expand Up @@ -511,6 +516,10 @@
Condition="Exists('$(NativeBinDir)dotnet.native.worker.mjs')"
SkipUnchangedFiles="true" />

<Copy SourceFiles="$(RepoRoot)\src\mono\browser\runtime\heapshot\diagnostics-panel.html"
DestinationFolder="$(MicrosoftNetCoreAppRuntimePackNativeDir)"
SkipUnchangedFiles="true" />

<Copy SourceFiles="$(NativeBinDir)dotnet.native.js.symbols"
DestinationFolder="$(MicrosoftNetCoreAppRuntimePackNativeDir)"
SkipUnchangedFiles="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/browser/build/BrowserWasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<!-- If dotnet.{wasm,js} weren't added already (eg. AOT can add them), then add the default ones -->
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.runtime.js" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)diagnostics-panel.html" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.wasm" Condition="'$(_HasDotnetWasm)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.js" Condition="'$(_HasDotnetNativeJs)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.worker.mjs" Condition="'$(_HasDotnetJsWorker)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.native.worker.mjs')" />
Expand Down Expand Up @@ -335,6 +336,8 @@
Dependencies="$(_WasmPInvokeHPath);$(_WasmPInvokeTablePath)" />
<_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)driver.c"
Dependencies="" />
<_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)heapshot.c"
Dependencies="" />
<_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)runtime.c"
Dependencies="@(_RuntimeCDependencies)" />
<_WasmRuntimePackSrcFile Include="$(_WasmRuntimePackSrcDir)corebindings.c" />
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ option(DISABLE_THREADS "defined if the build does NOT support multithreading" ON
option(ENABLE_JS_INTEROP_BY_VALUE "defined when JS interop without pointers to managed objects" OFF)

set(CMAKE_EXECUTABLE_SUFFIX ".js")
add_executable(dotnet.native runtime.c corebindings.c driver.c pinvoke.c)
add_executable(dotnet.native runtime.c corebindings.c driver.c heapshot.c pinvoke.c)

target_include_directories(dotnet.native PUBLIC ${MONO_INCLUDES} ${MONO_OBJ_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/include/wasm)
target_compile_options(dotnet.native PUBLIC @${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-compile.rsp -DGEN_PINVOKE=1 ${CONFIGURATION_COMPILE_OPTIONS})
Expand Down
3 changes: 3 additions & 0 deletions src/mono/browser/runtime/cwraps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_get_f32_unaligned", "number", ["number"]],
[true, "mono_wasm_get_f64_unaligned", "number", ["number"]],
[true, "mono_wasm_read_as_bool_or_null_unsafe", "number", ["number"]],
[true, "mono_wasm_perform_heapshot", "void", ["number"]],

// jiterpreter
[true, "mono_jiterp_trace_bailout", "void", ["number"]],
Expand Down Expand Up @@ -211,6 +212,8 @@ export interface t_Cwraps {
mono_wasm_get_f64_unaligned(source: VoidPtr): number;
mono_wasm_read_as_bool_or_null_unsafe(obj: MonoObject): number;

mono_wasm_perform_heapshot(full: number): void;

mono_jiterp_trace_bailout(reason: number): void;
mono_jiterp_get_trace_bailout_count(reason: number): number;
mono_jiterp_value_copy(destination: VoidPtr, source: VoidPtr, klass: MonoClass): void;
Expand Down
7 changes: 6 additions & 1 deletion src/mono/browser/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data)
free (user_data);
}

void
mono_wasm_heapshot_initialize (void);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_runtime (int debug_level)
{
Expand Down Expand Up @@ -225,6 +228,8 @@ mono_wasm_load_runtime (int debug_level)
root_domain = mono_wasm_load_runtime_common (debug_level, wasm_trace_logger, interp_opts);

bindings_initialize_internals();

mono_wasm_heapshot_initialize();
}

EMSCRIPTEN_KEEPALIVE void
Expand Down Expand Up @@ -558,4 +563,4 @@ mono_wasm_read_as_bool_or_null_unsafe (PVOLATILE(MonoObject) obj) {
end:
MONO_EXIT_GC_UNSAFE;
return result;
}
}
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/exports-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { mono_wasm_diagnostic_server_on_runtime_server_init, mono_wasm_event_pip
import { mono_wasm_diagnostic_server_stream_signal_work_available } from "./diagnostics/server_pthread/stream-queue";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter } from "./profiler";
import {
mono_wasm_heapshot_start, mono_wasm_heapshot_end, mono_wasm_heapshot_assembly,
mono_wasm_heapshot_class, mono_wasm_heapshot_object, mono_wasm_heapshot_gchandle,
mono_wasm_heapshot_roots, mono_wasm_heapshot_stats, mono_wasm_heapshot_counter
} from "./heapshot";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand Down Expand Up @@ -85,6 +90,17 @@ export const mono_wasm_imports = [
mono_wasm_set_entrypoint_breakpoint,
mono_wasm_event_pipe_early_startup_callback,

// heapshot.c
mono_wasm_heapshot_start,
mono_wasm_heapshot_assembly,
mono_wasm_heapshot_class,
mono_wasm_heapshot_object,
mono_wasm_heapshot_gchandle,
mono_wasm_heapshot_roots,
mono_wasm_heapshot_stats,
mono_wasm_heapshot_counter,
mono_wasm_heapshot_end,

// src/native/minipal/random.c
mono_wasm_browser_entropy,

Expand Down
9 changes: 9 additions & 0 deletions src/mono/browser/runtime/gc-handles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ export function assertNoProxies (): void {

let force_dispose_proxies_in_progress = false;

export function enumerateProxies (csObject: Function, jsObject: Function): void {
for (const tup of _js_owned_object_table)
csObject(tup[0], tup[1] ? tup[1].deref() : null);
for (let js_handle = 0; js_handle < _cs_owned_objects_by_js_handle.length; js_handle++)
jsObject(js_handle, _cs_owned_objects_by_js_handle[js_handle]);
for (let jsv_handle = 0; jsv_handle < _cs_owned_objects_by_jsv_handle.length; jsv_handle++)
jsObject(jsv_handle, _cs_owned_objects_by_jsv_handle[jsv_handle]);
}

// when we arrive here from UninstallWebWorkerInterop, the C# will unregister the handles too.
// when called from elsewhere, C# side could be unbalanced!!
export function forceDisposeProxies (disposeMethods: boolean, verbose: boolean): void {
Expand Down
Loading
Loading