2 * stop and copy GC based on Cheney's algorithm
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
18 #include <sys/resource.h>
24 #include "gc-internal.h"
26 #include "interrupt.h"
30 #include "genesis/static-symbols.h"
31 #include "genesis/primitive-objects.h"
34 /* So you need to debug? */
37 #define DEBUG_SPACE_PREDICATES
38 #define DEBUG_SCAVENGE_VERBOSE
39 #define DEBUG_COPY_VERBOSE
44 lispobj *from_space_free_pointer;
47 lispobj *new_space_free_pointer;
49 static void scavenge_newspace(void);
52 /* collecting garbage */
56 tv_diff(struct timeval *x, struct timeval *y)
58 return (((double) x->tv_sec + (double) x->tv_usec * 1.0e-6) -
59 ((double) y->tv_sec + (double) y->tv_usec * 1.0e-6));
63 #define BYTES_ZERO_BEFORE_END (1<<12)
65 /* FIXME do we need this? Doesn't it duplicate lisp code in
66 * scrub-control-stack? */
71 lispobj *ptr = current_control_stack_pointer;
77 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
82 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
89 gc_general_alloc(long bytes, int unboxed_p, int quick_p) {
90 lispobj *new=new_space_free_pointer;
91 new_space_free_pointer+=(bytes/N_WORD_BYTES);
95 lispobj copy_large_unboxed_object(lispobj object, long nwords) {
96 return copy_object(object,nwords);
98 lispobj copy_unboxed_object(lispobj object, long nwords) {
99 return copy_object(object,nwords);
101 lispobj copy_large_object(lispobj object, long nwords) {
102 return copy_object(object,nwords);
105 /* Note: The generic GC interface we're implementing passes us a
106 * last_generation argument. That's meaningless for us, since we're
107 * not a generational GC. So we ignore it. */
109 collect_garbage(generation_index_t ignore)
112 struct timeval start_tv, stop_tv;
113 struct rusage start_rusage, stop_rusage;
114 double real_time, system_time, user_time;
115 double percent_retained, gc_rate;
116 unsigned long size_discarded;
118 unsigned long size_retained;
119 lispobj *current_static_space_free_pointer;
120 unsigned long static_space_size;
121 unsigned long control_stack_size, binding_stack_size;
123 struct thread *th=arch_os_get_current_thread();
126 printf("[Collecting garbage ... \n");
128 getrusage(RUSAGE_SELF, &start_rusage);
129 gettimeofday(&start_tv, (struct timezone *) 0);
132 /* it's possible that signals are blocked already if this was called
133 * from a signal handler (e.g. with the sigsegv gc_trigger stuff) */
135 sigaddset_blockable(&tmp);
136 thread_sigmask(SIG_BLOCK, &tmp, &old);
138 current_static_space_free_pointer =
139 (lispobj *) ((unsigned long)
140 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
143 /* Set up from space and new space pointers. */
145 from_space = current_dynamic_space;
146 from_space_free_pointer = dynamic_space_free_pointer;
149 fprintf(stderr,"from_space = %lx\n",
150 (unsigned long) current_dynamic_space);
152 if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
153 new_space = (lispobj *)DYNAMIC_1_SPACE_START;
154 else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
155 new_space = (lispobj *) DYNAMIC_0_SPACE_START;
157 lose("GC lossage. Current dynamic space is bogus!\n");
159 new_space_free_pointer = new_space;
161 /* Initialize the weak pointer list. */
162 weak_pointers = (struct weak_pointer *) NULL;
165 /* Scavenge all of the roots. */
167 printf("Scavenging interrupt contexts ...\n");
169 scavenge_interrupt_contexts();
172 printf("Scavenging interrupt handlers (%d bytes) ...\n",
173 (int)sizeof(interrupt_handlers));
175 scavenge((lispobj *) interrupt_handlers,
176 sizeof(interrupt_handlers) / sizeof(lispobj));
178 /* _size quantities are in units of sizeof(lispobj) - i.e. 4 */
180 current_control_stack_pointer-
181 (lispobj *)th->control_stack_start;
183 printf("Scavenging the control stack at %p (%ld words) ...\n",
184 ((lispobj *)th->control_stack_start),
187 scavenge(((lispobj *)th->control_stack_start), control_stack_size);
191 current_binding_stack_pointer -
192 (lispobj *)th->binding_stack_start;
194 printf("Scavenging the binding stack %x - %x (%d words) ...\n",
195 th->binding_stack_start,current_binding_stack_pointer,
196 (int)(binding_stack_size));
198 scavenge(((lispobj *)th->binding_stack_start), binding_stack_size);
201 current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
203 printf("Scavenging static space %x - %x (%d words) ...\n",
204 STATIC_SPACE_START,current_static_space_free_pointer,
205 (int)(static_space_size));
207 scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
209 /* Scavenge newspace. */
211 printf("Scavenging new space (%d bytes) ...\n",
212 (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
217 #if defined(DEBUG_PRINT_GARBAGE)
218 print_garbage(from_space, from_space_free_pointer);
221 /* Scan the weak pointers. */
223 printf("Scanning weak hash tables ...\n");
225 scan_weak_hash_tables();
227 /* Scan the weak pointers. */
229 printf("Scanning weak pointers ...\n");
231 scan_weak_pointers();
235 printf("Flipping spaces ...\n");
238 /* Maybe FIXME: it's possible that we could significantly reduce
239 * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
240 * similar os-dependent tricks here */
241 os_zero((os_vm_address_t) from_space,
242 (os_vm_size_t) dynamic_space_size);
244 current_dynamic_space = new_space;
245 dynamic_space_free_pointer = new_space_free_pointer;
248 size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
250 size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
252 os_flush_icache((os_vm_address_t)new_space, size_retained);
256 printf("Zeroing empty part of control stack ...\n");
259 set_auto_gc_trigger(size_retained+bytes_consed_between_gcs);
260 thread_sigmask(SIG_SETMASK, &old, 0);
264 gettimeofday(&stop_tv, (struct timezone *) 0);
265 getrusage(RUSAGE_SELF, &stop_rusage);
269 percent_retained = (((float) size_retained) /
270 ((float) size_discarded)) * 100.0;
272 printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
273 size_retained, size_discarded, percent_retained);
275 real_time = tv_diff(&stop_tv, &start_tv);
276 user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
277 system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
279 printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
280 real_time, user_time, system_time);
282 gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
284 printf("%10.2f M bytes/sec collected.\n", gc_rate);
292 scavenge_newspace(void)
294 lispobj *here, *next;
297 while (here < new_space_free_pointer) {
298 /* printf("here=%lx, new_space_free_pointer=%lx\n",
299 here,new_space_free_pointer); */
300 next = new_space_free_pointer;
301 scavenge(here, next - here);
302 scav_weak_hash_tables();
305 /* printf("done with newspace\n"); */
308 /* scavenging interrupt contexts */
310 static int boxed_registers[] = BOXED_REGISTERS;
313 scavenge_interrupt_context(os_context_t *context)
318 unsigned long lip_offset;
319 int lip_register_pair;
321 unsigned long pc_code_offset;
322 #ifdef ARCH_HAS_LINK_REGISTER
323 unsigned long lr_code_offset;
325 #ifdef ARCH_HAS_NPC_REGISTER
326 unsigned long npc_code_offset;
328 #ifdef DEBUG_SCAVENGE_VERBOSE
329 fprintf(stderr, "Scavenging interrupt context at 0x%x\n",context);
331 /* Find the LIP's register pair and calculate its offset */
332 /* before we scavenge the context. */
334 lip = *os_context_register_addr(context, reg_LIP);
335 /* 0x7FFFFFFF on 32-bit platforms;
336 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
337 lip_offset = (((unsigned long)1) << (N_WORD_BITS - 1)) - 1;
338 lip_register_pair = -1;
339 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
344 index = boxed_registers[i];
345 reg = *os_context_register_addr(context, index);
346 /* would be using PTR if not for integer length issues */
347 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
349 if (offset < lip_offset) {
351 lip_register_pair = index;
357 /* Compute the PC's offset from the start of the CODE */
360 *os_context_pc_addr(context) -
361 *os_context_register_addr(context, reg_CODE);
362 #ifdef ARCH_HAS_NPC_REGISTER
364 *os_context_npc_addr(context) -
365 *os_context_register_addr(context, reg_CODE);
367 #ifdef ARCH_HAS_LINK_REGISTER
369 *os_context_lr_addr(context) -
370 *os_context_register_addr(context, reg_CODE);
373 /* Scavenge all boxed registers in the context. */
374 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
378 index = boxed_registers[i];
379 foo = *os_context_register_addr(context,index);
380 scavenge((lispobj *) &foo, 1);
381 *os_context_register_addr(context,index) = foo;
383 /* this is unlikely to work as intended on bigendian
384 * 64 bit platforms */
387 os_context_register_addr(context, index), 1);
392 *os_context_register_addr(context, reg_LIP) =
393 *os_context_register_addr(context, lip_register_pair) + lip_offset;
396 /* Fix the PC if it was in from space */
397 if (from_space_p(*os_context_pc_addr(context)))
398 *os_context_pc_addr(context) =
399 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
400 #ifdef ARCH_HAS_LINK_REGISTER
401 /* Fix the LR ditto; important if we're being called from
402 * an assembly routine that expects to return using blr, otherwise
404 if (from_space_p(*os_context_lr_addr(context)))
405 *os_context_lr_addr(context) =
406 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
409 #ifdef ARCH_HAS_NPC_REGISTER
410 if (from_space_p(*os_context_npc_addr(context)))
411 *os_context_npc_addr(context) =
412 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
416 void scavenge_interrupt_contexts(void)
419 os_context_t *context;
421 struct thread *th=arch_os_get_current_thread();
423 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
426 #ifdef DEBUG_SCAVENGE_VERBOSE
427 fprintf(stderr, "%d interrupt contexts to scan\n",index);
429 for (i = 0; i < index; i++) {
430 context = th->interrupt_contexts[i];
431 scavenge_interrupt_context(context);
439 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
442 int total_words_not_copied;
444 printf("Scanning from space ...\n");
446 total_words_not_copied = 0;
448 while (start < from_space_free_pointer) {
450 int forwardp, type, nwords;
454 forwardp = is_lisp_pointer(object) && new_space_p(object);
460 tag = lowtag_of(object);
463 case LIST_POINTER_LOWTAG:
466 case INSTANCE_POINTER_LOWTAG:
467 printf("Don't know about instances yet!\n");
470 case FUN_POINTER_LOWTAG:
473 case OTHER_POINTER_LOWTAG:
474 pointer = (lispobj *) native_pointer(object);
476 type = widetag_of(header);
477 nwords = (sizetab[type])(pointer);
479 default: nwords=1; /* shut yer whinging, gcc */
482 type = widetag_of(object);
483 nwords = (sizetab[type])(start);
484 total_words_not_copied += nwords;
485 printf("%4d words not copied at 0x%16lx; ",
486 nwords, (unsigned long) start);
487 printf("Header word is 0x%08x\n",
488 (unsigned int) object);
492 printf("%d total words not copied.\n", total_words_not_copied);
498 #define WEAK_POINTER_NWORDS \
499 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
502 scav_weak_pointer(lispobj *where, lispobj object)
504 /* Do not let GC scavenge the value slot of the weak pointer */
505 /* (that is why it is a weak pointer). Note: we could use */
506 /* the scav_unboxed method here. */
508 return WEAK_POINTER_NWORDS;
512 search_read_only_space(void *pointer)
514 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
515 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
516 if ((pointer < (void *)start) || (pointer >= (void *)end))
518 return (gc_search_space(start,
519 (((lispobj *)pointer)+2)-start,
520 (lispobj *)pointer));
524 search_static_space(void *pointer)
526 lispobj* start = (lispobj*)STATIC_SPACE_START;
527 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
528 if ((pointer < (void *)start) || (pointer >= (void *)end))
530 return (gc_search_space(start,
531 (((lispobj *)pointer)+2)-start,
532 (lispobj *)pointer));
536 search_dynamic_space(void *pointer)
538 lispobj *start = (lispobj *) current_dynamic_space;
539 lispobj *end = (lispobj *) dynamic_space_free_pointer;
540 if ((pointer < (void *)start) || (pointer >= (void *)end))
542 return (gc_search_space(start,
543 (((lispobj *)pointer)+2)-start,
544 (lispobj *)pointer));
547 /* initialization. if gc_init can be moved to after core load, we could
548 * combine these two functions */
554 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
558 gc_initialize_pointers(void)
560 /* FIXME: We do nothing here. We (briefly) misguidedly attempted
561 to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
562 forgetting that (a) actually it could be the other and (b) it's
563 set in coreparse.c anyway. There's a FIXME note left here to
564 note that current_dynamic_space is a violation of OAOO: we can
565 tell which dynamic space we're currently in by looking at
566 dynamic_space_free_pointer. -- CSR, 2002-08-09 */
572 /* noise to manipulate the gc trigger stuff */
574 /* Functions that substantially change the dynamic space free pointer
575 * (collect_garbage, purify) are responsible also for resetting the
577 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
579 os_vm_address_t addr;
582 addr = os_round_up_to_page((os_vm_address_t)current_dynamic_space
584 if (addr < (os_vm_address_t)dynamic_space_free_pointer)
585 lose("set_auto_gc_trigger: tried to set gc trigger too low! (%ld < 0x%08lx)\n",
586 (unsigned long)dynamic_usage,
587 (unsigned long)((os_vm_address_t)dynamic_space_free_pointer
588 - (os_vm_address_t)current_dynamic_space));
590 length = os_trunc_size_to_page(dynamic_space_size - dynamic_usage);
592 lose("set_auto_gc_trigger: tried to set gc trigger too high! (0x%08lx)\n",
593 (unsigned long)dynamic_usage);
595 #if defined(SUNOS) || defined(SOLARIS)
596 os_invalidate(addr, length);
598 os_protect(addr, length, 0);
601 current_auto_gc_trigger = (lispobj *)addr;
604 void clear_auto_gc_trigger(void)
606 os_vm_address_t addr;
609 if (current_auto_gc_trigger == NULL)
612 addr = (os_vm_address_t)current_auto_gc_trigger;
613 length = dynamic_space_size + (os_vm_address_t)current_dynamic_space - addr;
615 #if defined(SUNOS) || defined(SOLARIS)
616 /* don't want to force whole space into swapping mode... */
617 os_validate(addr, length);
619 os_protect(addr, length, OS_VM_PROT_ALL);
622 current_auto_gc_trigger = NULL;
626 gc_trigger_hit(void *addr)
628 if (current_auto_gc_trigger == NULL)
631 return (addr >= (void *)current_auto_gc_trigger &&
632 addr <((void *)current_dynamic_space + dynamic_space_size));
637 cheneygc_handle_wp_violation(os_context_t *context, void *addr)
639 if(!foreign_function_call_active && gc_trigger_hit(addr)){
640 struct thread *thread=arch_os_get_current_thread();
641 clear_auto_gc_trigger();
642 /* Don't flood the system with interrupts if the need to gc is
643 * already noted. This can happen for example when SUB-GC
644 * allocates or after a gc triggered in a WITHOUT-GCING. */
645 if (SymbolValue(GC_PENDING,thread) == NIL) {
646 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
647 if (arch_pseudo_atomic_atomic(context)) {
648 /* set things up so that GC happens when we finish
650 SetSymbolValue(GC_PENDING,T,thread);
651 arch_set_pseudo_atomic_interrupted(context);
656 SetSymbolValue(GC_PENDING,T,thread);