Skip to content

Commit fd492ed

Browse files
Fix spurious uses of BASE_SLOT_SIZE
In gc_sweep_plane, VALGRIND_MAKE_MEM_UNDEFINED was using BASE_SLOT_SIZE which only covers the smallest pool's slot size. For larger size pools this left the tail of the slot with stale state. Use the page's actual slot_size instead. In gc_prof_set_heap_info, heap_use_size and heap_total_size were computed as object_count * BASE_SLOT_SIZE, undercounting memory for objects in larger size pools. Sum across all heaps using each pool's actual slot size for correct byte totals.
1 parent 9ebef81 commit fd492ed

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

gc/default/default.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3546,7 +3546,7 @@ gc_sweep_plane(rb_objspace_t *objspace, rb_heap_t *heap, uintptr_t p, bits_t bit
35463546
rb_gc_event_hook(vp, RUBY_INTERNAL_EVENT_FREEOBJ);
35473547
}
35483548

3549-
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)p, BASE_SLOT_SIZE);
3549+
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)p, slot_size);
35503550
heap_page_add_freeobj(objspace, sweep_page, vp);
35513551
gc_report(3, objspace, "page_sweep: %s (fast path) added to freelist\n", rb_obj_info(vp));
35523552
ctx->freed_slots++;
@@ -3558,7 +3558,7 @@ gc_sweep_plane(rb_objspace_t *objspace, rb_heap_t *heap, uintptr_t p, bits_t bit
35583558

35593559
rb_gc_obj_free_vm_weak_references(vp);
35603560
if (rb_gc_obj_free(objspace, vp)) {
3561-
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)p, BASE_SLOT_SIZE);
3561+
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)p, slot_size);
35623562
heap_page_add_freeobj(objspace, sweep_page, vp);
35633563
gc_report(3, objspace, "page_sweep: %s is added to freelist\n", rb_obj_info(vp));
35643564
ctx->freed_slots++;
@@ -8647,18 +8647,29 @@ gc_prof_set_heap_info(rb_objspace_t *objspace)
86478647
{
86488648
if (gc_prof_enabled(objspace)) {
86498649
gc_profile_record *record = gc_prof_record(objspace);
8650-
size_t live = objspace->profile.total_allocated_objects_at_gc_start - total_freed_objects(objspace);
8651-
size_t total = objspace->profile.heap_total_slots_at_gc_start;
8650+
8651+
/* Sum across all size pools since each has a different slot size. */
8652+
size_t total = 0;
8653+
size_t use_size = 0;
8654+
size_t total_size = 0;
8655+
for (int i = 0; i < HEAP_COUNT; i++) {
8656+
rb_heap_t *heap = &heaps[i];
8657+
size_t heap_live = heap->total_allocated_objects - heap->total_freed_objects - heap->final_slots_count;
8658+
total += heap->total_slots;
8659+
use_size += heap_live * heap->slot_size;
8660+
total_size += heap->total_slots * heap->slot_size;
8661+
}
86528662

86538663
#if GC_PROFILE_MORE_DETAIL
8664+
size_t live = objspace->profile.total_allocated_objects_at_gc_start - total_freed_objects(objspace);
86548665
record->heap_use_pages = objspace->profile.heap_used_at_gc_start;
86558666
record->heap_live_objects = live;
86568667
record->heap_free_objects = total - live;
86578668
#endif
86588669

86598670
record->heap_total_objects = total;
8660-
record->heap_use_size = live * BASE_SLOT_SIZE;
8661-
record->heap_total_size = total * BASE_SLOT_SIZE;
8671+
record->heap_use_size = use_size;
8672+
record->heap_total_size = total_size;
86628673
}
86638674
}
86648675

0 commit comments

Comments
 (0)