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);
51 extern unsigned long bytes_consed_between_gcs;
54 /* collecting garbage */
58 tv_diff(struct timeval *x, struct timeval *y)
60 return (((double) x->tv_sec + (double) x->tv_usec * 1.0e-6) -
61 ((double) y->tv_sec + (double) y->tv_usec * 1.0e-6));
65 #define BYTES_ZERO_BEFORE_END (1<<12)
67 /* FIXME do we need this? Doesn't it duplicate lisp code in
68 * scrub-control-stack? */
73 lispobj *ptr = current_control_stack_pointer;
79 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
84 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
91 gc_general_alloc(long bytes, int unboxed_p, int quick_p) {
92 lispobj *new=new_space_free_pointer;
93 new_space_free_pointer+=(bytes/N_WORD_BYTES);
97 lispobj copy_large_unboxed_object(lispobj object, long nwords) {
98 return copy_object(object,nwords);
100 lispobj copy_unboxed_object(lispobj object, long nwords) {
101 return copy_object(object,nwords);
103 lispobj copy_large_object(lispobj object, long nwords) {
104 return copy_object(object,nwords);
107 /* Note: The generic GC interface we're implementing passes us a
108 * last_generation argument. That's meaningless for us, since we're
109 * not a generational GC. So we ignore it. */
111 collect_garbage(generation_index_t ignore)
114 struct timeval start_tv, stop_tv;
115 struct rusage start_rusage, stop_rusage;
116 double real_time, system_time, user_time;
117 double percent_retained, gc_rate;
118 unsigned long size_discarded;
120 unsigned long size_retained;
121 lispobj *current_static_space_free_pointer;
122 unsigned long static_space_size;
123 unsigned long control_stack_size, binding_stack_size;
125 struct thread *th=arch_os_get_current_thread();
128 printf("[Collecting garbage ... \n");
130 getrusage(RUSAGE_SELF, &start_rusage);
131 gettimeofday(&start_tv, (struct timezone *) 0);
134 /* it's possible that signals are blocked already if this was called
135 * from a signal handler (e.g. with the sigsegv gc_trigger stuff) */
137 sigaddset_blockable(&tmp);
138 thread_sigmask(SIG_BLOCK, &tmp, &old);
140 current_static_space_free_pointer =
141 (lispobj *) ((unsigned long)
142 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
145 /* Set up from space and new space pointers. */
147 from_space = current_dynamic_space;
148 from_space_free_pointer = dynamic_space_free_pointer;
151 fprintf(stderr,"from_space = %lx\n",
152 (unsigned long) current_dynamic_space);
154 if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
155 new_space = (lispobj *)DYNAMIC_1_SPACE_START;
156 else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
157 new_space = (lispobj *) DYNAMIC_0_SPACE_START;
159 lose("GC lossage. Current dynamic space is bogus!\n");
161 new_space_free_pointer = new_space;
163 /* Initialize the weak pointer list. */
164 weak_pointers = (struct weak_pointer *) NULL;
167 /* Scavenge all of the roots. */
169 printf("Scavenging interrupt contexts ...\n");
171 scavenge_interrupt_contexts();
174 printf("Scavenging interrupt handlers (%d bytes) ...\n",
175 (int)sizeof(interrupt_handlers));
177 scavenge((lispobj *) interrupt_handlers,
178 sizeof(interrupt_handlers) / sizeof(lispobj));
180 /* _size quantities are in units of sizeof(lispobj) - i.e. 4 */
182 current_control_stack_pointer-
183 (lispobj *)th->control_stack_start;
185 printf("Scavenging the control stack at %p (%ld words) ...\n",
186 ((lispobj *)th->control_stack_start),
189 scavenge(((lispobj *)th->control_stack_start), control_stack_size);
193 current_binding_stack_pointer -
194 (lispobj *)th->binding_stack_start;
196 printf("Scavenging the binding stack %x - %x (%d words) ...\n",
197 th->binding_stack_start,current_binding_stack_pointer,
198 (int)(binding_stack_size));
200 scavenge(((lispobj *)th->binding_stack_start), binding_stack_size);
203 current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
205 printf("Scavenging static space %x - %x (%d words) ...\n",
206 STATIC_SPACE_START,current_static_space_free_pointer,
207 (int)(static_space_size));
209 scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
211 /* Scavenge newspace. */
213 printf("Scavenging new space (%d bytes) ...\n",
214 (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
219 #if defined(DEBUG_PRINT_GARBAGE)
220 print_garbage(from_space, from_space_free_pointer);
223 /* Scan the weak pointers. */
225 printf("Scanning weak hash tables ...\n");
227 scan_weak_hash_tables();
229 /* Scan the weak pointers. */
231 printf("Scanning weak pointers ...\n");
233 scan_weak_pointers();
237 printf("Flipping spaces ...\n");
240 /* Maybe FIXME: it's possible that we could significantly reduce
241 * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
242 * similar os-dependent tricks here */
243 os_zero((os_vm_address_t) from_space,
244 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
246 current_dynamic_space = new_space;
247 dynamic_space_free_pointer = new_space_free_pointer;
250 size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
252 size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
254 os_flush_icache((os_vm_address_t)new_space, size_retained);
258 printf("Zeroing empty part of control stack ...\n");
261 set_auto_gc_trigger(size_retained+bytes_consed_between_gcs);
262 thread_sigmask(SIG_SETMASK, &old, 0);
266 gettimeofday(&stop_tv, (struct timezone *) 0);
267 getrusage(RUSAGE_SELF, &stop_rusage);
271 percent_retained = (((float) size_retained) /
272 ((float) size_discarded)) * 100.0;
274 printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
275 size_retained, size_discarded, percent_retained);
277 real_time = tv_diff(&stop_tv, &start_tv);
278 user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
279 system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
281 printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
282 real_time, user_time, system_time);
284 gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
286 printf("%10.2f M bytes/sec collected.\n", gc_rate);
294 scavenge_newspace(void)
296 lispobj *here, *next;
299 while (here < new_space_free_pointer) {
300 /* printf("here=%lx, new_space_free_pointer=%lx\n",
301 here,new_space_free_pointer); */
302 next = new_space_free_pointer;
303 scavenge(here, next - here);
304 scav_weak_hash_tables();
307 /* printf("done with newspace\n"); */
310 /* scavenging interrupt contexts */
312 static int boxed_registers[] = BOXED_REGISTERS;
315 scavenge_interrupt_context(os_context_t *context)
320 unsigned long lip_offset;
321 int lip_register_pair;
323 unsigned long pc_code_offset;
324 #ifdef ARCH_HAS_LINK_REGISTER
325 unsigned long lr_code_offset;
327 #ifdef ARCH_HAS_NPC_REGISTER
328 unsigned long npc_code_offset;
330 #ifdef DEBUG_SCAVENGE_VERBOSE
331 fprintf(stderr, "Scavenging interrupt context at 0x%x\n",context);
333 /* Find the LIP's register pair and calculate its offset */
334 /* before we scavenge the context. */
336 lip = *os_context_register_addr(context, reg_LIP);
337 /* 0x7FFFFFFF on 32-bit platforms;
338 0x7FFFFFFFFFFFFFFF on 64-bit platforms */
339 lip_offset = (((unsigned long)1) << (N_WORD_BITS - 1)) - 1;
340 lip_register_pair = -1;
341 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
346 index = boxed_registers[i];
347 reg = *os_context_register_addr(context, index);
348 /* would be using PTR if not for integer length issues */
349 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
351 if (offset < lip_offset) {
353 lip_register_pair = index;
359 /* Compute the PC's offset from the start of the CODE */
362 *os_context_pc_addr(context) -
363 *os_context_register_addr(context, reg_CODE);
364 #ifdef ARCH_HAS_NPC_REGISTER
366 *os_context_npc_addr(context) -
367 *os_context_register_addr(context, reg_CODE);
369 #ifdef ARCH_HAS_LINK_REGISTER
371 *os_context_lr_addr(context) -
372 *os_context_register_addr(context, reg_CODE);
375 /* Scavenge all boxed registers in the context. */
376 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
380 index = boxed_registers[i];
381 foo = *os_context_register_addr(context,index);
382 scavenge((lispobj *) &foo, 1);
383 *os_context_register_addr(context,index) = foo;
385 /* this is unlikely to work as intended on bigendian
386 * 64 bit platforms */
389 os_context_register_addr(context, index), 1);
394 *os_context_register_addr(context, reg_LIP) =
395 *os_context_register_addr(context, lip_register_pair) + lip_offset;
398 /* Fix the PC if it was in from space */
399 if (from_space_p(*os_context_pc_addr(context)))
400 *os_context_pc_addr(context) =
401 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
402 #ifdef ARCH_HAS_LINK_REGISTER
403 /* Fix the LR ditto; important if we're being called from
404 * an assembly routine that expects to return using blr, otherwise
406 if (from_space_p(*os_context_lr_addr(context)))
407 *os_context_lr_addr(context) =
408 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
411 #ifdef ARCH_HAS_NPC_REGISTER
412 if (from_space_p(*os_context_npc_addr(context)))
413 *os_context_npc_addr(context) =
414 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
418 void scavenge_interrupt_contexts(void)
421 os_context_t *context;
423 struct thread *th=arch_os_get_current_thread();
425 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
428 #ifdef DEBUG_SCAVENGE_VERBOSE
429 fprintf(stderr, "%d interrupt contexts to scan\n",index);
431 for (i = 0; i < index; i++) {
432 context = th->interrupt_contexts[i];
433 scavenge_interrupt_context(context);
441 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
444 int total_words_not_copied;
446 printf("Scanning from space ...\n");
448 total_words_not_copied = 0;
450 while (start < from_space_free_pointer) {
452 int forwardp, type, nwords;
456 forwardp = is_lisp_pointer(object) && new_space_p(object);
462 tag = lowtag_of(object);
465 case LIST_POINTER_LOWTAG:
468 case INSTANCE_POINTER_LOWTAG:
469 printf("Don't know about instances yet!\n");
472 case FUN_POINTER_LOWTAG:
475 case OTHER_POINTER_LOWTAG:
476 pointer = (lispobj *) native_pointer(object);
478 type = widetag_of(header);
479 nwords = (sizetab[type])(pointer);
481 default: nwords=1; /* shut yer whinging, gcc */
484 type = widetag_of(object);
485 nwords = (sizetab[type])(start);
486 total_words_not_copied += nwords;
487 printf("%4d words not copied at 0x%16lx; ",
488 nwords, (unsigned long) start);
489 printf("Header word is 0x%08x\n",
490 (unsigned int) object);
494 printf("%d total words not copied.\n", total_words_not_copied);
500 #define WEAK_POINTER_NWORDS \
501 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
504 scav_weak_pointer(lispobj *where, lispobj object)
506 /* Do not let GC scavenge the value slot of the weak pointer */
507 /* (that is why it is a weak pointer). Note: we could use */
508 /* the scav_unboxed method here. */
510 return WEAK_POINTER_NWORDS;
514 search_read_only_space(void *pointer)
516 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
517 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
518 if ((pointer < (void *)start) || (pointer >= (void *)end))
520 return (gc_search_space(start,
521 (((lispobj *)pointer)+2)-start,
522 (lispobj *)pointer));
526 search_static_space(void *pointer)
528 lispobj* start = (lispobj*)STATIC_SPACE_START;
529 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
530 if ((pointer < (void *)start) || (pointer >= (void *)end))
532 return (gc_search_space(start,
533 (((lispobj *)pointer)+2)-start,
534 (lispobj *)pointer));
538 search_dynamic_space(void *pointer)
540 lispobj *start = (lispobj *) current_dynamic_space;
541 lispobj *end = (lispobj *) dynamic_space_free_pointer;
542 if ((pointer < (void *)start) || (pointer >= (void *)end))
544 return (gc_search_space(start,
545 (((lispobj *)pointer)+2)-start,
546 (lispobj *)pointer));
549 /* initialization. if gc_init can be moved to after core load, we could
550 * combine these two functions */
556 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
560 gc_initialize_pointers(void)
562 /* FIXME: We do nothing here. We (briefly) misguidedly attempted
563 to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
564 forgetting that (a) actually it could be the other and (b) it's
565 set in coreparse.c anyway. There's a FIXME note left here to
566 note that current_dynamic_space is a violation of OAOO: we can
567 tell which dynamic space we're currently in by looking at
568 dynamic_space_free_pointer. -- CSR, 2002-08-09 */
574 /* noise to manipulate the gc trigger stuff */
576 /* Functions that substantially change the dynamic space free pointer
577 * (collect_garbage, purify) are responsible also for resetting the
579 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
581 os_vm_address_t addr;
584 addr = os_round_up_to_page((os_vm_address_t)current_dynamic_space
586 if (addr < (os_vm_address_t)dynamic_space_free_pointer)
587 lose("set_auto_gc_trigger: tried to set gc trigger too low! (%ld < 0x%08lx)\n",
588 (unsigned long)dynamic_usage,
589 (unsigned long)((os_vm_address_t)dynamic_space_free_pointer
590 - (os_vm_address_t)current_dynamic_space));
592 length = os_trunc_size_to_page(DYNAMIC_SPACE_SIZE - dynamic_usage);
594 lose("set_auto_gc_trigger: tried to set gc trigger too high! (0x%08lx)\n",
595 (unsigned long)dynamic_usage);
597 #if defined(SUNOS) || defined(SOLARIS)
598 os_invalidate(addr, length);
600 os_protect(addr, length, 0);
603 current_auto_gc_trigger = (lispobj *)addr;
606 void clear_auto_gc_trigger(void)
608 os_vm_address_t addr;
611 if (current_auto_gc_trigger == NULL)
614 addr = (os_vm_address_t)current_auto_gc_trigger;
615 length = DYNAMIC_SPACE_SIZE + (os_vm_address_t)current_dynamic_space - addr;
617 #if defined(SUNOS) || defined(SOLARIS)
618 /* don't want to force whole space into swapping mode... */
619 os_validate(addr, length);
621 os_protect(addr, length, OS_VM_PROT_ALL);
624 current_auto_gc_trigger = NULL;