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);
51 extern struct interrupt_data * global_interrupt_data;
53 extern unsigned long bytes_consed_between_gcs;
56 /* collecting garbage */
60 tv_diff(struct timeval *x, struct timeval *y)
62 return (((double) x->tv_sec + (double) x->tv_usec * 1.0e-6) -
63 ((double) y->tv_sec + (double) y->tv_usec * 1.0e-6));
67 #define BYTES_ZERO_BEFORE_END (1<<12)
69 /* FIXME do we need this? Doesn't it duplicate lisp code in
70 * scrub-control-stack? */
75 u32 *ptr = (u32 *)current_control_stack_pointer;
81 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
86 } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
93 gc_general_alloc(int bytes, int unboxed_p, int quick_p) {
94 lispobj *new=new_space_free_pointer;
95 new_space_free_pointer+=(bytes/4);
99 lispobj copy_large_unboxed_object(lispobj object, int nwords) {
100 return copy_object(object,nwords);
102 lispobj copy_unboxed_object(lispobj object, int nwords) {
103 return copy_object(object,nwords);
105 lispobj copy_large_object(lispobj object, int nwords) {
106 return copy_object(object,nwords);
109 /* Note: The generic GC interface we're implementing passes us a
110 * last_generation argument. That's meaningless for us, since we're
111 * not a generational GC. So we ignore it. */
113 collect_garbage(unsigned ignore)
116 struct timeval start_tv, stop_tv;
117 struct rusage start_rusage, stop_rusage;
118 double real_time, system_time, user_time;
119 double percent_retained, gc_rate;
120 unsigned long size_discarded;
122 unsigned long size_retained;
123 lispobj *current_static_space_free_pointer;
124 unsigned long static_space_size;
125 unsigned long control_stack_size, binding_stack_size;
127 struct thread *th=arch_os_get_current_thread();
128 struct interrupt_data *data=
129 th ? th->interrupt_data : global_interrupt_data;
133 printf("[Collecting garbage ... \n");
135 getrusage(RUSAGE_SELF, &start_rusage);
136 gettimeofday(&start_tv, (struct timezone *) 0);
139 /* it's possible that signals are blocked already if this was called
140 * from a signal handler (e.g. with the sigsegv gc_trigger stuff) */
142 sigaddset_blockable(&tmp);
143 sigprocmask(SIG_BLOCK, &tmp, &old);
145 current_static_space_free_pointer =
146 (lispobj *) ((unsigned long)
147 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
150 /* Set up from space and new space pointers. */
152 from_space = current_dynamic_space;
153 from_space_free_pointer = dynamic_space_free_pointer;
156 fprintf(stderr,"from_space = %lx\n",
157 (unsigned long) current_dynamic_space);
159 if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
160 new_space = (lispobj *)DYNAMIC_1_SPACE_START;
161 else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
162 new_space = (lispobj *) DYNAMIC_0_SPACE_START;
164 lose("GC lossage. Current dynamic space is bogus!\n");
166 new_space_free_pointer = new_space;
168 /* Initialize the weak pointer list. */
169 weak_pointers = (struct weak_pointer *) NULL;
172 /* Scavenge all of the roots. */
174 printf("Scavenging interrupt contexts ...\n");
176 scavenge_interrupt_contexts();
179 printf("Scavenging interrupt handlers (%d bytes) ...\n",
180 (int)sizeof(interrupt_handlers));
182 scavenge((lispobj *) data->interrupt_handlers,
183 sizeof(data->interrupt_handlers) / sizeof(lispobj));
185 /* _size quantities are in units of sizeof(lispobj) - i.e. 4 */
187 current_control_stack_pointer-
188 (lispobj *)th->control_stack_start;
190 printf("Scavenging the control stack at %p (%ld words) ...\n",
191 ((lispobj *)th->control_stack_start),
194 scavenge(((lispobj *)th->control_stack_start), control_stack_size);
198 current_binding_stack_pointer -
199 (lispobj *)th->binding_stack_start;
201 printf("Scavenging the binding stack %x - %x (%d words) ...\n",
202 th->binding_stack_start,current_binding_stack_pointer,
203 (int)(binding_stack_size));
205 scavenge(((lispobj *)th->binding_stack_start), binding_stack_size);
208 current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
210 printf("Scavenging static space %x - %x (%d words) ...\n",
211 STATIC_SPACE_START,current_static_space_free_pointer,
212 (int)(static_space_size));
214 scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
216 /* Scavenge newspace. */
218 printf("Scavenging new space (%d bytes) ...\n",
219 (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
224 #if defined(DEBUG_PRINT_GARBAGE)
225 print_garbage(from_space, from_space_free_pointer);
228 /* Scan the weak pointers. */
230 printf("Scanning weak pointers ...\n");
232 scan_weak_pointers();
237 printf("Flipping spaces ...\n");
240 os_zero((os_vm_address_t) current_dynamic_space,
241 (os_vm_size_t) DYNAMIC_SPACE_SIZE);
243 current_dynamic_space = new_space;
244 dynamic_space_free_pointer = new_space_free_pointer;
247 size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
249 size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
253 printf("Zeroing empty part of control stack ...\n");
256 set_auto_gc_trigger(size_retained+bytes_consed_between_gcs);
257 sigprocmask(SIG_SETMASK, &old, 0);
261 gettimeofday(&stop_tv, (struct timezone *) 0);
262 getrusage(RUSAGE_SELF, &stop_rusage);
266 percent_retained = (((float) size_retained) /
267 ((float) size_discarded)) * 100.0;
269 printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
270 size_retained, size_discarded, percent_retained);
272 real_time = tv_diff(&stop_tv, &start_tv);
273 user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
274 system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
277 printf("Statistics:\n");
278 printf("%10.2f sec of real time\n", real_time);
279 printf("%10.2f sec of user time,\n", user_time);
280 printf("%10.2f sec of system time.\n", system_time);
282 printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
283 real_time, user_time, system_time);
286 gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
288 printf("%10.2f M bytes/sec collected.\n", gc_rate);
290 /* os_flush_icache((os_vm_address_t) 0, sizeof(unsigned long)); */
291 /* Maybe FIXME: it's possible that we could significantly reduce
292 * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
293 * similar os-dependent tricks here */
300 scavenge_newspace(void)
302 lispobj *here, *next;
305 while (here < new_space_free_pointer) {
306 /* printf("here=%lx, new_space_free_pointer=%lx\n",
307 here,new_space_free_pointer); */
308 next = new_space_free_pointer;
309 scavenge(here, next - here);
312 /* printf("done with newspace\n"); */
315 /* scavenging interrupt contexts */
317 static int boxed_registers[] = BOXED_REGISTERS;
320 scavenge_interrupt_context(os_context_t *context)
325 unsigned long lip_offset;
326 int lip_register_pair;
328 unsigned long pc_code_offset;
329 #ifdef ARCH_HAS_LINK_REGISTER
330 unsigned long lr_code_offset;
332 #ifdef ARCH_HAS_NPC_REGISTER
333 unsigned long npc_code_offset;
335 #ifdef DEBUG_SCAVENGE_VERBOSE
336 fprintf(stderr, "Scavenging interrupt context at 0x%x\n",context);
338 /* Find the LIP's register pair and calculate its offset */
339 /* before we scavenge the context. */
341 lip = *os_context_register_addr(context, reg_LIP);
342 /* 0x7FFFFFFF or 0x7FFFFFFFFFFFFFFF ? */
343 lip_offset = 0x7FFFFFFF;
344 lip_register_pair = -1;
345 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
350 index = boxed_registers[i];
351 reg = *os_context_register_addr(context, index);
352 /* would be using PTR if not for integer length issues */
353 if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
355 if (offset < lip_offset) {
357 lip_register_pair = index;
363 /* Compute the PC's offset from the start of the CODE */
366 *os_context_pc_addr(context) -
367 *os_context_register_addr(context, reg_CODE);
368 #ifdef ARCH_HAS_NPC_REGISTER
370 *os_context_npc_addr(context) -
371 *os_context_register_addr(context, reg_CODE);
373 #ifdef ARCH_HAS_LINK_REGISTER
375 *os_context_lr_addr(context) -
376 *os_context_register_addr(context, reg_CODE);
379 /* Scavenge all boxed registers in the context. */
380 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
384 index = boxed_registers[i];
385 foo = *os_context_register_addr(context,index);
386 scavenge((lispobj *) &foo, 1);
387 *os_context_register_addr(context,index) = foo;
389 /* this is unlikely to work as intended on bigendian
390 * 64 bit platforms */
393 os_context_register_addr(context, index), 1);
398 *os_context_register_addr(context, reg_LIP) =
399 *os_context_register_addr(context, lip_register_pair) + lip_offset;
402 /* Fix the PC if it was in from space */
403 if (from_space_p(*os_context_pc_addr(context)))
404 *os_context_pc_addr(context) =
405 *os_context_register_addr(context, reg_CODE) + pc_code_offset;
406 #ifdef ARCH_HAS_LINK_REGISTER
407 /* Fix the LR ditto; important if we're being called from
408 * an assembly routine that expects to return using blr, otherwise
410 if (from_space_p(*os_context_lr_addr(context)))
411 *os_context_lr_addr(context) =
412 *os_context_register_addr(context, reg_CODE) + lr_code_offset;
415 #ifdef ARCH_HAS_NPC_REGISTER
416 if (from_space_p(*os_context_npc_addr(context)))
417 *os_context_npc_addr(context) =
418 *os_context_register_addr(context, reg_CODE) + npc_code_offset;
422 void scavenge_interrupt_contexts(void)
425 os_context_t *context;
427 struct thread *th=arch_os_get_current_thread();
428 struct interrupt_data *data=
429 th ? th->interrupt_data : global_interrupt_data;
431 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
434 #ifdef DEBUG_SCAVENGE_VERBOSE
435 fprintf(stderr, "%d interrupt contexts to scan\n",index);
437 for (i = 0; i < index; i++) {
438 context = th->interrupt_contexts[i];
439 scavenge_interrupt_context(context);
447 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
450 int total_words_not_copied;
452 printf("Scanning from space ...\n");
454 total_words_not_copied = 0;
456 while (start < from_space_free_pointer) {
458 int forwardp, type, nwords;
462 forwardp = is_lisp_pointer(object) && new_space_p(object);
468 tag = lowtag_of(object);
471 case LIST_POINTER_LOWTAG:
474 case INSTANCE_POINTER_LOWTAG:
475 printf("Don't know about instances yet!\n");
478 case FUN_POINTER_LOWTAG:
481 case OTHER_POINTER_LOWTAG:
482 pointer = (lispobj *) native_pointer(object);
484 type = widetag_of(header);
485 nwords = (sizetab[type])(pointer);
487 default: nwords=1; /* shut yer whinging, gcc */
490 type = widetag_of(object);
491 nwords = (sizetab[type])(start);
492 total_words_not_copied += nwords;
493 printf("%4d words not copied at 0x%16lx; ",
494 nwords, (unsigned long) start);
495 printf("Header word is 0x%08x\n",
496 (unsigned int) object);
500 printf("%d total words not copied.\n", total_words_not_copied);
504 /* code and code-related objects */
506 /* FIXME (1) this could probably be defined using something like
507 * sizeof(lispobj)*floor(sizeof(struct simple_fun)/sizeof(lispobj))
508 * - FUN_POINTER_LOWTAG
509 * as I'm reasonably sure that simple_fun->code must always be the
510 * last slot in the object
512 * FIXME (2) it also appears in purify.c, and it has a different value
513 * for SPARC users in that bit
516 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
518 /* Note: on the sparc we don't have to do anything special for fdefns, */
519 /* 'cause the raw-addr has a function lowtag. */
520 #ifndef LISP_FEATURE_SPARC
522 scav_fdefn(lispobj *where, lispobj object)
526 fdefn = (struct fdefn *)where;
528 if ((char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET)
529 == (char *)((unsigned long)(fdefn->raw_addr))) {
530 scavenge(where + 1, sizeof(struct fdefn)/sizeof(lispobj) - 1);
532 (u32) ((char *) LOW_WORD(fdefn->fun)) + FUN_RAW_ADDR_OFFSET;
533 return sizeof(struct fdefn) / sizeof(lispobj);
542 /* vector-like objects */
544 /* #define NWORDS(x,y) (CEILING((x),(y)) / (y)) */
547 scav_vector(lispobj *where, lispobj object)
549 if (HeaderValue(object) == subtype_VectorValidHashing) {
551 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
560 #define WEAK_POINTER_NWORDS \
561 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
564 scav_weak_pointer(lispobj *where, lispobj object)
566 /* Do not let GC scavenge the value slot of the weak pointer */
567 /* (that is why it is a weak pointer). Note: we could use */
568 /* the scav_unboxed method here. */
570 return WEAK_POINTER_NWORDS;
574 /* initialization. if gc_init can be moved to after core load, we could
575 * combine these two functions */
581 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
582 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
586 gc_initialize_pointers(void)
588 /* FIXME: We do nothing here. We (briefly) misguidedly attempted
589 to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
590 forgetting that (a) actually it could be the other and (b) it's
591 set in coreparse.c anyway. There's a FIXME note left here to
592 note that current_dynamic_space is a violation of OAOO: we can
593 tell which dynamic space we're currently in by looking at
594 dynamic_space_free_pointer. -- CSR, 2002-08-09 */
600 /* noise to manipulate the gc trigger stuff */
602 /* Functions that substantially change the dynamic space free pointer
603 * (collect_garbage, purify) are responsible also for resettting the
605 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
607 os_vm_address_t addr=(os_vm_address_t)current_dynamic_space
609 long length = DYNAMIC_SPACE_SIZE - dynamic_usage;
611 if (addr < (os_vm_address_t)dynamic_space_free_pointer) {
613 "set_auto_gc_trigger: tried to set gc trigger too low! (%d < %p)\n",
614 (unsigned int)dynamic_usage,
615 (os_vm_address_t)dynamic_space_free_pointer
616 - (os_vm_address_t)current_dynamic_space);
619 else if (length < 0) {
621 "set_auto_gc_trigger: tried to set gc trigger too high! (%p)\n",
626 addr=os_round_up_to_page(addr);
627 length=os_trunc_size_to_page(length);
629 #if defined(SUNOS) || defined(SOLARIS)
630 os_invalidate(addr,length);
632 os_protect(addr, length, 0);
635 current_auto_gc_trigger = (lispobj *)addr;
638 void clear_auto_gc_trigger(void)
640 if (current_auto_gc_trigger!=NULL){
641 #if defined(SUNOS) || defined(SOLARIS)/* don't want to force whole space into swapping mode... */
642 os_vm_address_t addr=(os_vm_address_t)current_auto_gc_trigger;
644 DYNAMIC_SPACE_SIZE + (os_vm_address_t)current_dynamic_space - addr;
646 os_validate(addr,length);
648 os_protect((os_vm_address_t)current_dynamic_space,
653 current_auto_gc_trigger = NULL;