summaryrefslogtreecommitdiff
path: root/gc.c
AgeCommit message (Collapse)Author
2022-07-28Lock the VM for rb_gc_writebarrier_unprotectPeter Zhu
When using Ractors, rb_gc_writebarrier_unprotect requries a VM lock since it modifies the bitmaps. Notes: Merged: https://github.com/ruby/ruby/pull/6192
2022-07-28Make array slices views rather than copiesPeter Zhu
Before this commit, if the slice fits in VWA, it would make a copy rather than a view. This is slower as it requires a memcpy of the contents. Notes: Merged: https://github.com/ruby/ruby/pull/6192
2022-07-28Refactor gc_ref_update_arrayPeter Zhu
Notes: Merged: https://github.com/ruby/ruby/pull/6192
2022-07-28Suppress use-after-free warning by gcc-12Nobuyoshi Nakada
2022-07-27Adjust styles [ci skip]Nobuyoshi Nakada
2022-07-27* expand tabs. [ci skip]git
Tabs were expanded because the file did not have any tab indentation in unedited lines. Please update your editor config, and use misc/expand_tabs.rb in the pre-commit hook.
2022-07-26Refactored poisoning and unpoisoning freelist to simpler APIJemma Issroff
2022-07-26Rename rb_ary_tmp_new to rb_ary_hidden_newPeter Zhu
rb_ary_tmp_new suggests that the array is temporary in some way, but that's not true, it just creates an array that's hidden and not on the transient heap. This commit renames it to rb_ary_hidden_new. Notes: Merged: https://github.com/ruby/ruby/pull/6180
2022-07-25Fix format specifierNobuyoshi Nakada
`uintptr_t` is not always `unsigned long`, but can be casted to void pointer safely.
2022-07-21Expand tabs [ci skip]Takashi Kokubun
[Misc #18891] Notes: Merged: https://github.com/ruby/ruby/pull/6094
2022-07-21[Bug #18929] Fix heap creation thrashing in GCPeter Zhu
Before this commit, if we don't have enough slots after sweeping but had pages on the tomb heap, then the GC would frequently allocate and deallocate pages. This is because after sweeping it would set allocatable pages (since there were not enough slots) but free the pages on the tomb heap. This commit reuses pages on the tomb heap if there's not enough slots after sweeping. Notes: Merged: https://github.com/ruby/ruby/pull/6156
2022-07-21Refactor macros of array.cPeter Zhu
Move some macros in array.c to internal/array.h so that other files can also access these macros. Notes: Merged: https://github.com/ruby/ruby/pull/6157
2022-07-20Ensure _id2ref finds symbols with the correct typeDaniel Colson
Prior to this commit it was possible to call `ObjectSpace._id2ref` with an offset static symbol object_id and get back a new, incorrectly tagged symbol: ``` > sensible_sym = ObjectSpace._id2ref(:a.object_id) => :a > nonsense_sym = ObjectSpace._id2ref(:a.object_id + 40) => :a > sensible_sym == nonsense_sym => false ``` `nonsense_sym` ends up tagged with `RUBY_ID_INSTANCE` instead of `RB_ID_LOCAL`. That means we can do silly things like: ``` > foo = Object.new > foo.instance_variable_set(:a, 123) (irb):2:in `instance_variable_set': `a' is not allowed as an instance variable name (NameError) > foo.instance_variable_set(ObjectSpace._id2ref(:a.object_id + 40), 123) => 123 > foo.instance_variables => [:a] ``` This was happening because `get_id_entry` ignores the tag bits when looking up the symbol. So `rb_id2str(symid)` would return a value and then we'd continue on with the nonsense `symid`. This commit prevents the situation by checking that the `symid` actually matches what we get back from `get_id_entry`. Now we get a `RangeError` for the nonsense id: ``` > ObjectSpace._id2ref(:a.object_id) => :a > ObjectSpace._id2ref(:a.object_id + 40) (irb):1:in `_id2ref': 0x000000000013f408 is not symbol id value (RangeError) ``` Co-authored-by: John Hawthorn <jhawthorn@github.com> Notes: Merged: https://github.com/ruby/ruby/pull/6147
2022-07-20[Bug #18928] Fix crash in WeakMapPeter Zhu
In wmap_live_p, if is_pointer_to_heap returns false, then the page is either in the tomb or has already been freed, so the object is dead. In this case, wmap_live_p should return false. Notes: Merged: https://github.com/ruby/ruby/pull/6152
2022-07-20Fix free objects count conditionNobuyoshi Nakada
Free objects have `T_NONE` as the builtin type. A pointer to a valid array element will never be `NULL`. Notes: Merged: https://github.com/ruby/ruby/pull/6153 Merged-By: nobu <nobu@ruby-lang.org>
2022-07-15Implement Objects on VWAPeter Zhu
This commit implements Objects on Variable Width Allocation. This allows Objects with more ivars to be embedded (i.e. contents directly follow the object header) which improves performance through better cache locality. Notes: Merged: https://github.com/ruby/ruby/pull/6117
2022-07-12[Feature #18901] Support size pool movement for ArraysMatt Valentine-House
This commit enables Arrays to move between size pools during compaction. This can occur if the array is mutated such that it would fit in a different size pool when embedded. The move is carried out in two stages: 1. The RVALUE is moved to a destination heap during object movement phase of compaction 2. The array data is re-embedded and the original buffer free'd if required. This happens during the update references step Notes: Merged: https://github.com/ruby/ruby/pull/6099
2022-07-11Add expand_heap option to GC.verify_compaction_referencesMatt Valentine-House
In order to reliably test compaction we need to be able to move objects between size pools. In order for this to happen there must be pages in a size pool into which we can allocate. The existing implementation of `double_heap` only doubled the existing number of pages in the heap, so if a size pool had a low number of pages (or 0) it's not guaranteed that enough space will be created to move objects into that size pool. This commit deprecates the `double_heap` option and replaces it with `expand_heap` instead. expand heap will expand each heap by enough pages to hold a number of slots defined by `GC_HEAP_INIT_SLOTS` or by `heap->total_pags` whichever is larger. If both `double_heap` and `expand_heap` are present, a deprecation warning will be shown for `double_heap` and the `expand_heap` behaviour will take precedence Given that this is an API intended for debugging and testing GC compaction I'm not concerned about the extra memory usage or time taken to create the pages. However, for completeness: Running the following `test.rb` and using `time` on my Macbook Pro shows the following memory usage and time impact: pp "RSS (kb): #{`ps -o rss #{Process.pid}`.lines.last.to_i}" GC.verify_compaction_references(double_heap: true, toward: :empty) pp "RSS (kb): #{`ps -o rss #{Process.pid}`.lines.last.to_i}" ❯ time make run ./miniruby -I./lib -I. -I.ext/common -r./arm64-darwin21-fake ./test.rb "RSS (kb): 24000" <internal:gc>:251: warning: double_heap is deprecated and will be removed "RSS (kb): 25232" ________________________________________________________ Executed in 124.37 millis fish external usr time 82.22 millis 0.09 millis 82.12 millis sys time 28.76 millis 2.61 millis 26.15 millis ❯ time make run ./miniruby -I./lib -I. -I.ext/common -r./arm64-darwin21-fake ./test.rb "RSS (kb): 24000" "RSS (kb): 49040" ________________________________________________________ Executed in 150.13 millis fish external usr time 103.32 millis 0.10 millis 103.22 millis sys time 35.73 millis 2.59 millis 33.14 millis Notes: Merged: https://github.com/ruby/ruby/pull/6107
2022-07-10Extract `atomic_inc_wraparound` functionNobuyoshi Nakada
2022-07-10Add `asan_unpoisoning_object` to execute the block with unpoisoningNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-10Split `rb_raw_obj_info`Nobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-10Cycle `obj_info_buffers_index` atomicallyNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-10`APPEND_S` for no conversion formatsNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-10Rewrite `APPENDF` using variadic argumentsNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-10Use `size_t` for `rb_raw_obj_info`Nobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-10Use `asan_unpoison_object_temporary`Nobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-10Get rid of static buffer in `obj_info`Nobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6092
2022-07-07Gather heap page size conditions combinationNobuyoshi Nakada
When similar combination of conditions are separated in two places, it is harder to make sure the conditional blocks match each other, Notes: Merged: https://github.com/ruby/ruby/pull/6100
2022-07-07Improve error message for segv in read_barrier_handlerPeter Zhu
If the page_body is a null pointer, then read_barrier_handler will crash with an unrelated message. This commit improves the error message. Before: test.rb:1: [BUG] Couldn't unprotect page 0x0000000000000000, errno: Cannot allocate memory After: test.rb:1: [BUG] read_barrier_handler: segmentation fault at 0x14 Notes: Merged: https://github.com/ruby/ruby/pull/6096
2022-07-07Fix crash in compaction due to unlocked pagePeter Zhu
The page of src could be partially compacted, so it may contain T_MOVED. Sweeping a page may read objects on this page, so we need to lock the page. Notes: Merged: https://github.com/ruby/ruby/pull/6096
2022-07-07Fix typo in gc_compact_movePeter Zhu
The page we're sweeping is on the destination heap `dheap`, not the source heap `heap`. Notes: Merged: https://github.com/ruby/ruby/pull/6096
2022-07-06Adjust indents [ci skip]Nobuyoshi Nakada
2022-06-18Extract `protect_page_body` to fix mismatched bracesNobuyoshi Nakada
2022-06-18Disable Mach exception handlers when read barriers in placeKJ Tsanaktsidis
The GC compaction mechanism implements a kind of read barrier by marking some (OS) pages as unreadable, and installing a SIGBUS/SIGSEGV handler to detect when they're accessed and invalidate an attempt to move the object. Unfortunately, when a debugger is attached to the Ruby interpreter on Mac OS, the debugger will trap the EXC_BAD_ACCES mach exception before the runtime can transform that into a SIGBUS signal and dispatch it. Thus, execution gets stuck; any attempt to continue from the debugger re-executes the line that caused the exception and no forward progress can be made. This makes it impossible to debug either the Ruby interpreter or a C extension whilst compaction is in use. To fix this, we disable the EXC_BAD_ACCESS handler when installing the SIGBUS/SIGSEGV handlers, and re-enable them once the compaction is done. The debugger will still trap on the attempt to read the bad page, but it will be trapping the SIGBUS signal, rather than the EXC_BAD_ACCESS mach exception. It's possible to continue from this in the debugger, which invokes the signal handler and allows forward progress to be made. Notes: Merged: https://github.com/ruby/ruby/pull/5991
2022-06-17Suppress code unused unless GC_CAN_COMPILE_COMPACTIONNobuyoshi Nakada
2022-06-16Include runtime checks for compaction supportPeter Zhu
Commit 0c36ba53192c5a0d245c9b626e4346a32d7d144e changed GC compaction methods to not be implemented when not supported. However, that commit only does compile time checks (which currently only checks for WASM), but there are additional compaction support checks during run time. This commit changes it so that GC compaction methods aren't defined during run time if the platform does not support GC compaction. [Bug #18829] Notes: Merged: https://github.com/ruby/ruby/pull/6019
2022-06-16Rename GC_COMPACTION_SUPPORTEDPeter Zhu
Naming this macro GC_COMPACTION_SUPPORTED is misleading because it only checks whether compaction is supported at compile time. [Bug #18829] Notes: Merged: https://github.com/ruby/ruby/pull/6019
2022-06-15Remove MJIT worker thread (#6006)Takashi Kokubun
[Misc #18830] Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
2022-06-13Move String RVALUES between poolsMatt Valentine-House
And re-embed any strings that can now fit inside the slot they've been moved to Notes: Merged: https://github.com/ruby/ruby/pull/5986
2022-06-08Fix major GC thrashingPeter Zhu
Only growth heaps are allowed to start major GCs. Before this patch, growth heaps are defined as size pools that freed more slots than had empty slots (i.e. there were more dead objects that empty space). But if the size pool is relatively stable and tightly packed with mostly old objects and has allocatable pages, then it would be incorrectly classified as a growth heap and trigger major GC. But since it's stable, it would not use any of the allocatable pages and forever be classified as a growth heap, causing major GC thrashing. This commit changes the definition of growth heap to require that the size pool to have no allocatable pages. Notes: Merged: https://github.com/ruby/ruby/pull/5993
2022-06-08Fix compilation error when USE_RVARGC=0Peter Zhu
force_major_gc_count was not defined when USE_RVARGC=0.
2022-06-08Add key force_major_gc_count to GC.stat_heapPeter Zhu
force_major_gc_count is the number of times the size pool forced major GC to run.
2022-06-07Remove while loop over heap_preparePeter Zhu
Having a while loop over `heap_prepare` makes the GC logic difficult to understand (it is difficult to understand when and why `heap_prepare` yields a free page). It is also a source of bugs and can cause an infinite loop if `heap_page` never yields a free page. Notes: Merged: https://github.com/ruby/ruby/pull/5907
2022-06-02Typedef built-in function typesNobuyoshi Nakada
2022-06-02Move `GC.verify_compaction_references` [Bug #18779]Nobuyoshi Nakada
Define `GC.verify_compaction_references` as a built-in ruby method, according to GC compaction support via `GC::OPTS`. Notes: Merged: https://github.com/ruby/ruby/pull/5972
2022-06-02Adjust indent and nesting [ci skip]Nobuyoshi Nakada
2022-05-24Define unsupported GC compaction methods as rb_f_notimplementMike Dalessio
Fixes [Bug #18779] Define the following methods as `rb_f_notimplement` on unsupported platforms: - GC.compact - GC.auto_compact - GC.auto_compact= - GC.latest_compact_info - GC.verify_compaction_references This change allows users to call `GC.respond_to?(:compact)` to properly test for compaction support. Previously, it was necessary to invoke `GC.compact` or `GC.verify_compaction_references` and check if those methods raised `NotImplementedError` to determine if compaction was supported. This follows the precedent set for other platform-specific methods. For example, in `process.c` for methods such as `Process.fork`, `Process.setpgid`, and `Process.getpriority`. Notes: Merged: https://github.com/ruby/ruby/pull/5934
2022-05-24Move compaction-related methods into gc.cMike Dalessio
These methods are removed from gc.rb and added to gc.c: - GC.compact - GC.auto_compact - GC.auto_compact= - GC.latest_compact_info - GC.verify_compaction_references This is a prefactor to allow setting these methods to `rb_f_notimplement` in a followup commit. Notes: Merged: https://github.com/ruby/ruby/pull/5934
2022-05-13Fix compiler warning when USE_RVARGC=0Matt Valentine-House
Notes: Merged: https://github.com/ruby/ruby/pull/5909
2022-05-10Write have instead of have have [ci skipKaíque Kandy Koga
Notes: Merged: https://github.com/ruby/ruby/pull/5897 Merged-By: nobu <nobu@ruby-lang.org>