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"
35 /* So you need to debug? */
38 #define DEBUG_SPACE_PREDICATES
39 #define DEBUG_SCAVENGE_VERBOSE
40 #define DEBUG_COPY_VERBOSE
45 lispobj *from_space_free_pointer;
48 lispobj *new_space_free_pointer;
50 static void scavenge_newspace(void);
53 /* collecting garbage */
57 tv_diff(struct timeval *x, struct timeval *y)
59 return (((double) x->tv_sec + (double) x->tv_usec * 1.0e-6) -
60 ((double) y->tv_sec + (double) y->tv_usec * 1.0e-6));
64 #define BYTES_ZERO_BEFORE_END (1<<12)
66 /* FIXME do we need this? Doesn't it duplicate lisp code in
67 * scrub-control-stack? */
72 lispobj *ptr = current_control_stack_pointer;
78 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
83 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
90 gc_general_alloc(long bytes, int page_type_flag, int quick_p) {
91 lispobj *new=new_space_free_pointer;
92 new_space_free_pointer+=(bytes/N_WORD_BYTES);
96 lispobj copy_large_unboxed_object(lispobj object, long nwords) {
97 return copy_object(object,nwords);
99 lispobj copy_unboxed_object(lispobj object, long nwords) {
100 return copy_object(object,nwords);
102 lispobj copy_large_object(lispobj object, long nwords) {
103 return copy_object(object,nwords);
106 /* Note: The generic GC interface we're implementing passes us a
107 * last_generation argument. That's meaningless for us, since we're
108 * not a generational GC. So we ignore it. */
110 collect_garbage(generation_index_t ignore)
113 struct timeval start_tv, stop_tv;
114 struct rusage start_rusage, stop_rusage;
115 double real_time, system_time, user_time;
116 double percent_retained, gc_rate;
117 unsigned long size_discarded;
119 unsigned long size_retained;
120 lispobj *current_static_space_free_pointer;
121 unsigned long static_space_size;
122 unsigned long control_stack_size, binding_stack_size;
124 struct thread *th=arch_os_get_current_thread();
127 printf("[Collecting garbage ... \n");
129 getrusage(RUSAGE_SELF, &start_rusage);
130 gettimeofday(&start_tv, (struct timezone *) 0);
133 /* it's possible that signals are blocked already if this was called
134 * from a signal handler (e.g. with the sigsegv gc_trigger stuff) */
135 block_blockable_signals(0, &old);
137 current_static_space_free_pointer =
138 (lispobj *) ((unsigned long)
139 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
142 /* Set up from space and new space pointers. */
144 from_space = current_dynamic_space;
145 from_space_free_pointer = dynamic_space_free_pointer;
148 fprintf(stderr,"from_space = %lx\n",
149 (unsigned long) current_dynamic_space);
151 if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
152 new_space = (lispobj *)DYNAMIC_1_SPACE_START;
153 else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
154 new_space = (lispobj *) DYNAMIC_0_SPACE_START;
156 lose("GC lossage. Current dynamic space is bogus!\n");
158 new_space_free_pointer = new_space;
160 /* Initialize the weak pointer list. */
161 weak_pointers = (struct weak_pointer *) NULL;
164 /* Scavenge all of the roots. */
166 printf("Scavenging interrupt contexts ...\n");
168 scavenge_interrupt_contexts();
171 printf("Scavenging interrupt handlers (%d bytes) ...\n",
172 (int)sizeof(interrupt_handlers));
174 scavenge((lispobj *) interrupt_handlers,
175 sizeof(interrupt_handlers) / sizeof(lispobj));
177 /* _size quantities are in units of sizeof(lispobj) - i.e. 4 */
179 current_control_stack_pointer-
180 (lispobj *)th->control_stack_start;
182 printf("Scavenging the control stack at %p (%ld words) ...\n",
183 ((lispobj *)th->control_stack_start),
186 scavenge(((lispobj *)th->control_stack_start), control_stack_size);
190 current_binding_stack_pointer -
191 (lispobj *)th->binding_stack_start;
193 printf("Scavenging the binding stack %x - %x (%d words) ...\n",
194 th->binding_stack_start,current_binding_stack_pointer,
195 (int)(binding_stack_size));
197 scavenge(((lispobj *)th->binding_stack_start), binding_stack_size);
200 current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
202 printf("Scavenging static space %x - %x (%d words) ...\n",
203 STATIC_SPACE_START,current_static_space_free_pointer,
204 (int)(static_space_size));
206 scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
208 /* Scavenge newspace. */
210 printf("Scavenging new space (%d bytes) ...\n",
211 (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
216 #if defined(DEBUG_PRINT_GARBAGE)
217 print_garbage(from_space, from_space_free_pointer);
220 /* Scan the weak pointers. */
222 printf("Scanning weak hash tables ...\n");
224 scan_weak_hash_tables();
226 /* Scan the weak pointers. */
228 printf("Scanning weak pointers ...\n");
230 scan_weak_pointers();
234 printf("Flipping spaces ...\n");
237 /* Maybe FIXME: it's possible that we could significantly reduce
238 * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
239 * similar os-dependent tricks here */
240 #ifdef LISP_FEATURE_HPUX
241 /* hpux cant handle unmapping areas that are not 100% mapped */
242 clear_auto_gc_trigger();
244 os_zero((os_vm_address_t) from_space,
245 (os_vm_size_t) dynamic_space_size);
247 current_dynamic_space = new_space;
248 dynamic_space_free_pointer = new_space_free_pointer;
251 size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
253 size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
255 os_flush_icache((os_vm_address_t)new_space, size_retained);
259 printf("Zeroing empty part of control stack ...\n");
262 set_auto_gc_trigger(size_retained+bytes_consed_between_gcs);
263 thread_sigmask(SIG_SETMASK, &old, 0);
267 gettimeofday(&stop_tv, (struct timezone *) 0);
268 getrusage(RUSAGE_SELF, &stop_rusage);
272 percent_retained = (((float) size_retained) /
273 ((float) size_discarded)) * 100.0;
275 printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
276 size_retained, size_discarded, percent_retained);
278 real_time = tv_diff(&stop_tv, &start_tv);
279 user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
280 system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
282 printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
283 real_time, user_time, system_time);
285 gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
287 printf("%10.2f M bytes/sec collected.\n", gc_rate);
295 scavenge_newspace(void)
297 lispobj *here, *next;
300 while (here < new_space_free_pointer) {
301 /* printf("here=%lx, new_space_free_pointer=%lx\n",
302 here,new_space_free_pointer); */
303 next = new_space_free_pointer;
304 scavenge(here, next - here);
305 scav_weak_hash_tables();
308 /* printf("done with newspace\n"); */
311 /* scavenging interrupt contexts */
313 static int boxed_registers[] = BOXED_REGISTERS;
316 scavenge_interrupt_context(os_context_t *context)
321 unsigned long lip_offset;
322 int lip_register_pair;
324 unsigned long pc_code_offset;
325 #ifdef ARCH_HAS_LINK_REGISTER
326 unsigned long lr_code_offset;
328 #ifdef ARCH_HAS_NPC_REGISTER
329 unsigned long npc_code_offset;
331 #ifdef DEBUG_SCAVENGE_VERBOSE
332 fprintf(stderr, "Scavenging interrupt context at 0x%x\n",context);
334 /* Find the LIP's register pair and calculate its offset */
335 /* before we scavenge the context. */
337 lip = *os_context_register_addr(context, reg_LIP);
338 /* 0x7FFFFFFF on 32-bit platforms;
339 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
340 lip_offset = (((unsigned long)1) << (N_WORD_BITS - 1)) - 1;
341 lip_register_pair = -1;
342 for (i = 0; i < (int)(sizeof(boxed_registers) / sizeof(int)); i++) {
344 unsigned long offset;
347 index = boxed_registers[i];
348 reg = *os_context_register_addr(context, index);
349 /* would be using PTR if not for integer length issues */
350 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
352 if (offset < lip_offset) {
354 lip_register_pair = index;
360 /* Compute the PC's offset from the start of the CODE */
363 *os_context_pc_addr(context) -
364 *os_context_register_addr(context, reg_CODE);
365 #ifdef ARCH_HAS_NPC_REGISTER
367 *os_context_npc_addr(context) -
368 *os_context_register_addr(context, reg_CODE);
370 #ifdef ARCH_HAS_LINK_REGISTER
372 *os_context_lr_addr(context) -
373 *os_context_register_addr(context, reg_CODE);
376 /* Scavenge all boxed registers in the context. */
377 for (i = 0; i < (int)(sizeof(boxed_registers) / sizeof(int)); i++) {
381 index = boxed_registers[i];
382 foo = *os_context_register_addr(context,index);
383 scavenge((lispobj *) &foo, 1);
384 *os_context_register_addr(context,index) = foo;
386 /* this is unlikely to work as intended on bigendian
387 * 64 bit platforms */
390 os_context_register_addr(context, index), 1);
395 *os_context_register_addr(context, reg_LIP) =
396 *os_context_register_addr(context, lip_register_pair) + lip_offset;
399 /* Fix the PC if it was in from space */
400 if (from_space_p(*os_context_pc_addr(context)))
401 *os_context_pc_addr(context) =
402 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
403 #ifdef ARCH_HAS_LINK_REGISTER
404 /* Fix the LR ditto; important if we're being called from
405 * an assembly routine that expects to return using blr, otherwise
407 if (from_space_p(*os_context_lr_addr(context)))
408 *os_context_lr_addr(context) =
409 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
412 #ifdef ARCH_HAS_NPC_REGISTER
413 if (from_space_p(*os_context_npc_addr(context)))
414 *os_context_npc_addr(context) =
415 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
419 void scavenge_interrupt_contexts(void)
422 os_context_t *context;
424 struct thread *th=arch_os_get_current_thread();
426 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
429 #ifdef DEBUG_SCAVENGE_VERBOSE
430 fprintf(stderr, "%d interrupt contexts to scan\n",index);
432 for (i = 0; i < index; i++) {
433 context = th->interrupt_contexts[i];
434 scavenge_interrupt_context(context);
442 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
445 int total_words_not_copied;
447 printf("Scanning from space ...\n");
449 total_words_not_copied = 0;
451 while (start < from_space_free_pointer) {
453 int forwardp, type, nwords;
457 forwardp = is_lisp_pointer(object) && new_space_p(object);
463 tag = lowtag_of(object);
466 case LIST_POINTER_LOWTAG:
469 case INSTANCE_POINTER_LOWTAG:
470 printf("Don't know about instances yet!\n");
473 case FUN_POINTER_LOWTAG:
476 case OTHER_POINTER_LOWTAG:
477 pointer = (lispobj *) native_pointer(object);
479 type = widetag_of(header);
480 nwords = (sizetab[type])(pointer);
482 default: nwords=1; /* shut yer whinging, gcc */
485 type = widetag_of(object);
486 nwords = (sizetab[type])(start);
487 total_words_not_copied += nwords;
488 printf("%4d words not copied at 0x%16lx; ",
489 nwords, (unsigned long) start);
490 printf("Header word is 0x%08x\n",
491 (unsigned int) object);
495 printf("%d total words not copied.\n", total_words_not_copied);
501 #define WEAK_POINTER_NWORDS \
502 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
505 scav_weak_pointer(lispobj *where, lispobj object)
507 /* Do not let GC scavenge the value slot of the weak pointer */
508 /* (that is why it is a weak pointer). Note: we could use */
509 /* the scav_unboxed method here. */
511 return WEAK_POINTER_NWORDS;
515 search_read_only_space(void *pointer)
517 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
518 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
519 if ((pointer < (void *)start) || (pointer >= (void *)end))
521 return (gc_search_space(start,
522 (((lispobj *)pointer)+2)-start,
523 (lispobj *)pointer));
527 search_static_space(void *pointer)
529 lispobj* start = (lispobj*)STATIC_SPACE_START;
530 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
531 if ((pointer < (void *)start) || (pointer >= (void *)end))
533 return (gc_search_space(start,
534 (((lispobj *)pointer)+2)-start,
535 (lispobj *)pointer));
539 search_dynamic_space(void *pointer)
541 lispobj *start = (lispobj *) current_dynamic_space;
542 lispobj *end = (lispobj *) dynamic_space_free_pointer;
543 if ((pointer < (void *)start) || (pointer >= (void *)end))
545 return (gc_search_space(start,
546 (((lispobj *)pointer)+2)-start,
547 (lispobj *)pointer));
550 /* initialization. if gc_init can be moved to after core load, we could
551 * combine these two functions */
557 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
561 gc_initialize_pointers(void)
563 /* FIXME: We do nothing here. We (briefly) misguidedly attempted
564 to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
565 forgetting that (a) actually it could be the other and (b) it's
566 set in coreparse.c anyway. There's a FIXME note left here to
567 note that current_dynamic_space is a violation of OAOO: we can
568 tell which dynamic space we're currently in by looking at
569 dynamic_space_free_pointer. -- CSR, 2002-08-09 */
575 /* noise to manipulate the gc trigger stuff */
577 /* Functions that substantially change the dynamic space free pointer
578 * (collect_garbage, purify) are responsible also for resetting the
580 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
582 os_vm_address_t addr;
585 addr = os_round_up_to_page((os_vm_address_t)current_dynamic_space
587 if (addr < (os_vm_address_t)dynamic_space_free_pointer)
588 lose("set_auto_gc_trigger: tried to set gc trigger too low! (%ld < 0x%08lx)\n",
589 (unsigned long)dynamic_usage,
590 (unsigned long)((os_vm_address_t)dynamic_space_free_pointer
591 - (os_vm_address_t)current_dynamic_space));
593 length = os_trunc_size_to_page(dynamic_space_size - dynamic_usage);
595 lose("set_auto_gc_trigger: tried to set gc trigger too high! (0x%08lx)\n",
596 (unsigned long)dynamic_usage);
598 #if defined(SUNOS) || defined(SOLARIS) || defined(LISP_FEATURE_HPUX)
599 os_invalidate(addr, length);
601 os_protect(addr, length, 0);
604 current_auto_gc_trigger = (lispobj *)addr;
607 void clear_auto_gc_trigger(void)
609 os_vm_address_t addr;
612 if (current_auto_gc_trigger == NULL)
615 addr = (os_vm_address_t)current_auto_gc_trigger;
616 length = dynamic_space_size + (os_vm_address_t)current_dynamic_space - addr;
618 #if defined(SUNOS) || defined(SOLARIS) || defined(LISP_FEATURE_HPUX)
619 /* don't want to force whole space into swapping mode... */
620 os_validate(addr, length);
622 os_protect(addr, length, OS_VM_PROT_ALL);
625 current_auto_gc_trigger = NULL;
629 gc_trigger_hit(void *addr)
631 if (current_auto_gc_trigger == NULL)
634 return (addr >= (void *)current_auto_gc_trigger &&
635 addr <((void *)current_dynamic_space + dynamic_space_size));
640 cheneygc_handle_wp_violation(os_context_t *context, void *addr)
642 if(!foreign_function_call_active && gc_trigger_hit(addr)){
643 struct thread *thread=arch_os_get_current_thread();
644 clear_auto_gc_trigger();
645 /* Don't flood the system with interrupts if the need to gc is
646 * already noted. This can happen for example when SUB-GC
647 * allocates or after a gc triggered in a WITHOUT-GCING. */
648 if (SymbolValue(GC_PENDING,thread) == NIL) {
649 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
650 if (arch_pseudo_atomic_atomic(context)) {
651 /* set things up so that GC happens when we finish
653 SetSymbolValue(GC_PENDING,T,thread);
654 arch_set_pseudo_atomic_interrupted(context);
655 maybe_save_gc_mask_and_block_deferrables
656 (os_context_sigmask_addr(context));
661 SetSymbolValue(GC_PENDING,T,thread);