Unbreak sparc build.
[sbcl.git] / src / runtime / cheneygc.c
blob35bf6c41987bd4f5d6fe03701af397130ef8cca1
1 /*
2 * stop and copy GC based on Cheney's algorithm
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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.
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 #include "arch.h"
35 /* So you need to debug? */
36 #if 0
37 #define PRINTNOISE
38 #define DEBUG_SPACE_PREDICATES
39 #define DEBUG_SCAVENGE_VERBOSE
40 #define DEBUG_COPY_VERBOSE
41 #define DEBUG_CODE_GC
42 #endif
44 lispobj *from_space;
45 lispobj *from_space_free_pointer;
47 lispobj *new_space;
48 lispobj *new_space_free_pointer;
50 static void scavenge_newspace(void);
53 /* collecting garbage */
55 #ifdef PRINTNOISE
56 static double
57 tv_diff(struct timeval *x, struct timeval *y)
59 return (((double) x->tv_sec + (double) x->tv_usec * 1.0e-6) -
60 ((double) y->tv_sec + (double) y->tv_usec * 1.0e-6));
62 #endif
64 void *
65 gc_general_alloc(word_t bytes, int page_type_flag, int quick_p) {
66 lispobj *new=new_space_free_pointer;
67 new_space_free_pointer+=(bytes/N_WORD_BYTES);
68 return new;
71 lispobj copy_large_unboxed_object(lispobj object, sword_t nwords) {
72 return copy_object(object,nwords);
74 lispobj copy_unboxed_object(lispobj object, sword_t nwords) {
75 return copy_object(object,nwords);
77 lispobj copy_large_object(lispobj object, sword_t nwords) {
78 return copy_object(object,nwords);
81 /* Note: The generic GC interface we're implementing passes us a
82 * last_generation argument. That's meaningless for us, since we're
83 * not a generational GC. So we ignore it. */
84 void
85 collect_garbage(generation_index_t ignore)
87 #ifdef PRINTNOISE
88 struct timeval start_tv, stop_tv;
89 struct rusage start_rusage, stop_rusage;
90 double real_time, system_time, user_time;
91 double percent_retained, gc_rate;
92 unsigned long size_discarded;
93 #endif
94 unsigned long size_retained;
95 lispobj *current_static_space_free_pointer;
96 unsigned long static_space_size;
97 unsigned long control_stack_size, binding_stack_size;
98 sigset_t tmp, old;
99 struct thread *th=arch_os_get_current_thread();
101 #ifdef PRINTNOISE
102 printf("[Collecting garbage ... \n");
104 getrusage(RUSAGE_SELF, &start_rusage);
105 gettimeofday(&start_tv, (struct timezone *) 0);
106 #endif
108 /* it's possible that signals are blocked already if this was called
109 * from a signal handler (e.g. with the sigsegv gc_trigger stuff) */
110 block_blockable_signals(&old);
112 current_static_space_free_pointer =
113 (lispobj *) ((unsigned long)
114 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
117 /* Set up from space and new space pointers. */
119 from_space = current_dynamic_space;
120 from_space_free_pointer = dynamic_space_free_pointer;
122 #ifdef PRINTNOISE
123 fprintf(stderr,"from_space = %lx\n",
124 (unsigned long) current_dynamic_space);
125 #endif
126 if (current_dynamic_space == (lispobj *) DYNAMIC_0_SPACE_START)
127 new_space = (lispobj *)DYNAMIC_1_SPACE_START;
128 else if (current_dynamic_space == (lispobj *) DYNAMIC_1_SPACE_START)
129 new_space = (lispobj *) DYNAMIC_0_SPACE_START;
130 else {
131 lose("GC lossage. Current dynamic space is bogus!\n");
133 new_space_free_pointer = new_space;
135 /* Initialize the weak pointer list. */
136 weak_pointers = (struct weak_pointer *) NULL;
139 /* Scavenge all of the roots. */
140 #ifdef PRINTNOISE
141 printf("Scavenging interrupt contexts ...\n");
142 #endif
143 scavenge_interrupt_contexts(th);
145 #ifdef PRINTNOISE
146 printf("Scavenging interrupt handlers (%d bytes) ...\n",
147 (int)sizeof(interrupt_handlers));
148 #endif
149 scavenge((lispobj *) interrupt_handlers,
150 sizeof(interrupt_handlers) / sizeof(lispobj));
152 #ifdef PRINTNOISE
153 printf("Scavenging the control stack ...\n");
154 #endif
155 scavenge_control_stack(th);
158 binding_stack_size =
159 (lispobj *)get_binding_stack_pointer(th) -
160 (lispobj *)th->binding_stack_start;
161 #ifdef PRINTNOISE
162 printf("Scavenging the binding stack %x - %x (%d words) ...\n",
163 th->binding_stack_start,get_binding_stack_pointer(th),
164 (int)(binding_stack_size));
165 #endif
166 scavenge(((lispobj *)th->binding_stack_start), binding_stack_size);
168 static_space_size =
169 current_static_space_free_pointer - (lispobj *) STATIC_SPACE_START;
170 #ifdef PRINTNOISE
171 printf("Scavenging static space %x - %x (%d words) ...\n",
172 STATIC_SPACE_START,current_static_space_free_pointer,
173 (int)(static_space_size));
174 #endif
175 scavenge(((lispobj *)STATIC_SPACE_START), static_space_size);
177 /* Scavenge newspace. */
178 #ifdef PRINTNOISE
179 printf("Scavenging new space (%d bytes) ...\n",
180 (int)((new_space_free_pointer - new_space) * sizeof(lispobj)));
181 #endif
182 scavenge_newspace();
185 #if defined(DEBUG_PRINT_GARBAGE)
186 print_garbage(from_space, from_space_free_pointer);
187 #endif
189 /* Scan the weak pointers. */
190 #ifdef PRINTNOISE
191 printf("Scanning weak hash tables ...\n");
192 #endif
193 scan_weak_hash_tables();
195 /* Scan the weak pointers. */
196 #ifdef PRINTNOISE
197 printf("Scanning weak pointers ...\n");
198 #endif
199 scan_weak_pointers();
201 /* Flip spaces. */
202 #ifdef PRINTNOISE
203 printf("Flipping spaces ...\n");
204 #endif
206 /* Maybe FIXME: it's possible that we could significantly reduce
207 * RSS by zeroing the from_space or madvise(MADV_DONTNEED) or
208 * similar os-dependent tricks here */
209 #ifdef LISP_FEATURE_HPUX
210 /* hpux cant handle unmapping areas that are not 100% mapped */
211 clear_auto_gc_trigger();
212 #endif
213 os_zero((os_vm_address_t) from_space,
214 (os_vm_size_t) dynamic_space_size);
216 current_dynamic_space = new_space;
217 dynamic_space_free_pointer = new_space_free_pointer;
219 #ifdef PRINTNOISE
220 size_discarded = (from_space_free_pointer - from_space) * sizeof(lispobj);
221 #endif
222 size_retained = (new_space_free_pointer - new_space) * sizeof(lispobj);
224 os_flush_icache((os_vm_address_t)new_space, size_retained);
226 /* Zero stack. */
227 #ifdef PRINTNOISE
228 printf("Zeroing empty part of control stack ...\n");
229 #endif
230 scrub_control_stack();
231 set_auto_gc_trigger(size_retained+bytes_consed_between_gcs);
232 thread_sigmask(SIG_SETMASK, &old, 0);
235 #ifdef PRINTNOISE
236 gettimeofday(&stop_tv, (struct timezone *) 0);
237 getrusage(RUSAGE_SELF, &stop_rusage);
239 printf("done.]\n");
241 percent_retained = (((float) size_retained) /
242 ((float) size_discarded)) * 100.0;
244 printf("Total of %ld bytes out of %ld bytes retained (%3.2f%%).\n",
245 size_retained, size_discarded, percent_retained);
247 real_time = tv_diff(&stop_tv, &start_tv);
248 user_time = tv_diff(&stop_rusage.ru_utime, &start_rusage.ru_utime);
249 system_time = tv_diff(&stop_rusage.ru_stime, &start_rusage.ru_stime);
251 printf("Statistics: %10.2fs real, %10.2fs user, %10.2fs system.\n",
252 real_time, user_time, system_time);
254 gc_rate = ((float) size_retained / (float) (1<<20)) / real_time;
256 printf("%10.2f M bytes/sec collected.\n", gc_rate);
257 #endif
261 /* scavenging */
263 static void
264 scavenge_newspace(void)
266 lispobj *here, *next;
268 here = new_space;
269 while (here < new_space_free_pointer) {
270 /* printf("here=%lx, new_space_free_pointer=%lx\n",
271 here,new_space_free_pointer); */
272 next = new_space_free_pointer;
273 scavenge(here, next - here);
274 scav_weak_hash_tables();
275 here = next;
277 /* printf("done with newspace\n"); */
280 /* debugging code */
282 void
283 print_garbage(lispobj *from_space, lispobj *from_space_free_pointer)
285 lispobj *start;
286 int total_words_not_copied;
288 printf("Scanning from space ...\n");
290 total_words_not_copied = 0;
291 start = from_space;
292 while (start < from_space_free_pointer) {
293 lispobj object;
294 int forwardp, type, nwords;
295 lispobj header;
297 object = *start;
298 forwardp = is_lisp_pointer(object) && new_space_p(object);
300 if (forwardp) {
301 int tag;
302 lispobj *pointer;
304 tag = lowtag_of(object);
306 switch (tag) {
307 case LIST_POINTER_LOWTAG:
308 nwords = 2;
309 break;
310 case INSTANCE_POINTER_LOWTAG:
311 printf("Don't know about instances yet!\n");
312 nwords = 1;
313 break;
314 case FUN_POINTER_LOWTAG:
315 nwords = 1;
316 break;
317 case OTHER_POINTER_LOWTAG:
318 pointer = (lispobj *) native_pointer(object);
319 header = *pointer;
320 type = widetag_of(header);
321 nwords = (sizetab[type])(pointer);
322 break;
323 default: nwords=1; /* shut yer whinging, gcc */
325 } else {
326 type = widetag_of(object);
327 nwords = (sizetab[type])(start);
328 total_words_not_copied += nwords;
329 printf("%4d words not copied at 0x%16lx; ",
330 nwords, (unsigned long) start);
331 printf("Header word is 0x%08x\n",
332 (unsigned int) object);
334 start += nwords;
336 printf("%d total words not copied.\n", total_words_not_copied);
340 /* weak pointers */
342 #define WEAK_POINTER_NWORDS \
343 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
345 static long
346 scav_weak_pointer(lispobj *where, lispobj object)
348 /* Do not let GC scavenge the value slot of the weak pointer */
349 /* (that is why it is a weak pointer). Note: we could use */
350 /* the scav_unboxed method here. */
352 return WEAK_POINTER_NWORDS;
355 lispobj *
356 search_read_only_space(void *pointer)
358 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
359 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
360 if ((pointer < (void *)start) || (pointer >= (void *)end))
361 return NULL;
362 return (gc_search_space(start,
363 (((lispobj *)pointer)+2)-start,
364 (lispobj *)pointer));
367 lispobj *
368 search_static_space(void *pointer)
370 lispobj* start = (lispobj*)STATIC_SPACE_START;
371 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
372 if ((pointer < (void *)start) || (pointer >= (void *)end))
373 return NULL;
374 return (gc_search_space(start,
375 (((lispobj *)pointer)+2)-start,
376 (lispobj *)pointer));
379 lispobj *
380 search_dynamic_space(void *pointer)
382 lispobj *start = (lispobj *) current_dynamic_space;
383 lispobj *end = (lispobj *) dynamic_space_free_pointer;
384 if ((pointer < (void *)start) || (pointer >= (void *)end))
385 return NULL;
386 return (gc_search_space(start,
387 (((lispobj *)pointer)+2)-start,
388 (lispobj *)pointer));
391 /* initialization. if gc_init can be moved to after core load, we could
392 * combine these two functions */
394 void
395 gc_init(void)
397 gc_init_tables();
398 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
401 void
402 gc_initialize_pointers(void)
404 /* FIXME: We do nothing here. We (briefly) misguidedly attempted
405 to set current_dynamic_space to DYNAMIC_0_SPACE_START here,
406 forgetting that (a) actually it could be the other and (b) it's
407 set in coreparse.c anyway. There's a FIXME note left here to
408 note that current_dynamic_space is a violation of OAOO: we can
409 tell which dynamic space we're currently in by looking at
410 dynamic_space_free_pointer. -- CSR, 2002-08-09 */
416 /* noise to manipulate the gc trigger stuff */
418 /* Functions that substantially change the dynamic space free pointer
419 * (collect_garbage, purify) are responsible also for resetting the
420 * auto_gc_trigger */
421 void set_auto_gc_trigger(os_vm_size_t dynamic_usage)
423 os_vm_address_t addr;
424 os_vm_size_t length;
426 addr = os_round_up_to_page((os_vm_address_t)current_dynamic_space
427 + dynamic_usage);
428 if (addr < (os_vm_address_t)dynamic_space_free_pointer)
429 lose("set_auto_gc_trigger: tried to set gc trigger too low! (%ld < 0x%08lx)\n",
430 (unsigned long)dynamic_usage,
431 (unsigned long)((os_vm_address_t)dynamic_space_free_pointer
432 - (os_vm_address_t)current_dynamic_space));
434 if (dynamic_usage > dynamic_space_size)
435 lose("set_auto_gc_trigger: tried to set gc trigger too high! (0x%08lx)\n",
436 (unsigned long)dynamic_usage);
437 length = os_trunc_size_to_page(dynamic_space_size - dynamic_usage);
439 #if defined(SUNOS) || defined(SOLARIS) || defined(LISP_FEATURE_HPUX)
440 os_invalidate(addr, length);
441 #else
442 os_protect(addr, length, 0);
443 #endif
445 current_auto_gc_trigger = (lispobj *)addr;
448 void clear_auto_gc_trigger(void)
450 os_vm_address_t addr;
451 os_vm_size_t length;
453 if (current_auto_gc_trigger == NULL)
454 return;
456 addr = (os_vm_address_t)current_auto_gc_trigger;
457 length = dynamic_space_size + (os_vm_address_t)current_dynamic_space - addr;
459 #if defined(SUNOS) || defined(SOLARIS) || defined(LISP_FEATURE_HPUX)
460 /* don't want to force whole space into swapping mode... */
461 os_validate(addr, length);
462 #else
463 os_protect(addr, length, OS_VM_PROT_ALL);
464 #endif
466 current_auto_gc_trigger = NULL;
469 static boolean
470 gc_trigger_hit(void *addr)
472 if (current_auto_gc_trigger == NULL)
473 return 0;
474 else{
475 return (addr >= (void *)current_auto_gc_trigger &&
476 addr <((void *)current_dynamic_space + dynamic_space_size));
480 boolean
481 cheneygc_handle_wp_violation(os_context_t *context, void *addr)
483 if(!foreign_function_call_active && gc_trigger_hit(addr)){
484 struct thread *thread=arch_os_get_current_thread();
485 clear_auto_gc_trigger();
486 /* Don't flood the system with interrupts if the need to gc is
487 * already noted. This can happen for example when SUB-GC
488 * allocates or after a gc triggered in a WITHOUT-GCING. */
489 if (SymbolValue(GC_PENDING,thread) == NIL) {
490 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
491 if (arch_pseudo_atomic_atomic(context)) {
492 /* set things up so that GC happens when we finish
493 * the PA section */
494 SetSymbolValue(GC_PENDING,T,thread);
495 arch_set_pseudo_atomic_interrupted(context);
496 maybe_save_gc_mask_and_block_deferrables
497 (os_context_sigmask_addr(context));
498 } else {
499 maybe_gc(context);
501 } else {
502 SetSymbolValue(GC_PENDING,T,thread);
505 return 1;
507 return 0;