8000 YJIT: Add stats option to RubyVM::YJIT.enable · ruby/ruby@e333661 · GitHub
[go: up one dir, main page]

Skip to content

Commit e333661

Browse files
committed
YJIT: Add stats option to RubyVM::YJIT.enable
1 parent df0bfde commit e333661

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

test/ruby/test_yjit.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ def test_yjit_enable
6767
RUBY
6868
end
6969

70+
def test_yjit_enable_stats
71+
args = []
72+
args << "--disable=yjit" if RubyVM::YJIT.enabled?
73+
assert_separately(args, <<~RUBY, ignore_stderr: true)
74+
assert_false RubyVM::YJIT.enabled?
75+
assert_nil RubyVM::YJIT.runtime_stats
76+
77+
RubyVM::YJIT.enable(stats: true)
78+
79+
assert_true RubyVM::YJIT.enabled?
80+
assert_true RubyVM::YJIT.runtime_stats[:all_stats]
81+
RUBY
82+
end
83+
84+
def test_yjit_enable_stats_quiet
85+
assert_in_out_err(['--yjit-disable', '-e', 'RubyVM::YJIT.enable(stats: true)']) do |_stdout, stderr, _status|
86+
assert_not_empty stderr
87+
end
88+
assert_in_out_err(['--yjit-disable', '-e', 'RubyVM::YJIT.enable(stats: :quiet)']) do |_stdout, stderr, _status|
89+
assert_empty stderr
90+
end
91+
end
92+
7093
def test_yjit_enable_with_call_threshold
7194
assert_separately(%w[--yjit-disable --yjit-call-threshold=1], <<~RUBY)
7295
def not_compiled = nil

yjit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ VALUE rb_yjit_insns_compiled(rb_execution_context_t *ec, VALUE self, VALUE iseq)
11711171
VALUE rb_yjit_code_gc(rb_execution_context_t *ec, VALUE self);
11721172
VALUE rb_yjit_simulate_oom_bang(rb_execution_context_t *ec, VALUE self);
11731173
VALUE rb_yjit_get_exit_locations(rb_execution_context_t *ec, VALUE self);
1174-
VALUE rb_yjit_enable(rb_execution_context_t *ec, VALUE self);
1174+
VALUE rb_yjit_enable(rb_execution_context_t *ec, VALUE self, VALUE gen_stats, VALUE print_stats);
11751175

11761176
// Preprocessed yjit.rb generated during build
11771177
#include "yjit.rbinc"

yjit.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ def self.reset_stats!
2929
end
3030

3131
# Enable \YJIT compilation.
32-
def self.enable
33-
Primitive.rb_yjit_enable
32+
def self.enable(stats: false)
33+
return false if enabled?
34+
at_exit { print_and_dump_stats } if stats
35+
Primitive.rb_yjit_enable(stats, stats != :quiet)
3436
end
3537

3638
# If --yjit-trace-exits is enabled parse the hashes from
@@ -225,16 +227,19 @@ def self.simulate_oom! # :nodoc:
225227

226228
# Avoid calling a method here to not interfere with compilation tests
227229
if Primitive.rb_yjit_stats_enabled_p
228-
at_exit do
230+
at_exit { print_and_dump_stats }
231+
end
232+
233+
class << self # :stopdoc:
234+
private
235+
236+
# Print stats and dump exit locations
237+
def print_and_dump_stats
229238
if Primitive.rb_yjit_print_stats_p
230239
_print_stats
231240
end
232241
_dump_locations
233242
end
234-
end
235-
236-
class << self # :stopdoc:
237-
private
238243

239244
def _dump_locations # :nodoc:
240245
return unless tra 8000 ce_exit_locations_enabled?

yjit/src/options.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ pub fn parse_option(str_ptr: *const std::os::raw::c_char) -> Option<()> {
249249
("no-type-prop", "") => unsafe { OPTIONS.no_type_prop = true },
250250
("stats", _) => match opt_val {
251251
"" => unsafe { OPTIONS.gen_stats = true },
252-
"quiet" => {
253-
unsafe { OPTIONS.gen_stats = true }
254-
unsafe { OPTIONS.print_stats = false }
252+
"quiet" => unsafe {
253+
OPTIONS.gen_stats = true;
254+
OPTIONS.print_stats = false;
255255
},
256256
_ => {
257257
return None;

yjit/src/yjit.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ pub extern "C" fn rb_yjit_code_gc(_ec: EcPtr, _ruby_self: VALUE) -> VALUE {
168168

169169
/// Enable YJIT compilation, returning true if YJIT was previously disabled
170170
#[no_mangle]
171-
pub extern "C" fn rb_yjit_enable(_ec: EcPtr, _ruby_self: VALUE) -> VALUE {
171+
pub extern "C" fn rb_yjit_enable(_ec: EcPtr, _ruby_self: VALUE, gen_stats: VALUE, print_stats: VALUE) -> VALUE {
172172
with_vm_lock(src_loc!(), || {
173-
if yjit_enabled_p() {
174-
return Qfalse;
175-
}
176-
177173
// Initialize and enable YJIT if currently disabled
174+
unsafe {
175+
OPTIONS.gen_stats = gen_stats.test();
176+
OPTIONS.print_stats = print_stats.test();
177+
}
178178
yjit_init();
179179

180180
// Add "+YJIT" to RUBY_DESCRIPTION

0 commit comments

Comments
 (0)
0