@@ -187,13 +187,15 @@ static bool sweep_span(mcentral_t *central, mspan_t *span) {
187187 allocated_bytes -= span -> obj_size ;
188188 has_freed = true;
189189
190- DEBUGF ("[sweep_span] will sweep, span_base=%p obj_addr=%p" , span -> base , (void * ) (span -> base + i * span -> obj_size ));
190+ DEBUGF ("[sweep_span] will sweep, span_base=%p obj_addr=%p" , span -> base ,
191+ (void * ) (span -> base + i * span -> obj_size ));
191192 // memset((void *) (span->base + i * span->obj_size), 0, span->obj_size);
192193 } else {
193- DEBUGF ("[sweep_span] will sweep, span_base=%p, obj_addr=%p, not calc allocated_bytes, alloc_bit=%d, gcmark_bit=%d" ,
194- span -> base ,
195- (void * ) (span -> base + i * span -> obj_size ), bitmap_test (span -> alloc_bits , i ),
196- bitmap_test (span -> gcmark_bits , i ));
194+ DEBUGF (
195+ "[sweep_span] will sweep, span_base=%p, obj_addr=%p, not calc allocated_bytes, alloc_bit=%d, gcmark_bit=%d" ,
196+ span -> base ,
197+ (void * ) (span -> base + i * span -> obj_size ), bitmap_test (span -> alloc_bits , i ),
198+ bitmap_test (span -> gcmark_bits , i ));
197199 }
198200 }
199201
@@ -330,11 +332,11 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
330332 addr_t sp_value = (addr_t ) co -> aco .reg [ACO_REG_IDX_SP ];
331333
332334 DEBUGF (
333- "[runtime_gc.scan_stack] start, p_index=%d(%lu), p_status=%d, co=%p, co_status=%d, co_stack_size=%zu, save_stack=%p(%zu), "
334- "bp_value=%p, sp_value=%p, share_stack.base=%p" ,
335- p -> index , (uint64_t ) p -> thread_id , p -> status , co , co -> status , co -> aco .save_stack .valid_sz ,
336- co -> aco .save_stack .ptr ,
337- co -> aco .save_stack .sz , bp_value , sp_value , co -> aco .share_stack -> align_retptr );
335+ "[runtime_gc.scan_stack] start, p_index=%d(%lu), p_status=%d, co=%p, co_status=%d, co_stack_size=%zu, save_stack=%p(%zu), "
336+ "bp_value=%p, sp_value=%p, share_stack.base=%p" ,
337+ p -> index , (uint64_t ) p -> thread_id , p -> status , co , co -> status , co -> aco .save_stack .valid_sz ,
338+ co -> aco .save_stack .ptr ,
339+ co -> aco .save_stack .sz , bp_value , sp_value , co -> aco .share_stack -> align_retptr );
338340
339341 // save_stack 也是通过 gc 申请,即使是 gc_work 也需要标记一下
340342 assert (p -> gc_work_finished < memory -> gc_count && "gc work finished, cannot insert to gc worklist" );
@@ -352,8 +354,8 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
352354 insert_gc_worklist (worklist , co -> error );
353355 }
354356
355- if (co -> traces ) {
356- insert_gc_worklist (worklist , co -> traces );
357+ if (co -> traces . data && span_of (( addr_t ) co -> traces . data ) ) {
358+ insert_gc_worklist (worklist , co -> traces . data );
357359 }
358360
359361 if (co -> flag & FLAG (CO_FLAG_RTFN )) {
@@ -408,7 +410,9 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
408410
409411 if (ret_addr == assist_preempt_yield_ret_addr ) {
410412 found_assist = true;
411- DEBUGF ("[runtime_gc.scan_stack] find assist_preempt_yield_ret_addr %p, conservative treatment will be carried out" , (void * ) assist_preempt_yield_ret_addr );
413+ DEBUGF (
414+ "[runtime_gc.scan_stack] find assist_preempt_yield_ret_addr %p, conservative treatment will be carried out" ,
415+ (void * ) assist_preempt_yield_ret_addr );
412416 break ;
413417 }
414418 // check prev value is nature fn
@@ -489,8 +493,9 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
489493
490494 fndef_t * fn = find_fn (ret_addr , p );
491495 if (!fn ) {
492- DEBUGF ("[runtime_gc.scan_stack] fn not found by ret_addr, return_addr=%p, share_stack_frame_bp=%p, bp_offset = %ld" ,
493- (void * ) ret_addr , (void * ) share_stack_frame_bp , share_stack_frame_bp - sp_value );
496+ DEBUGF (
497+ "[runtime_gc.scan_stack] fn not found by ret_addr, return_addr=%p, share_stack_frame_bp=%p, bp_offset = %ld" ,
498+ (void * ) ret_addr , (void * ) share_stack_frame_bp , share_stack_frame_bp - sp_value );
494499
495500 // next and continue
496501 addr_t bp_offset = share_stack_frame_bp - sp_value ;
@@ -591,11 +596,12 @@ static void handle_gc_ptr(n_processor_t *p, addr_t addr) {
591596 }
592597
593598 bitmap_set (span -> gcmark_bits , obj_index );
594- DEBUGF ("[runtime_gc.handle_gc_ptr] addr=%p, span=%p, span_base=%p, obj_index=%lu marked, test=%d, obj_size=%d, spanclass_has_ptr=%d" ,
595- (void * ) addr ,
596- span ,
597- (void * ) span -> base ,
598- obj_index , bitmap_test (span -> gcmark_bits , obj_index ), span -> obj_size , spanclass_has_ptr (span -> spanclass ));
599+ DEBUGF (
600+ "[runtime_gc.handle_gc_ptr] addr=%p, span=%p, span_base=%p, obj_index=%lu marked, test=%d, obj_size=%d, spanclass_has_ptr=%d" ,
601+ (void * ) addr ,
602+ span ,
603+ (void * ) span -> base ,
604+ obj_index , bitmap_test (span -> gcmark_bits , obj_index ), span -> obj_size , spanclass_has_ptr (span -> spanclass ));
599605
600606 mutex_unlock (& span -> gcmark_locker );
601607
@@ -621,9 +627,9 @@ static void handle_gc_ptr(n_processor_t *p, addr_t addr) {
621627 addr_t value = fetch_addr_value (temp_addr );
622628
623629 DEBUGF (
624- "[handle_gc_ptr] addr is ptr,base=%p cursor=%p cursor_value=%p, obj_size=%ld, bit_index=%lu, in_heap=%d" ,
625- (void * ) addr ,
626- (void * ) temp_addr , (void * ) value , span -> obj_size , bit_index , in_heap (value ));
630+ "[handle_gc_ptr] addr is ptr,base=%p cursor=%p cursor_value=%p, obj_size=%ld, bit_index=%lu, in_heap=%d" ,
631+ (void * ) addr ,
632+ (void * ) temp_addr , (void * ) value , span -> obj_size , bit_index , in_heap (value ));
627633
628634 if (span_of (value )) {
629635 // assert(span_of(heap_addr) && "heap_addr not belong active span");
@@ -640,9 +646,9 @@ static void handle_gc_ptr(n_processor_t *p, addr_t addr) {
640646 }
641647 } else {
642648 DEBUGF (
643- "[handle_gc_ptr] addr not ptr,base=%p cursor=%p cursor_value(int)=%p, obj_size=%ld, bit_index=%lu" ,
644- (void * ) addr ,
645- (void * ) temp_addr , fetch_int_value (temp_addr , POINTER_SIZE ), span -> obj_size , bit_index );
649+ "[handle_gc_ptr] addr not ptr,base=%p cursor=%p cursor_value(int)=%p, obj_size=%ld, bit_index=%lu" ,
650+ (void * ) addr ,
651+ (void * ) temp_addr , fetch_int_value (temp_addr , POINTER_SIZE ), span -> obj_size , bit_index );
646652 }
647653 }
648654}
@@ -703,10 +709,10 @@ static void gc_work() {
703709 current = current -> succ ;
704710
705711 DEBUGF (
706- "[runtime_gc.gc_work] will scan_stack p_index=%d, co=%p, status=%d, is_main=%d, gc_black=%lu/gc_count=%lu, aco=%p" ,
707- share_p -> index ,
708- wait_co , wait_co -> status , wait_co -> main , wait_co -> gc_black , memory -> gc_count ,
709- & wait_co -> aco );
712+ "[runtime_gc.gc_work] will scan_stack p_index=%d, co=%p, status=%d, is_main=%d, gc_black=%lu/gc_count=%lu, aco=%p" ,
713+ share_p -> index ,
714+ wait_co , wait_co -> status , wait_co -> main , wait_co -> gc_black , memory -> gc_count ,
715+ & wait_co -> aco );
710716
711717 if (wait_co -> status == CO_STATUS_DEAD ) {
712718 DEBUGF ("[runtime_gc.gc_work] co=%p, main=%d, status=dead, will remove" ,
@@ -820,7 +826,9 @@ static void scan_pool() {
820826 n_processor_t * p = processor_index [0 ];
821827 n_string_t * value ;
822828 sc_map_foreach_value (& const_str_pool , value ) {
823- rt_linked_fixalloc_push (& p -> gc_worklist , value );
829+ if (value && span_of ((addr_t ) value -> data )) {
830+ rt_linked_fixalloc_push (& p -> gc_worklist , value -> data );
831+ }
824832 }
825833}
826834
@@ -859,7 +867,8 @@ static void scan_global() {
859867 gc_bits = (uint8_t * ) & rtype -> gc_bits ;
860868 }
861869
862- if (bitmap_test (gc_bits , index )) { // need gc
870+ if (bitmap_test (gc_bits , index )) {
871+ // need gc
863872 addr_t addr = fetch_addr_value (current );
864873 if (span_of (addr )) {
865874 DEBUGF ("[runtime.scan_global] name=%s, kind=%s, base=%p(%p), index=%d, addr=%p need gc" ,
@@ -1010,8 +1019,10 @@ void runtime_gc() {
10101019 next_gc_bytes = heap_live + (heap_live * GC_PERCENT / 100 );
10111020
10121021 gc_stage = GC_STAGE_OFF ;
1013- DEBUGF ("[runtime_gc] gc stage: GC_OFF, gc_barrier_stop, before=%ldKB, current=%ldKB, cleanup=%ldKB, alloc_total=%ldKB, remove_to_sys=%ldKB, used=%ldKB, next_gc=%ldKB" ,
1014- before / 1024 ,
1015- allocated_bytes / 1024 ,
1016- (before - allocated_bytes ) / 1024 , allocated_total_bytes / 1024 , remove_total_bytes / 1024 , (allocated_total_bytes - remove_total_bytes ) / 1024 , next_gc_bytes / 1024 );
1022+ DEBUGF (
1023+ "[runtime_gc] gc stage: GC_OFF, gc_barrier_stop, before=%ldKB, current=%ldKB, cleanup=%ldKB, alloc_total=%ldKB, remove_to_sys=%ldKB, used=%ldKB, next_gc=%ldKB" ,
1024+ before / 1024 ,
1025+ allocated_bytes / 1024 ,
1026+ (before - allocated_bytes ) / 1024 , allocated_total_bytes / 1024 , remove_total_bytes / 1024 ,
1027+ (allocated_total_bytes - remove_total_bytes ) / 1024 , next_gc_bytes / 1024 );
10171028}
0 commit comments