#include "gc.h"
#include "gc-internal.h"
#include "thread.h"
+#include "pseudo-atomic.h"
#include "alloc.h"
#include "genesis/vector.h"
#include "genesis/weak-pointer.h"
/* the verbosity level. All non-error messages are disabled at level 0;
* and only a few rare messages are printed at level 1. */
-#ifdef QSHOW
+#if QSHOW
boolean gencgc_verbose = 1;
#else
boolean gencgc_verbose = 0;
return count;
}
-#ifdef QSHOW
+#if QSHOW
static long
count_dont_move_pages(void)
{
/* The verbose argument controls how much to print: 0 for normal
* level of detail; 1 for debugging. */
-static void
-print_generation_stats(int verbose) /* FIXME: should take FILE argument */
+extern void
+print_generation_stats() /* FIXME: should take FILE argument, or construct a string */
{
- generation_index_t i, gens;
+ generation_index_t i;
#if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
#define FPU_STATE_SIZE 27
* so they need to be saved and reset for C. */
fpu_save(fpu_state);
- /* highest generation to print */
- if (verbose)
- gens = SCRATCH_GENERATION;
- else
- gens = PSEUDO_STATIC_GENERATION;
-
/* Print the heap stats. */
fprintf(stderr,
" Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
- for (i = 0; i < gens; i++) {
+ for (i = 0; i < SCRATCH_GENERATION; i++) {
page_index_t j;
long boxed_cnt = 0;
long unboxed_cnt = 0;
generations[i].num_gc,
gen_av_mem_age(i));
}
- fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
+ fprintf(stderr," Total bytes allocated = %lu\n", bytes_allocated);
+ fprintf(stderr," Dynamic-space-size bytes = %u\n", dynamic_space_size);
fpu_restore(fpu_state);
}
int orig_first_page_bytes_used;
long byte_cnt;
int more;
- long bytes_used;
+ unsigned long bytes_used;
page_index_t next_page;
int ret;
void
gc_heap_exhausted_error_or_lose (long available, long requested)
{
+ struct thread *thread = arch_os_get_current_thread();
/* Write basic information before doing anything else: if we don't
* call to lisp this is a must, and even if we do there is always
* the danger that we bounce back here before the error has been
/* If we are in GC, or totally out of memory there is no way
* to sanely transfer control to the lisp-side of things.
*/
- struct thread *thread = arch_os_get_current_thread();
- print_generation_stats(1);
+ print_generation_stats();
fprintf(stderr, "GC control variables:\n");
fprintf(stderr, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
- SymbolValue(GC_PENDING,thread)==NIL ? "false" : "true");
+ (SymbolValue(GC_PENDING, thread) == T) ?
+ "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
+ "false" : "in progress"));
#ifdef LISP_FEATURE_SB_THREAD
fprintf(stderr, " *STOP-FOR-GC-PENDING* = %s\n",
SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
else {
/* FIXME: assert free_pages_lock held */
(void)thread_mutex_unlock(&free_pages_lock);
+ gc_assert(get_pseudo_atomic_atomic(thread));
+ clear_pseudo_atomic_atomic(thread);
+ if (get_pseudo_atomic_interrupted(thread))
+ do_pending_interrupt();
+ /* Another issue is that signalling HEAP-EXHAUSTED error leads
+ * to running user code at arbitrary places, even in a
+ * WITHOUT-INTERRUPTS which may lead to a deadlock without
+ * running out of the heap. So at this point all bets are
+ * off. */
+ if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
+ corruption_warning_and_maybe_lose
+ ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR),
alloc_number(available), alloc_number(requested));
lose("HEAP-EXHAUSTED-ERROR fell through");
}
page_index_t
-gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes, int page_type_flag)
+gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes,
+ int page_type_flag)
{
page_index_t first_page, last_page;
page_index_t restart_page = *restart_page_ptr;
restart_page = gencgc_alloc_start_page;
}
- if (nbytes>=PAGE_BYTES) {
+ gc_assert(nbytes>=0);
+ if (((unsigned long)nbytes)>=PAGE_BYTES) {
/* Search for a contiguous free space of at least nbytes,
* aligned on a page boundary. The page-alignment is strictly
* speaking needed only for objects at least large_object_size
gc_assert(from_space_p(object));
gc_assert((nwords & 0x01) == 0);
- if ((nwords > 1024*1024) && gencgc_verbose)
+ if ((nwords > 1024*1024) && gencgc_verbose) {
FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n",
nwords*N_WORD_BYTES));
+ }
/* Check whether it's a large object. */
first_page = find_page_index((void *)object);
next_page++;
}
- if ((bytes_freed > 0) && gencgc_verbose)
+ if ((bytes_freed > 0) && gencgc_verbose) {
FSHOW((stderr,
"/copy_large_unboxed bytes_freed=%d\n",
bytes_freed));
+ }
generations[from_space].bytes_allocated -=
nwords*N_WORD_BYTES + bytes_freed;
unsigned d2 = *((unsigned char *)p - 2);
unsigned d3 = *((unsigned char *)p - 3);
unsigned d4 = *((unsigned char *)p - 4);
-#ifdef QSHOW
+#if QSHOW
unsigned d5 = *((unsigned char *)p - 5);
unsigned d6 = *((unsigned char *)p - 6);
#endif
case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
if ((unsigned long)pointer !=
((unsigned long)start_addr+FUN_POINTER_LOWTAG)) {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wf2: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
break;
default:
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wf3: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
break;
case LIST_POINTER_LOWTAG:
if ((unsigned long)pointer !=
((unsigned long)start_addr+LIST_POINTER_LOWTAG)) {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wl1: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
/* Is it plausible cons? */
is_lisp_immediate(start_addr[1])))
break;
else {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wl2: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
case INSTANCE_POINTER_LOWTAG:
if ((unsigned long)pointer !=
((unsigned long)start_addr+INSTANCE_POINTER_LOWTAG)) {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wi1: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wi2: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
break;
case OTHER_POINTER_LOWTAG:
if ((unsigned long)pointer !=
((unsigned long)start_addr+OTHER_POINTER_LOWTAG)) {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wo1: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
/* Is it plausible? Not a cons. XXX should check the headers. */
if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wo2: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
switch (widetag_of(start_addr[0])) {
#if N_WORD_BITS == 64
case SINGLE_FLOAT_WIDETAG:
#endif
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"*Wo3: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
/* only pointed to by function pointers? */
case CLOSURE_HEADER_WIDETAG:
case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"*Wo4: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
case INSTANCE_HEADER_WIDETAG:
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"*Wo5: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
/* the valid other immediate pointer objects */
break;
default:
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"/Wo6: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
break;
default:
- if (gencgc_verbose)
+ if (gencgc_verbose) {
FSHOW((stderr,
"*W?: %x %x %x\n",
pointer, start_addr, *start_addr));
+ }
return 0;
}
* probability that random garbage will be bogusly interpreted as
* a pointer which prevents a page from moving. */
if (!(code_page_p(addr_page_index)
- || (is_lisp_pointer(addr) &&
+ || (is_lisp_pointer((lispobj)addr) &&
possibly_valid_dynamic_space_pointer(addr))))
return;
/* New areas of objects allocated have been lost so need to do a
* full scan to be sure! If this becomes a problem try
* increasing NUM_NEW_AREAS. */
- if (gencgc_verbose)
+ if (gencgc_verbose) {
SHOW("new_areas overflow, doing full scavenge");
+ }
/* Don't need to record new areas that get scavenged
* anyway during scavenge_newspace_generation_one_scan. */
}
#endif
-#ifdef QSHOW
+#if QSHOW
if (gencgc_verbose > 1) {
long num_dont_move_pages = count_dont_move_pages();
fprintf(stderr,
"/non-movable pages due to conservative pointers = %d (%d bytes)\n",
num_dont_move_pages,
- npage_bytes(num_dont_move_pages);
+ npage_bytes(num_dont_move_pages));
}
#endif
generations[generation].alloc_large_unboxed_start_page = 0;
if (generation >= verify_gens) {
- if (gencgc_verbose)
+ if (gencgc_verbose) {
SHOW("verifying");
+ }
verify_gc();
verify_dynamic_space();
}
}
if (gencgc_verbose > 1)
- print_generation_stats(0);
+ print_generation_stats();
do {
/* Collect the generation. */
if (gencgc_verbose > 1) {
FSHOW((stderr, "GC of generation %d finished:\n", gen));
- print_generation_stats(0);
+ print_generation_stats();
}
gen++;
{
page_index_t page;
- if (gencgc_verbose > 1)
+ if (gencgc_verbose > 1) {
SHOW("entering gc_free_heap");
+ }
for (page = 0; page < page_table_pages; page++) {
/* Skip free pages which should already be zero filled. */
}
if (gencgc_verbose > 1)
- print_generation_stats(0);
+ print_generation_stats();
/* Initialize gc_alloc(). */
gc_alloc_generation = 0;
generation_index_t gen = PSEUDO_STATIC_GENERATION;
do {
lispobj *first,*ptr= (lispobj *)page_address(page);
- page_table[page].allocated = BOXED_PAGE_FLAG;
page_table[page].gen = gen;
page_table[page].bytes_used = PAGE_BYTES;
page_table[page].large_object = 0;
page_table[page].need_to_zero = 1;
if (!gencgc_partial_pickup) {
+ page_table[page].allocated = BOXED_PAGE_FLAG;
first=gc_search_space(prev,(ptr+2)-prev,ptr);
- if(ptr == first) prev=ptr;
+ if(ptr == first)
+ prev=ptr;
page_table[page].region_start_offset =
page_address(page) - (void *)prev;
}
/* set things up so that GC happens when we finish the PA
* section */
SetSymbolValue(GC_PENDING,T,thread);
- if (SymbolValue(GC_INHIBIT,thread) == NIL)
- set_pseudo_atomic_interrupted(thread);
+ if (SymbolValue(GC_INHIBIT,thread) == NIL) {
+ set_pseudo_atomic_interrupted(thread);
+#ifdef LISP_FEATURE_PPC
+ /* PPC calls alloc() from a trap or from pa_alloc(),
+ * look up the most context if it's from a trap. */
+ {
+ os_context_t *context =
+ thread->interrupt_data->allocation_trap_context;
+ maybe_save_gc_mask_and_block_deferrables
+ (context ? os_context_sigmask_addr(context) : NULL);
+ }
+#else
+ maybe_save_gc_mask_and_block_deferrables(NULL);
+#endif
+ }
}
}
new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
if ((signed long) alloc_signal <= 0) {
SetSymbolValue(ALLOC_SIGNAL, T, thread);
-#ifdef LISP_FEATURE_SB_THREAD
- kill_thread_safely(thread->os_thread, SIGPROF);
-#else
raise(SIGPROF);
-#endif
} else {
SetSymbolValue(ALLOC_SIGNAL,
alloc_signal - (1 << N_FIXNUM_TAG_BITS),
lispobj *
alloc(long nbytes)
{
+ gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
return general_alloc(nbytes, BOXED_PAGE_FLAG);
}
\f
{
page_index_t page_index = find_page_index(fault_addr);
-#ifdef QSHOW_SIGNALS
+#if QSHOW_SIGNALS
FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
fault_addr, page_index));
#endif
return 0;
} else {
+ int ret;
+ ret = thread_mutex_lock(&free_pages_lock);
+ gc_assert(ret == 0);
if (page_table[page_index].write_protected) {
/* Unprotect the page. */
os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
page_index, boxed_region.first_page,
boxed_region.last_page);
}
+ ret = thread_mutex_unlock(&free_pages_lock);
+ gc_assert(ret == 0);
/* Don't worry, we can handle it. */
return 1;
}