Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -210,6 +210,7 @@
<PlatformManifestFileEntry Include="libmono-icall-table.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-ilgen.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono.proj
Original file line numberDiff line numberDiff line change
Expand Up@@ -905,6 +905,9 @@
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-aot.a">
<Destination>$(RuntimeBinDir)libmono-profiler-aot.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-profiler-browser.a">
<Destination>$(RuntimeBinDir)libmono-profiler-browser.a</Destination>
</_MonoRuntimeArtifacts>
<_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-js.a">
<Destination>$(RuntimeBinDir)libmono-wasm-eh-js.a</Destination>
</_MonoRuntimeArtifacts>
Expand Down
16 changes: 16 additions & 0 deletions src/mono/mono/metadata/profiler.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,10 @@ typedef void (*MonoProfilerInitializer) (const char *);
#define OLD_INITIALIZER_NAME "mono_profiler_startup"
#define NEW_INITIALIZER_NAME "mono_profiler_init"

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
MONO_API void mono_profiler_init_browser (const char *desc);
#endif

static gboolean
load_profiler (MonoDl *module, const char *name, const char *desc)
{
Expand DownExpand Up@@ -148,6 +152,9 @@ load_profiler_from_installation (const char *libname, const char *name, const ch
*
* This function may \b only be called by embedders prior to running managed
* code.
*
* This could could be triggered by \c MONO_PROFILE env variable in normal mono process or
* by \c --profile=foo argument to mono-aot-cross.exe command line.
*/
void
mono_profiler_load (const char *desc)
Expand All@@ -171,6 +178,15 @@ mono_profiler_load (const char *desc)
mname = g_strdup (desc);
}

#if defined(TARGET_WASM) && defined(MONO_CROSS_COMPILE)
// this code could be running as part of mono-aot-cross.exe
// in case of WASM we staticaly link in the browser.c profiler plugin
if(strcmp (mname, "browser") == 0) {
mono_profiler_init_browser (desc);
goto done;
}
#endif

if (load_profiler_from_executable (mname, desc))
goto done;

Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono/mini/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -325,7 +325,13 @@ else()
set(llvm_runtime_sources)
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${llvm_runtime_sources}")
if(TARGET_WASM AND MONO_CROSS_COMPILE)
set(profiler_sources ../profiler/browser.c)
else()
set(profiler_sources "")
endif()

set(mini_sources "main-core.c;${mini_common_sources};${arch_sources};${os_sources};${mini_interp_sources};${llvm_sources};${debugger_sources};${profiler_sources};${llvm_runtime_sources}")

if(LLVM_INCLUDEDIR)
include_directories(BEFORE SYSTEM "${LLVM_INCLUDEDIR}")
Expand Down
7 changes: 7 additions & 0 deletions src/mono/mono/profiler/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,4 +35,11 @@ if(NOT DISABLE_LIBS)
set_target_properties(mono-profiler-aot-static PROPERTIES OUTPUT_NAME mono-profiler-aot)
install(TARGETS mono-profiler-aot-static LIBRARY)
endif()

if(HOST_WASM)
add_library(mono-profiler-browser-static STATIC browser.c)
target_link_libraries(mono-profiler-browser-static monoapi)
set_target_properties(mono-profiler-browser-static PROPERTIES OUTPUT_NAME mono-profiler-browser)
install(TARGETS mono-profiler-browser-static LIBRARY)
endif()
endif()
107 changes: 107 additions & 0 deletions src/mono/mono/profiler/browser.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
/*
* browser.c: Exporting profiler events to browser dev tools.

* Copyright 2022 Microsoft Corporation
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/

#include <config.h>

#ifdef TARGET_WASM

#include <mono/metadata/profiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <glib.h>

#include <mono/metadata/object-internals.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/utils/mono-publib.h>
#include <mono/jit/jit.h>

struct _MonoProfiler {
gboolean verbose;
};

static MonoProfiler browser_profiler;

#ifdef HOST_WASM

void
mono_wasm_profiler_enter ();

void
mono_wasm_profiler_leave (MonoMethod *method);

static void
method_enter (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_enter ();
}

static void
method_leave (MonoProfiler *prof, MonoMethod *method, MonoProfilerCallContext *ctx)
{
mono_wasm_profiler_leave (method);
}

static void
tail_call (MonoProfiler *prof, MonoMethod *method, MonoMethod *target)
{
method_leave (prof, method, NULL);
}

static void
method_exc_leave (MonoProfiler *prof, MonoMethod *method, MonoObject *exc)
{
method_leave (prof, method, NULL);
}

#endif /* HOST_WASM */

static MonoProfilerCallInstrumentationFlags
method_filter (MonoProfiler *prof, MonoMethod *method)
{
// TODO filter by namespace ?
return MONO_PROFILER_CALL_INSTRUMENTATION_ENTER |
MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE |
MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL |
MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE;
}


MONO_API void
mono_profiler_init_browser (const char *desc);

/**
* mono_profiler_init_browser:
* the entry point
*/
void
mono_profiler_init_browser (const char *desc)
{
MonoProfilerHandle handle = mono_profiler_create (&browser_profiler);

mono_profiler_set_call_instrumentation_filter_callback (handle, method_filter);

if (mono_jit_aot_compiling ()) {
return;
}

#ifdef HOST_WASM
// install this only in production run, not in AOT run
mono_profiler_set_method_enter_callback (handle, method_enter);
mono_profiler_set_method_leave_callback (handle, method_leave);
mono_profiler_set_method_tail_call_callback (handle, tail_call);
mono_profiler_set_method_exception_leave_callback (handle, method_exc_leave);
#endif /* HOST_WASM */
}

#endif /* TARGET_WASM */
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,8 @@
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
<_ServeHeaders>$(_ServeHeaders) -h &quot;Content-Security-Policy: default-src 'self' 'wasm-unsafe-eval'&quot;</_ServeHeaders>
<!-- enable reporting to profiler in browser dev tools -->
<WasmProfilers>browser;</WasmProfilers>
</PropertyGroup>
<ItemGroup>
<!-- add export GL object from Module -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/sample/wasm/browser-advanced/main.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,8 @@ try {
// This is called during emscripten `dotnet.wasm` instantiation, after we fetched config.
console.log('user code Module.onConfigLoaded');
// config is loaded and could be tweaked before the rest of the runtime startup sequence
config.environmentVariables["MONO_LOG_LEVEL"] = "debug"
config.environmentVariables["MONO_LOG_LEVEL"] = "debug";
config.browserProfilerOptions = {};
},
preInit: () => { console.log('user code Module.preInit'); },
preRun: () => { console.log('user code Module.preRun'); },
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-profile/Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ TOP=../../../../..
include ../wasm.mk

PROJECT_NAME=Wasm.BrowserProfile.Sample.csproj
BUILD_ARGS=/p:WasmBuildNative=true /p:EnableProfiler=true
BUILD_ARGS=/p:WasmBuildNative=true
BUILD_PROFILED_ARGS=/p:RunAOTCompilation=true /p:AOTProfilePath=$(PROFILE_PATH)

run: run-browser
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,10 @@
<WasmMainJSPath>main.js</WasmMainJSPath>
<WasmProfilers>aot;</WasmProfilers>
<WasmBuildNative>true</WasmBuildNative>
<WasmNativeStrip>false</WasmNativeStrip>
</PropertyGroup>

<ItemGroup>
<WasmExtraConfig Include="enableProfiler" Value="true" />
<WasmExtraFilesToDeploy Include="index.html" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,6 +215,7 @@
<_EmccCFlags Include="-DINVARIANT_GLOBALIZATION=1" Condition="'$(InvariantGlobalization)' == 'true'" />
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
<_EmccCFlags Include="-DCORE_BINDINGS" />
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
<_EmccCFlags Include="-emit-llvm" />
Expand DownExpand Up@@ -598,6 +599,7 @@
AOTProfilePath="$(AOTProfilePath)"
AotModulesTablePath="$(_DriverGenCPath)"
UseLLVM="true"
Profilers="$(WasmProfilers)"
DisableParallelAot="$(DisableParallelAot)"
DedupAssembly="$(_WasmDedupAssembly)"
CacheFilePath="$(_AOTCompilerCacheFile)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line numberDiff line numberDiff line change
Expand Up@@ -75,7 +75,7 @@
- @(WasmNativeAsset) - Native files to be added to `NativeAssets` in the bundle.

- @(WasmExtraConfig) - json elements to add to `mono-config.json`
Eg. <WasmExtraConfig Include="enableProfiler" Value="true" />
Eg. <WasmExtraConfig Include="xxx" Value="true" />

- Value attribute can have a number, bool, quoted string, or json string

Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/assets.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import { mono_wasm_load_icu_data } from "./icu";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_load_bytes_into_heap } from "./memory";
import { MONO } from "./net6-legacy/imports";
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
import { createPromiseController, PromiseAndController } from "./promise-controller";
import { delay } from "./promise-utils";
import { abort_startup, beforeOnRuntimeInitialized } from "./startup";
Expand DownExpand Up@@ -346,6 +347,7 @@ function download_resource(request: ResourceRequest): LoadingResource {
function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: Loaded:${asset.name} as ${asset.behavior} size ${bytes.length} from ${url}`);
const mark = startMeasure();

const virtualName: string = typeof (asset.virtualPath) === "string"
? asset.virtualPath
Expand DownExpand Up@@ -422,6 +424,7 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
else if (asset.behavior === "resource") {
cwraps.mono_wasm_add_satellite_assembly(virtualName, asset.culture!, offset!, bytes.length);
}
endMeasure(mark, MeasuredBlock.instantiateAsset, asset.name);
++actual_instantiated_assets_count;
}

Expand Down
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,8 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_getenv", "number", ["string"]],
[true, "mono_wasm_set_main_args", "void", ["number", "number"]],
[false, "mono_wasm_enable_on_demand_gc", "void", ["number"]],
[false, "mono_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_aot", "void", ["number"]],
[false, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_method_bound", "number", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -93,6 +94,7 @@ const fn_signatures: SigLine[] = [
[true, "mono_wasm_u52_to_f64", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_i52", "number", ["number", "number"]],
[true, "mono_wasm_f64_to_u52", "number", ["number", "number"]],
[true, "mono_wasm_method_get_name", "number", ["number"]],
];

export interface t_Cwraps {
Expand DownExpand Up@@ -193,7 +195,8 @@ export interface t_Cwraps {
mono_wasm_getenv(name: string): CharPtr;
mono_wasm_enable_on_demand_gc(enable: number): void;
mono_wasm_set_main_args(argc: number, argv: VoidPtr): void;
mono_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_aot(desc: string): void;
mono_wasm_profiler_init_browser(desc: string): void;
mono_wasm_exec_regression(verbose_level: number, image: string): number;
mono_wasm_invoke_method_bound(method: MonoMethod, args: JSMarshalerArguments): MonoString;
mono_wasm_write_managed_pointer_unsafe(destination: VoidPtr | MonoObjectRef, pointer: ManagedPointer): void;
Expand All@@ -203,6 +206,7 @@ export interface t_Cwraps {
mono_wasm_f64_to_i52(destination: VoidPtr, value: number): I52Error;
mono_wasm_f64_to_u52(destination: VoidPtr, value: number): I52Error;
mono_wasm_runtime_run_module_cctor(assembly: MonoAssembly): void;
mono_wasm_method_get_name(method: MonoMethod): CharPtr;
}

const wrapped_c_functions: t_Cwraps = <any>{};
Expand Down
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -66,6 +66,7 @@ int32_t monoeg_g_hasenv(const char *variable);
void mono_free (void*);
int32_t mini_parse_debug_option (const char *option);
char *mono_method_get_full_name (MonoMethod *method);
char *mono_method_full_name (MonoMethod *method, int signature);

static void mono_wasm_init_finalizer_thread (void);

Expand DownExpand Up@@ -1399,13 +1400,25 @@ mono_wasm_copy_managed_pointer (PPVOLATILE(MonoObject) destination, PPVOLATILE(M
void mono_profiler_init_aot (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_load_profiler_aot (const char *desc)
mono_wasm_profiler_init_aot (const char *desc)
{
mono_profiler_init_aot (desc);
}

#endif

#ifdef ENABLE_BROWSER_PROFILER

void mono_profiler_init_browser (const char *desc);

EMSCRIPTEN_KEEPALIVE void
mono_wasm_profiler_init_browser (const char *desc)
{
mono_profiler_init_browser (desc);
}

#endif

static void
mono_wasm_init_finalizer_thread (void)
{
Expand DownExpand Up@@ -1467,3 +1480,7 @@ EMSCRIPTEN_KEEPALIVE int mono_wasm_f64_to_i52 (int64_t *destination, double valu
*destination = (int64_t)value;
return I52_ERROR_NONE;
}

EMSCRIPTEN_KEEPALIVE const char* mono_wasm_method_get_name (MonoMethod *method) {
return mono_method_full_name(method, 0);
}
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,10 @@ const linked_functions = [
// mono-threads-wasm.c
"schedule_background_exec",

// interp.c
"mono_wasm_profiler_enter",
"mono_wasm_profiler_leave",

// driver.c
"mono_wasm_invoke_js_blazor",
"mono_wasm_trace_logger",
Expand Down
6 changes: 4 additions & 2 deletions src/mono/wasm/runtime/exports-internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,8 @@ export function export_internal(): any {
// tests
mono_wasm_exit: (exit_code: number) => { Module.printErr("MONO_WASM: early exit " + exit_code); },
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
mono_method_resolve,//MarshalTests.cs
mono_intern_string,// MarshalTests.cs
Expand DownExpand Up@@ -81,7 +82,8 @@ export function cwraps_internal(internal: any): void {
Object.assign(internal, {
mono_wasm_exit: cwraps.mono_wasm_exit,
mono_wasm_enable_on_demand_gc: cwraps.mono_wasm_enable_on_demand_gc,
mono_profiler_init_aot: cwraps.mono_profiler_init_aot,
mono_wasm_profiler_init_aot: cwraps.mono_wasm_profiler_init_aot,
mono_wasm_profiler_init_browser: cwraps.mono_wasm_profiler_init_browser,
mono_wasm_exec_regression: cwraps.mono_wasm_exec_regression,
});
}
Loading