module RubyVM::YJIT
此模块允许内省 YJIT,即 CRuby 的即时编译器。模块中的所有内容都高度依赖于实现,与标准库相比,API 可能不太稳定。
如果 YJIT 不支持为其构建 CRuby 的特定平台,则此模块可能不存在。
Public Class Methods
Source
# File yjit.rb, line 253 def self.code_gc Primitive.rb_yjit_code_gc end
丢弃现有的编译代码以回收内存并允许将来的重新编译。
Source
# File yjit.rb, line 176 def self.dump_exit_locations(filename) unless trace_exit_locations_enabled? raise ArgumentError, "--yjit-trace-exits must be enabled to use dump_exit_locations." end File.binwrite(filename, Marshal.dump(RubyVM::YJIT.exit_locations)) end
Marshal 将退出位置转储到指定的文件名。
用法
如果传递了 --yjit-exit-locations,将自动生成名为“yjit_exit_locations.dump”的文件。
如果您想手动收集跟踪,请直接调用 dump_exit_locations。
请注意,在脚本中调用此方法会在转储创建后生成统计信息,因此统计数据可能包含转储本身的退出信息。
在脚本调用中
at_exit do RubyVM::YJIT.dump_exit_locations("my_file.dump") end
然后使用以下选项运行文件
ruby --yjit --yjit-trace-exits test.rb
代码运行完成后,使用 Stackprof 读取转储文件。有关选项,请参阅 Stackprof 文档。
Source
# File yjit.rb, line 48 def self.enable(stats: false, log: false, mem_size: nil, call_threshold: nil) return false if enabled? if Primitive.cexpr! 'RBOOL(rb_zjit_enabled_p)' warn("Only one JIT can be enabled at the same time.") return false end if mem_size raise ArgumentError, "mem_size must be a Integer" unless mem_size.is_a?(Integer) raise ArgumentError, "mem_size must be between 1 and 2048 MB" unless (1..2048).include?(mem_size) end if call_threshold raise ArgumentError, "call_threshold must be a Integer" unless call_threshold.is_a?(Integer) raise ArgumentError, "call_threshold must be a positive integer" unless call_threshold.positive? end at_exit { print_and_dump_stats } if stats Primitive.rb_yjit_enable(stats, stats != :quiet, log, log != :quiet, mem_size, call_threshold) end
启用 YJIT 编译。stats 选项决定是否启用 YJIT 统计信息。log 决定是否启用 YJIT 编译日志。可选的 mem_size 和 call_threshold 可用于覆盖默认配置。
-
stats:-
false:不启用统计信息。 -
true:启用统计信息。在退出时打印统计信息。 -
:quiet:启用统计信息。在退出时不要打印统计信息。
-
-
log:-
false:不启用日志。 -
true:启用日志。在退出时打印日志。 -
:quiet:启用日志。在退出时不要打印日志。
-
Source
# File yjit.rb, line 12 def self.enabled? Primitive.cexpr! 'RBOOL(rb_yjit_enabled_p)' end
检查 YJIT 是否已启用。
Source
# File yjit.rb, line 206 def self.log return nil unless log_enabled? Primitive.rb_yjit_get_log.map do |timestamp, path| [Time.at(timestamp), path] end end
返回日志条目数组。当未传递选项或选项不可用时返回 nil。
Source
# File yjit.rb, line 22 def self.log_enabled? Primitive.rb_yjit_log_enabled_p end
检查是否使用了 --yjit-log。
Source
# File yjit.rb, line 32 def self.reset_stats! Primitive.rb_yjit_reset_stats_bang end
丢弃为 --yjit-stats 收集的统计信息。
Source
# File yjit.rb, line 188 def self.runtime_stats(key = nil) raise TypeError, "non-symbol given" unless key.nil? || Symbol === key Primitive.rb_yjit_get_stats(key) end
返回用于 --yjit-stats 命令行选项生成的统计信息的哈希。当未传递选项或选项不可用时返回 nil。如果提供了符号参数,则仅返回命名统计项的值。如果提供了任何其他类型,则会引发 TypeError。
Source
# File yjit.rb, line 17 def self.stats_enabled? Primitive.rb_yjit_stats_enabled_p end
检查是否使用了 --yjit-stats。
Source
# File yjit.rb, line 196 def self.stats_string # Lazily require StringIO to avoid breaking miniruby require 'stringio' strio = StringIO.new _print_stats(out: strio) strio.string end
将计数器格式化并打印为 String。仅当启用了 --yjit-stats 时,此方法才返回非空内容。