alexcrichton · GitHub

This commit adds basic support for profiling the Pulley interpreter.
This is partially achievable previously through the use of native
profilers, but the downside of that approach is that you can find hot
instructions but it's not clear in what context the hot instructions are
being executed nor what functions are hot. The goal of this profiler is
to show pulley bytecode and time spent in bytecode itself to better
understand the shape of code around a hot instruction to identify new
macro opcodes for example.
The general structure of this new profiler is:
* There is a compile-time feature for Pulley which is off-by-default
  where, when enabled, Pulley will record its current program counter
  into an `AtomicUsize` before each instruction.
* When the CLI has `--profile pulley` Wasmtime will spawn a sampling
  thread in the same process which will periodically read from this
  `AtomicUsize` to record where the program is currently executing.
* The Pulley profiler additionally records all bytecode through the use
  of the `ProfilingAgent` trait to ensure that the recording has access
  to all bytecode as well.
* Samples are taken throughout the process and emitted to a
  `pulley-$pid.data` file. This file is then interpreted and printed by
  an "example" program `profiler-html.rs` in the `pulley/examples`
  directory.
The end result is that hot functions of Pulley bytecode can be seen and
instructions are annotated with how frequently they were executed. This
enables finding hot loops and understanding more about the whole loop,
bytecodes that were selected, and such.

@alexcrichton

@alexcrichton

alexcrichton

@alexcrichton

@alexcrichton

@alexcrichton

@alexcrichton

github-merge-queue Bot pushed a commit that referenced this pull request

Jan 16, 2025
* Add basic support for profiling Pulley
This commit adds basic support for profiling the Pulley interpreter.
This is partially achievable previously through the use of native
profilers, but the downside of that approach is that you can find hot
instructions but it's not clear in what context the hot instructions are
being executed nor what functions are hot. The goal of this profiler is
to show pulley bytecode and time spent in bytecode itself to better
understand the shape of code around a hot instruction to identify new
macro opcodes for example.
The general structure of this new profiler is:
* There is a compile-time feature for Pulley which is off-by-default
  where, when enabled, Pulley will record its current program counter
  into an `AtomicUsize` before each instruction.
* When the CLI has `--profile pulley` Wasmtime will spawn a sampling
  thread in the same process which will periodically read from this
  `AtomicUsize` to record where the program is currently executing.
* The Pulley profiler additionally records all bytecode through the use
  of the `ProfilingAgent` trait to ensure that the recording has access
  to all bytecode as well.
* Samples are taken throughout the process and emitted to a
  `pulley-$pid.data` file. This file is then interpreted and printed by
  an "example" program `profiler-html.rs` in the `pulley/examples`
  directory.
The end result is that hot functions of Pulley bytecode can be seen and
instructions are annotated with how frequently they were executed. This
enables finding hot loops and understanding more about the whole loop,
bytecodes that were selected, and such.
* Add missing source file
* Check the profile-pulley feature in CI
* Miscellaneous fixes for CI
* Fix type-checking of `become` on nightly Rust
* Fix more misc CI issues
* Fix dispatch in tail loop
* Update test expectations
* Review comments

@github-merge-queue

@alexcrichton

@alexcrichton

@alexcrichton

@alexcrichton

Read the original on github.com ↗