YJIT: Add compilation log (#11818)

* YJIT: Add `--yjit-compilation-log` flag to print out the compilation log at exit.

* YJIT: Add an option to enable the compilation log at runtime.

* YJIT: Fix a typo in the `IseqPayload` docs.

* YJIT: Add stubs for getting the YJIT compilation log in memory.

* YJIT: Add a compilation log based on a circular buffer to cap the log size.

* YJIT: Allow specifying either a file or directory name for the YJIT compilation log.

The compilation log will be populated as compilation events occur. If a directory is supplied, then a filename based on the PID will be used as the write target. If a file name is supplied instead, the log will be written to that file.

* YJIT: Add JIT compilation of C function substitutions to the compilation log.

* YJIT: Add compilation events to the circular buffer even if output is sent to a file.

Previously, the two modes were treated as being exclusive of one another. However, it could be beneficial to log all events to a file while also allowing for direct access of the last N events via `RubyVM::YJIT.compilation_log`.

* YJIT: Make timestamps the first element in the YJIT compilation log tuple.

* YJIT: Stream log to stderr if `--yjit-compilation-log` is supplied without an argument.

* YJIT: Eagerly compute compilation log messages to avoid hanging on to references that may GC.

* YJIT: Log all compiled blocks, not just the method entry points.

* YJIT: Remove all compilation events other than block compilation to slim down the log.

* YJIT: Replace circular buffer iterator with a consuming loop.

* YJIT: Support `--yjit-compilation-log=quiet` as a way to activate the in-memory log without printing it.

Co-authored-by: Randy Stauner <randy.stauner@shopify.com>

* YJIT: Promote the compilation log to being the one YJIT log.

Co-authored-by: Randy Stauner <randy.stauner@shopify.com>

* Update doc/yjit/yjit.md

* Update doc/yjit/yjit.md

---------

Co-authored-by: Randy Stauner <randy.stauner@shopify.com>
Co-authored-by: Maxime Chevalier-Boisvert <maximechevalierb@gmail.com>
This commit is contained in:
Kevin Menard 2024-10-17 17:36:43 -04:00 committed by GitHub
parent c78e2987d4
commit 158b8cb52e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
Notes: git 2024-10-17 21:37:02 +00:00
Merged-By: maximecb <maximecb@ruby-lang.org>
10 changed files with 299 additions and 7 deletions

28
yjit.rb
View file

@ -18,6 +18,11 @@ module RubyVM::YJIT
Primitive.rb_yjit_stats_enabled_p
end
# Check if `--yjit-log` is used.
def self.log_enabled?
Primitive.rb_yjit_log_enabled_p
end
# Check if rb_yjit_trace_exit_locations_enabled_p is enabled.
def self.trace_exit_locations_enabled? # :nodoc:
Primitive.rb_yjit_trace_exit_locations_enabled_p
@ -28,15 +33,22 @@ module RubyVM::YJIT
Primitive.rb_yjit_reset_stats_bang
end
# Enable \YJIT compilation. `stats` option decides whether to enable \YJIT stats or not.
# Enable \YJIT compilation. `stats` option decides whether to enable \YJIT stats or not. `compilation_log` decides
# whether to enable \YJIT compilation logging or not.
#
# `stats`:
# * `false`: Disable stats.
# * `true`: Enable stats. Print stats at exit.
# * `:quiet`: Enable stats. Do not print stats at exit.
def self.enable(stats: false)
#
# `log`:
# * `false`: Don't enable the log.
# * `true`: Enable the log. Print log at exit.
# * `:quiet`: Enable the log. Do not print log at exit.
def self.enable(stats: false, log: false)
return false if enabled?
at_exit { print_and_dump_stats } if stats
Primitive.rb_yjit_enable(stats, stats != :quiet)
Primitive.rb_yjit_enable(stats, stats != :quiet, log, log != :quiet)
end
# If --yjit-trace-exits is enabled parse the hashes from
@ -173,6 +185,16 @@ module RubyVM::YJIT
strio.string
end
# Return an array of log entries.
# Return `nil` when option is not passed or unavailable.
def self.log
return nil unless log_enabled?
Primitive.rb_yjit_get_log.map do |timestamp, path|
[Time.at(timestamp), path]
end
end
# Produce disassembly for an iseq. This requires a `--enable-yjit=dev` build.
def self.disasm(iseq) # :nodoc:
# If a method or proc is passed in, get its iseq