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] add and load empty ES6 module dotnet.diag.js when FeaturePerfTracing #112787

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions eng/liveBuilds.targets
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@
$(LibrariesNativeArtifactsPath)dotnet.native.js;
$(LibrariesNativeArtifactsPath)dotnet.runtime.js;
$(LibrariesNativeArtifactsPath)dotnet.runtime.js.map;
$(LibrariesNativeArtifactsPath)dotnet.diag.js;
$(LibrariesNativeArtifactsPath)dotnet.diag.js.map;
$(LibrariesNativeArtifactsPath)dotnet.d.ts;
$(LibrariesNativeArtifactsPath)package.json;
$(LibrariesNativeArtifactsPath)dotnet.native.wasm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@
<PlatformManifestFileEntry Include="dotnet.js.map" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.runtime.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.runtime.js.map" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.diag.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.diag.js.map" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.native.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.native.worker.mjs" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.native.js.symbols" IsNative="true" />
Expand Down
4 changes: 4 additions & 0 deletions src/mono/browser/browser.proj
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@
{ "identity": "WasmSingleFileBundle", "defaultValueInRuntimePack": "$(WasmSingleFileBundle)" },
{ "identity": "WasmEnableSIMD", "defaultValueInRuntimePack": "$(WasmEnableSIMD)" },
{ "identity": "WasmEnableExceptionHandling", "defaultValueInRuntimePack": "$(WasmEnableExceptionHandling)" },
{ "identity": "FeaturePerfTracing", "defaultValueInRuntimePack": "$(FeaturePerfTracing)" },
{ "identity": "WasmProfilers", "defaultValueInRuntimePack": "$(WasmProfilers)" },
{ "identity": "EmccMaximumHeapSize", "defaultValueInRuntimePack": "$(EmccMaximumHeapSize)" }
]
}
Expand Down Expand Up @@ -497,6 +499,8 @@
$(NativeBinDir)dotnet.js.map;
$(NativeBinDir)dotnet.runtime.js;
$(NativeBinDir)dotnet.runtime.js.map;
$(NativeBinDir)dotnet.diag.js;
$(NativeBinDir)dotnet.diag.js.map;
$(NativeBinDir)dotnet.native.js;
$(NativeBinDir)dotnet.d.ts;
$(NativeBinDir)package.json;
Expand Down
6 changes: 5 additions & 1 deletion src/mono/browser/build/BrowserWasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<!-- Post Wasm MVP features -->
<WasmEnableExceptionHandling Condition="'$(WasmEnableExceptionHandling)' == ''">true</WasmEnableExceptionHandling>
<WasmEnableSIMD Condition="'$(WasmEnableSIMD)' == ''">$(WasmEnableExceptionHandling)</WasmEnableSIMD>
<FeaturePerfTracing Condition="'$(FeaturePerfTracing)' == '' and '$(Configuration)' == 'Debug'">true</FeaturePerfTracing>
<!-- we are not triggering FeaturePerfTracing in Debug automatically, because it would trigger re-link, making dev-loop slow -->
<FeaturePerfTracing Condition="'$(WasmProfilers)' != ''">true</FeaturePerfTracing>
<FeaturePerfTracing Condition="'$(FeaturePerfTracing)' == ''">false</FeaturePerfTracing>
</PropertyGroup>

Expand Down Expand Up @@ -90,6 +91,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)dotnet.diag.js" Condition="'$(FeaturePerfTracing)' == 'true'" />
<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 All @@ -101,6 +103,8 @@
Condition="'$(WasmEmitSourceMap)' != 'false'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.runtime.js.map"
Condition="'$(WasmEmitSourceMap)' != 'false'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.diag.js.map"
Condition="'$(WasmEmitSourceMap)' != 'false' and '$(FeaturePerfTracing)' == 'true'" />
</ItemGroup>

<ItemGroup Condition="'$(InvariantGlobalization)' != 'true'">
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/build/WasmApp.InTree.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<RunAOTCompilation Condition="'$(RunAOTCompilation)' == ''">false</RunAOTCompilation>
<PublishTrimmed>true</PublishTrimmed>
<RunAnalyzers>false</RunAnalyzers>
<FeaturePerfTracing>true</FeaturePerfTracing>

<DisableImplicitFrameworkReferences>false</DisableImplicitFrameworkReferences>
<UseLocalTargetingRuntimePack Condition="'$(UseLocalTargetingRuntimePack)' == ''">true</UseLocalTargetingRuntimePack>
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function instantiate_asset (asset: AssetEntry, url: string, bytes: Uint8A
switch (asset.behavior) {
case "dotnetwasm":
case "js-module-threads":
case "js-module-diag":
case "symbols":
// do nothing
break;
Expand Down
26 changes: 26 additions & 0 deletions src/mono/browser/runtime/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import type { CharPtr, VoidPtr } from "./types/emscripten";

import { diagHelpers } from "./globals";

export function ds_rt_websocket_create (urlPtr :CharPtr):number {
return diagHelpers.ds_rt_websocket_create(urlPtr);
}

export function ds_rt_websocket_send (client_socket :number, buffer:VoidPtr, bytes_to_write:number):number {
return diagHelpers.ds_rt_websocket_send(client_socket, buffer, bytes_to_write);
}

export function ds_rt_websocket_poll (client_socket :number):number {
return diagHelpers.ds_rt_websocket_poll(client_socket);
}

export function ds_rt_websocket_recv (client_socket :number, buffer:VoidPtr, bytes_to_read:number):number {
return diagHelpers.ds_rt_websocket_recv(client_socket, buffer, bytes_to_read);
}

export function ds_rt_websocket_close (client_socket :number):number {
return diagHelpers.ds_rt_websocket_close(client_socket);
}
23 changes: 23 additions & 0 deletions src/mono/browser/runtime/diagnostics/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import type { DiagHelpers, GlobalObjects, LoaderHelpers, RuntimeHelpers, DotnetModuleInternal } from "../types/internal";

export let _diagModuleLoaded = false; // please keep it in place also as rollup guard

export let diagHelpers: DiagHelpers = null as any;
export let runtimeHelpers: RuntimeHelpers = null as any;
export let loaderHelpers: LoaderHelpers = null as any;
export let Module: DotnetModuleInternal = null as any;

export function setRuntimeGlobalsImpl (globalObjects: GlobalObjects): void {
if (_diagModuleLoaded) {
throw new Error("Diag module already loaded");
}
_diagModuleLoaded = true;
diagHelpers = globalObjects.diagHelpers;
runtimeHelpers = globalObjects.runtimeHelpers;
loaderHelpers = globalObjects.loaderHelpers;
Module = globalObjects.module;
}
32 changes: 32 additions & 0 deletions src/mono/browser/runtime/diagnostics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import type { GlobalObjects } from "../types/internal";
import type { CharPtr, VoidPtr } from "../types/emscripten";

import { diagHelpers, setRuntimeGlobalsImpl } from "./globals";

/* eslint-disable @typescript-eslint/no-unused-vars */
export function setRuntimeGlobals (globalObjects: GlobalObjects): void {
setRuntimeGlobalsImpl(globalObjects);

diagHelpers.ds_rt_websocket_create = (urlPtr :CharPtr):number => {
throw new Error("Not implemented");
};

diagHelpers.ds_rt_websocket_send = (client_socket :number, buffer:VoidPtr, bytes_to_write:number):number => {
throw new Error("Not implemented");
};

diagHelpers.ds_rt_websocket_poll = (client_socket :number):number => {
throw new Error("Not implemented");
};

diagHelpers.ds_rt_websocket_recv = (client_socket :number, buffer:VoidPtr, bytes_to_read:number):number => {
throw new Error("Not implemented");
};

diagHelpers. ds_rt_websocket_close = (client_socket :number):number => {
throw new Error("Not implemented");
};
}
39 changes: 39 additions & 0 deletions src/mono/browser/runtime/diagnostics/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { loaderHelpers } from "./globals";

/* eslint-disable no-console */

const prefix = "MONO_WASM: ";

export function mono_log_debug (messageFactory: string | (() => string)) {
if (loaderHelpers.diagnosticTracing) {
const message = (typeof messageFactory === "function"
? messageFactory()
: messageFactory);
console.debug(prefix + message);
}
}

export function mono_log_info (msg: string, ...data: any) {
console.info(prefix + msg, ...data);
}

export function mono_log_warn (msg: string, ...data: any) {
console.warn(prefix + msg, ...data);
}

export function mono_log_error (msg: string, ...data: any) {
if (data && data.length > 0 && data[0] && typeof data[0] === "object") {
// don't log silent errors
if (data[0].silent) {
return;
}
if (data[0].toString) {
console.error(prefix + msg, data[0].toString());
return;
}
}
console.error(prefix + msg, ...data);
}
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ interface ResourceGroups {
corePdb?: ResourceList;
pdb?: ResourceList;
jsModuleWorker?: ResourceList;
jsModuleGlobalization?: ResourceList;
jsModuleDiag?: ResourceList;
jsModuleNative: ResourceList;
jsModuleRuntime: ResourceList;
wasmSymbols?: ResourceList;
Expand Down Expand Up @@ -360,6 +360,10 @@ type SingleAssetBehaviors =
* The javascript module for threads.
*/
| "js-module-threads"
/**
* The javascript module for diagnostic server and client.
*/
| "js-module-diag"
/**
* The javascript module for runtime.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function createWasmImportStubsFrom(collection) {
// we will replace them with the real implementation in replace_linker_placeholders
function injectDependencies() {
createWasmImportStubsFrom(methodIndexByName.mono_wasm_imports);
createWasmImportStubsFrom(methodIndexByName.mono_wasm_js_globalization_imports);
if (FEATURE_PERFTRACING) createWasmImportStubsFrom(methodIndexByName.mono_wasm_diag_imports);

#if USE_PTHREADS
createWasmImportStubsFrom(methodIndexByName.mono_wasm_threads_imports);
Expand Down
35 changes: 20 additions & 15 deletions src/mono/browser/runtime/exports-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand All @@ -24,7 +23,10 @@ import {
} from "./pthreads";
import { mono_wasm_dump_threads } from "./pthreads/ui-thread";
import { mono_wasm_schedule_synchronization_context } from "./pthreads/shared";
import { mono_wasm_js_globalization_imports } from "./globalization";
import { mono_wasm_get_locale_info } from "./globalization-locale";

import { mono_wasm_profiler_record, mono_wasm_profiler_now } from "./profiler";
import { ds_rt_websocket_create, ds_rt_websocket_send, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_close } from "./diagnostics";

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

Expand All @@ -48,6 +50,18 @@ export const mono_wasm_threads_imports = !WasmEnableThreads ? [] : [
mono_wasm_warn_about_blocking_wait,
];

export const mono_wasm_diag_imports = [
mono_wasm_profiler_now,
mono_wasm_profiler_record,

//event pipe
ds_rt_websocket_create,
ds_rt_websocket_send,
ds_rt_websocket_poll,
ds_rt_websocket_recv,
ds_rt_websocket_close,
];

export const mono_wasm_imports = [
// mini-wasm.c
mono_wasm_schedule_timer,
Expand All @@ -70,9 +84,6 @@ export const mono_wasm_imports = [
mono_interp_flush_jitcall_queue,
mono_wasm_free_method_data,

mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
mono_wasm_set_entrypoint_breakpoint,
Expand All @@ -88,22 +99,16 @@ export const mono_wasm_imports = [
mono_wasm_invoke_jsimport_ST,
mono_wasm_resolve_or_reject_promise,
mono_wasm_cancel_promise,

//event pipe
ds_rt_websocket_create,
ds_rt_websocket_send,
ds_rt_websocket_poll,
ds_rt_websocket_recv,
ds_rt_websocket_close,
mono_wasm_get_locale_info,
];


// !!! Keep in sync with exports-linker.ts
const wasmImports: Function[] = [
...mono_wasm_imports,
// threading exports, if threading is enabled
...mono_wasm_diag_imports,
// threading exports, if threading is enabled
...mono_wasm_threads_imports,
// globalization exports
...mono_wasm_js_globalization_imports,
];

export function replace_linker_placeholders (imports: WebAssembly.Imports) {
Expand Down
9 changes: 4 additions & 5 deletions src/mono/browser/runtime/exports-linker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { mono_wasm_imports, mono_wasm_threads_imports } from "./exports-binding";
import { mono_wasm_diag_imports, mono_wasm_imports, mono_wasm_threads_imports } from "./exports-binding";
import gitHash from "consts:gitHash";
import { mono_wasm_js_globalization_imports } from "./globalization";

export function export_linker_indexes_as_code (): string {
const indexByName: any = {
mono_wasm_imports: {},
mono_wasm_threads_imports: {},
mono_wasm_js_globalization_imports: {},
mono_wasm_diag_imports: {},
};
let idx = 0;
for (const wi of mono_wasm_imports) {
Expand All @@ -20,8 +19,8 @@ export function export_linker_indexes_as_code (): string {
indexByName.mono_wasm_threads_imports[wi.name] = idx;
idx++;
}
for (const wi of mono_wasm_js_globalization_imports) {
indexByName.mono_wasm_js_globalization_imports[wi.name] = idx;
for (const wi of mono_wasm_diag_imports) {
indexByName.mono_wasm_diag_imports[wi.name] = idx;
idx++;
}
return `
Expand Down
5 changes: 4 additions & 1 deletion src/mono/browser/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { forceDisposeProxies } from "./gc-handles";
import { mono_wasm_dump_threads } from "./pthreads";

import { threads_c_functions as tcwraps } from "./cwraps";
import { utf8ToString } from "./strings";

export let runtimeList: RuntimeList;

Expand All @@ -40,7 +41,9 @@ function initializeExports (globalObjects: GlobalObjects): RuntimeAPI {
instantiate_asset,
jiterpreter_dump_stats,
forceDisposeProxies,

utf8ToString,
mono_background_exec: () => tcwraps.mono_background_exec(),
mono_wasm_ds_exec: () => tcwraps.mono_wasm_ds_exec(),
};
if (WasmEnableThreads) {
rh.dumpThreads = mono_wasm_dump_threads;
Expand Down
10 changes: 0 additions & 10 deletions src/mono/browser/runtime/globalization.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/mono/browser/runtime/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import gitHash from "consts:gitHash";

import { RuntimeAPI } from "./types/index";
import type { GlobalObjects, EmscriptenInternals, RuntimeHelpers, LoaderHelpers, DotnetModuleInternal, PromiseAndController, EmscriptenBuildOptions, GCHandle } from "./types/internal";
import type { GlobalObjects, EmscriptenInternals, RuntimeHelpers, LoaderHelpers, DotnetModuleInternal, PromiseAndController, EmscriptenBuildOptions, GCHandle, DiagHelpers } from "./types/internal";
import { mono_log_error } from "./logging";

// these are our public API (except internal)
Expand All @@ -29,6 +29,8 @@ export let ENVIRONMENT_IS_PTHREAD: boolean;
export let exportedRuntimeAPI: RuntimeAPI = null as any;
export let runtimeHelpers: RuntimeHelpers = null as any;
export let loaderHelpers: LoaderHelpers = null as any;
export let diagHelpers: DiagHelpers = null as any;
export let globalObjectsRoot: GlobalObjects = null as any;

export let _runtimeModuleLoaded = false; // please keep it in place also as rollup guard

Expand All @@ -49,10 +51,12 @@ export function setRuntimeGlobals (globalObjects: GlobalObjects) {
throw new Error("Runtime module already loaded");
}
_runtimeModuleLoaded = true;
globalObjectsRoot = globalObjects;
Module = globalObjects.module;
INTERNAL = globalObjects.internal;
runtimeHelpers = globalObjects.runtimeHelpers;
loaderHelpers = globalObjects.loaderHelpers;
diagHelpers = globalObjects.diagHelpers;
exportedRuntimeAPI = globalObjects.api;

const rh: Partial<RuntimeHelpers> = {
Expand Down
Loading
Loading