Skip to content

Commit

Permalink
[wasm] browser profiler (#77449)
Browse files Browse the repository at this point in the history
* wip

* wip

* cleanup

* feedback

* feedback
  • Loading branch information
pavelsavara authored Nov 1, 2022
1 parent 0e6fa62 commit 0e24ea7
Show file tree
Hide file tree
Showing 26 changed files with 353 additions and 39 deletions.
Original file line number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 Down Expand 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 Down Expand 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 Down Expand 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 Down Expand 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 number Diff line number Diff 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 Down Expand 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 number Diff line number Diff 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 Down Expand 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 Down Expand 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 number Diff line number Diff 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 number Diff line number Diff 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 Down Expand 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

0 comments on commit 0e24ea7

Please sign in to comment.