Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
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
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
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
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
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
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
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
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
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
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
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
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
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
Show all changes
30 commits
Select commit Hold shift + click to select a range
3417db1
wip
pavelsavara Feb 10, 2025
449be73
fix calculation
pavelsavara Feb 10, 2025
e7ae514
sliding
pavelsavara Feb 10, 2025
b5df7cd
Timing-Allow-Origin header
pavelsavara Feb 10, 2025
8134a3c
more
pavelsavara Feb 10, 2025
f86d001
fix
pavelsavara Feb 12, 2025
ea00205
todo
pavelsavara Feb 12, 2025
ea8bf7f
less
pavelsavara Feb 12, 2025
07b79f7
fix
pavelsavara Feb 12, 2025
35beefc
more aot
pavelsavara Feb 12, 2025
ba8528b
AOT
pavelsavara Feb 13, 2025
388647b
jiterp, sampling in C, INTERP_PROFILER_RAISE macro
pavelsavara Feb 14, 2025
271c7f5
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 14, 2025
fe2ac7e
cleanup
pavelsavara Feb 14, 2025
147ff0e
cleanup
pavelsavara Feb 17, 2025
4be7e98
Update src/mono/browser/runtime/types/internal.ts
pavelsavara Feb 17, 2025
a3d753b
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
e65b4d8
feedback:
pavelsavara Feb 18, 2025
8cef96d
- feedback
pavelsavara Feb 18, 2025
eae9602
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 18, 2025
82584ce
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 20, 2025
a9d2a84
put samplepoint at the start of BB
pavelsavara Feb 20, 2025
27e5489
more
pavelsavara Feb 20, 2025
d517eb7
more
pavelsavara Feb 20, 2025
cf07bc2
- add MONO_PROFILER_CALL_INSTRUMENTATION_SAMPLEPOINT variant without …
pavelsavara Feb 24, 2025
c2fc83e
fix for empty sample point block
pavelsavara Feb 24, 2025
e00fc07
- fix raising profiler event for method_exception_leave in mono_handl…
pavelsavara Feb 24, 2025
1c8c0ab
improve short names like .ctor
pavelsavara Feb 24, 2025
a1ba8e8
Merge branch 'main' into mono_profile_safepoint
pavelsavara Feb 25, 2025
83b49cd
feedback
pavelsavara Feb 25, 2025
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
6 changes: 5 additions & 1 deletion src/mono/browser/runtime/cwraps.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,6 @@ const fn_signatures: SigLine[] = [
[() => !runtimeHelpers.emscriptenBuildOptions.enableAotProfiler, "mono_wasm_profiler_init_aot", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "mono_wasm_profiler_init_browser", "void", ["string"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enableLogProfiler, "mono_wasm_profiler_init_log", "void", ["string"]],
[true, "mono_wasm_profiler_init_browser", "void", ["number"]],
[false, "mono_wasm_exec_regression", "number", ["number", "string"]],
[false, "mono_wasm_invoke_jsexport", "void", ["number", "number"]],
[true, "mono_wasm_write_managed_pointer_unsafe", "void", ["number", "number"]],
Expand All@@ -73,6 +72,7 @@ const fn_signatures: SigLine[] = [
[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"]],
[true, "mono_wasm_method_get_name_ex", "number", ["number"]],
[true, "mono_wasm_method_get_full_name", "number", ["number"]],
[true, "mono_wasm_gc_lock", "void", []],
[true, "mono_wasm_gc_unlock", "void", []],
Expand DownExpand Up@@ -131,6 +131,9 @@ const fn_signatures: SigLine[] = [
[true, "mono_jiterp_end_catch", "void", []],
[true, "mono_interp_pgo_load_table", "number", ["number", "number"]],
[true, "mono_interp_pgo_save_table", "number", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_enter", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_samplepoint", "void", ["number", "number"]],
[() => !runtimeHelpers.emscriptenBuildOptions.enablePerfTracing, "mono_jiterp_prof_leave", "void", ["number", "number"]],

...threading_cwraps,
];
Expand DownExpand Up@@ -193,6 +196,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_method_get_name(method: MonoMethod): CharPtr;
mono_wasm_method_get_name_ex(method: MonoMethod): CharPtr;
mono_wasm_method_get_full_name(method: MonoMethod): CharPtr;
mono_wasm_gc_lock(): void;
mono_wasm_gc_unlock(): void;
Expand Down
16 changes: 16 additions & 0 deletions src/mono/browser/runtime/driver.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -534,6 +534,22 @@ EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name (MonoMethod *method)
return res;
}

EMSCRIPTEN_KEEPALIVE const char * mono_wasm_method_get_name_ex (MonoMethod *method) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you hand out ownership of this pointer and expect others to free it, it shouldn't be const.

const char *res;
MONO_ENTER_GC_UNSAFE;
res = mono_method_get_name (method);
// starts with .ctor or .cctor
if (mono_method_get_flags (method, NULL) & 0x0800 /* METHOD_ATTRIBUTE_SPECIAL_NAME */ && strlen (res) < 7) {
char *res_ex = (char *) malloc (128);
snprintf (res_ex, 128,"%s.%s", mono_class_get_name (mono_method_get_class (method)), res);
res = res_ex;
} else {
res = strdup (res);
}
MONO_EXIT_GC_UNSAFE;
return res;
}

EMSCRIPTEN_KEEPALIVE float mono_wasm_get_f32_unaligned (const float *src) {
return *src;
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/browser/runtime/es6/dotnet.es6.lib.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -90,6 +90,7 @@ function injectDependencies() {
`enablePerfTracing: ${FEATURE_PERFTRACING ? "true" : "false"}, ` +
`enableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` +
`enableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
`enablePerfTracing: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}, ` +
Comment thread
pavelsavara marked this conversation as resolved.
`enableLogProfiler: ${ENABLE_LOG_PROFILER ? "true" : "false"}, ` +
`runAOTCompilation: ${RUN_AOT_COMPILATION ? "true" : "false"}, ` +
`wasmEnableThreads: ${USE_PTHREADS ? "true" : "false"}, ` +
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/exports-binding.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,14 +6,14 @@ import WasmEnableThreads from "consts:wasmEnableThreads";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_bind_js_import_ST, mono_wasm_invoke_js_function, mono_wasm_invoke_jsimport_MT, mono_wasm_invoke_jsimport_ST } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter, mono_jiterp_free_method_data_js } from "./jiterpreter";
import { mono_interp_tier_prepare_jiterpreter, mono_wasm_free_method_data } from "./jiterpreter";
import { mono_interp_jit_wasm_entry_trampoline, mono_interp_record_interp_entry } from "./jiterpreter-interp-entry";
import { mono_interp_jit_wasm_jit_call_trampoline, mono_interp_invoke_wasm_jit_call_trampoline, mono_interp_flush_jitcall_queue } from "./jiterpreter-jit-call";
import { mono_wasm_resolve_or_reject_promise } from "./marshal-to-js";
import { mono_wasm_schedule_timer, schedule_background_exec } from "./scheduling";
import { mono_wasm_asm_loaded } from "./startup";
import { mono_log_warn, mono_wasm_console_clear, mono_wasm_trace_logger } from "./logging";
import { mono_wasm_profiler_leave, mono_wasm_profiler_enter, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_profiler_record, mono_wasm_profiler_now, ds_rt_websocket_close, ds_rt_websocket_create, ds_rt_websocket_poll, ds_rt_websocket_recv, ds_rt_websocket_send } from "./profiler";
import { mono_wasm_browser_entropy } from "./crypto";
import { mono_wasm_cancel_promise } from "./cancelable-promise";

Expand DownExpand Up@@ -68,10 +68,10 @@ export const mono_wasm_imports = [
mono_interp_jit_wasm_jit_call_trampoline,
mono_interp_invoke_wasm_jit_call_trampoline,
mono_interp_flush_jitcall_queue,
mono_jiterp_free_method_data_js,
mono_wasm_free_method_data,

mono_wasm_profiler_enter,
mono_wasm_profiler_leave,
mono_wasm_profiler_now,
mono_wasm_profiler_record,

// driver.c
mono_wasm_trace_logger,
Expand Down
21 changes: 21 additions & 0 deletions src/mono/browser/runtime/jiterpreter-support.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1600,6 +1600,27 @@ export const _now = (globalThis.performance && globalThis.performance.now)

let scratchBuffer: NativePointer = <any>0;

export function append_profiler_event (builder: WasmBuilder, ip: MintOpcodePtr, opcode: MintOpcode) {
let event_name:string;
switch (opcode) {
case MintOpcode.MINT_PROF_ENTER:
event_name = "prof_enter";
break;
case MintOpcode.MINT_PROF_SAMPLEPOINT:
event_name = "prof_samplepoint";
break;
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
event_name = "prof_leave";
break;
default:
throw new Error(`Unimplemented profiler event ${opcode}`);
}
builder.local("frame");
builder.i32_const(ip);
builder.callImport(event_name);
}

export function append_safepoint (builder: WasmBuilder, ip: MintOpcodePtr) {
// safepoints are never triggered in a single-threaded build
if (!WasmEnableThreads)
Expand Down
9 changes: 8 additions & 1 deletion src/mono/browser/runtime/jiterpreter-trace-generator.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,7 @@ import {
try_append_memmove_fast, getOpcodeTableValue,
getMemberOffset, isZeroPageReserved, CfgBranchType,
append_safepoint, modifyCounter, simdFallbackCounters,
append_profiler_event,
} from "./jiterpreter-support";
import {
sizeOfDataItem, sizeOfV128, sizeOfStackval,
Expand DownExpand Up@@ -1415,9 +1416,15 @@ export function generateWasmBody (
}

case MintOpcode.MINT_RETHROW:
ip = abort;
break;

// call C
case MintOpcode.MINT_PROF_ENTER:
case MintOpcode.MINT_PROF_SAMPLEPOINT:
case MintOpcode.MINT_PROF_EXIT:
case MintOpcode.MINT_PROF_EXIT_VOID:
ip = abort;
append_profiler_event(builder, ip, opcode);
break;

// Generating code for these is kind of complex due to the intersection of JS and int64,
Expand Down
37 changes: 36 additions & 1 deletion src/mono/browser/runtime/jiterpreter.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,6 +26,7 @@ import { mono_jiterp_free_method_data_interp_entry } from "./jiterpreter-interp-
import { mono_jiterp_free_method_data_jit_call } from "./jiterpreter-jit-call";
import { mono_log_error, mono_log_info, mono_log_warn } from "./logging";
import { utf8ToString } from "./strings";
import { mono_wasm_profiler_free_method } from "./profiler";

// Controls miscellaneous diagnostic output.
export const trace = 0;
Expand DownExpand Up@@ -298,6 +299,12 @@ function getTraceImports () {
if (nullCheckValidation)
traceImports.push(importDef("notnull", assert_not_null));

if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
traceImports.push(importDef("prof_enter", getRawCwrap("mono_jiterp_prof_enter")));
traceImports.push(importDef("prof_samplepoint", getRawCwrap("mono_jiterp_prof_samplepoint")));
traceImports.push(importDef("prof_leave", getRawCwrap("mono_jiterp_prof_leave")));
}

const pushMathOps = (list: string[], type: string) => {
for (let i = 0; i < list.length; i++) {
const mop = list[i];
Expand DownExpand Up@@ -579,6 +586,30 @@ function initialize_builder (builder: WasmBuilder) {
},
WasmValtype.void, true
);
builder.defineType(
"prof_enter",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_samplepoint",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"prof_leave",
{
"frame": WasmValtype.i32,
"ip": WasmValtype.i32,
},
WasmValtype.void, true
);
builder.defineType(
"hashcode",
{
Expand DownExpand Up@@ -1050,9 +1081,13 @@ export function mono_interp_tier_prepare_jiterpreter (

// NOTE: This will potentially be called once for every trace entry point
// in a given method, not just once per method
export function mono_jiterp_free_method_data_js (
export function mono_wasm_free_method_data (
method: MonoMethod, imethod: number, traceIndex: number
) {
if (runtimeHelpers.emscriptenBuildOptions.enablePerfTracing) {
mono_wasm_profiler_free_method(method);
}

// TODO: Uninstall the trace function pointer from the function pointer table,
// so that the compiled trace module can be freed by the browser eventually
// Release the trace info object, if present
Expand Down
45 changes: 26 additions & 19 deletions src/mono/browser/runtime/profiler.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@ import { ENVIRONMENT_IS_WEB, mono_assert, runtimeHelpers } from "./globals";
import { MonoMethod, AOTProfilerOptions, BrowserProfilerOptions, LogProfilerOptions } from "./types/internal";
import { profiler_c_functions as cwraps } from "./cwraps";
import { utf8ToString } from "./strings";
import { free } from "./memory";

// Initialize the AOT profiler with OPTIONS.
// Requires the AOT profiler to be linked into the app.
Expand All@@ -32,7 +33,13 @@ export function mono_wasm_init_browser_profiler (options: BrowserProfilerOptions
mono_assert(runtimeHelpers.emscriptenBuildOptions.enableBrowserProfiler, "Browser profiler is not enabled, please use <WasmProfilers>browser;</WasmProfilers> in your project file.");
if (options == null)
options = {};
const arg = "browser:";
let arg = "browser:";
if (typeof options.callSpec === "string") {
arg += `callspec=${options.callSpec},`;
}
if (typeof options.sampleIntervalMs === "number") {
arg += `interval=${options.sampleIntervalMs},`;
}
cwraps.mono_wasm_profiler_init_browser(arg);
}

Expand DownExpand Up@@ -76,6 +83,7 @@ export function startMeasure (): TimeStamp {

export function endMeasure (start: TimeStamp, block: string, id?: string) {
if (runtimeHelpers.enablePerfMeasure && start) {
// API is slightly different between web and Nodejs
const options = ENVIRONMENT_IS_WEB
? { start: start as any }
: { startTime: start as any };
Expand All@@ -84,28 +92,27 @@ export function endMeasure (start: TimeStamp, block: string, id?: string) {
}
}

const stackFrames: number[] = [];
export function mono_wasm_profiler_enter (): void {
if (runtimeHelpers.enablePerfMeasure) {
stackFrames.push(globalThis.performance.now());
}
export function mono_wasm_profiler_now (): number {
return globalThis.performance.now();
}

export function mono_wasm_profiler_free_method (method: MonoMethod): void {
methodNames.delete(method as any);
}

const methodNames: Map<number, string> = new Map();
export function mono_wasm_profiler_leave (method: MonoMethod): void {
if (runtimeHelpers.enablePerfMeasure) {
const start = stackFrames.pop();
const options = ENVIRONMENT_IS_WEB
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
}
globalThis.performance.measure(methodName, options);
export function mono_wasm_profiler_record (method: MonoMethod, start: number): void {
const options = ENVIRONMENT_IS_WEB
Comment thread
pavelsavara marked this conversation as resolved.
? { start: start }
: { startTime: start };
let methodName = methodNames.get(method as any);
if (!methodName) {
const chars = cwraps.mono_wasm_method_get_name_ex(method);
methodName = utf8ToString(chars);
methodNames.set(method as any, methodName);
Comment thread
pavelsavara marked this conversation as resolved.
free(chars as any);
}
globalThis.performance.measure(methodName, options);
}

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
6 changes: 6 additions & 0 deletions src/mono/browser/runtime/types/internal.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,12 @@ export type AOTProfilerOptions = {
}

export type BrowserProfilerOptions = {
sampleIntervalMs?: number, // default: 1000
/**
* See callspec in https://github.com/dotnet/runtime/blob/main/docs/design/mono/diagnostics-tracing.md#trace-monovm-profiler-events-during-startup
* When used together with Mono AOT, the callspec needs to match one in <WasmProfilers>browser:callspec=N:Sample;</WasmProfilers> in your project file.
*/
callSpec?: string,
}

export type LogProfilerOptions = {
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/jit-icall-reg.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -250,6 +250,7 @@ MONO_JIT_ICALL (mono_ppc_throw_exception) \
MONO_JIT_ICALL (mono_profiler_raise_exception_clause) \
MONO_JIT_ICALL (mono_profiler_raise_gc_allocation) \
MONO_JIT_ICALL (mono_profiler_raise_method_enter) \
MONO_JIT_ICALL (mono_profiler_raise_method_samplepoint) \
MONO_JIT_ICALL (mono_profiler_raise_method_leave) \
MONO_JIT_ICALL (mono_profiler_raise_method_tail_call) \
MONO_JIT_ICALL (mono_resolve_generic_virtual_call) \
Expand DownExpand Up@@ -298,6 +299,7 @@ MONO_JIT_ICALL (mono_throw_platform_not_supported) \
MONO_JIT_ICALL (mono_throw_invalid_program) \
MONO_JIT_ICALL (mono_throw_type_load) \
MONO_JIT_ICALL (mono_trace_enter_method) \
MONO_JIT_ICALL (mono_trace_samplepoint_method) \
MONO_JIT_ICALL (mono_trace_leave_method) \
MONO_JIT_ICALL (mono_trace_tail_method) \
MONO_JIT_ICALL (mono_upgrade_remote_class_wrapper) \
Expand Down
Loading