2298 Commits

Author SHA1 Message Date
Nobuyoshi Nakada
5944a31614
[DOC] Update sample callback of rb_objspace_each_objects
* refine liveness check
* fix missing closing brace
2023-04-15 11:48:11 +09:00
Peter Zhu
91dcce5ed1 Change max_iv_count to type attr_index_t
max_iv_count is calculated from next_iv_index of the shape, which is of
type attr_index_t, so we can also make max_iv_count of type
attr_index_t.
2023-04-11 15:02:44 -04:00
Peter Zhu
b4571097df Enable 5 size pools on 32 bit systems
This commit will allow 32 bit systems to take advantage of VWA.
2023-04-11 11:25:12 -04:00
git
84ce6fc873 * expand tabs. [ci skip]
Please consider using misc/expand_tabs.rb as a pre-commit hook.
2023-04-07 04:43:21 +00:00
Nobuyoshi Nakada
4adcfc8cd7
[Bug #19584] [DOC] Tweek description of rb_gc_register_address 2023-04-07 13:42:58 +09:00
Peter Zhu
bccec7fb46 Fix crash in rb_gc_register_address
[Bug #19584]

Some C extensions pass a pointer to a global variable to
rb_gc_register_address. However, if a GC is triggered inside of
rb_gc_register_address, then the object could get swept since it does
not exist on the stack.
2023-04-06 13:19:19 -04:00
Matt Valentine-House
026321c5b9 [Feature #19474] Refactor NEWOBJ macros
NEWOBJ_OF is now our canonical newobj macro. It takes an optional ec
2023-04-06 11:07:16 +01:00
Matt Valentine-House
b0297feb1f Remove newobj_of_cr
We can just make newobj_of take a ractor
2023-04-06 11:07:16 +01:00
Mike Dalessio
52e571fa72 Ensure ruby_xfree won't segfault if called after vm_destruct
[Bug #19580]

The real-world scenario motivating this change is libxml2's pthread
code which uses `pthread_key_create` to set up a destructor that is
called at thread exit to free thread-local storage.

There is a small window of time -- after ruby_vm_destruct but before
the process exits -- in which a pthread may exit and the destructor is
called, leading to a segfault.

Please note that this window of time may be relatively large if
`atexit` is being used.
2023-04-05 12:57:32 -04:00
Peter Zhu
1da2e7fca3
[Feature #19579] Remove !USE_RVARGC code (#7655)
Remove !USE_RVARGC code

[Feature #19579]

The Variable Width Allocation feature was turned on by default in Ruby
3.2. Since then, we haven't received bug reports or backports to the
non-Variable Width Allocation code paths, so we assume that nobody is
using it. We also don't plan on maintaining the non-Variable Width
Allocation code, so we are going to remove it.
2023-04-04 17:30:06 -04:00
Aaron Patterson
8525603c72
Revert "Fix transient heap mode"
This reverts commit 87253d047ce35e7836b6f97edbb4f819879a3b25.

Revert "Implement `Process.warmup`"

This reverts commit ba6ccd871442f55080bffd53e33678c0726787d2.
2023-04-04 12:59:14 -07:00
Aaron Patterson
87253d047c Fix transient heap mode
Make sure the transient heap is in the right mode when we finish warming
the heap.  Also ensure the GC isn't allowed to run while we iterate and
mutate the heap.
2023-04-04 19:49:08 +02:00
Jean Boussier
ba6ccd8714 Implement Process.warmup
[Feature #18885]

For now, the optimizations performed are:

  - Run a major GC
  - Compact the heap
  - Promote all surviving objects to oldgen

Other optimizations may follow.
2023-04-04 19:49:08 +02:00
Koichi Sasada
66755164aa add RUBY_DEBUG_LOG fo each_machine_stack_value 2023-03-31 17:27:56 +09:00
Peter Zhu
417b1a3644 Fix memory leak for iclass
[Bug #19550]

If !RCLASS_EXT_EMBEDDED (e.g. 32 bit systems) then the rb_classext_t is
allocated throug malloc so it must be freed.

The issue can be seen in the following script:

```
20.times do
  100_000.times do
    mod = Module.new
    Class.new do
      include mod
    end
  end

  # Output the Resident Set Size (memory usage, in KB) of the current Ruby process
  puts `ps -o rss= -p #{$$}`
end
```

Before this fix, the max RSS is 280MB, while after this change, it's
30MB.
2023-03-28 08:20:06 -04:00
Aaron Patterson
54dbd8bea8 Use an st table for "too complex" objects
st tables will maintain insertion order so we can marshal dump / load
objects with instance variables in the same order they were set on that
particular instance

[ruby-core:112926] [Bug #19535]

Co-Authored-By: Jemma Issroff <jemmaissroff@gmail.com>
2023-03-20 13:54:18 -07:00
Matt Valentine-House
7142328a94 [Feature #19406] Allow declarative definition of references
When using rb_data_type_struct to wrap a C struct, that C struct can
contain VALUE references to other Ruby objects.

If this is the case then one must also define dmark and optionally
dcompact callbacks in order to allow these objects to be correctly
handled by the GC. This is suboptimal as it requires GC related logic to
be implemented by extension developers. This can be a cause of subtle
bugs when references are not marked of updated correctly inside these
callbacks.

This commit provides an alternative approach, useful in the simple case
where the C struct contains VALUE members (ie. there isn't any
conditional logic, or data structure manipulation required to traverse
these references).

In this case references can be defined using a declarative syntax
as a list of edges (or, pointers to references).

A flag can be set on the rb_data_type_struct to notify the GC that
declarative references are being used, and a list of those references
can be assigned to the dmark pointer instead of a function callback, on
the rb_data_type_struct.

Macros are also provided for simple declaration of the reference list,
and building edges.

To avoid having to also find space in the struct to define a length for
the references list, I've chosed to always terminate the references list
with RUBY_REF_END - defined as UINTPTR_MAX. My assumption is that no
single struct will ever be large enough that UINTPTR_MAX is actually a
valid reference.
2023-03-17 19:20:40 +00:00
Peter Zhu
a206ee6709 Assume that FL_FINALIZE is in finalizer_table
If the flag FL_FINALIZE is set, then it's guaranteed to be in the
finalizer_table, so we can directly assume that without checking.
2023-03-17 11:12:45 -04:00
Matt Valentine-House
90d3bbb52b [Feature #19442] Remove GC_ENABLE_INCREMENTAL_MARK
Ruby doesn't compile when this is disabled, and it's not tested on CI.
We should remove it.

Co-Authored-By: Peter Zhu <peter@peterzhu.ca>
2023-03-16 09:32:08 +00:00
Matt Valentine-House
b3a271665b [Feature #19442] Remove USE_RINCGC flag
Ruby doesn't compile when this is set to 0. Let's remove it.
2023-03-16 09:32:08 +00:00
pkubaj
4e6c956741 Use __builtin_ppc_get_timebase on POWER with clang 2023-03-14 10:42:42 +09:00
Peter Zhu
d0b8bdb392 Remove duplicate code in gc_marks_finish
There is an identical block a few lines down that does the exact same
thing.
2023-03-10 13:13:34 -05:00
Aaron Patterson
365fed6369
Revert "Allow classes and modules to become too complex"
This reverts commit 69465df4242f3b2d8e55fbe18d7c45b47b40a626.
2023-03-10 08:50:43 -08:00
Peter Zhu
f98a7fd28d Move WeakMap and WeakKeyMap code to weakmap.c
These classes don't belong in gc.c as they're not actually part of the
GC. This commit refactors the code by moving all the code into a
weakmap.c file.
2023-03-10 09:32:10 -05:00
HParker
69465df424 Allow classes and modules to become too complex
This makes the behavior of classes and modules when there are too many instance variables match the behavior of objects with too many instance variables.
2023-03-09 15:34:49 -08:00
KJ Tsanaktsidis
7bd7aee02e Fix interpreter crash caused by RUBY_INTERNAL_EVENT_NEWOBJ + Ractors
When a Ractor is created whilst a tracepoint for
RUBY_INTERNAL_EVENT_NEWOBJ is active, the interpreter crashes. This is
because during the early setup of the Ractor, the stdio objects are
created, which allocates Ruby objects, which fires the tracepoint.
However, the tracepoint machinery tries to dereference the control frame
(ec->cfp->pc), which isn't set up yet and so crashes with a null pointer
dereference.

Fix this by not firing GC tracepoints if cfp isn't yet set up.
2023-03-09 09:46:14 +01:00
Peter Zhu
e1bd45624c Fix crash when allocating classes with newobj hook
We need to zero out the whole slot when running the newobj hook for a
newly allocated class because the slot could be filled with garbage,
which would cause a crash if a GC runs inside of the newobj hook.

For example, the following script crashes:

```
require "objspace"

GC.stress = true

ObjectSpace.trace_object_allocations {
  100.times do
    Class.new
  end
}
```

[Bug #19482]
2023-03-08 08:47:18 -05:00
Nobuyoshi Nakada
00d6772e40
Adjust styles [ci skip] 2023-03-08 14:02:46 +09:00
Peter Zhu
c78138abd3 Add function rb_data_free
This commit adds a function rb_data_free used by obj_free and
rb_objspace_call_finalizer to free T_DATA objects. This change also
means that RUBY_TYPED_FREE_IMMEDIATELY objects can be freed immediately
in rb_objspace_call_finalizer rather than being created into a zombie.
2023-03-07 08:28:03 -05:00
Takashi Kokubun
23ec248e48 s/mjit/rjit/ 2023-03-06 23:44:01 -08:00
Takashi Kokubun
233ddfac54 Stop exporting symbols for MJIT 2023-03-06 21:59:23 -08:00
Peter Zhu
a1758fbd7f Crash when malloc during GC
This feature was introduced in commit 2ccf6e5, but I realized that
using rb_warn is a bad idea because it allocates objects, which causes
a different crash ("object allocation during garbage collection phase").
We should just hard crash here instead.
2023-03-06 09:09:03 -05:00
John Bampton
2f7270c681
Fix spelling (#7389) 2023-02-27 09:56:06 -08:00
Peter Zhu
fa1eb31fca [ci skip] Add note in gc.c about ambiguous case 2023-02-24 16:10:54 -05:00
Peter Zhu
3e09822407 Fix incorrect line numbers in GC hook
If the previous instruction is not a leaf instruction, then the PC was
incremented before the instruction was ran (meaning the currently
executing instruction is actually the previous instruction), so we
should not increment the PC otherwise we will calculate the source
line for the next instruction.

This bug can be reproduced in the following script:

```
require "objspace"

ObjectSpace.trace_object_allocations_start
a =

  1.0 / 0.0
p [ObjectSpace.allocation_sourceline(a), ObjectSpace.allocation_sourcefile(a)]
```

Which outputs: [4, "test.rb"]

This is incorrect because the object was allocated on line 10 and not
line 4. The behaviour is correct when we use a leaf instruction (e.g.
if we replaced `1.0 / 0.0` with `"hello"`), then the output is:
[10, "test.rb"].

[Bug #19456]
2023-02-24 14:10:09 -05:00
Takashi Kokubun
1fdaa06660 Fix a warning on typedef
../gc.c:13317:1: warning: ‘typedef’ is not at beginning of declaration [-Wold-style-declaration]
13317 | } typedef weakkeymap_entry_t;
      | ^
2023-02-23 10:13:13 -08:00
Jean Boussier
2a5354e593 Implement ObjectSpace::WeakKeyMap basic allocator
[Feature #18498]
2023-02-23 16:01:57 +01:00
git
4f48debdcf * remove trailing spaces. [ci skip] 2023-02-22 21:09:22 +00:00
Peter Zhu
29ec8e151b Make GC faster when RGENGC_CHECK_MODE >= 2
We shouldn't run gc_verify_internal_consistency after every GC step
when RGENGC_CHECK_MODE >= 2, only when GC has finished. Running it
on every GC step makes it too slow.
2023-02-22 16:09:05 -05:00
Peter Zhu
93ac7405b8 Add marking and sweeping time to GC.stat
There is a `time` key in GC.stat that gives us the total time spent in
GC. However, we don't know what proportion of the time is spent between
marking and sweeping. This makes it difficult to tune the GC as we're
not sure where to focus our efforts on.

This PR adds keys `marking_time` and `sweeping_time` to GC.stat for the
time spent marking and sweeping, in milliseconds.

[Feature #19437]
2023-02-21 08:05:31 -05:00
Peter Zhu
d7c1ca48bf Refactor to separate marking and sweeping phases
This commit separates the marking and sweeping phases so that marking
functions do not directly call sweeping functions.
2023-02-21 08:05:31 -05:00
Matt Valentine-House
81dc3a1780 Remove USE_RGENGC_LOGGING_WB_UNPROTECT
This macro is broken when set to anything other than 0. And has had a
comment saying that it's broken for 3 years.

This commit deletes it and the associated logging code. It's clearly
not being used.

Co-Authored-By: Peter Zhu <peter@peterzhu.ca>
2023-02-17 09:49:45 -05:00
Nobuyoshi Nakada
21543ac86c
Fix compilation error when USE_RINCGC=0 2023-02-16 22:15:54 +09:00
Jean Boussier
1a4b4cd7f8 Move attached_object into rb_classext_struct
Given that signleton classes don't have an allocator,
we can re-use these bytes to store the attached object
in `rb_classext_struct` without making it larger.
2023-02-16 08:14:44 +01:00
Jean Boussier
bac4d2eefa Check !RCLASS_EXT_EMBEDDED instead of SIZE_POOL_COUNT == 1
It's much more self documenting and consistent
2023-02-15 10:47:22 +01:00
Peter Zhu
0ddf29f4d1 Remove unused preprocessor block 2023-02-09 11:38:32 -05:00
Matt Valentine-House
72aba64fff Merge gc.h and internal/gc.h
[Feature #19425]
2023-02-09 10:32:29 -05:00
Peter Zhu
861d70e383 Rename iseq_mark_and_update to iseq_mark_and_move
The new name is more consistent.
2023-02-08 12:43:25 -05:00
Jean Boussier
3ab3455145 Add RUBY_GC_HEAP_INIT_SIZE_%d_SLOTS to pre-init pools granularly
The old RUBY_GC_HEAP_INIT_SLOTS isn't really usable anymore as
it initalize all the pools by the same factor, but it's unlikely
that pools will need similar sizes.

In production our 40B pool is 5 to 6 times bigger than our 80B pool.
2023-02-08 09:26:07 +01:00
Jean byroot Boussier
4713b084da Revert "Revert "Consider DATA objects without a mark function as protected""
This reverts commit 6eae8e5f514db716e52ad06a2ac97e2cc3910d83.
2023-02-07 22:33:12 +01:00