Граф коммитов

1958 Коммитов

Автор SHA1 Сообщение Дата
Aaron Patterson 417c648f08 Free iv index table
IV index tables weren't being freed.  This program would leak memory:

```ruby
loop do
  k = Class.new do
    def initialize
      @a = 1
      @b = 1
      @c = 1
      @d = 1
      @e = 1
      @f = 1
      @g = 1
    end
  end
  k.new
end
```

This commit fixes the leak.
2021-03-23 13:10:25 -07:00
S.H 71ba09632b
Remove unneeded declarations 2021-03-20 21:00:29 +09:00
Yusuke Endoh c576e63ee7 gc.c: Use dedicated APIs for conservative GC in Emscripten
Emscripten provides "emscripten_scan_stack" to get the beginning and end
pointers of the stack for conservative GC.
Also, "emscripten_scan_registers" allows the GC to mark local variables
in WASM.
2021-03-19 12:35:48 +09:00
Nobuyoshi Nakada 90c12defb3
Constified variables for getenv 2021-03-12 17:13:53 +09:00
Peter Zhu 0bd1bc559f Don't use mmap on platforms that have large OS page sizes 2021-03-02 10:04:49 -08:00
Peter Zhu 6d834371c0 Fix typo 2021-03-02 10:04:49 -08:00
Peter Zhu 1c0e79e87b Disable auto compaction on platforms that do not support it 2021-02-25 11:01:50 -08:00
Peter Zhu 1e13548953 Use mmap for allocating heap pages 2021-02-25 11:01:50 -08:00
Aaron Patterson 08d5db4064
Reverting PR #4221
It seems this breaks tests on Solaris, so I'm reverting it until we
figure out the right fix.

  http://rubyci.s3.amazonaws.com/solaris11-sunc/ruby-master/log/20210224T210007Z.fail.html.gz
2021-02-24 13:44:10 -08:00
Peter Zhu a80366c922 Disable auto compaction on platforms that do not support it 2021-02-24 12:25:30 -08:00
Peter Zhu 785f5eb8f0 Use mmap for allocating heap pages 2021-02-24 12:25:30 -08:00
Koichi Sasada d260cbe295 show more information about imemo_ment
rb_obj_info(obj) (rp(obj)) doesn't show enough information for
non-iseq methods, so this patch shows more.
2021-02-19 16:54:31 +09:00
Koichi Sasada 07ab172ebe sync check_rvalue_consistency_force()
check_rvalue_consistency_force() uses is_pointer_to_heap() and
it should be synchronized with other ractors.
[Bug #17636]
2021-02-18 17:04:59 +09:00
Koichi Sasada 100e464bee clear RVALUE on NEWOBJ event.
NEWOBJ event is called without clearing RVALUE values (v1, v2, v3).
This patch clear them before NEWOBJ tracepoint internal hook.
[Bug #17599]
2021-02-18 17:04:23 +09:00
Koichi Sasada 969b824a0c sync GC rest if needed
marking requires a barrier (stop all Ractors) and gc_enter() does it.
However, it doesn't check rest event which can start marking.
[Bug #17599]
2021-02-18 16:40:59 +09:00
Nobuyoshi Nakada 42a16e5974
Removed no-longer used variable 2021-02-17 20:15:05 +09:00
Peter Zhu 33b8bd97a8 Remove unreachable if statement in gc_page_sweep
This if statement is not reachable because `was_compacting` cannot be true when `heap->compact_cursor` is NULL.
2021-02-16 15:07:59 -08:00
Aaron Patterson 75b96c3a05 Don't register non-heap allocated objects
`rb_define_const` can add objects as "mark objects".  This is to make
code like this work:

  33d6e92e0c/ext/etc/etc.c (L1201)

```
    rb_define_const(rb_cStruct, "Passwd", sPasswd); /* deprecated name */
```

sPasswd is a heap allocated object that is also a C global, so we can't
move it (it needs to be pinned).  However, we have many calls to
`rb_define_const` that just pass in an integer like this:

```
rb_define_const(rb_cDBM, "WRITER",  INT2FIX(O_RDWR|RUBY_DBM_RW_BIT));
```

Non heap allocated objects like integers will never move, so there is no
reason to waste time in the GC marking / pinning them.
2021-02-04 09:49:00 -08:00
Matt Valentine-House e3ef21c307 Use RCLASS_EXT macro instead of directly accessing ptr 2021-02-01 08:42:54 -08:00
Matt Valentine-House e0f999a2ed Add RCLASS_SUBCLASSES Macro 2021-02-01 08:42:54 -08:00
Nobuyoshi Nakada e44870c225
Removed static assertion about size of `RVALUE`
It is unable where unaligned word access is disallowed and
`double` is wider than pointers.
2021-01-31 17:45:35 +09:00
Nobuyoshi Nakada ae0a179c4b
Narrowed down the condition to pack RValue
Because of `double` in `RFloat`, `RValue` would be packed by
`sizeof(double)` by default, on platforms where `double` is wider
than `VALUE`.  Size of `RValue` is multiple of 5 now.
2021-01-31 13:20:15 +09:00
Peter Zhu d2ffd269a7 [Fixes #17538] Fix assertion failure when rincgc is turned off
Co-Authored-By: Matt Valentine-House <31869+eightbitraptor@users.noreply.github.com>
2021-01-27 16:17:46 -08:00
Matt Valentine-House 8a3f816675 Re-enable RGENGC_DEBUG for platforms with HAVE_VA_ARGS_MACRO
after this commit turned it off globally.

888cf28a7e

Co-authored-by: peterzhu2118 <peter@peterzhu.ca>
2021-01-26 08:18:44 -08:00
Matt Valentine-House 479e4d13cb Fix RGENGC CHECK MODE >= 4
[A previous commit](b59077eecf) removes some macro definitions that are used when RGENGC_CHECK_MODE >=4 because they were using data stored against objspace, which is not ractor safe

This commit reinstates those macro definitions, using the current ractor

Co-authored-by: peterzhu2118 <peter@peterzhu.ca>
2021-01-26 08:17:58 -08:00
Yusuke Endoh 47d6c55755 gc.c: stop overflow check on emscripten build 2021-01-23 10:11:50 +09:00
Koichi Sasada e586345b77 check is_incremental_marking() again
is_incremental_marking() can be changed after checking the
flag without locking, especially on `GC.stress = true`.
2021-01-22 18:15:57 +09:00
Aaron Patterson 32b7dcfb56
Fix more assumptions about the read barrier
This is a continuation of 0130e17a41.  We
need to always use the read barrier
2021-01-21 11:19:44 -08:00
Aaron Patterson 0130e17a41
Always enabled read barrier even on GC.compact
Some objects can survive the GC before compaction, but get collected in
the second compaction.  This means we could have objects reference
T_MOVED during "free" in the second, compacting GC.  If that is the
case, we need to invalidate those "moved" addresses.  Invalidation is
done via read barrier, so we need to make sure the read barrier is
active even during `GC.compact`.

This also means we don't actually need to do one GC before compaction,
we can just do the compaction and GC in one step.
2021-01-21 09:55:18 -08:00
Aaron Patterson 589a8026f0 fix ASAN errors 2021-01-13 14:53:45 -08:00
David CARLIER 161a20df28 gc fix typo for the timer instruction for ARM64. 2021-01-09 22:37:27 +09:00
Koichi Sasada 442bd0e92c show more info about imemo_callcache 2021-01-06 14:57:48 +09:00
Marcus Stollsteimer 3108ad7bf3 [DOC] Fix grammar: "is same as" -> "is the same as" 2021-01-05 15:13:53 +01:00
Koichi Sasada e7fc353f04 enable constant cache on ractors
constant cache `IC` is accessed by non-atomic manner and there are
thread-safety issues, so Ruby 3.0 disables to use const cache on
non-main ractors.

This patch enables it by introducing `imemo_constcache` and allocates
it by every re-fill of const cache like `imemo_callcache`.
[Bug #17510]

Now `IC` only has one entry `IC::entry` and it points to
`iseq_inline_constant_cache_entry`, managed by T_IMEMO object.

`IC` is atomic data structure so `rb_mjit_before_vm_ic_update()` and
`rb_mjit_after_vm_ic_update()` is not needed.
2021-01-05 02:27:58 +09:00
Takashi Kokubun ac2df89113
Stop managing valid class serials
`mjit_valid_class_serial_p` has no longer been used since b9007b6c54.
2020-12-29 23:01:11 -08:00
Nobuyoshi Nakada 09aca50fc4
Adjusted styles [ci skip] 2020-12-28 19:52:14 +09:00
Nobuyoshi Nakada 292230cbf9 Fixed leaked global symbols 2020-12-26 09:39:53 +09:00
Koichi Sasada 888cf28a7e define RGENGC_DEBUG_ENABLED() as 0
on RUBY_DEVEL==0 and !HAVE_VA_ARGS_MACRO.

gc_report() is always enabled on such configuration
(maybe it is a bug) so disable RGENGC_DEBUG_ENABLED().
2020-12-25 11:20:23 +09:00
Nobuyoshi Nakada 313d63c2ac
Use rb_init_identtable instead of direct use of rb_hashtype_ident 2020-12-23 18:06:27 +09:00
Koichi Sasada 02d9524cda separate rb_ractor_pub from rb_ractor_t
separate some fields from rb_ractor_t to rb_ractor_pub and put it
at the beggining of rb_ractor_t and declare it in vm_core.h so
vm_core.h can access rb_ractor_pub fields.

Now rb_ec_ractor_hooks() is a complete inline function and no
MJIT related issue.
2020-12-22 00:03:00 +09:00
Koichi Sasada 74ab2c3b46 finalizing should be checked before VM lock 2020-12-18 17:59:26 +09:00
Nobuyoshi Nakada 7d32bf7853
Removed a moved local variable 2020-12-18 17:56:08 +09:00
Koichi Sasada 61236770e6 need to sync gc_finalize_deferred
gc_finalize_deferred() runs finalizers and it accesses objspace,
so it need to sync.
2020-12-18 17:50:01 +09:00
Nobuyoshi Nakada 75416b8628 Removed old GC.stat keys deprecated since 2.2 2020-12-18 16:27:43 +09:00
Nobuyoshi Nakada 763d5f9c6b Removed old GC tuning environment variables deprecated since 2.1 2020-12-18 16:27:43 +09:00
Koichi Sasada cfa124ef05 acquire VM lock on gc_verify_internal_consistency()
There is a case to call this function without VM lock acquiring.
2020-12-18 14:16:06 +09:00
Koichi Sasada 29e42b8bfd add explicit check
To debug this issue:
https://rubyci.org/logs/rubyci.s3.amazonaws.com/solaris10-gcc/ruby-master/log/20201217T220004Z.fail.html.gz
2020-12-18 08:26:25 +09:00
Koichi Sasada 6538c89f1c gc_verify_internal_consistency() needs barrier
gc_verify_internal_consistency() accesses all slots (objects) so
all ractors should stop before starting this function.
2020-12-18 01:20:02 +09:00
Koichi Sasada da3438a504 sync obj_to_id_tbl
objspace->obj_to_id_tbl is a shared table so we need to synchronize
it to access.
2020-12-17 18:13:40 +09:00
Koichi Sasada 7f11c8086a reduce barrier counts for GC events
mark needs barrier (stop other ractors), but other GC events don't need
barriers (maybe...).
2020-12-17 18:13:26 +09:00
Koichi Sasada 99b9145380 relax synchronization on WB
Current synchronization is too much on write barriers.
2020-12-17 17:37:52 +09:00
Koichi Sasada c42948d784 add debug counters for gc start events 2020-12-17 17:03:05 +09:00
Koichi Sasada 93ba3ac036 RGENGC_PROFILE=0
Enabled this flag, maybe accidentally.
2020-12-17 03:46:44 +09:00
Nobuyoshi Nakada 289d42c932
Removed unneeded cast and use the real name 2020-12-15 23:43:08 +09:00
Koichi Sasada 974e89ae07 revert da3bca513f
It seems introduce critical problems. Now I could not find
out the issue.

http://ci.rvm.jp/results/trunk-test@ruby-sky1/3286048
2020-12-11 09:54:34 +09:00
Koichi Sasada 72f1c43584 ObjectSpace._id2ref should not support unshareable
ObjectSpace._id2ref(id) can return any objects even if they are
unshareable, so this patch raises RangeError if it runs on multi-ractor
mode and the found object is unshareable.
2020-12-10 18:27:44 +09:00
Nobuyoshi Nakada 142f154a0a
Unpoison freelist to chain 2020-12-10 18:16:22 +09:00
Koichi Sasada da3bca513f cache free pages per ractor
Per ractor method cache (GH-#3842) only cached 1 page and this patch
caches several pages to keep at least 512 free slots if available.
If you increase the number of cached free slots, all cached slots
will be collected when the GC is invoked.
2020-12-10 13:05:43 +09:00
Koichi Sasada 554c094977 set min/maximum free slots relative to ractor cnt
A program with multiple ractors can consume more objects per
unit time, so this patch set minimum/maximum free_slots to
relative to ractors count (upto 8).
2020-12-10 13:05:43 +09:00
Koichi Sasada eafe000af3 lazy sweep tries to collect 2048 slots
Lazy sweep tries to collect free (unused) slots incrementally, and
it only collect a few pages. This patch makes lazy sweep collects
more objects (at least 2048 objects) and GC overhead of multi-ractor
execution will be reduced.
2020-12-10 13:05:43 +09:00
Koichi Sasada 45b29754cf need the lock for debug checking.
Checking code (RGENGC_CHECK_MODE > 0) need a VM lock because it
refers objspace.
2020-12-09 15:15:46 +09:00
Koichi Sasada 1ba05f5b2d need more lock in finalize_list()
Some data should be accessed in parallel so they should be protected
by the lock.
2020-12-07 13:32:50 +09:00
Koichi Sasada 0ebf6bd0a2 RB_VM_LOCK_ENTER_NO_BARRIER
Write barrier requires VM lock because it accesses VM global bitmap
but RB_VM_LOCK_ENTER() can invoke GC because another ractor can wait
to invoke GC and RB_VM_LOCK_ENTER() is barrier point. This means that
before protecting by a write barrier, GC can invoke.
To prevent such situation, RB_VM_LOCK_ENTER_NO_BARRIER() is introduced.
This lock primitive does not become GC barrier points.
2020-12-07 11:27:25 +09:00
Koichi Sasada 8dd03e5cf0 skip assertion on multi-ractor
This assertion is not considerred on multi-ractor mdoe.
2020-12-07 11:10:18 +09:00
Koichi Sasada 59ddb88da6 RB_EC_NEWOBJ_OF
NEWOBJ with current ec.
2020-12-07 08:28:36 +09:00
Koichi Sasada 91d99025e4 per-ractor object allocation
Now object allocation requires VM global lock to synchronize objspace.
However, of course, it introduces huge overhead.
This patch caches some slots (in a page) by each ractor and use cached
slots for object allocation. If there is no cached slots, acquire the global lock
and get new cached slots, or start GC (marking or lazy sweeping).
2020-12-07 08:28:36 +09:00
Aaron Patterson a9d773a288
Revert "Skip repeated scan of object during compaction"
This seems to be breaking the build for some reason.

This command can reproduce it:

`make yes-test-all TESTS=--repeat-count=20`

This reverts commit 88bb1a672c.
2020-12-03 17:19:15 -08:00
Peter Zhu 88bb1a672c Skip repeated scan of object during compaction 2020-12-03 11:58:05 -08:00
Aaron Patterson 51268be7fe When allocating new pages, add them to the end of the linked list
When we allocate new pages, allocate them on the end of the linked list.
Then when we compact we can move things to the head of the list
2020-12-02 10:47:10 -08:00
Aaron Patterson 0bebea985d Incremental sweeping should not require page allocation
Incremental sweeping should sweep until we find a slot for objects to
use.  `heap_increment` was adding a page to the heap even though we
would sweep immediately after.

Co-authored-by: John Hawthorn <john@hawthorn.email>
2020-12-02 08:23:31 -08:00
Koichi Sasada d2cfb5228a show with sharing info 2020-12-01 11:10:19 +09:00
Koichi Sasada 67693d8d80 ractor local storage C-API
To manage ractor-local data for C extension, the following APIs
are defined.

* rb_ractor_local_storage_value_newkey
* rb_ractor_local_storage_value
* rb_ractor_local_storage_value_set
* rb_ractor_local_storage_ptr_newkey
* rb_ractor_local_storage_ptr
* rb_ractor_local_storage_ptr_set

At first, you need to create a key of storage by
rb_ractor_local_(value|ptr)_newkey().
For ptr storage, it accepts the type of storage,
how to mark and how to free with ractor's lifetime.

rb_ractor_local_storage_value/set are used to access a VALUE
and rb_ractor_local_storage_ptr/set are used to access a pointer.

random.c uses this API.
2020-12-01 09:39:30 +09:00
Koichi Sasada 77936ad679 support SIGSEGV/BUS while read_barrier_handler()
read_barrier_handler() can cause SIGSEGV/BUS so it should show
the errors.
2020-11-30 05:10:48 +09:00
Takashi Kokubun 69e77e81dc
Run rb_print_backtrace first on ruby_on_ci
Unfortunately we couldn't see a C backtrace with the previous commit
http://ci.rvm.jp/results/trunk-random2@phosphorus-docker/3272697.
2020-11-26 20:37:47 -08:00
Takashi Kokubun 4dbf6f1e51
Call rb_bug_without_die on CI
when GC.compact's SEGV handler is installed
2020-11-26 20:09:57 -08:00
Aaron Patterson c32218de1b
Disable auto compaction on platforms that can't support it
Both explicit compaction routines (gc_compact and the verify references form)
need to clear the heap before executing compaction.  Otherwise some
objects may not be alive, and we'll need the read barrier.  The heap
must only contain *live* objects if we want to disable the read barrier
during explicit compaction.

The previous commit was missing the "clear the heap" phase from the
"verify references" explicit compaction function.

Fixes [Bug #17306]
2020-11-25 11:29:14 -08:00
Aaron Patterson fed67fe6b2
Revert "Disable auto compaction on platforms that can't support it"
This reverts commit 63ad55cd88.

Revert "Disable read barrier on explicit compaction request"

This reverts commit 490b57783d.
2020-11-24 21:30:13 -08:00
Aaron Patterson 63ad55cd88
Disable auto compaction on platforms that can't support it
Auto Compaction uses mprotect to implement a read barrier.  mprotect can
only work on regions of memory that are a multiple of the OS page size.
Ruby's pages are a multiple of 4kb, but some platforms (like ppc64le)
don't have 4kb page sizes.  This commit disables the features on those
platforms.

Fixes [Bug #17306]
2020-11-24 14:48:19 -08:00
Aaron Patterson 87d21ee996
add HEAP_PAGE_SIZE to internal constants 2020-11-24 13:30:26 -08:00
Aaron Patterson 490b57783d Disable read barrier on explicit compaction request
We don't need a read barrier when the user calls `GC.compact` because we
don't allow allocations during GC, and all references should be "live"
2020-11-24 12:38:05 -08:00
Koichi Sasada 5e3259ea74 fix public interface
To make some kind of Ractor related extensions, some functions
should be exposed.

* include/ruby/thread_native.h
  * rb_native_mutex_*
  * rb_native_cond_*
* include/ruby/ractor.h
  * RB_OBJ_SHAREABLE_P(obj)
  * rb_ractor_shareable_p(obj)
  * rb_ractor_std*()
  * rb_cRactor

and rm ractor_pub.h
and rename srcdir/ractor.h to srcdir/ractor_core.h
    (to avoid conflict with include/ruby/ractor.h)
2020-11-18 03:52:41 +09:00
Aaron Patterson 6d17c9fa5d
gc_rest can change the total pages, so we need to do that first 2020-11-05 12:28:50 -08:00
Aaron Patterson d8da5c1983
add asserts to find crash 2020-11-05 12:27:09 -08:00
Aaron Patterson ab5f2fa4fb
Refactor verification method
Combine everything in to one C function
2020-11-05 11:13:04 -08:00
Aaron Patterson 68a3a2d90f
take VM lock when mutating the heap 2020-11-05 08:51:40 -08:00
Aaron Patterson a8581ce673 ensure T_OBJECT objects have internals initialized 2020-11-04 14:40:50 -08:00
Aaron Patterson 67b2c21c32
Add `GC.auto_compact= true/false` and `GC.auto_compact`
* `GC.auto_compact=`, `GC.auto_compact` can be used to control when
  compaction runs.  Setting `auto_compact=` to true will cause
  compaction to occurr duing major collections.  At the moment,
  compaction adds significant overhead to major collections, so please
  test first!

[Feature #17176]
2020-11-02 14:42:48 -08:00
Koichi Sasada db7a3b63ba suppport Ractor.send(move: true) for more deta
This patch allows to move more data types.
2020-11-02 01:37:28 +09:00
Aaron Patterson d8b0f1f7a8
Objects are born embedded, so we don't need to check ivpr
It's not necessary to check ivpt because objects are allocated as
"embedded" by default
2020-10-28 16:11:30 -07:00
Aaron Patterson a99f52d511
Remove another unnecessary test
Same as 5be42c1ef4
2020-10-28 10:16:57 -07:00
Aaron Patterson 5be42c1ef4
Remove unnecessary conditional
As of 0b81a484f3, `ROBJECT_IVPTR` will
always return a value, so we don't need to test whether or not we got
one.  T_OBJECTs always come to life as embedded objects, so they will
return an ivptr, and when they become "unembedded" they will have an
ivptr at that point too
2020-10-28 09:57:44 -07:00
Aaron Patterson 2c19c1484a
If an object isn't embedded it will have an ivptr
We don't need to check the existence if an ivptr because non-embedded
objects will always have one
2020-10-28 09:45:22 -07:00
Aaron Patterson abf678a439 Use a lock level for a less granular lock.
We are seeing an error where code that is generated with MJIT contains
references to objects that have been moved.  I believe this is due to a
race condition in the compaction function.

`gc_compact` has two steps:

1. Run a full GC to pin objects
2. Compact / update references

Step one is executed with `garbage_collect`.  `garbage_collect` calls
`gc_enter` / `gc_exit`, these functions acquire a JIT lock and release a
JIT lock.  So a lock is held for the duration of step 1.

Step two is executed by `gc_compact_after_gc`.  It also holds a JIT
lock.

I believe the problem is that the JIT is free to execute between step 1
and step 2.  It copies call cache values, but doesn't pin them when it
copies them.  So the compactor thinks it's OK to move the call cache
even though it is not safe.

We need to hold a lock for the duration of `garbage_collect` *and*
`gc_compact_after_gc`.  This patch introduces a lock level which
increments and decrements.  The compaction function can increment and
decrement the lock level and prevent MJIT from executing during both
steps.
2020-10-22 07:59:06 -07:00
Koichi Sasada b59077eecf Ractor-safe rb_objspace_reachable_objects_from
rb_objspace_reachable_objects_from(obj) is used to traverse all
reachable objects from obj. This function modify objspace but it
is not ractor-safe (thread-safe). This patch fix the problem.

Strategy:
(1) call GC mark process during_gc
(2) call Ractor-local custom mark func when !during_gc
2020-10-21 16:15:22 +09:00
Koichi Sasada ade411465d ObjectSpace.each_object with Ractors
Unshareable objects should not be touched from multiple ractors
so ObjectSpace.each_object should be restricted. On multi-ractor
mode, ObjectSpace.each_object only iterates shareable objects.
[Feature #17270]
2020-10-20 15:39:37 +09:00
Koichi Sasada f6661f5085 sync RClass::ext::iv_index_tbl
iv_index_tbl manages instance variable indexes (ID -> index).
This data structure should be synchronized with other ractors
so introduce some VM locks.

This patch also introduced atomic ivar cache used by
set/getinlinecache instructions. To make updating ivar cache (IVC),
we changed iv_index_tbl data structure to manage (ID -> entry)
and an entry points serial and index. IVC points to this entry so
that cache update becomes atomically.
2020-10-17 08:18:04 +09:00
Koichi Sasada 0406898a3f add NULL check.
DATA_PTR(ractor) can be NULL just after creation.
2020-10-03 23:22:17 +09:00
Aaron Patterson d598654c74
Fix ASAN and don't check SPECIAL_CONST_P
Heap allocated objects are never special constants.  Since we're walking
the heap, we know none of these objects can be special.  Also, adding
the object to the freelist will poison the object, so we can't check
that the type is T_NONE after poison.
2020-09-28 09:45:04 -07:00
Aaron Patterson 664eeda66e
Fix ASAN errors when updating call cache
Invalidating call cache walks the heap, so we need to take care to
un-poison objects when examining them
2020-09-28 09:45:04 -07:00
Koichi Sasada 4a588e70b8 sync rb_gc_register_mark_object()
rb_vm_t::mark_object_ary is global resource so we need to
synchronize to access it.
2020-09-24 17:09:12 +09:00