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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

pavelsavara
Copy link
Member

@pavelsavara pavelsavara commented Feb 10, 2025

Motivation

Collect sample stack traces in the single-threaded browser.
Single threaded environment can't have dedicated thread for "stop-the-world + collect stack traces" implementation.
So we need to collect samples by instrumenting the code instead.

Previously we implemented browser based profiler which collects "sample" on every method entry/leave. In realistic application, that generates too much traffic for the browser profiler.

This enhancement could also be used to implement sampling profiler with ST diagnostic server.

Goal

  • add new samplepoint instrumentation into Mono profiler, use the existing enter/leave too.
  • inject the event/call into same locations as GC safepoint.
    • we can skip emitting it at the method start, because there is already method_enter event/instrumentation.
    • This is also same location where cooperative MT would stop-the-world to take a sample.
  • take sample on each Nth visit to the samplepoint (because measuring current time is more expensive)
  • keep updating the size of N to match expected sample time interval

Implementation

  • new interp IR instruction MINT_PROF_SAMPLEPOINT
  • new profiler event method_samplepoint
  • new Mono JIT method mini_profiler_emit_samplepoint
  • new config option sampleIntervalMs for browser profiler. 1ms by default. All samples when set to 0.
  • example of code emitted by AOT with call $mono_profiler_raise_method_samplepoint
  • new jiterp helper append_profiler_event calling new mono_jiterp_prof_enter, mono_jiterp_prof_samplepoint, mono_jiterp_prof_leave
  • new interp macro INTERP_PROFILER_RAISE
  • changes JavaScript called only to get time mono_wasm_profiler_now and mono_wasm_profiler_record
  • all sampling logic in browser.c new should_record_frame
  • stack of start times profiler_stack and skip counter sample_skip_counter
  • default sample interval 10ms
  • callspec parametr to profiler allows to filter the instrumentation, for example by namespace callspec=N:Sample.FooNs

Todo

  • how to

Contributes to #76316

@pavelsavara pavelsavara added arch-wasm WebAssembly architecture area-Diagnostics-mono os-browser Browser variant of arch-wasm labels Feb 10, 2025
@pavelsavara pavelsavara added this to the 10.0.0 milestone Feb 10, 2025
@pavelsavara pavelsavara self-assigned this Feb 10, 2025
@pavelsavara pavelsavara changed the title [browser] add sample point instrumentation into Mono profiler [browser] Sample point instrumentation into Mono profiler Feb 10, 2025
@pavelsavara
Copy link
Member Author

Interpreter: 1ms, skip rate between 200-500 sample points.
image

@pavelsavara

This comment was marked as resolved.

@pavelsavara

This comment was marked as resolved.

@hakenr
Copy link
Member

hakenr commented Feb 11, 2025

Hi @pavelsavara,
I'm in! I'll try to reach you via Teams.

@pavelsavara pavelsavara changed the title [browser] Sample point instrumentation into Mono profiler [browser] samplepoint instrumentation into Mono profiler Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly architecture area-Diagnostics-mono os-browser Browser variant of arch-wasm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants