模块 RubyVM::YJIT
此模块允许对 YJIT(CRuby 的即时编译器)进行内省。模块中的所有内容都高度依赖于具体实现,并且与标准库相比,API 的稳定性可能较低。
如果 YJIT 不支持构建 CRuby 的特定平台,则此模块可能不存在。
公共类方法
源码
# File yjit.rb, line 237 def self.code_gc Primitive.rb_yjit_code_gc end
丢弃现有的已编译代码以回收内存,并允许将来重新编译。
源码
# File yjit.rb, line 160 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 文档以获取选项。
源码
# File yjit.rb, line 47 def self.enable(stats: false, log: false) return false if enabled? at_exit { print_and_dump_stats } if stats call_yjit_hooks Primitive.rb_yjit_enable(stats, stats != :quiet, log, log != :quiet) end
启用 YJIT 编译。stats
选项决定是否启用 YJIT 统计信息。compilation_log
决定是否启用 YJIT 编译日志记录。
-
stats
:-
false
:不启用统计信息。 -
true
:启用统计信息。在退出时打印统计信息。 -
:quiet
:启用统计信息。不在退出时打印统计信息。
-
-
log
:-
false
:不启用日志。 -
true
:启用日志。在退出时打印日志。 -
:quiet
:启用日志。不在退出时打印日志。
-
源码
# File yjit.rb, line 12 def self.enabled? Primitive.cexpr! 'RBOOL(rb_yjit_enabled_p)' end
检查是否启用了 YJIT。
源码
# File yjit.rb, line 190 def self.log return nil unless log_enabled? Primitive.rb_yjit_get_log.map do |timestamp, path| [Time.at(timestamp), path] end end
返回日志条目的数组。当未传递选项或不可用时返回 nil
。
源码
# File yjit.rb, line 22 def self.log_enabled? Primitive.rb_yjit_log_enabled_p end
检查是否使用了 --yjit-log
。
源码
# File yjit.rb, line 32 def self.reset_stats! Primitive.rb_yjit_reset_stats_bang end
丢弃为 --yjit-stats
收集的统计信息。
源码
# File yjit.rb, line 172 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
。
源码
# File yjit.rb, line 17 def self.stats_enabled? Primitive.rb_yjit_stats_enabled_p end
检查是否使用了 --yjit-stats
。
源码
# File yjit.rb, line 180 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
时,此方法才会返回非空内容。