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

Add basic support for profiling Pulley #10034

Merged
merged 10 commits into from
Jan 16, 2025

Conversation

alexcrichton
Copy link
Member

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.

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 requested review from a team as code owners January 16, 2025 19:48
@alexcrichton alexcrichton requested review from dicej and fitzgen and removed request for a team and dicej January 16, 2025 19:48
Comment on lines -34 to +39
type Handler = fn(Interpreter<'_>) -> Done;
/// ABI signature of each opcode handler.
///
/// Note that this "explodes" the internals of `Interpreter` to individual
/// arguments to help get them all into registers.
type Handler = fn(&mut MachineState, UnsafeBytecodeStream, ExecutingPcRef<'_>) -> Done;
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll note that this change was done to ensure/guarantee that these three components of Interpreter are passed in registers. I was worried about crossing a threshold where if Interpeter got too big it would be passed by-ref instead of "exploded" into components like we want.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll also note that ExecutingPcRef is a zero-sized-type when profile is disabled, otherwise it's a pointer-large.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense, thanks for the explanation.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Super excited for this!

Comment on lines -34 to +39
type Handler = fn(Interpreter<'_>) -> Done;
/// ABI signature of each opcode handler.
///
/// Note that this "explodes" the internals of `Interpreter` to individual
/// arguments to help get them all into registers.
type Handler = fn(&mut MachineState, UnsafeBytecodeStream, ExecutingPcRef<'_>) -> Done;
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense, thanks for the explanation.

@alexcrichton alexcrichton added this pull request to the merge queue Jan 16, 2025
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 github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 16, 2025
@alexcrichton alexcrichton added this pull request to the merge queue Jan 16, 2025
Merged via the queue into bytecodealliance:main with commit 1d1c06f Jan 16, 2025
39 checks passed
@alexcrichton alexcrichton deleted the pulley-profile branch January 16, 2025 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants