![]() Some GC modules, notably MMTk, support parallel GC, i.e. multiple GC threads work in parallel during a GC. Currently, when two GC threads scan two iseq objects simultaneously when YJIT is enabled, both threads will attempt to borrow `CodeBlock::mem_block`, which will result in panic. This commit makes one part of the change. We now set the YJIT code memory to writable in bulk before the reference-updating phase, and reset it to executable in bulk after the reference-updating phase. Previously, YJIT lazily sets memory pages writable while updating object references embedded in JIT-compiled machine code, and sets the memory back to executable by calling `mark_all_executable`. This approach is inherently unfriendly to parallel GC because (1) it borrows `CodeBlock::mem_block`, and (2) it sets the whole `CodeBlock` as executable which races with other GC threads that are updating other iseq objects. It also has performance overhead due to the frequent invocation of system calls. We now set the permission of all the code memory in bulk before and after the reference updating phase. Multiple GC threads can now perform raw memory writes in parallel. We should also see performance improvement during moving GC because of the reduced number of `mprotect` system calls. |
||
---|---|---|
.. | ||
default | ||
mmtk | ||
extconf_base.rb | ||
gc.h | ||
gc_impl.h | ||
README.md |
Ruby's Garbage Collectors
This directory contains implementations for Ruby's garbage collector (GC). The GC implementations use the Modular GC API to interact with Ruby. For more details about this API, see the Modular GC API section.
Two GC implementations are included in Ruby:
- Default: The GC implementation that is used by default in Ruby. This GC is stable and production ready. The implementation uses a mark-sweep-compact algorithm.
- MMTk: An experimental implementation using the MMTk framework. The code lives in the ruby/mmtk repository and is synchronized here. MMTk provides a wide variety of GC algorithms to choose from. For usage instructions and current progress, refer to the ruby/mmtk repository.
Building guide
Tip
If you are not sure how to build Ruby, follow the Building Ruby guide.
Important
Ruby's modular GC feature is experimental and subject to change. There may be bugs or performance impacts. Use at your own risk.
Building Ruby with Modular GC
- Configure Ruby with the
--with-modular-gc=<dir>
option, wheredir
is the directory you want to place the built GC libraries into. - Build Ruby as usual.
Building GC implementations shipped with Ruby
- Build your desired GC implementation with
make install-modular-gc MODULAR_GC=<impl>
. This will build the GC implementation and place the built library into thedir
specified in step 1.impl
can be one of: - Run your desired GC implementation by setting the
RUBY_GC_LIBRARY=<lib>
environment variable, wherelib
could bedefault
,mmtk
, or your own implementation (as long as you place it in thedir
specified in step 1).
Modular GC API
Warning
The Modular GC API is experimental and subject to change without notice.
GC implementations interact with Ruby via the Modular GC API. All implementations must provide the functions in gc/gc_impl.h for Ruby to hook into. GC implementations can use any public C API in Ruby, along with additional APIs defined in gc/gc.h.
Additionally, create an extconf.rb file to build the GC library. This file must use gc/extconf_base.rb and the create_gc_makefile
method.