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);
50 static void scavenge_interrupt_contexts(void);
52 extern unsigned long bytes_consed_between_gcs;
55 /* collecting garbage */
59 tv_diff(struct timeval *x, struct timeval *y)
61 return (((double) x->tv_sec + (double) x->tv_usec * 1.0e-6) -
62 ((double) y->tv_sec + (double) y->tv_usec * 1.0e-6));
66 #define BYTES_ZERO_BEFORE_END (1<<12)
68 /* FIXME do we need this? Doesn't it duplicate lisp code in
69 * scrub-control-stack? */
74 lispobj *ptr = current_control_stack_pointer;
80 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
85 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
92 gc_general_alloc(long bytes, int unboxed_p, int quick_p) {
93 lispobj *new=new_space_free_pointer;
94 new_space_free_pointer+=(bytes/N_WORD_BYTES);
98 lispobj copy_large_unboxed_object(lispobj object, long nwords) {
99 return copy_object(object,nwords);
101 lispobj copy_unboxed_object(lispobj object, long nwords) {
102 return copy_object(object,nwords);
104 lispobj copy_large_object(lispobj object, long nwords) {
105 return copy_object(object,nwords);
108 /* Note: The generic GC interface we're implementing passes us a
109 * last_generation argument. That's meaningless for us, since we're
110 * not a generational GC. So we ignore it. */
112 collect_garbage(unsigned ignore)
115 struct timeval start_tv, stop_tv;
116 struct rusage start_rusage, stop_rusage;
117 double real_time, system_time, user_time;
118 double percent_retained, gc_rate;
119 unsigned long size_discarded;
121 unsigned long size_retained;
122 lispobj *current_static_space_free_pointer;
123 unsigned long static_space_size;
124 unsigned long control_stack_size, binding_stack_size;
126 struct thread *th=arch_os_get_current_thread();
129 printf("[Collecting garbage ... \n");
131 getrusage(RUSAGE_SELF, &start_rusage);
132 gettimeofday(&start_tv, (struct timezone *) 0);
135 /* it's possible that signals are blocked already if this was called
136 * from a signal handler (e.g. with the sigsegv gc_trigger stuff) */
138 sigaddset_blockable(&tmp);
139 thread_sigmask(SIG_BLOCK, &tmp, &old);
141 current_static_space_free_pointer =
142 (lispobj *) ((unsigned long)
143 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
146 /* Set up from space and new space pointers. */
148 from_space = current_dynamic_space;
149 from_space_free_pointer = dynamic_space_free_pointer;
152 fprintf(stderr,"from_space = %lx\n",
153 (unsigned long) current_dynamic_space);
155 if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
156 new_space = (lispobj *)DYNAMIC_1_SPACE_START;
157 else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
158 new_space = (lispobj *) DYNAMIC_0_SPACE_START;
160 lose("GC lossage. Current dynamic space is bogus!\n");
162 new_space_free_pointer = new_space;
164 /* Initialize the weak pointer list. */
165 weak_pointers = (struct weak_pointer *) NULL;
168 /* Scavenge all of the roots. */
170 printf("Scavenging interrupt contexts ...\n");
172 scavenge_interrupt_contexts();
175 printf("Scavenging interrupt handlers (%d bytes) ...\n",
176 (int)sizeof(interrupt_handlers));
178 scavenge((lispobj *) interrupt_handlers,
179 sizeof(interrupt_handlers) / sizeof(lispobj));
181 /* _size quantities are in units of sizeof(lispobj) - i.e. 4 */
183 current_control_stack_pointer-
184 (lispobj *)th->control_stack_start;
186 printf("Scavenging the control stack at %p (%ld words) ...\n",
187 ((lispobj *)th->control_stack_start),
190 scavenge(((lispobj *)th->control_stack_start), control_stack_size);
194 current_binding_stack_pointer -
195 (lispobj *)th->binding_stack_start;
197 printf("Scavenging the binding stack %x - %x (%d words) ...\n",
198 th->binding_stack_start,current_binding_stack_pointer,
199 (int)(binding_stack_size));
201 scavenge(((lispobj *)th->binding_stack_start), binding_stack_size);
204 current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
206 printf("Scavenging static space %x - %x (%d words) ...\n",
207 STATIC_SPACE_START,current_static_space_free_pointer,
208 (int)(static_space_size));
210 scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
212 /* Scavenge newspace. */
214 printf("Scavenging new space (%d bytes) ...\n",
215 (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
220 #if defined(DEBUG_PRINT_GARBAGE)
221 print_garbage(from_space, from_space_free_pointer);
224 /* Scan the weak pointers. */
226 printf("Scanning weak pointers ...\n");
228 scan_weak_pointers();
233 printf("Flipping spaces ...\n");
236 /* Maybe FIXME: it's possible that we could significantly reduce
237 * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
238 * similar os-dependent tricks here */
239 os_zero((os_vm_address_t) from_space,
240 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
242 current_dynamic_space = new_space;
243 dynamic_space_free_pointer = new_space_free_pointer;
246 size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
248 size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
250 os_flush_icache((os_vm_address_t)new_space, size_retained);
254 printf("Zeroing empty part of control stack ...\n");
257 set_auto_gc_trigger(size_retained+bytes_consed_between_gcs);
258 thread_sigmask(SIG_SETMASK, &old, 0);
262 gettimeofday(&stop_tv, (struct timezone *) 0);
263 getrusage(RUSAGE_SELF, &stop_rusage);
267 percent_retained = (((float) size_retained) /
268 ((float) size_discarded)) * 100.0;
270 printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
271 size_retained, size_discarded, percent_retained);
273 real_time = tv_diff(&stop_tv, &start_tv);
274 user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
275 system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
277 printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
278 real_time, user_time, system_time);
280 gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
282 printf("%10.2f M bytes/sec collected.\n", gc_rate);
290 scavenge_newspace(void)
292 lispobj *here, *next;
295 while (here < new_space_free_pointer) {
296 /* printf("here=%lx, new_space_free_pointer=%lx\n",
297 here,new_space_free_pointer); */
298 next = new_space_free_pointer;
299 scavenge(here, next - here);
302 /* printf("done with newspace\n"); */
305 /* scavenging interrupt contexts */
307 static int boxed_registers[] = BOXED_REGISTERS;
310 scavenge_interrupt_context(os_context_t *context)
315 unsigned long lip_offset;
316 int lip_register_pair;
318 unsigned long pc_code_offset;
319 #ifdef ARCH_HAS_LINK_REGISTER
320 unsigned long lr_code_offset;
322 #ifdef ARCH_HAS_NPC_REGISTER
323 unsigned long npc_code_offset;
325 #ifdef DEBUG_SCAVENGE_VERBOSE
326 fprintf(stderr, "Scavenging interrupt context at 0x%x\n",context);
328 /* Find the LIP's register pair and calculate its offset */
329 /* before we scavenge the context. */
331 lip = *os_context_register_addr(context, reg_LIP);
332 /* 0x7FFFFFFF on 32-bit platforms;
333 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
334 lip_offset = (((unsigned long)1) << (N_WORD_BITS - 1)) - 1;
335 lip_register_pair = -1;
336 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
341 index = boxed_registers[i];
342 reg = *os_context_register_addr(context, index);
343 /* would be using PTR if not for integer length issues */
344 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
346 if (offset < lip_offset) {
348 lip_register_pair = index;
354 /* Compute the PC's offset from the start of the CODE */
357 *os_context_pc_addr(context) -
358 *os_context_register_addr(context, reg_CODE);
359 #ifdef ARCH_HAS_NPC_REGISTER
361 *os_context_npc_addr(context) -
362 *os_context_register_addr(context, reg_CODE);
364 #ifdef ARCH_HAS_LINK_REGISTER
366 *os_context_lr_addr(context) -
367 *os_context_register_addr(context, reg_CODE);
370 /* Scavenge all boxed registers in the context. */
371 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
375 index = boxed_registers[i];
376 foo = *os_context_register_addr(context,index);
377 scavenge((lispobj *) &foo, 1);
378 *os_context_register_addr(context,index) = foo;
380 /* this is unlikely to work as intended on bigendian
381 * 64 bit platforms */
384 os_context_register_addr(context, index), 1);
389 *os_context_register_addr(context, reg_LIP) =
390 *os_context_register_addr(context, lip_register_pair) + lip_offset;
393 /* Fix the PC if it was in from space */
394 if (from_space_p(*os_context_pc_addr(context)))
395 *os_context_pc_addr(context) =
396 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
397 #ifdef ARCH_HAS_LINK_REGISTER
398 /* Fix the LR ditto; important if we're being called from
399 * an assembly routine that expects to return using blr, otherwise
401 if (from_space_p(*os_context_lr_addr(context)))
402 *os_context_lr_addr(context) =
403 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
406 #ifdef ARCH_HAS_NPC_REGISTER
407 if (from_space_p(*os_context_npc_addr(context)))
408 *os_context_npc_addr(context) =
409 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
413 void scavenge_interrupt_contexts(void)
416 os_context_t *context;
418 struct thread *th=arch_os_get_current_thread();
420 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
423 #ifdef DEBUG_SCAVENGE_VERBOSE
424 fprintf(stderr, "%d interrupt contexts to scan\n",index);
426 for (i = 0; i < index; i++) {
427 context = th->interrupt_contexts[i];
428 scavenge_interrupt_context(context);
436 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
439 int total_words_not_copied;
441 printf("Scanning from space ...\n");
443 total_words_not_copied = 0;
445 while (start < from_space_free_pointer) {
447 int forwardp, type, nwords;
451 forwardp = is_lisp_pointer(object) && new_space_p(object);
457 tag = lowtag_of(object);
460 case LIST_POINTER_LOWTAG:
463 case INSTANCE_POINTER_LOWTAG:
464 printf("Don't know about instances yet!\n");
467 case FUN_POINTER_LOWTAG:
470 case OTHER_POINTER_LOWTAG:
471 pointer = (lispobj *) native_pointer(object);
473 type = widetag_of(header);
474 nwords = (sizetab[type])(pointer);
476 default: nwords=1; /* shut yer whinging, gcc */
479 type = widetag_of(object);
480 nwords = (sizetab[type])(start);
481 total_words_not_copied += nwords;
482 printf("%4d words not copied at 0x%16lx; ",
483 nwords, (unsigned long) start);
484 printf("Header word is 0x%08x\n",
485 (unsigned int) object);
489 printf("%d total words not copied.\n", total_words_not_copied);
493 /* vector-like objects */
496 scav_vector(lispobj *where, lispobj object)
498 if (HeaderValue(object) == subtype_VectorValidHashing) {
500 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
509 #define WEAK_POINTER_NWORDS \
510 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
513 scav_weak_pointer(lispobj *where, lispobj object)
515 /* Do not let GC scavenge the value slot of the weak pointer */
516 /* (that is why it is a weak pointer). Note: we could use */
517 /* the scav_unboxed method here. */
519 return WEAK_POINTER_NWORDS;
523 search_read_only_space(void *pointer)
525 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
526 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
527 if ((pointer < (void *)start) || (pointer >= (void *)end))
529 return (gc_search_space(start,
530 (((lispobj *)pointer)+2)-start,
531 (lispobj *)pointer));
535 search_static_space(void *pointer)
537 lispobj* start = (lispobj*)STATIC_SPACE_START;
538 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
539 if ((pointer < (void *)start) || (pointer >= (void *)end))
541 return (gc_search_space(start,
542 (((lispobj *)pointer)+2)-start,
543 (lispobj *)pointer));
547 search_dynamic_space(void *pointer)
549 lispobj *start = (lispobj *) current_dynamic_space;
550 lispobj *end = (lispobj *) dynamic_space_free_pointer;
551 if ((pointer < (void *)start) || (pointer >= (void *)end))
553 return (gc_search_space(start,
554 (((lispobj *)pointer)+2)-start,
555 (lispobj *)pointer));
558 /* initialization. if gc_init can be moved to after core load, we could
559 * combine these two functions */
565 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
566 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
570 gc_initialize_pointers(void)
572 /* FIXME: We do nothing here. We (briefly) misguidedly attempted
573 to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
574 forgetting that (a) actually it could be the other and (b) it's
575 set in coreparse.c anyway. There's a FIXME note left here to
576 note that current_dynamic_space is a violation of OAOO: we can
577 tell which dynamic space we're currently in by looking at
578 dynamic_space_free_pointer. -- CSR, 2002-08-09 */
584 /* noise to manipulate the gc trigger stuff */
586 /* Functions that substantially change the dynamic space free pointer
587 * (collect_garbage, purify) are responsible also for resetting the
589 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
591 os_vm_address_t addr;
594 addr = os_round_up_to_page((os_vm_address_t)current_dynamic_space
596 if (addr < (os_vm_address_t)dynamic_space_free_pointer)
597 lose("set_auto_gc_trigger: tried to set gc trigger too low! (%ld < 0x%08lx)\n",
598 (unsigned long)dynamic_usage,
599 (unsigned long)((os_vm_address_t)dynamic_space_free_pointer
600 - (os_vm_address_t)current_dynamic_space));
602 length = os_trunc_size_to_page(DYNAMIC_SPACE_SIZE - dynamic_usage);
604 lose("set_auto_gc_trigger: tried to set gc trigger too high! (0x%08lx)\n",
605 (unsigned long)dynamic_usage);
607 #if defined(SUNOS) || defined(SOLARIS)
608 os_invalidate(addr, length);
610 os_protect(addr, length, 0);
613 current_auto_gc_trigger = (lispobj *)addr;
616 void clear_auto_gc_trigger(void)
618 os_vm_address_t addr;
621 if (current_auto_gc_trigger == NULL)
624 addr = (os_vm_address_t)current_auto_gc_trigger;
625 length = DYNAMIC_SPACE_SIZE + (os_vm_address_t)current_dynamic_space - addr;
627 #if defined(SUNOS) || defined(SOLARIS)
628 /* don't want to force whole space into swapping mode... */
629 os_validate(addr, length);
631 os_protect(addr, length, OS_VM_PROT_ALL);
634 current_auto_gc_trigger = NULL;