6679f111fcb866f7faeb833b8d3aebe468901e09
[sbcl.git] / src / runtime / cheneygc.c
1 /*
2  * stop and copy GC based on Cheney's algorithm
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
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.
14  */
15
16 #include <stdio.h>
17 #include <sys/time.h>
18 #include <sys/resource.h>
19 #include <signal.h>
20 #include "sbcl.h"
21 #include "runtime.h"
22 #include "os.h"
23 #include "gc.h"
24 #include "gc-internal.h"
25 #include "globals.h"
26 #include "interrupt.h"
27 #include "validate.h"
28 #include "lispregs.h"
29 #include "interr.h"
30 #include "genesis/static-symbols.h"
31 #include "genesis/primitive-objects.h"
32 #include "thread.h"
33
34 /* So you need to debug? */
35 #if 0
36 #define PRINTNOISE
37 #define DEBUG_SPACE_PREDICATES
38 #define DEBUG_SCAVENGE_VERBOSE
39 #define DEBUG_COPY_VERBOSE
40 #define DEBUG_CODE_GC
41 #endif
42
43 lispobj *from_space;
44 lispobj *from_space_free_pointer;
45
46 lispobj *new_space;
47 lispobj *new_space_free_pointer;
48
49 static void scavenge_newspace(void);
50 static void scavenge_interrupt_contexts(void);
51
52 extern unsigned long bytes_consed_between_gcs;
53
54 \f
55 /* collecting garbage */
56
57 #ifdef PRINTNOISE
58 static double
59 tv_diff(struct timeval *x, struct timeval *y)
60 {
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));
63 }
64 #endif
65
66 #define BYTES_ZERO_BEFORE_END (1<<12)
67
68 /* FIXME do we need this?  Doesn't it duplicate lisp code in
69  * scrub-control-stack? */
70
71 static void
72 zero_stack(void)
73 {
74     lispobj *ptr = current_control_stack_pointer;
75  search:
76     do {
77         if (*ptr)
78             goto fill;
79         ptr++;
80     } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
81     return;
82  fill:
83     do {
84         *ptr++ = 0;
85     } while (((unsigned long)ptr) & (BYTES_ZERO_BEFORE_END-1));
86
87     goto search;
88 }
89
90
91 void *
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);
95     return new;
96 }
97
98 lispobj  copy_large_unboxed_object(lispobj object, long nwords) {
99     return copy_object(object,nwords);
100 }
101 lispobj  copy_unboxed_object(lispobj object, long nwords) {
102     return copy_object(object,nwords);
103 }
104 lispobj  copy_large_object(lispobj object, long nwords) {
105     return copy_object(object,nwords);
106 }
107
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. */
111 void
112 collect_garbage(unsigned ignore)
113 {
114 #ifdef PRINTNOISE
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;
120 #endif
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;
125     sigset_t tmp, old;
126     struct thread *th=arch_os_get_current_thread();
127     struct interrupt_data *data=th->interrupt_data;
128
129
130 #ifdef PRINTNOISE
131     printf("[Collecting garbage ... \n");
132
133     getrusage(RUSAGE_SELF, &start_rusage);
134     gettimeofday(&start_tv, (struct timezone *) 0);
135 #endif
136
137     /* it's possible that signals are blocked already if this was called
138      * from a signal handler (e.g. with the sigsegv gc_trigger stuff) */
139     sigemptyset(&tmp);
140     sigaddset_blockable(&tmp);
141     thread_sigmask(SIG_BLOCK, &tmp, &old);
142
143     current_static_space_free_pointer =
144         (lispobj *) ((unsigned long)
145                      SymbolValue(STATIC_SPACE_FREE_POINTER,0));
146
147
148     /* Set up from space and new space pointers. */
149
150     from_space = current_dynamic_space;
151     from_space_free_pointer = dynamic_space_free_pointer;
152
153 #ifdef PRINTNOISE
154     fprintf(stderr,"from_space = %lx\n",
155             (unsigned long) current_dynamic_space);
156 #endif
157     if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
158         new_space = (lispobj *)DYNAMIC_1_SPACE_START;
159     else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
160         new_space = (lispobj *) DYNAMIC_0_SPACE_START;
161     else {
162         lose("GC lossage.  Current dynamic space is bogus!\n");
163     }
164     new_space_free_pointer = new_space;
165
166     /* Initialize the weak pointer list. */
167     weak_pointers = (struct weak_pointer *) NULL;
168
169
170     /* Scavenge all of the roots. */
171 #ifdef PRINTNOISE
172     printf("Scavenging interrupt contexts ...\n");
173 #endif
174     scavenge_interrupt_contexts();
175
176 #ifdef PRINTNOISE
177     printf("Scavenging interrupt handlers (%d bytes) ...\n",
178            (int)sizeof(interrupt_handlers));
179 #endif
180     scavenge((lispobj *) data->interrupt_handlers,
181              sizeof(data->interrupt_handlers) / sizeof(lispobj));
182
183     /* _size quantities are in units of sizeof(lispobj) - i.e. 4 */
184     control_stack_size =
185         current_control_stack_pointer-
186         (lispobj *)th->control_stack_start;
187 #ifdef PRINTNOISE
188     printf("Scavenging the control stack at %p (%ld words) ...\n",
189            ((lispobj *)th->control_stack_start),
190            control_stack_size);
191 #endif
192     scavenge(((lispobj *)th->control_stack_start), control_stack_size);
193
194
195     binding_stack_size =
196         current_binding_stack_pointer -
197         (lispobj *)th->binding_stack_start;
198 #ifdef PRINTNOISE
199     printf("Scavenging the binding stack %x - %x (%d words) ...\n",
200            th->binding_stack_start,current_binding_stack_pointer,
201            (int)(binding_stack_size));
202 #endif
203     scavenge(((lispobj *)th->binding_stack_start), binding_stack_size);
204
205     static_space_size =
206         current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
207 #ifdef PRINTNOISE
208     printf("Scavenging static space %x - %x (%d words) ...\n",
209            STATIC_SPACE_START,current_static_space_free_pointer,
210            (int)(static_space_size));
211 #endif
212     scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
213
214     /* Scavenge newspace. */
215 #ifdef PRINTNOISE
216     printf("Scavenging new space (%d bytes) ...\n",
217            (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
218 #endif
219     scavenge_newspace();
220
221
222 #if defined(DEBUG_PRINT_GARBAGE)
223     print_garbage(from_space, from_space_free_pointer);
224 #endif
225
226     /* Scan the weak pointers. */
227 #ifdef PRINTNOISE
228     printf("Scanning weak pointers ...\n");
229 #endif
230     scan_weak_pointers();
231
232
233     /* Flip spaces. */
234 #ifdef PRINTNOISE
235     printf("Flipping spaces ...\n");
236 #endif
237
238     /* Maybe FIXME: it's possible that we could significantly reduce
239      * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
240      * similar os-dependent tricks here */
241     os_zero((os_vm_address_t) from_space,
242             (os_vm_size_t) DYNAMIC_SPACE_SIZE);
243
244     current_dynamic_space = new_space;
245     dynamic_space_free_pointer = new_space_free_pointer;
246
247 #ifdef PRINTNOISE
248     size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
249 #endif
250     size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
251
252     os_flush_icache((os_vm_address_t)new_space, size_retained);
253
254     /* Zero stack. */
255 #ifdef PRINTNOISE
256     printf("Zeroing empty part of control stack ...\n");
257 #endif
258     zero_stack();
259     set_auto_gc_trigger(size_retained+bytes_consed_between_gcs);
260     thread_sigmask(SIG_SETMASK, &old, 0);
261
262
263 #ifdef PRINTNOISE
264     gettimeofday(&stop_tv, (struct timezone *) 0);
265     getrusage(RUSAGE_SELF, &stop_rusage);
266
267     printf("done.]\n");
268
269     percent_retained = (((float) size_retained) /
270                         ((float) size_discarded)) * 100.0;
271
272     printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
273            size_retained, size_discarded, percent_retained);
274
275     real_time = tv_diff(&stop_tv, &start_tv);
276     user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
277     system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
278
279     printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
280            real_time, user_time, system_time);
281
282     gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
283
284     printf("%10.2f M bytes/sec collected.\n", gc_rate);
285 #endif
286 }
287
288 \f
289 /* scavenging */
290
291 static void
292 scavenge_newspace(void)
293 {
294     lispobj *here, *next;
295
296     here = new_space;
297     while (here < new_space_free_pointer) {
298         /*      printf("here=%lx, new_space_free_pointer=%lx\n",
299                 here,new_space_free_pointer); */
300         next = new_space_free_pointer;
301         scavenge(here, next - here);
302         here = next;
303     }
304     /* printf("done with newspace\n"); */
305 }
306 \f
307 /* scavenging interrupt contexts */
308
309 static int boxed_registers[] = BOXED_REGISTERS;
310
311 static void
312 scavenge_interrupt_context(os_context_t *context)
313 {
314     int i;
315 #ifdef reg_LIP
316     unsigned long lip;
317     unsigned long lip_offset;
318     int lip_register_pair;
319 #endif
320     unsigned long pc_code_offset;
321 #ifdef ARCH_HAS_LINK_REGISTER
322     unsigned long lr_code_offset;
323 #endif
324 #ifdef ARCH_HAS_NPC_REGISTER
325     unsigned long npc_code_offset;
326 #endif
327 #ifdef DEBUG_SCAVENGE_VERBOSE
328     fprintf(stderr, "Scavenging interrupt context at 0x%x\n",context);
329 #endif
330     /* Find the LIP's register pair and calculate its offset */
331     /* before we scavenge the context. */
332 #ifdef reg_LIP
333     lip = *os_context_register_addr(context, reg_LIP);
334     /* 0x7FFFFFFF on 32-bit platforms;
335        0x7FFFFFFFFFFFFFFF on 64-bit platforms */
336     lip_offset = (((unsigned long)1) << (N_WORD_BITS - 1)) - 1;
337     lip_register_pair = -1;
338     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
339         unsigned long reg;
340         long offset;
341         int index;
342
343         index = boxed_registers[i];
344         reg = *os_context_register_addr(context, index);
345         /* would be using PTR if not for integer length issues */
346         if ((reg & ~((1L<<N_LOWTAG_BITS)-1)) <= lip) {
347             offset = lip - reg;
348             if (offset < lip_offset) {
349                 lip_offset = offset;
350                 lip_register_pair = index;
351             }
352         }
353     }
354 #endif /* reg_LIP */
355
356     /* Compute the PC's offset from the start of the CODE */
357     /* register. */
358     pc_code_offset =
359         *os_context_pc_addr(context) -
360         *os_context_register_addr(context, reg_CODE);
361 #ifdef ARCH_HAS_NPC_REGISTER
362     npc_code_offset =
363         *os_context_npc_addr(context) -
364         *os_context_register_addr(context, reg_CODE);
365 #endif
366 #ifdef ARCH_HAS_LINK_REGISTER
367     lr_code_offset =
368         *os_context_lr_addr(context) -
369         *os_context_register_addr(context, reg_CODE);
370 #endif
371
372     /* Scavenge all boxed registers in the context. */
373     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
374         int index;
375         lispobj foo;
376
377         index = boxed_registers[i];
378         foo = *os_context_register_addr(context,index);
379         scavenge((lispobj *) &foo, 1);
380         *os_context_register_addr(context,index) = foo;
381
382         /* this is unlikely to work as intended on bigendian
383          * 64 bit platforms */
384
385         scavenge((lispobj *)
386                  os_context_register_addr(context, index), 1);
387     }
388
389 #ifdef reg_LIP
390     /* Fix the LIP */
391     *os_context_register_addr(context, reg_LIP) =
392         *os_context_register_addr(context, lip_register_pair) + lip_offset;
393 #endif /* reg_LIP */
394
395     /* Fix the PC if it was in from space */
396     if (from_space_p(*os_context_pc_addr(context)))
397         *os_context_pc_addr(context) =
398             *os_context_register_addr(context, reg_CODE) + pc_code_offset;
399 #ifdef ARCH_HAS_LINK_REGISTER
400     /* Fix the LR ditto; important if we're being called from
401      * an assembly routine that expects to return using blr, otherwise
402      * harmless */
403     if (from_space_p(*os_context_lr_addr(context)))
404         *os_context_lr_addr(context) =
405             *os_context_register_addr(context, reg_CODE) + lr_code_offset;
406 #endif
407
408 #ifdef ARCH_HAS_NPC_REGISTER
409     if (from_space_p(*os_context_npc_addr(context)))
410         *os_context_npc_addr(context) =
411             *os_context_register_addr(context, reg_CODE) + npc_code_offset;
412 #endif
413 }
414
415 void scavenge_interrupt_contexts(void)
416 {
417     int i, index;
418     os_context_t *context;
419
420     struct thread *th=arch_os_get_current_thread();
421
422     index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,0));
423
424
425 #ifdef DEBUG_SCAVENGE_VERBOSE
426     fprintf(stderr, "%d interrupt contexts to scan\n",index);
427 #endif
428     for (i = 0; i < index; i++) {
429         context = th->interrupt_contexts[i];
430         scavenge_interrupt_context(context);
431     }
432 }
433
434 \f
435 /* debugging code */
436
437 void
438 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
439 {
440     lispobj *start;
441     int total_words_not_copied;
442
443     printf("Scanning from space ...\n");
444
445     total_words_not_copied = 0;
446     start = from_space;
447     while (start < from_space_free_pointer) {
448         lispobj object;
449         int forwardp, type, nwords;
450         lispobj header;
451
452         object = *start;
453         forwardp = is_lisp_pointer(object) && new_space_p(object);
454
455         if (forwardp) {
456             int tag;
457             lispobj *pointer;
458
459             tag = lowtag_of(object);
460
461             switch (tag) {
462             case LIST_POINTER_LOWTAG:
463                 nwords = 2;
464                 break;
465             case INSTANCE_POINTER_LOWTAG:
466                 printf("Don't know about instances yet!\n");
467                 nwords = 1;
468                 break;
469             case FUN_POINTER_LOWTAG:
470                 nwords = 1;
471                 break;
472             case OTHER_POINTER_LOWTAG:
473                 pointer = (lispobj *) native_pointer(object);
474                 header = *pointer;
475                 type = widetag_of(header);
476                 nwords = (sizetab[type])(pointer);
477                 break;
478             default: nwords=1;  /* shut yer whinging, gcc */
479             }
480         } else {
481             type = widetag_of(object);
482             nwords = (sizetab[type])(start);
483             total_words_not_copied += nwords;
484             printf("%4d words not copied at 0x%16lx; ",
485                    nwords, (unsigned long) start);
486             printf("Header word is 0x%08x\n",
487                    (unsigned int) object);
488         }
489         start += nwords;
490     }
491     printf("%d total words not copied.\n", total_words_not_copied);
492 }
493
494 \f
495 /* vector-like objects */
496
497 static long
498 scav_vector(lispobj *where, lispobj object)
499 {
500     if (HeaderValue(object) == subtype_VectorValidHashing) {
501         *where =
502             (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
503     }
504
505     return 1;
506 }
507
508 \f
509 /* weak pointers */
510
511 #define WEAK_POINTER_NWORDS \
512         CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
513
514 static long
515 scav_weak_pointer(lispobj *where, lispobj object)
516 {
517     /* Do not let GC scavenge the value slot of the weak pointer */
518     /* (that is why it is a weak pointer).  Note:  we could use */
519     /* the scav_unboxed method here. */
520
521     return WEAK_POINTER_NWORDS;
522 }
523 \f
524 lispobj *
525 search_read_only_space(void *pointer)
526 {
527     lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
528     lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
529     if ((pointer < (void *)start) || (pointer >= (void *)end))
530         return NULL;
531     return (gc_search_space(start,
532                             (((lispobj *)pointer)+2)-start,
533                             (lispobj *)pointer));
534 }
535
536 lispobj *
537 search_static_space(void *pointer)
538 {
539     lispobj* start = (lispobj*)STATIC_SPACE_START;
540     lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
541     if ((pointer < (void *)start) || (pointer >= (void *)end))
542         return NULL;
543     return (gc_search_space(start,
544                             (((lispobj *)pointer)+2)-start,
545                             (lispobj *)pointer));
546 }
547
548 lispobj *
549 search_dynamic_space(void *pointer)
550 {
551     lispobj *start = (lispobj *) current_dynamic_space;
552     lispobj *end = (lispobj *) dynamic_space_free_pointer;
553     if ((pointer < (void *)start) || (pointer >= (void *)end))
554         return NULL;
555     return (gc_search_space(start,
556                             (((lispobj *)pointer)+2)-start,
557                             (lispobj *)pointer));
558 }
559 \f
560 /* initialization.  if gc_init can be moved to after core load, we could
561  * combine these two functions */
562
563 void
564 gc_init(void)
565 {
566     gc_init_tables();
567     scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
568     scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
569 }
570
571 void
572 gc_initialize_pointers(void)
573 {
574     /* FIXME: We do nothing here.  We (briefly) misguidedly attempted
575        to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
576        forgetting that (a) actually it could be the other and (b) it's
577        set in coreparse.c anyway.  There's a FIXME note left here to
578        note that current_dynamic_space is a violation of OAOO: we can
579        tell which dynamic space we're currently in by looking at
580        dynamic_space_free_pointer.  -- CSR, 2002-08-09 */
581 }
582
583
584
585 \f
586 /* noise to manipulate the gc trigger stuff */
587
588 /* Functions that substantially change the dynamic space free pointer
589  * (collect_garbage, purify) are responsible also for resettting the
590  * auto_gc_trigger */
591 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
592 {
593     os_vm_address_t addr=(os_vm_address_t)current_dynamic_space
594         + dynamic_usage;
595     long length = DYNAMIC_SPACE_SIZE - dynamic_usage;
596
597     if (addr < (os_vm_address_t)dynamic_space_free_pointer) {
598         fprintf(stderr,
599            "set_auto_gc_trigger: tried to set gc trigger too low! (%ld < 0x%08lx)\n",
600                 (unsigned long)dynamic_usage,
601                 (unsigned long)((os_vm_address_t)dynamic_space_free_pointer
602                                 - (os_vm_address_t)current_dynamic_space));
603         lose("lost");
604     }
605     else if (length < 0) {
606         fprintf(stderr,
607                 "set_auto_gc_trigger: tried to set gc trigger too high! (0x%08lx)\n",
608                 (unsigned long)dynamic_usage);
609         lose("lost");
610     }
611
612     addr=os_round_up_to_page(addr);
613     length=os_trunc_size_to_page(length);
614
615 #if defined(SUNOS) || defined(SOLARIS)
616     os_invalidate(addr,length);
617 #else
618     os_protect(addr, length, 0);
619 #endif
620
621     current_auto_gc_trigger = (lispobj *)addr;
622 }
623
624 void clear_auto_gc_trigger(void)
625 {
626     if (current_auto_gc_trigger!=NULL){
627 #if defined(SUNOS) || defined(SOLARIS)/* don't want to force whole space into swapping mode... */
628         os_vm_address_t addr=(os_vm_address_t)current_auto_gc_trigger;
629         os_vm_size_t length=
630             DYNAMIC_SPACE_SIZE + (os_vm_address_t)current_dynamic_space - addr;
631
632         os_validate(addr,length);
633 #else
634         os_protect((os_vm_address_t)current_dynamic_space,
635                    DYNAMIC_SPACE_SIZE,
636                    OS_VM_PROT_ALL);
637 #endif
638
639         current_auto_gc_trigger = NULL;
640     }
641 }