Skip to content

Commit 2b39640

Browse files
authored
YJIT: Add RubyVM::YJIT.code_gc (ruby#6644)
* YJIT: Add RubyVM::YJIT.code_gc * Rename compiled_page_count to live_page_count
1 parent 5e6633f commit 2b39640

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

test/ruby/test_yjit.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -830,10 +830,10 @@ def foo
830830
def test_code_gc
831831
assert_compiles(code_gc_helpers + <<~'RUBY', exits: :any, result: :ok)
832832
return :not_paged unless add_pages(100) # prepare freeable pages
833-
code_gc # first code GC
833+
RubyVM::YJIT.code_gc # first code GC
834834
return :not_compiled1 unless compiles { nil } # should be JITable again
835835
836-
code_gc # second code GC
836+
RubyVM::YJIT.code_gc # second code GC
837837
return :not_compiled2 unless compiles { nil } # should be JITable again
838838
839839
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
@@ -854,7 +854,7 @@ def test_on_stack_code_gc_call
854854
855855
return :not_paged1 unless add_pages(400) # go to a page without initial ocb code
856856
return :broken_resume1 if fiber.resume != 0 # JIT the fiber
857-
code_gc # first code GC, which should not free the fiber page
857+
RubyVM::YJIT.code_gc # first code GC, which should not free the fiber page
858858
return :broken_resume2 if fiber.resume != 0 # The code should be still callable
859859
860860
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
@@ -873,19 +873,19 @@ def test_on_stack_code_gc_twice
873873
874874
return :not_paged1 unless add_pages(400) # go to a page without initial ocb code
875875
return :broken_resume1 if fiber.resume(true) != 0 # JIT the fiber
876-
code_gc # first code GC, which should not free the fiber page
876+
RubyVM::YJIT.code_gc # first code GC, which should not free the fiber page
877877
878878
return :not_paged2 unless add_pages(300) # add some stuff to be freed
879879
# Not calling fiber.resume here to test the case that the YJIT payload loses some
880880
# information at the previous code GC. The payload should still be there, and
881881
# thus we could know the fiber ISEQ is still on stack on this second code GC.
882-
code_gc # second code GC, which should still not free the fiber page
882+
RubyVM::YJIT.code_gc # second code GC, which should still not free the fiber page
883883
884884
return :not_paged3 unless add_pages(200) # attempt to overwrite the fiber page (it shouldn't)
885885
return :broken_resume2 if fiber.resume(true) != 0 # The fiber code should be still fine
886886
887887
return :broken_resume3 if fiber.resume(false) != nil # terminate the fiber
888-
code_gc # third code GC, freeing a page that used to be on stack
888+
RubyVM::YJIT.code_gc # third code GC, freeing a page that used to be on stack
889889
890890
return :not_paged4 unless add_pages(100) # check everything still works
891891
@@ -933,11 +933,6 @@ def add_pages(num_jits)
933933
num_jits.times { return false unless eval('compiles { nil.to_i }') }
934934
pages.nil? || pages < RubyVM::YJIT.runtime_stats[:compiled_page_count]
935935
end
936-
937-
def code_gc
938-
RubyVM::YJIT.simulate_oom! # bump write_pos
939-
eval('proc { nil }.call') # trigger code GC
940-
end
941936
RUBY
942937
end
943938

yjit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ VALUE rb_yjit_get_stats(rb_execution_context_t *ec, VALUE self);
10531053
VALUE rb_yjit_reset_stats_bang(rb_execution_context_t *ec, VALUE self);
10541054
VALUE rb_yjit_disasm_iseq(rb_execution_context_t *ec, VALUE self, VALUE iseq);
10551055
VALUE rb_yjit_insns_compiled(rb_execution_context_t *ec, VALUE self, VALUE iseq);
1056+
VALUE rb_yjit_code_gc(rb_execution_context_t *ec, VALUE self);
10561057
VALUE rb_yjit_simulate_oom_bang(rb_execution_context_t *ec, VALUE self);
10571058
VALUE rb_yjit_get_exit_locations(rb_execution_context_t *ec, VALUE self);
10581059

yjit.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ def self.insns_compiled(iseq)
162162
end
163163
end
164164

165+
# Free and recompile all existing JIT code
166+
def self.code_gc
167+
Primitive.rb_yjit_code_gc
168+
end
169+
165170
def self.simulate_oom!
166171
Primitive.rb_yjit_simulate_oom_bang
167172
end
@@ -214,14 +219,14 @@ def _print_stats
214219
$stderr.puts "compilation_failure: " + ("%10d" % compilation_failure) if compilation_failure != 0
215220
$stderr.puts "compiled_block_count: " + ("%10d" % stats[:compiled_block_count])
216221
$stderr.puts "compiled_iseq_count: " + ("%10d" % stats[:compiled_iseq_count])
217-
$stderr.puts "compiled_page_count: " + ("%10d" % stats[:compiled_page_count])
218222
$stderr.puts "freed_iseq_count: " + ("%10d" % stats[:freed_iseq_count])
219-
$stderr.puts "freed_page_count: " + ("%10d" % stats[:freed_page_count])
220223
$stderr.puts "invalidation_count: " + ("%10d" % stats[:invalidation_count])
221224
$stderr.puts "constant_state_bumps: " + ("%10d" % stats[:constant_state_bumps])
222225
$stderr.puts "inline_code_size: " + ("%10d" % stats[:inline_code_size])
223226
$stderr.puts "outlined_code_size: " + ("%10d" % stats[:outlined_code_size])
224227
$stderr.puts "freed_code_size: " + ("%10d" % stats[:freed_code_size])
228+
$stderr.puts "live_page_count: " + ("%10d" % stats[:live_page_count])
229+
$stderr.puts "freed_page_count: " + ("%10d" % stats[:freed_page_count])
225230
$stderr.puts "code_gc_count: " + ("%10d" % stats[:code_gc_count])
226231
$stderr.puts "num_gc_obj_refs: " + ("%10d" % stats[:num_gc_obj_refs])
227232

yjit/src/asm/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,25 @@ impl CodeBlock {
209209
self.page_size
210210
}
211211

212-
/// Return the number of code pages that have been allocated by the VirtualMemory.
213-
pub fn num_pages(&self) -> usize {
212+
/// Return the number of code pages that have been mapped by the VirtualMemory.
213+
pub fn num_mapped_pages(&self) -> usize {
214214
let mapped_region_size = self.mem_block.borrow().mapped_region_size();
215215
// CodeBlock's page size != VirtualMem's page size on Linux,
216216
// so mapped_region_size % self.page_size may not be 0
217217
((mapped_region_size - 1) / self.page_size) + 1
218218
}
219219

220+
/// Return the number of code pages that have been reserved by the VirtualMemory.
221+
pub fn num_virtual_pages(&self) -> usize {
222+
let virtual_region_size = self.mem_block.borrow().virtual_region_size();
223+
// CodeBlock's page size != VirtualMem's page size on Linux,
224+
// so mapped_region_size % self.page_size may not be 0
225+
((virtual_region_size - 1) / self.page_size) + 1
226+
}
227+
220228
/// Return the number of code pages that have been freed and not used yet.
221229
pub fn num_freed_pages(&self) -> usize {
222-
(0..self.num_pages()).filter(|&page_idx| self.has_freed_page(page_idx)).count()
230+
(0..self.num_mapped_pages()).filter(|&page_idx| self.has_freed_page(page_idx)).count()
223231
}
224232

225233
pub fn has_freed_page(&self, page_idx: usize) -> bool {
@@ -303,7 +311,7 @@ impl CodeBlock {
303311
pub fn code_size(&self) -> usize {
304312
let mut size = 0;
305313
let current_page_idx = self.write_pos / self.page_size;
306-
for page_idx in 0..self.num_pages() {
314+
for page_idx in 0..self.num_mapped_pages() {
307315
if page_idx == current_page_idx {
308316
// Count only actually used bytes for the current page.
309317
size += (self.write_pos % self.page_size).saturating_sub(self.page_start());
@@ -546,7 +554,7 @@ impl CodeBlock {
546554
}
547555

548556
// Check which pages are still in use
549-
let mut pages_in_use = vec![false; self.num_pages()];
557+
let mut pages_in_use = vec![false; self.num_mapped_pages()];
550558
// For each ISEQ, we currently assume that only code pages used by inline code
551559
// are used by outlined code, so we mark only code pages used by inlined code.
552560
for_each_on_stack_iseq_payload(|iseq_payload| {
@@ -560,10 +568,14 @@ impl CodeBlock {
560568
}
561569

562570
// Let VirtuamMem free the pages
563-
let freed_pages: Vec<usize> = pages_in_use.iter().enumerate()
571+
let mut freed_pages: Vec<usize> = pages_in_use.iter().enumerate()
564572
.filter(|&(_, &in_use)| !in_use).map(|(page, _)| page).collect();
565573
self.free_pages(&freed_pages);
566574

575+
// Append virtual pages in case RubyVM::YJIT.code_gc is manually triggered.
576+
let mut virtual_pages: Vec<usize> = (self.num_mapped_pages()..self.num_virtual_pages()).collect();
577+
freed_pages.append(&mut virtual_pages);
578+
567579
// Invalidate everything to have more compact code after code GC.
568580
// This currently patches every ISEQ, which works, but in the future,
569581
// we could limit that to patch only on-stack ISEQs for optimizing code GC.

yjit/src/stats.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ fn rb_yjit_gen_stats_dict() -> VALUE {
381381
// GCed code size
382382
hash_aset_usize!(hash, "freed_code_size", freed_page_count * cb.page_size());
383383

384-
// Compiled pages
385-
hash_aset_usize!(hash, "compiled_page_count", cb.num_pages() - freed_page_count);
384+
// Live pages
385+
hash_aset_usize!(hash, "live_page_count", cb.num_mapped_pages() - freed_page_count);
386386
}
387387

388388
// If we're not generating stats, the hash is done

yjit/src/yjit.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ pub extern "C" fn rb_yjit_iseq_gen_entry_point(iseq: IseqPtr, ec: EcPtr) -> *con
7979
}
8080
}
8181

82+
/// Free and recompile all existing JIT code
83+
#[no_mangle]
84+
pub extern "C" fn rb_yjit_code_gc(_ec: EcPtr, _ruby_self: VALUE) -> VALUE {
85+
if !yjit_enabled_p() {
86+
return Qnil;
87+
}
88+
89+
let cb = CodegenGlobals::get_inline_cb();
90+
cb.code_gc();
91+
Qnil
92+
}
93+
8294
/// Simulate a situation where we are out of executable memory
8395
#[no_mangle]
8496
pub extern "C" fn rb_yjit_simulate_oom_bang(_ec: EcPtr, _ruby_self: VALUE) -> VALUE {

0 commit comments

Comments
 (0)