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"
33 /* So you need to debug? */
36 #define DEBUG_SPACE_PREDICATES
37 #define DEBUG_SCAVENGE_VERBOSE
38 #define DEBUG_COPY_VERBOSE
43 lispobj *from_space_free_pointer;
46 lispobj *new_space_free_pointer;
48 static void scavenge_newspace(void);
49 static void scavenge_interrupt_contexts(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 u32 *ptr = (u32 *)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(int bytes, int unboxed_p, int quick_p) {
90 lispobj *new=new_space_free_pointer;
91 new_space_free_pointer+=(bytes/4);
95 lispobj copy_large_unboxed_object(lispobj object, int nwords) {
96 return copy_object(object,nwords);
98 lispobj copy_unboxed_object(lispobj object, int nwords) {
99 return copy_object(object,nwords);
101 lispobj copy_large_object(lispobj object, int 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(unsigned 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;
117 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;
125 printf("[Collecting garbage ... \n");
127 getrusage(RUSAGE_SELF, &start_rusage);
128 gettimeofday(&start_tv, (struct timezone *) 0);
132 sigaddset_blockable(&tmp);
133 sigprocmask(SIG_BLOCK, &tmp, &old);
135 current_static_space_free_pointer =
136 (lispobj *) ((unsigned long)
137 SymbolValue(STATIC_SPACE_FREE_POINTER));
140 /* Set up from space and new space pointers. */
142 from_space = current_dynamic_space;
143 from_space_free_pointer = dynamic_space_free_pointer;
146 fprintf(stderr,"from_space = %lx\n",
147 (unsigned long) current_dynamic_space);
149 if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
150 new_space = (lispobj *)DYNAMIC_1_SPACE_START;
151 else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
152 new_space = (lispobj *) DYNAMIC_0_SPACE_START;
154 lose("GC lossage. Current dynamic space is bogus!\n");
156 new_space_free_pointer = new_space;
158 /* Initialize the weak pointer list. */
159 weak_pointers = (struct weak_pointer *) NULL;
162 /* Scavenge all of the roots. */
164 printf("Scavenging interrupt contexts ...\n");
166 scavenge_interrupt_contexts();
169 printf("Scavenging interrupt handlers (%d bytes) ...\n",
170 (int)sizeof(interrupt_handlers));
172 scavenge((lispobj *) interrupt_handlers,
173 sizeof(interrupt_handlers) / sizeof(lispobj));
175 /* _size quantities are in units of sizeof(lispobj) - i.e. 4 */
177 current_control_stack_pointer-
178 (lispobj *)CONTROL_STACK_START;
180 printf("Scavenging the control stack at %p (%ld words) ...\n",
181 ((lispobj *)CONTROL_STACK_START),
184 scavenge(((lispobj *)CONTROL_STACK_START), control_stack_size);
188 current_binding_stack_pointer -
189 (lispobj *)BINDING_STACK_START;
191 printf("Scavenging the binding stack %x - %x (%d words) ...\n",
192 BINDING_STACK_START,current_binding_stack_pointer,
193 (int)(binding_stack_size));
195 scavenge(((lispobj *)BINDING_STACK_START), binding_stack_size);
198 current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
200 printf("Scavenging static space %x - %x (%d words) ...\n",
201 STATIC_SPACE_START,current_static_space_free_pointer,
202 (int)(static_space_size));
204 scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
206 /* Scavenge newspace. */
208 printf("Scavenging new space (%d bytes) ...\n",
209 (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
214 #if defined(DEBUG_PRINT_GARBAGE)
215 print_garbage(from_space, from_space_free_pointer);
218 /* Scan the weak pointers. */
220 printf("Scanning weak pointers ...\n");
222 scan_weak_pointers();
227 printf("Flipping spaces ...\n");
230 os_zero((os_vm_address_t) current_dynamic_space,
231 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
233 current_dynamic_space = new_space;
234 dynamic_space_free_pointer = new_space_free_pointer;
237 size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
238 size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
243 printf("Zeroing empty part of control stack ...\n");
247 sigprocmask(SIG_SETMASK, &old, 0);
251 gettimeofday(&stop_tv, (struct timezone *) 0);
252 getrusage(RUSAGE_SELF, &stop_rusage);
256 percent_retained = (((float) size_retained) /
257 ((float) size_discarded)) * 100.0;
259 printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
260 size_retained, size_discarded, percent_retained);
262 real_time = tv_diff(&stop_tv, &start_tv);
263 user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
264 system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
267 printf("Statistics:\n");
268 printf("%10.2f sec of real time\n", real_time);
269 printf("%10.2f sec of user time,\n", user_time);
270 printf("%10.2f sec of system time.\n", system_time);
272 printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
273 real_time, user_time, system_time);
276 gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
278 printf("%10.2f M bytes/sec collected.\n", gc_rate);
280 /* os_flush_icache((os_vm_address_t) 0, sizeof(unsigned long)); */
281 /* Maybe FIXME: it's possible that we could significantly reduce
282 * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
283 * similar os-dependent tricks here */
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 or 0x7FFFFFFFFFFFFFFF ? */
333 lip_offset = 0x7FFFFFFF;
334 lip_register_pair = -1;
335 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
340 index = boxed_registers[i];
341 reg = *os_context_register_addr(context, index);
342 /* would be using PTR if not for integer length issues */
343 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
345 if (offset < lip_offset) {
347 lip_register_pair = index;
353 /* Compute the PC's offset from the start of the CODE */
356 *os_context_pc_addr(context) -
357 *os_context_register_addr(context, reg_CODE);
358 #ifdef ARCH_HAS_NPC_REGISTER
360 *os_context_npc_addr(context) -
361 *os_context_register_addr(context, reg_CODE);
363 #ifdef ARCH_HAS_LINK_REGISTER
365 *os_context_lr_addr(context) -
366 *os_context_register_addr(context, reg_CODE);
369 /* Scavenge all boxed registers in the context. */
370 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
374 index = boxed_registers[i];
375 foo = *os_context_register_addr(context,index);
376 scavenge((lispobj *) &foo, 1);
377 *os_context_register_addr(context,index) = foo;
379 /* this is unlikely to work as intended on bigendian
380 * 64 bit platforms */
383 os_context_register_addr(context, index), 1);
388 *os_context_register_addr(context, reg_LIP) =
389 *os_context_register_addr(context, lip_register_pair) + lip_offset;
392 /* Fix the PC if it was in from space */
393 if (from_space_p(*os_context_pc_addr(context)))
394 *os_context_pc_addr(context) =
395 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
396 #ifdef ARCH_HAS_LINK_REGISTER
397 /* Fix the LR ditto; important if we're being called from
398 * an assembly routine that expects to return using blr, otherwise
400 if (from_space_p(*os_context_lr_addr(context)))
401 *os_context_lr_addr(context) =
402 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
405 #ifdef ARCH_HAS_NPC_REGISTER
406 if (from_space_p(*os_context_npc_addr(context)))
407 *os_context_npc_addr(context) =
408 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
412 void scavenge_interrupt_contexts(void)
415 os_context_t *context;
417 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX));
419 #ifdef DEBUG_SCAVENGE_VERBOSE
420 fprintf(stderr, "%d interrupt contexts to scan\n",index);
422 for (i = 0; i < index; i++) {
423 context = lisp_interrupt_contexts[i];
424 scavenge_interrupt_context(context);
432 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
435 int total_words_not_copied;
437 printf("Scanning from space ...\n");
439 total_words_not_copied = 0;
441 while (start < from_space_free_pointer) {
443 int forwardp, type, nwords;
447 forwardp = is_lisp_pointer(object) && new_space_p(object);
453 tag = lowtag_of(object);
456 case LIST_POINTER_LOWTAG:
459 case INSTANCE_POINTER_LOWTAG:
460 printf("Don't know about instances yet!\n");
463 case FUN_POINTER_LOWTAG:
466 case OTHER_POINTER_LOWTAG:
467 pointer = (lispobj *) native_pointer(object);
469 type = widetag_of(header);
470 nwords = (sizetab[type])(pointer);
472 default: nwords=1; /* shut yer whinging, gcc */
475 type = widetag_of(object);
476 nwords = (sizetab[type])(start);
477 total_words_not_copied += nwords;
478 printf("%4d words not copied at 0x%16lx; ",
479 nwords, (unsigned long) start);
480 printf("Header word is 0x%08x\n",
481 (unsigned int) object);
485 printf("%d total words not copied.\n", total_words_not_copied);
489 /* code and code-related objects */
491 /* FIXME (1) this could probably be defined using something like
492 * sizeof(lispobj)*floor(sizeof(struct simple_fun)/sizeof(lispobj))
493 * - FUN_POINTER_LOWTAG
494 * as I'm reasonably sure that simple_fun->code must always be the
495 * last slot in the object
497 * FIXME (2) it also appears in purify.c, and it has a different value
498 * for SPARC users in that bit
501 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
503 /* Note: on the sparc we don't have to do anything special for fdefns, */
504 /* 'cause the raw-addr has a function lowtag. */
505 #ifndef LISP_FEATURE_SPARC
507 scav_fdefn(lispobj *where, lispobj object)
511 fdefn = (struct fdefn *)where;
513 if ((char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET)
514 == (char *)((unsigned long)(fdefn->raw_addr))) {
515 scavenge(where + 1, sizeof(struct fdefn)/sizeof(lispobj) - 1);
517 (u32) ((char *) LOW_WORD(fdefn->fun)) + FUN_RAW_ADDR_OFFSET;
518 return sizeof(struct fdefn) / sizeof(lispobj);
527 /* vector-like objects */
529 /* #define NWORDS(x,y) (CEILING((x),(y)) / (y)) */
532 scav_vector(lispobj *where, lispobj object)
534 if (HeaderValue(object) == subtype_VectorValidHashing) {
536 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
545 #define WEAK_POINTER_NWORDS \
546 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
549 scav_weak_pointer(lispobj *where, lispobj object)
551 /* Do not let GC scavenge the value slot of the weak pointer */
552 /* (that is why it is a weak pointer). Note: we could use */
553 /* the scav_unboxed method here. */
555 return WEAK_POINTER_NWORDS;
559 /* initialization. if gc_init can be moved to after core load, we could
560 * combine these two functions */
566 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
567 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
571 gc_initialize_pointers(void)
573 /* FIXME: We do nothing here. We (briefly) misguidedly attempted
574 to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
575 forgetting that (a) actually it could be the other and (b) it's
576 set in coreparse.c anyway. There's a FIXME note left here to
577 note that current_dynamic_space is a violation of OAOO: we can
578 tell which dynamic space we're currently in by looking at
579 dynamic_space_free_pointer. -- CSR, 2002-08-09 */
585 /* noise to manipulate the gc trigger stuff */
587 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
589 os_vm_address_t addr=(os_vm_address_t)current_dynamic_space
592 long length = DYNAMIC_SPACE_SIZE - dynamic_usage;
594 if (addr < (os_vm_address_t)dynamic_space_free_pointer) {
596 "set_auto_gc_trigger: tried to set gc trigger too low! (%d < %p)\n",
597 (unsigned int)dynamic_usage,
598 (os_vm_address_t)dynamic_space_free_pointer
599 - (os_vm_address_t)current_dynamic_space);
602 else if (length < 0) {
604 "set_auto_gc_trigger: tried to set gc trigger too high! (%p)\n",
609 addr=os_round_up_to_page(addr);
610 length=os_trunc_size_to_page(length);
612 #if defined(SUNOS) || defined(SOLARIS)
613 os_invalidate(addr,length);
615 os_protect(addr, length, 0);
618 current_auto_gc_trigger = (lispobj *)addr;
621 void clear_auto_gc_trigger(void)
623 if (current_auto_gc_trigger!=NULL){
624 #if defined(SUNOS) || defined(SOLARIS)/* don't want to force whole space into swapping mode... */
625 os_vm_address_t addr=(os_vm_address_t)current_auto_gc_trigger;
627 DYNAMIC_SPACE_SIZE + (os_vm_address_t)current_dynamic_space - addr;
629 os_validate(addr,length);
631 os_protect((os_vm_address_t)current_dynamic_space,
636 current_auto_gc_trigger = NULL;