Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser] samplepoint instrumentation into Mono profiler #112352

Merged
merged 30 commits into from
Feb 25, 2025
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 number Diff line number Diff 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 Down Expand 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 Down Expand 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 number Diff line number Diff 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
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 number Diff line number Diff 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"}, ` +
`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 number Diff line number Diff 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 Down Expand 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 number Diff line number Diff 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 number Diff line number Diff 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 Down Expand 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 number Diff line number Diff 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 Down Expand 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 Down Expand 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 Down Expand 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 number Diff line number Diff 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 Down Expand 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
? { 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);
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 number Diff line number Diff 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 number Diff line number Diff 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 Down Expand 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
Loading