10#include "gc/gc_impl.h"
11#include "gc/mmtk/mmtk.h"
14# include "gc/mmtk/zjit_fastpath.h"
17#include "ccan/list/list.h"
21#include <sys/sysctl.h>
25 bool user_gc_disabled;
30 size_t moving_gc_count;
32 size_t total_allocated_objects;
38 struct ccan_list_head ractor_caches;
39 unsigned long live_ractor_cache_count;
41 pthread_mutex_t mutex;
44 pthread_cond_t cond_world_stopped;
45 pthread_cond_t cond_world_started;
46 size_t start_the_world_count;
48 pthread_mutex_t event_hook_mutex;
51 bool gc_thread_crashed;
57 unsigned int fork_hook_vm_lock_lev;
59 uintptr_t vo_bit_log_region_size;
60 uintptr_t vo_bit_base_addr;
61 size_t max_non_los_default_alloc_bytes;
64#define OBJ_FREE_BUF_CAPACITY 128
67 struct ccan_list_node list_node;
69 MMTk_Mutator *mutator;
74 MMTk_ObjectReference obj_free_parallel_buf[OBJ_FREE_BUF_CAPACITY];
75 size_t obj_free_parallel_count;
76 MMTk_ObjectReference obj_free_non_parallel_buf[OBJ_FREE_BUF_CAPACITY];
77 size_t obj_free_non_parallel_count;
84 MMTK_FINAL_JOB_FINALIZE,
93 VALUE finalizer_array;
98#ifdef RB_THREAD_LOCAL_SPECIFIER
101RB_THREAD_LOCAL_SPECIFIER
VALUE marking_parent_object;
103# error We currently need language-supported TLS
107# define MMTK_ASSERT(expr, ...) RUBY_ASSERT_ALWAYS(expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
109# define MMTK_ASSERT(expr, ...) ((void)0)
114#define MMTK_ALLOCATION_SEMANTICS_DEFAULT 0
115#define MMTK_ALLOCATION_SEMANTICS_LOS 2
122# define RB_MMTK_UPCALL __attribute__((visibility("hidden")))
124# define RB_MMTK_UPCALL
127static inline VALUE rb_mmtk_call_object_closure(
VALUE obj,
bool pin);
132 rb_mmtk_gc_thread_tls = gc_thread_tls;
136rb_mmtk_is_mutator(
void)
142rb_mmtk_stop_the_world(
void)
147 if ((err = pthread_mutex_lock(&
objspace->mutex)) != 0) {
148 rb_bug(
"ERROR: cannot lock objspace->mutex: %s", strerror(err));
155 if ((err = pthread_mutex_unlock(&
objspace->mutex)) != 0) {
156 rb_bug(
"ERROR: cannot release objspace->mutex: %s", strerror(err));
161rb_mmtk_resume_mutators(
bool current_gc_may_move)
166 if ((err = pthread_mutex_lock(&
objspace->mutex)) != 0) {
167 rb_bug(
"ERROR: cannot lock objspace->mutex: %s", strerror(err));
172 if (current_gc_may_move)
objspace->moving_gc_count++;
173 pthread_cond_broadcast(&
objspace->cond_world_started);
175 if ((err = pthread_mutex_unlock(&
objspace->mutex)) != 0) {
176 rb_bug(
"ERROR: cannot release objspace->mutex: %s", strerror(err));
187 size_t starting_gc_count =
objspace->gc_count;
189 int lock_lev = RB_GC_VM_LOCK();
192 if ((err = pthread_mutex_lock(&
objspace->mutex)) != 0) {
193 rb_bug(
"ERROR: cannot lock objspace->mutex: %s", strerror(err));
196 if (
objspace->gc_count == starting_gc_count) {
199 rb_gc_initialize_vm_context(&
objspace->vm_context);
201 mutator->gc_mutator_p =
true;
205 clock_gettime(CLOCK_MONOTONIC, &gc_start_time);
208 rb_gc_save_machine_context();
213 ccan_list_for_each(&
objspace->ractor_caches, rc, list_node) {
214 mmtk_flush_obj_free_buffer(rc);
219 pthread_cond_broadcast(&
objspace->cond_world_stopped);
226 if (RB_UNLIKELY(
objspace->crash_context.gc_thread_crashed)) {
227 rb_bug(
"%s",
objspace->crash_context.crash_msg);
232 clock_gettime(CLOCK_MONOTONIC, &gc_end_time);
235 (gc_end_time.tv_sec - gc_start_time.tv_sec) * (1000 * 1000 * 1000) +
236 (gc_end_time.tv_nsec - gc_start_time.tv_nsec);
240 if ((err = pthread_mutex_unlock(&
objspace->mutex)) != 0) {
241 rb_bug(
"ERROR: cannot release objspace->mutex: %s", strerror(err));
243 RB_GC_VM_UNLOCK(lock_lev);
247rb_mmtk_before_updating_jit_code(
void)
249 rb_gc_before_updating_jit_code();
253rb_mmtk_after_updating_jit_code(
void)
255 rb_gc_after_updating_jit_code();
259rb_mmtk_number_of_mutators(
void)
262 return objspace->live_ractor_cache_count;
266rb_mmtk_get_mutators(
void (*visit_mutator)(MMTk_Mutator *mutator,
void *data),
void *data)
271 ccan_list_for_each(&
objspace->ractor_caches, ractor_cache, list_node) {
272 visit_mutator(ractor_cache->mutator, data);
277rb_mmtk_scan_gc_roots(
void)
285pin_value(st_data_t key, st_data_t value, st_data_t data)
287 rb_gc_impl_mark_and_pin((
void *)data, (
VALUE)value);
293rb_mmtk_scan_objspace(
void)
297 if (
objspace->finalizer_table != NULL) {
302 while (job != NULL) {
304 case MMTK_FINAL_JOB_DFREE:
306 case MMTK_FINAL_JOB_FINALIZE:
307 rb_gc_impl_mark(
objspace, job->as.finalize.finalizer_array);
310 rb_bug(
"rb_mmtk_scan_objspace: unknown final job type %d", job->kind);
318rb_mmtk_move_obj_during_marking(MMTk_ObjectReference from, MMTk_ObjectReference to)
320 rb_gc_move_obj_during_marking((
VALUE)from, (
VALUE)to);
322 size_t from_slot_size = rb_gc_impl_obj_slot_size((
VALUE)from);
323 size_t to_slot_size = rb_gc_impl_obj_slot_size((
VALUE)to);
324 if (from_slot_size != to_slot_size) {
329 rb_gc_obj_changed_slot_size((
VALUE)to, to_slot_size);
334rb_mmtk_obj_optimal_size(MMTk_ObjectReference obj)
336 return rb_gc_obj_optimal_size((
VALUE)obj);
340rb_mmtk_update_object_references(MMTk_ObjectReference mmtk_object)
345 marking_parent_object = object;
346 rb_gc_update_object_references(rb_gc_get_objspace(),
object);
347 marking_parent_object = 0;
352rb_mmtk_call_gc_mark_children(MMTk_ObjectReference
object)
354 marking_parent_object = (
VALUE)
object;
355 rb_gc_mark_children(rb_gc_get_objspace(), (
VALUE)
object);
356 marking_parent_object = 0;
360rb_mmtk_handle_weak_references(MMTk_ObjectReference mmtk_object,
bool moving)
364 marking_parent_object = object;
366 rb_gc_handle_weak_references(
object);
369 rb_gc_update_object_references(rb_gc_get_objspace(),
object);
372 marking_parent_object = 0;
376rb_mmtk_call_obj_free(MMTk_ObjectReference
object)
382 pthread_mutex_lock(&
objspace->event_hook_mutex);
384 pthread_mutex_unlock(&
objspace->event_hook_mutex);
387 if (RB_UNLIKELY(rb_gc_obj_needs_cleanup_p(obj))) {
392 memset((
void *)obj, 0, rb_gc_impl_obj_slot_size(obj));
397rb_mmtk_vm_live_bytes(
void)
408 job->next =
objspace->finalizer_jobs;
409 job->kind = MMTK_FINAL_JOB_FINALIZE;
410 job->as.finalize.finalizer_array = table;
416rb_mmtk_update_finalizer_table_i(st_data_t key, st_data_t value, st_data_t data,
int error)
418 MMTK_ASSERT(mmtk_is_reachable((MMTk_ObjectReference)value));
423 if (mmtk_is_reachable((MMTk_ObjectReference)key)) {
424 VALUE new_key_location = rb_mmtk_call_object_closure((
VALUE)key,
false);
428 if (new_key_location != key) {
444rb_mmtk_update_finalizer_table_replace_i(st_data_t *key, st_data_t *value, st_data_t data,
int existing)
446 *key = rb_mmtk_call_object_closure((
VALUE)*key,
false);
452rb_mmtk_update_finalizer_table(
void)
456 st_foreach_with_replace(
458 rb_mmtk_update_finalizer_table_i,
459 rb_mmtk_update_finalizer_table_replace_i,
465rb_mmtk_global_tables_count(
void)
467 return RB_GC_VM_WEAK_TABLE_COUNT;
470static inline VALUE rb_mmtk_call_object_closure(
VALUE obj,
bool pin);
473rb_mmtk_update_global_tables_i(
VALUE val,
void *data)
475 if (!mmtk_is_reachable((MMTk_ObjectReference)val)) {
480 if (rb_mmtk_call_object_closure(val,
false) != val) {
488rb_mmtk_update_global_tables_replace_i(
VALUE *ptr,
void *data)
491 *ptr = rb_mmtk_call_object_closure(*ptr,
false);
497rb_mmtk_update_global_tables(
int table,
bool moving)
499 MMTK_ASSERT(table < RB_GC_VM_WEAK_TABLE_COUNT);
501 rb_gc_vm_weak_table_foreach(
502 rb_mmtk_update_global_tables_i,
503 rb_mmtk_update_global_tables_replace_i,
506 (
enum rb_gc_vm_weak_tables)table
513rb_mmtk_gc_thread_bug(const
char *msg, ...)
517 objspace->crash_context.gc_thread_crashed =
true;
521 vsnprintf(
objspace->crash_context.crash_msg,
sizeof(
objspace->crash_context.crash_msg), msg, args);
524 fprintf(stderr,
"-- GC thread backtrace "
525 "-------------------------------------------\n");
526 rb_gc_print_backtrace();
527 fprintf(stderr,
"\n");
529 rb_mmtk_resume_mutators(
false);
533 rb_bug(
"rb_mmtk_gc_thread_bug");
538rb_mmtk_gc_thread_panic_handler(
void)
540 rb_mmtk_gc_thread_bug(
"MMTk GC thread panicked");
545rb_mmtk_mutator_thread_panic_handler(
void)
547 rb_bug(
"Ruby mutator thread panicked");
553#define RB_MMTK_HEAP_LIMIT_PERC 80
554#define RB_MMTK_DEFAULT_HEAP_MIN (1024 * 1024)
555#define RB_MMTK_DEFAULT_HEAP_MAX (rb_mmtk_system_physical_memory() / 100 * RB_MMTK_HEAP_LIMIT_PERC)
558 RB_MMTK_DYNAMIC_HEAP,
563rb_mmtk_builder_init(
void)
565 MMTk_Builder *builder = mmtk_builder_default();
571rb_gc_impl_objspace_alloc(
void)
576 static struct objspace *the_objspace = NULL;
577 if (the_objspace == NULL) {
578 MMTk_Builder *builder = rb_mmtk_builder_init();
582 mmtk_init_binding(builder, &binding_options);
583 the_objspace = calloc(1,
sizeof(
struct objspace));
590rb_gc_impl_multi_objspace_p(
void)
595static void gc_run_finalizers(
void *data);
598rb_gc_impl_objspace_init(
void *objspace_ptr)
604 if (
objspace->finalizer_table != NULL)
return;
608 objspace->finalizer_table = st_init_numtable();
611 ccan_list_head_init(&
objspace->ractor_caches);
613 objspace->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
614 objspace->cond_world_stopped = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
615 objspace->cond_world_started = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
617 objspace->event_hook_mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
619 objspace->vo_bit_log_region_size = mmtk_get_vo_bit_log_region_size();
620 objspace->vo_bit_base_addr = mmtk_get_vo_bit_base_addr();
621 objspace->max_non_los_default_alloc_bytes = mmtk_max_non_los_default_alloc_bytes();
625rb_gc_impl_objspace_free(
void *objspace_ptr)
631rb_gc_impl_ractor_cache_alloc(
void *objspace_ptr,
void *ractor)
634 if (
objspace->live_ractor_cache_count == 0) {
635 mmtk_initialize_collection(ractor);
637 objspace->live_ractor_cache_count++;
640 ccan_list_add(&
objspace->ractor_caches, &cache->list_node);
642 cache->mutator = mmtk_bind_mutator(cache);
643 cache->bump_pointer = mmtk_get_bump_pointer_allocator(cache->mutator);
649rb_gc_impl_objspace_retire_gc(
void *objspace_ptr)
655rb_gc_impl_ractor_cache_free(
void *objspace_ptr,
void *cache_ptr)
660 ccan_list_del(&cache->list_node);
662 mmtk_flush_obj_free_buffer(cache);
664 if (ruby_free_at_exit_p()) {
665 MMTK_ASSERT(
objspace->live_ractor_cache_count > 0);
668 MMTK_ASSERT(
objspace->live_ractor_cache_count > 1);
671 objspace->live_ractor_cache_count--;
673 mmtk_destroy_mutator(cache->mutator);
679zjit_mmtk_gc_stress_p(
void *objspace_ptr)
681 return ((
struct objspace *)objspace_ptr)->gc_stress;
685zjit_mmtk_newobj_tracing_p(
void)
690void rb_gc_impl_set_params(
void *objspace_ptr) { }
692static VALUE gc_verify_internal_consistency(
VALUE self) {
return Qnil; }
695rb_mmtk_align_obj_size(
size_t object_size)
697 return (object_size + MMTk_MIN_OBJ_ALIGN - 1) & ~((size_t)MMTk_MIN_OBJ_ALIGN - 1);
701rb_gc_impl_zjit_new_obj_fastpath(
void *objspace_ptr,
size_t alloc_size,
VALUE flags,
VALUE klass,
707 size_t total_size = rb_mmtk_align_obj_size(alloc_size +
sizeof(
VALUE));
708 size_t object_size = total_size -
sizeof(
VALUE);
709 size_t value_size_shift =
sizeof(
VALUE) == 8 ? 3 : 2;
711 if (total_size >
objspace->max_non_los_default_alloc_bytes)
return false;
715 offsetof(
struct objspace, total_allocated_objects),
725 MMTK_ALLOCATION_SEMANTICS_DEFAULT,
726 (uintptr_t)zjit_mmtk_gc_stress_p,
727 (uintptr_t)zjit_mmtk_newobj_tracing_p,
728 (uintptr_t)mmtk_post_alloc,
729 OBJ_FREE_BUF_CAPACITY - 1,
735 memset(fastpath, 0,
sizeof(*fastpath));
736 fastpath->kind = RB_GC_ZJIT_FASTPATH_MMTK;
737 memcpy(fastpath->data.words, &mmtk_fastpath,
sizeof(mmtk_fastpath));
746rb_gc_impl_zjit_writebarrier_required_offset(
void)
756 VALUE gc_constants = rb_hash_new();
757 rb_hash_aset(gc_constants,
ID2SYM(rb_intern(
"RVALUE_SIZE")),
SIZET2NUM(
sizeof(
struct RBasic) +
sizeof(
VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX])));
759 rb_hash_aset(gc_constants,
ID2SYM(rb_intern(
"RVALUE_OVERHEAD")),
INT2NUM(0));
760 rb_hash_aset(gc_constants,
ID2SYM(rb_intern(
"RVALUE_OLD_AGE")),
761 INT2FIX(strcmp((
const char *)mmtk_plan(),
"StickyImmix") == 0 ? 1 : 0));
763 rb_define_const(
rb_mGC,
"INTERNAL_CONSTANTS", gc_constants);
776rb_mmtk_obj_free_iter_wrapper(
VALUE obj,
void *data)
781 rb_gc_obj_free_vm_weak_references(obj);
792rb_gc_impl_shutdown_free_objects(
void *objspace_ptr)
794 mmtk_set_gc_enabled(
false);
795 each_object(objspace_ptr, rb_mmtk_obj_free_iter_wrapper, objspace_ptr);
796 mmtk_set_gc_enabled(
true);
801rb_gc_impl_start(
void *objspace_ptr,
bool full_mark,
bool immediate_mark,
bool immediate_sweep,
bool compact,
bool global)
803 mmtk_handle_user_collection_request(rb_gc_get_ractor_newobj_cache(),
true, full_mark);
807rb_gc_impl_during_gc_p(
void *objspace_ptr)
814rb_gc_impl_prepare_heap_i(MMTk_ObjectReference obj,
void *d)
816 rb_gc_prepare_heap_process_object((
VALUE)obj);
820rb_gc_impl_prepare_heap(
void *objspace_ptr)
822 mmtk_enumerate_objects(rb_gc_impl_prepare_heap_i, NULL);
826rb_gc_impl_gc_enable(
void *objspace_ptr)
828 mmtk_set_gc_enabled(
true);
832rb_gc_impl_gc_disable(
void *objspace_ptr,
bool finish_current_gc)
834 mmtk_set_gc_enabled(
false);
838rb_gc_impl_user_gc_disabled_set(
void *objspace_ptr,
bool disable)
841 const bool was =
objspace->user_gc_disabled;
842 objspace->user_gc_disabled = disable;
844 if (was != disable) {
845 mmtk_set_gc_enabled(!disable);
846 objspace->user_gc_disabled = disable;
853rb_gc_impl_user_gc_disabled_p(
void *objspace_ptr)
860rb_gc_impl_gc_enabled_p(
void *objspace_ptr)
862 return mmtk_gc_enabled_p();
866rb_gc_impl_stress_set(
void *objspace_ptr,
VALUE flag)
874rb_gc_impl_stress_get(
void *objspace_ptr)
882rb_gc_impl_config_get(
void *objspace_ptr)
884 VALUE hash = rb_hash_new();
889 size_t heap_min = mmtk_heap_min();
897rb_gc_impl_config_set(
void *objspace_ptr,
VALUE hash)
903rb_gc_impl_get_vm_context(
void *objspace_ptr)
916 if (bump_pointer == NULL)
return 0;
918 uintptr_t cursor = bump_pointer->cursor;
921 size_t mask = align - 1;
922 cursor = (cursor + mask) & ~mask;
926 if (cursor > bump_pointer->limit) {
930 VALUE obj = cursor - size;
931 bump_pointer->cursor = cursor;
937obj_can_parallel_free_p(
VALUE obj)
958obj_need_obj_free_p(
VALUE obj)
974 if (cache->obj_free_parallel_count > 0) {
975 mmtk_add_obj_free_candidates(cache->obj_free_parallel_buf,
976 cache->obj_free_parallel_count,
true);
977 cache->obj_free_parallel_count = 0;
979 if (cache->obj_free_non_parallel_count > 0) {
980 mmtk_add_obj_free_candidates(cache->obj_free_non_parallel_buf,
981 cache->obj_free_non_parallel_count,
false);
982 cache->obj_free_non_parallel_count = 0;
989 if (obj_can_parallel_free_p(obj)) {
990 cache->obj_free_parallel_buf[cache->obj_free_parallel_count++] = (MMTk_ObjectReference)obj;
991 if (cache->obj_free_parallel_count >= OBJ_FREE_BUF_CAPACITY) {
992 mmtk_add_obj_free_candidates(cache->obj_free_parallel_buf,
993 cache->obj_free_parallel_count,
true);
994 cache->obj_free_parallel_count = 0;
998 cache->obj_free_non_parallel_buf[cache->obj_free_non_parallel_count++] = (MMTk_ObjectReference)obj;
999 if (cache->obj_free_non_parallel_count >= OBJ_FREE_BUF_CAPACITY) {
1000 mmtk_add_obj_free_candidates(cache->obj_free_non_parallel_buf,
1001 cache->obj_free_non_parallel_count,
false);
1002 cache->obj_free_non_parallel_count = 0;
1010 uintptr_t region_offset = obj >>
objspace->vo_bit_log_region_size;
1011 uintptr_t byte_offset = region_offset / 8;
1012 uintptr_t bit_offset = region_offset % 8;
1013 uintptr_t meta_byte_address =
objspace->vo_bit_base_addr + byte_offset;
1014 uint8_t
byte = 1 << bit_offset;
1015 uint8_t *meta_byte_ptr = (uint8_t*)meta_byte_address;
1016 *meta_byte_ptr |= byte;
1020rb_gc_impl_new_obj(
void *objspace_ptr,
void *cache_ptr,
VALUE klass,
VALUE flags,
bool wb_protected,
size_t alloc_size,
size_t *actual_alloc_size)
1025 if (alloc_size == 0) {
1026 rb_bug(
"rb_gc_impl_new_obj: allocation size out of range (size=%"PRIuSIZE
")", alloc_size);
1030 size_t total_size = rb_mmtk_align_obj_size(alloc_size +
sizeof(
VALUE));
1031 size_t object_size = total_size -
sizeof(
VALUE);
1032 MMTk_AllocationSemantics semantics = total_size >
objspace->max_non_los_default_alloc_bytes
1033 ? MMTK_ALLOCATION_SEMANTICS_LOS
1034 : MMTK_ALLOCATION_SEMANTICS_DEFAULT;
1035 *actual_alloc_size = object_size;
1038 mmtk_handle_user_collection_request(ractor_cache,
false,
false);
1041 VALUE *alloc_obj = semantics == MMTK_ALLOCATION_SEMANTICS_DEFAULT
1042 ? (
VALUE *)rb_mmtk_alloc_fast_path(
objspace, ractor_cache, total_size, MMTk_MIN_OBJ_ALIGN)
1045 alloc_obj = mmtk_alloc(ractor_cache->mutator, total_size, MMTk_MIN_OBJ_ALIGN, 0, semantics);
1048 if (RB_UNLIKELY(alloc_obj == NULL)) {
1054 alloc_obj[-1] = object_size;
1055 alloc_obj[0] = flags;
1056 alloc_obj[1] = klass;
1058 if (RB_UNLIKELY(!wb_protected)) {
1059 mmtk_register_wb_unprotected_object((MMTk_ObjectReference)alloc_obj);
1062 if (semantics == MMTK_ALLOCATION_SEMANTICS_LOS || ractor_cache->bump_pointer == NULL) {
1063 mmtk_post_alloc(ractor_cache->mutator, (
void*)alloc_obj, total_size, semantics);
1067 mmtk_post_alloc_fast_immix(
objspace, ractor_cache, (uintptr_t)alloc_obj);
1070 if (obj_need_obj_free_p((
VALUE)alloc_obj)) {
1071 mmtk_buffer_obj_free_candidate(ractor_cache, (
VALUE)alloc_obj);
1074 objspace->total_allocated_objects++;
1076 return (
VALUE)alloc_obj;
1080rb_gc_impl_obj_slot_size(
VALUE obj)
1082 return ((
VALUE *)obj)[-1];
1086rb_gc_impl_size_slot_size(
void *objspace_ptr,
size_t size)
1089 rb_bug(
"rb_gc_impl_size_slot_size: size too large (size=%"PRIuSIZE
")", size);
1092 return rb_mmtk_align_obj_size(size +
sizeof(
VALUE)) -
sizeof(
VALUE);
1096rb_gc_impl_size_allocatable_p(
size_t size)
1102rb_gc_impl_max_allocation_size(
void)
1109rb_gc_impl_malloc(
void *objspace_ptr,
size_t size,
bool gc_allowed)
1112 return malloc(size);
1116rb_gc_impl_calloc(
void *objspace_ptr,
size_t size,
bool gc_allowed)
1119 return calloc(1, size);
1123rb_gc_impl_realloc(
void *objspace_ptr,
void *ptr,
size_t new_size,
size_t old_size,
bool gc_allowed)
1126 return realloc(ptr, new_size);
1130rb_gc_impl_free(
void *objspace_ptr,
void *ptr,
size_t old_size)
1136void rb_gc_impl_adjust_memory_usage(
void *objspace_ptr, ssize_t diff) { }
1140rb_mmtk_call_object_closure(
VALUE obj,
bool pin)
1143 enum { info_size = 256 };
1144 char obj_info_buf[info_size];
1145 rb_raw_obj_info(obj_info_buf, info_size, obj);
1147 char parent_obj_info_buf[info_size];
1148 rb_raw_obj_info(parent_obj_info_buf, info_size, marking_parent_object);
1150 rb_mmtk_gc_thread_bug(
"try to mark T_NONE object (obj: %s, parent: %s)", obj_info_buf, parent_obj_info_buf);
1155 rb_mmtk_gc_thread_tls->gc_context,
1156 (MMTk_ObjectReference)obj,
1162rb_gc_impl_mark(
void *objspace_ptr,
VALUE obj)
1166 rb_mmtk_call_object_closure(obj,
false);
1170rb_gc_impl_mark_and_move(
void *objspace_ptr,
VALUE *ptr)
1174 VALUE new_obj = rb_mmtk_call_object_closure(*ptr,
false);
1175 if (new_obj != *ptr) {
1181rb_gc_impl_mark_and_pin(
void *objspace_ptr,
VALUE obj)
1185 rb_mmtk_call_object_closure(obj,
true);
1189rb_gc_impl_mark_maybe(
void *objspace_ptr,
VALUE obj)
1191 if (rb_gc_impl_live_object_p(objspace_ptr, (
const void *)obj)) {
1192 rb_gc_impl_mark_and_pin(objspace_ptr, obj);
1197rb_gc_impl_declare_weak_references(
void *objspace_ptr,
VALUE obj)
1200 mmtk_declare_weak_references((MMTk_ObjectReference)obj);
1204rb_gc_impl_handle_weak_references_alive_p(
void *objspace_ptr,
VALUE obj)
1206 return mmtk_weak_references_alive_p((MMTk_ObjectReference)obj);
1211rb_gc_impl_register_pinning_obj(
void *objspace_ptr,
VALUE obj)
1213 mmtk_register_pinning_obj((MMTk_ObjectReference)obj);
1217rb_gc_impl_object_moved_p(
void *objspace_ptr,
VALUE obj)
1219 return rb_mmtk_call_object_closure(obj,
false) != obj;
1223rb_gc_impl_pinned_p(
void *objspace_ptr,
VALUE obj)
1225 return mmtk_is_pinned((MMTk_ObjectReference)obj);
1229rb_gc_impl_location(
void *objspace_ptr,
VALUE obj)
1231 return rb_mmtk_call_object_closure(obj,
false);
1236rb_gc_impl_writebarrier(
void *objspace_ptr,
VALUE a,
VALUE b)
1243 if (!rb_gc_impl_live_object_p(objspace_ptr, (
void *)a)) {
1245 rb_bug(
"a: %s is not an object", rb_raw_obj_info(buff, 256, a));
1248 if (!rb_gc_impl_live_object_p(objspace_ptr, (
void *)b)) {
1250 rb_bug(
"b: %s is not an object", rb_raw_obj_info(buff, 256, b));
1257 mmtk_object_reference_write_post(cache->mutator, (MMTk_ObjectReference)a);
1261rb_gc_impl_writebarrier_unprotect(
void *objspace_ptr,
VALUE obj)
1263 mmtk_register_wb_unprotected_object((MMTk_ObjectReference)obj);
1267rb_gc_impl_obj_became_shareable(
void *objspace_ptr,
VALUE obj)
1273rb_gc_impl_writebarrier_remember(
void *objspace_ptr,
VALUE obj)
1287 mmtk_object_reference_write_post(cache->mutator, (MMTk_ObjectReference)obj);
1292each_objects_i(MMTk_ObjectReference obj,
void *d)
1294 rb_darray(
VALUE) *objs = d;
1296 rb_darray_append(objs, (
VALUE)obj);
1302 rb_darray(
VALUE) objs;
1303 rb_darray_make(&objs, 0);
1305 mmtk_enumerate_objects(each_objects_i, &objs);
1308 rb_darray_foreach(objs, i, obj_ptr) {
1309 if (!mmtk_is_mmtk_object((MMTk_ObjectReference)*obj_ptr))
continue;
1311 if (func(*obj_ptr, data) != 0) {
1316 rb_darray_free(objs);
1320 int (*func)(
void *,
void *, size_t,
void *);
1325rb_gc_impl_each_objects_i(
VALUE obj,
void *d)
1329 size_t slot_size = rb_gc_impl_obj_slot_size(obj);
1331 return data->func((
void *)obj, (
void *)(obj + slot_size), slot_size, data->data);
1335rb_gc_impl_each_objects(
void *objspace_ptr,
int (*func)(
void *,
void *,
size_t,
void *),
void *data)
1342 each_object(objspace_ptr, rb_gc_impl_each_objects_i, &each_objects_data);
1346 void (*func)(
VALUE,
void *);
1351rb_gc_impl_each_object_i(
VALUE obj,
void *d)
1355 data->func(obj, data->data);
1361rb_gc_impl_each_object(
void *objspace_ptr,
void (*func)(
VALUE,
void *),
void *data)
1368 each_object(objspace_ptr, rb_gc_impl_each_object_i, &each_object_data);
1373gc_run_finalizers_get_final(
rb_len_t i,
void *data)
1381gc_run_finalizers(
void *data)
1385 rb_gc_set_pending_interrupt();
1387 while (
objspace->finalizer_jobs != NULL) {
1389 objspace->finalizer_jobs = job->next;
1391 switch (job->kind) {
1392 case MMTK_FINAL_JOB_DFREE:
1393 job->as.dfree.func(job->as.dfree.data);
1395 case MMTK_FINAL_JOB_FINALIZE: {
1396 VALUE finalizer_array = job->as.finalize.finalizer_array;
1398 rb_gc_run_obj_finalizer(
1401 gc_run_finalizers_get_final,
1402 (
void *)finalizer_array
1413 rb_gc_unset_pending_interrupt();
1417rb_gc_impl_make_zombie(
void *objspace_ptr,
VALUE obj,
void (*dfree)(
void *),
void *data)
1419 if (dfree == NULL)
return;
1424 job->kind = MMTK_FINAL_JOB_DFREE;
1425 job->as.dfree.func = dfree;
1426 job->as.dfree.data = data;
1430 job->next =
objspace->finalizer_jobs;
1432 }
while (prev != job->next);
1434 if (!ruby_free_at_exit_p()) {
1440rb_gc_impl_define_finalizer(
void *objspace_ptr,
VALUE obj,
VALUE block)
1448 int lev = RB_GC_VM_LOCK();
1450 if (st_lookup(
objspace->finalizer_table, obj, &data)) {
1451 table = (
VALUE)data;
1458 for (i = 0; i <
len; i++) {
1461 RB_GC_VM_UNLOCK(lev);
1472 st_add_direct(
objspace->finalizer_table, obj, table);
1475 RB_GC_VM_UNLOCK(lev);
1481rb_gc_impl_undefine_finalizer(
void *objspace_ptr,
VALUE obj)
1485 st_data_t data = obj;
1487 int lev = RB_GC_VM_LOCK();
1488 st_delete(
objspace->finalizer_table, &data, 0);
1489 RB_GC_VM_UNLOCK(lev);
1495rb_gc_impl_copy_finalizer(
void *objspace_ptr,
VALUE dest,
VALUE obj)
1503 int lev = RB_GC_VM_LOCK();
1504 if (RB_LIKELY(st_lookup(
objspace->finalizer_table, obj, &data))) {
1507 st_insert(
objspace->finalizer_table, dest, table);
1511 rb_bug(
"rb_gc_copy_finalizer: FL_FINALIZE set but not found in finalizer_table: %s", rb_obj_info(obj));
1513 RB_GC_VM_UNLOCK(lev);
1517move_finalizer_from_table_i(st_data_t key, st_data_t val, st_data_t arg)
1527rb_gc_impl_shutdown_call_finalizer(
void *objspace_ptr)
1531 while (
objspace->finalizer_table->num_entries) {
1532 st_foreach(
objspace->finalizer_table, move_finalizer_from_table_i, (st_data_t)
objspace);
1537 unsigned int lev = RB_GC_VM_LOCK();
1540 ccan_list_for_each(&
objspace->ractor_caches, rc, list_node) {
1541 mmtk_flush_obj_free_buffer(rc);
1545 for (
size_t i = 0; i < registered_candidates.len; i++) {
1546 VALUE obj = (
VALUE)registered_candidates.ptr[i];
1548 if (rb_gc_shutdown_call_finalizer_p(obj)) {
1549 rb_gc_obj_free(objspace_ptr, obj);
1553 mmtk_free_raw_vec_of_obj_ref(registered_candidates);
1555 RB_GC_VM_UNLOCK(lev);
1563rb_gc_impl_before_fork(
void *objspace_ptr)
1568 objspace->fork_hook_vm_lock_lev = RB_GC_VM_LOCK();
1582 if (mutator_blocking_count != 0) {
1583 RB_GC_VM_UNLOCK(
objspace->fork_hook_vm_lock_lev);
1591rb_gc_impl_after_fork(
void *objspace_ptr, rb_pid_t pid)
1595 mmtk_after_fork(rb_gc_get_ractor_newobj_cache());
1597 RB_GC_VM_UNLOCK(
objspace->fork_hook_vm_lock_lev);
1603rb_gc_impl_set_measure_total_time(
void *objspace_ptr,
VALUE flag)
1611rb_gc_impl_get_measure_total_time(
void *objspace_ptr)
1619rb_gc_impl_get_total_time(
void *objspace_ptr)
1627rb_gc_impl_gc_count(
void *objspace_ptr)
1635rb_gc_impl_latest_gc_info(
void *objspace_ptr,
VALUE hash_or_key)
1646 rb_bug(
"gc_info_decode: non-hash or symbol given");
1649#define SET(name, attr) \
1650 if (key == ID2SYM(rb_intern_const(#name))) \
1652 else if (hash != Qnil) \
1653 rb_hash_aset(hash, ID2SYM(rb_intern_const(#name)), (attr));
1670 gc_stat_sym_moving_gc_count,
1672 gc_stat_sym_total_allocated_objects,
1673 gc_stat_sym_total_bytes,
1674 gc_stat_sym_used_bytes,
1675 gc_stat_sym_free_bytes,
1676 gc_stat_sym_starting_heap_address,
1677 gc_stat_sym_last_heap_address,
1678 gc_stat_sym_weak_references_count,
1682static VALUE gc_stat_symbols[gc_stat_sym_last];
1685setup_gc_stat_symbols(
void)
1687 if (gc_stat_symbols[0] == 0) {
1688#define S(s) gc_stat_symbols[gc_stat_sym_##s] = ID2SYM(rb_intern_const(#s))
1692 S(total_allocated_objects);
1696 S(starting_heap_address);
1697 S(last_heap_address);
1698 S(weak_references_count);
1703rb_gc_impl_stat(
void *objspace_ptr,
VALUE hash_or_sym)
1705 if (objspace_ptr == NULL) {
1712 setup_gc_stat_symbols();
1721 rb_bug(
"non-hash or symbol given");
1724#define SET(name, attr) \
1725 if (key == gc_stat_symbols[gc_stat_sym_##name]) \
1726 return SIZET2NUM(attr); \
1727 else if (hash != Qnil) \
1728 rb_hash_aset(hash, gc_stat_symbols[gc_stat_sym_##name], SIZET2NUM(attr));
1731 SET(moving_gc_count,
objspace->moving_gc_count);
1732 SET(time,
objspace->total_gc_time / (1000 * 1000));
1733 SET(total_allocated_objects,
objspace->total_allocated_objects);
1734 SET(total_bytes, mmtk_total_bytes());
1735 SET(used_bytes, mmtk_used_bytes());
1736 SET(free_bytes, mmtk_free_bytes());
1737 SET(starting_heap_address, (
size_t)mmtk_starting_heap_address());
1738 SET(last_heap_address, (
size_t)mmtk_last_heap_address());
1739 SET(weak_references_count, mmtk_weak_references_count());
1751rb_gc_impl_stat_heap(
void *objspace_ptr,
VALUE heap_name,
VALUE hash_or_sym)
1762#define RB_GC_OBJECT_METADATA_ENTRY_COUNT 2
1766rb_gc_impl_object_metadata(
void *objspace_ptr,
VALUE obj)
1768 static ID ID_wb_protected;
1769 static ID ID_object_id;
1771 if (!ID_object_id) {
1772#define I(s) ID_##s = rb_intern(#s);
1780#define SET_ENTRY(na, v) do { \
1781 MMTK_ASSERT(n <= RB_GC_OBJECT_METADATA_ENTRY_COUNT); \
1782 object_metadata_entries[n].name = ID_##na; \
1783 object_metadata_entries[n].val = v; \
1787 if (!mmtk_object_wb_unprotected_p((MMTk_ObjectReference)obj)) SET_ENTRY(wb_protected,
Qtrue);
1788 if (rb_obj_id_p(obj)) SET_ENTRY(object_id, rb_obj_id(obj));
1790 object_metadata_entries[n].name = 0;
1791 object_metadata_entries[n].val = 0;
1793 return object_metadata_entries;
1797rb_gc_impl_live_object_p(
void *objspace_ptr,
const void *ptr)
1799 if (ptr == NULL)
return false;
1800 if ((uintptr_t)ptr %
sizeof(
void*) != 0)
return false;
1801 return mmtk_is_mmtk_object((MMTk_Address)ptr);
1805rb_gc_impl_garbage_object_p(
void *objspace_ptr,
VALUE obj)
1810void rb_gc_impl_set_event_hook(
void *objspace_ptr,
const rb_event_flag_t event) { }
1813rb_gc_impl_copy_attributes(
void *objspace_ptr,
VALUE dest,
VALUE obj)
1815 if (mmtk_object_wb_unprotected_p((MMTk_ObjectReference)obj)) {
1816 rb_gc_impl_writebarrier_unprotect(objspace_ptr, dest);
1819 rb_gc_impl_copy_finalizer(objspace_ptr, dest, obj);
1825rb_gc_impl_active_gc_name(
void)
1831rb_gc_impl_during_global_gc_p(
void *objspace_ptr)
1841rb_gc_impl_during_postmortem_p(
void *objspace_ptr)
1848rb_gc_impl_obj_foreign_p(
void *objspace_ptr,
VALUE obj)
1855rb_gc_impl_shref_marked_p(
void *objspace_ptr,
VALUE obj)
1862rb_gc_impl_objspace_zombify(
void *objspace_ptr)
1868rb_gc_impl_objspace_absorb(
void *dst_ptr,
void *src_ptr)
1874rb_gc_impl_gc_rest(
void *objspace_ptr)
1881 int (*func)(
void *,
void *, size_t,
void *);
1886each_objects_shareable_i(
void *start,
void *end,
size_t stride,
void *d)
1891 int ret = data->func((
void *)obj, (
void *)(obj + stride), stride, data->data);
1892 if (ret)
return ret;
1899rb_gc_impl_each_objects_shareable(
void *objspace_ptr,
int (*func)(
void *,
void *,
size_t,
void *),
void *data)
1902 rb_gc_impl_each_objects(objspace_ptr, each_objects_shareable_i, &d);
1906rb_gc_impl_each_objects_foreign(
void *objspace_ptr,
int (*func)(
void *,
void *,
size_t,
void *),
void *data)
#define RUBY_ATOMIC_INC(var)
Atomically increments the value pointed by var.
#define RUBY_ATOMIC_PTR_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are void*.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_DEC(var)
Atomically decrements the value pointed by var.
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
unsigned int rb_postponed_job_handle_t
The type of a handle returned from rb_postponed_job_preregister and passed to rb_postponed_job_trigge...
void rb_postponed_job_trigger(rb_postponed_job_handle_t h)
Triggers a pre-registered job registered with rb_postponed_job_preregister, scheduling it for executi...
rb_postponed_job_handle_t rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
Pre-registers a func in Ruby's postponed job preregistration table, returning an opaque handle which ...
#define RUBY_INTERNAL_EVENT_FREEOBJ
Object swept.
#define RUBY_INTERNAL_EVENT_GC_START
GC started.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_INTERNAL_EVENT_NEWOBJ
Object allocated.
static VALUE RB_FL_TEST(VALUE obj, VALUE flags)
Tests if the given flag(s) are set or not.
static VALUE RB_FL_TEST_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_TEST().
static void RB_FL_SET(VALUE obj, VALUE flags)
Sets the given flag(s).
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
@ RUBY_FL_FINALIZE
This flag has something to do with finalisers.
@ RUBY_FL_WEAK_REFERENCE
This object weakly refers to other objects.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define T_NONE
Old name of RUBY_T_NONE.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define xmalloc
Old name of ruby_xmalloc.
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_SET
Old name of RB_FL_SET.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_TEST
Old name of RB_FL_TEST.
#define FL_UNSET
Old name of RB_FL_UNSET.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_ary_dup(VALUE ary)
Duplicates an array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
Raises rb_eNotImpError.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
int len
Length of the buffer.
#define RB_ULONG2NUM
Just another name of rb_ulong2num_inline.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
#define RBASIC(obj)
Convenient casting macro.
int ruby_native_thread_p(void)
Queries if the thread which calls this function is a ruby's thread.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
void * rust_closure
The pointer to the Rust-level closure object.
MMTk_ObjectClosureFunction c_function
The function to be called from C.
Ruby object's base components.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.