Fix traceroot output for object directly referenced by stack
[sbcl.git] / src / runtime / traceroot.c
bloba219fd10c7ba9b65f4532289f06c64411f67412d
2 #include "sbcl.h"
3 #include "arch.h"
4 #include "runtime.h"
5 #include "lispregs.h"
6 #include "gc-internal.h"
7 #include "gc-private.h"
8 #include "genesis/closure.h"
9 #include "genesis/cons.h"
10 #include "genesis/constants.h"
11 #include "genesis/gc-tables.h"
12 #include "genesis/instance.h"
13 #include "genesis/layout.h"
14 #include "genesis/package.h"
15 #include "genesis/vector.h"
16 #include "pseudo-atomic.h" // for get_alloc_pointer()
17 #include "search.h"
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <sys/time.h>
22 #ifndef LISP_FEATURE_WIN32
23 #define HAVE_GETRUSAGE 1
24 #endif
25 #if HAVE_GETRUSAGE
26 #include <sys/resource.h> // for getrusage()
27 #endif
29 int heap_trace_verbose = 0;
31 extern generation_index_t gencgc_oldest_gen_to_gc;
33 /// Each "layer" is a set of objects reachable by tracing one reverse pointer
34 /// from any object in the previously built layer.
35 /// An object will not appear in more than one layer.
36 struct layer {
37 struct __attribute((packed)) node {
38 lispobj object; // With lowtag
39 // Which 0-relative word in this object points to any object
40 // in the next layer (closer to the intended target object).
41 int wordindex;
42 } *nodes;
43 struct layer* next;
44 int count;
47 /// memory in which to build the object lists comprising the
48 /// values in 'inverted_heap', the hashtable which maps each object
49 /// to a list of objects pointing to it.
50 struct scratchpad {
51 char* base, *free, *end;
54 struct scan_state {
55 long n_objects;
56 long n_scanned_words;
57 long n_immediates;
58 long n_pointers;
59 int record_ptrs;
60 // A hashtable mapping each object to a list of objects pointing to it
61 struct hopscotch_table inverted_heap;
62 struct scratchpad scratchpad;
65 static int gen_of(lispobj obj) {
66 #ifdef LISP_FEATURE_IMMOBILE_SPACE
67 if (immobile_space_p(obj))
68 return immobile_obj_gen_bits(native_pointer(obj));
69 #endif
70 int page = find_page_index((void*)obj);
71 if (page >= 0) return page_table[page].gen;
72 return -1;
75 const char* classify_obj(lispobj ptr)
77 extern lispobj* instance_classoid_name(lispobj*);
79 lispobj* name; // a Lisp string
80 switch(lowtag_of(ptr)) {
81 case INSTANCE_POINTER_LOWTAG:
82 name = instance_classoid_name(native_pointer(ptr));
83 if (widetag_of(*name) == SIMPLE_BASE_STRING_WIDETAG)
84 return (char*)(name + 2);
85 case LIST_POINTER_LOWTAG:
86 return "cons";
87 case FUN_POINTER_LOWTAG:
88 case OTHER_POINTER_LOWTAG:
89 return widetag_names[widetag_of(*native_pointer(ptr))>>2];
91 static char buf[8];
92 sprintf(buf, "#x%x", widetag_of(*native_pointer(ptr)));
93 return buf;
96 static void add_to_layer(lispobj* obj, int wordindex,
97 struct layer* layer, int* capacity)
99 // Resurrect the containing object's lowtag
100 lispobj ptr = compute_lispobj(obj);
101 int staticp = ptr <= STATIC_SPACE_END;
102 int gen = staticp ? -1 : gen_of(ptr);
103 if (heap_trace_verbose>2)
104 // Show the containing object, its type and generation, and pointee
105 fprintf(stderr,
106 " add_to_layer(%p,%d) = %s,g%c -> %p\n",
107 (void*)ptr, wordindex, classify_obj(ptr), (staticp ? 'S' : '0'+gen),
108 (void*)obj[wordindex]);
109 int count = layer->count;
110 if (count >= *capacity) {
111 *capacity = *capacity ? 2 * *capacity : 4;
112 layer->nodes = realloc(layer->nodes, *capacity * sizeof (struct node));
114 layer->nodes[count].object = ptr;
115 layer->nodes[count].wordindex = wordindex;
116 ++layer->count;
119 /// If 'obj' is a simple-fun, return its code component,
120 /// otherwise return obj directly.
121 static lispobj canonical_obj(lispobj obj)
123 if (lowtag_of(obj) == FUN_POINTER_LOWTAG &&
124 widetag_of(*native_pointer(obj)) == SIMPLE_FUN_WIDETAG)
125 return make_lispobj(fun_code_header(obj-FUN_POINTER_LOWTAG),
126 OTHER_POINTER_LOWTAG);
127 return obj;
130 /* Return the word index of the pointer in 'source' which references 'target'.
131 * Return -1 on failure. (This is an error if it happens)
133 #define check_ptr(index,ptr) if(canonical_obj(ptr)==target) return index;
134 static int find_ref(lispobj* source, lispobj target)
136 lispobj layout, bitmap;
137 int scan_limit, i, j;
139 lispobj header = *source;
140 if (is_cons_half(header)) {
141 check_ptr(0, header);
142 check_ptr(1, source[1]);
143 return -1;
145 int widetag = widetag_of(header);
146 scan_limit = sizetab[widetag](source);
147 switch (widetag) {
148 case INSTANCE_WIDETAG:
149 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
150 case FUNCALLABLE_INSTANCE_WIDETAG:
151 #endif
152 // mixed boxed/unboxed objects
153 // Unlike in scav_instance where the slot loop is unswitched for
154 // speed into three cases (no raw slots, fixnum bitmap, bignum bitmap),
155 // here we just go for clarity by abstracting out logbitp.
156 layout = instance_layout(source);
157 check_ptr(0, layout);
158 bitmap = layout ? LAYOUT(layout)->bitmap : make_fixnum(-1);
159 for(i=1; i<scan_limit; ++i)
160 if (layout_bitmap_logbitp(i-1, bitmap)) check_ptr(i, source[i]);
161 return -1;
162 case CLOSURE_WIDETAG:
163 check_ptr(1, ((struct closure*)source)->fun - FUN_RAW_ADDR_OFFSET);
164 break;
165 case CODE_HEADER_WIDETAG:
166 for_each_simple_fun(i, function_ptr, (struct code*)source, 0, {
167 int wordindex = &function_ptr->name - source;
168 for (j=0; j<4; ++j) check_ptr(wordindex+j, source[wordindex+j]);
170 scan_limit = code_header_words(header);
171 break;
172 case FDEFN_WIDETAG:
173 check_ptr(3, fdefn_callee_lispobj((struct fdefn*)source));
174 scan_limit = 3;
175 break;
177 for(i=1; i<scan_limit; ++i) check_ptr(i, source[i]);
178 return -1;
180 #undef check_ptr
182 enum ref_kind { HEAP, CONTROL_STACK, BINDING_STACK, TLS };
183 char *ref_kind_name[4] = {"heap","C stack","bindings","TLS"};
185 /// This unfortunately entails a heap scan,
186 /// but it's quite fast if the symbol is found in immobile space.
187 #ifdef LISP_FEATURE_SB_THREAD
188 static lispobj* find_sym_by_tls_index(lispobj tls_index)
190 lispobj* where = 0;
191 lispobj* end = 0;
192 #ifdef LISP_FEATURE_IMMOBILE_SPACE
193 where = (lispobj*)IMMOBILE_SPACE_START;
194 end = immobile_fixedobj_free_pointer;
195 #endif
196 while (1) {
197 while (where < end) {
198 lispobj header = *where;
199 int widetag = widetag_of(header);
200 if (widetag == SYMBOL_WIDETAG &&
201 tls_index_of(((struct symbol*)where)) == tls_index)
202 return where;
203 where += OBJECT_SIZE(header, where);
205 if (where >= (lispobj*)DYNAMIC_SPACE_START)
206 break;
207 where = (lispobj*)DYNAMIC_SPACE_START;
208 end = (lispobj*)get_alloc_pointer();
210 return 0;
212 #endif
214 static inline int interestingp(lispobj ptr, struct hopscotch_table* targets)
216 return is_lisp_pointer(ptr) && hopscotch_containsp(targets, ptr);
219 /* Try to find the call frame that contains 'addr', which is the address
220 * in which a conservative root was seen.
221 * Return the program counter associated with that frame. */
222 static char* deduce_thread_pc(struct thread* th, void** addr)
224 uword_t* fp = __builtin_frame_address(0);
225 char* return_pc = 0;
227 if (th != arch_os_get_current_thread()) {
228 int i = fixnum_value(read_TLS(FREE_INTERRUPT_CONTEXT_INDEX,th));
229 os_context_t *c = th->interrupt_contexts[i-1];
230 fp = (uword_t*)*os_context_register_addr(c,reg_FP);
232 while (1) {
233 if ((uword_t*)addr < fp)
234 return return_pc;
235 uword_t prev_fp = fp[0];
236 if (prev_fp == 0 || (uword_t*)prev_fp < fp || (lispobj*)prev_fp >= th->control_stack_end)
237 return 0;
238 return_pc = (void*)fp[1];
239 fp = (uword_t*)prev_fp;
243 static struct { void* pointer; boolean found; } pin_seek_state;
244 static void compare_pointer(void *addr) {
245 if (addr == pin_seek_state.pointer)
246 pin_seek_state.found = 1;
249 /* Figure out which thread's control stack contains 'pointer'
250 * and the PC within the active function in the referencing frame */
251 static struct thread* deduce_thread(void (*context_scanner)(),
252 uword_t pointer, char** pc)
254 struct thread *th;
256 *pc = 0;
257 pin_seek_state.found = 0;
258 for_each_thread(th) {
259 void **esp=(void **)-1;
260 sword_t i,free;
261 if (th == arch_os_get_current_thread())
262 esp = (void **)((void *)&pointer);
263 else {
264 void **esp1;
265 free = fixnum_value(read_TLS(FREE_INTERRUPT_CONTEXT_INDEX,th));
266 for(i=free-1;i>=0;i--) {
267 os_context_t *c=th->interrupt_contexts[i];
268 esp1 = (void **) *os_context_register_addr(c,reg_SP);
269 if (esp1>=(void **)th->control_stack_start && esp1<(void **)th->control_stack_end) {
270 if(esp1<esp) esp=esp1;
271 pin_seek_state.pointer = (void*)pointer;
272 context_scanner(compare_pointer, c);
273 pin_seek_state.pointer = 0;
274 if (pin_seek_state.found) return th;
278 if (!esp || esp == (void*) -1)
279 lose("deduce_thread: no SP known for thread %x (OS %x)", th, th->os_thread);
280 void** where;
281 for (where = ((void **)th->control_stack_end)-1; where >= esp; where--)
282 if ((uword_t)*where == pointer) {
283 *pc = deduce_thread_pc(th, where);
284 return th;
287 return 0;
290 /* KNOWN BUG: stack reference to pinned large object or immobile object
291 * won't be found in pins hashtable */
292 static lispobj examine_stacks(struct hopscotch_table* targets,
293 void (*context_scanner)(),
294 int n_pins, lispobj* pins,
295 enum ref_kind *root_kind,
296 struct thread** root_thread,
297 char** thread_pc,
298 lispobj *tls_index)
300 boolean world_stopped = context_scanner != 0;
301 struct thread *th;
303 for_each_thread(th) {
304 lispobj *where, *end;
305 #ifdef LISP_FEATURE_SB_THREAD
306 // Examine thread-local storage
307 *root_kind = TLS;
308 where = (lispobj*)(th+1);
309 end = (lispobj*)((char*)th + SymbolValue(FREE_TLS_INDEX,0));
310 for( ; where < end ; ++where)
311 if (interestingp(*where, targets)) {
312 *root_thread = th;
313 *tls_index = (char*)where - (char*)th;
314 return *where;
316 #endif
317 // Examine the binding stack
318 *root_kind = BINDING_STACK;
319 where = (lispobj*)th->binding_stack_start;
320 end = (lispobj*)get_binding_stack_pointer(th);
321 for( ; where < end ; where += 2)
322 if (interestingp(*where, targets)) {
323 *root_thread = th;
324 *tls_index = where[1];
325 return *where;
328 // Look in the control stacks
329 *root_kind = CONTROL_STACK;
330 uword_t pin;
331 int i;
332 for (i=n_pins-1; i>=0; --i)
333 // Bypass interestingp() to avoid one test - pins are known pointers.
334 if (hopscotch_containsp(targets, pin = pins[i])) {
335 if (world_stopped) {
336 *root_thread = deduce_thread(context_scanner, pin, thread_pc);
337 } else {
338 *root_thread = 0;
339 *thread_pc = 0;
340 // Scan just the current thread's stack
341 // (We don't know where the other stack pointers are)
342 th = arch_os_get_current_thread();
343 void **esp = __builtin_frame_address(0);
344 void **where;
345 for (where = ((void **)th->control_stack_end)-1; where >= esp; --where)
346 if (*where == (void*)pin) {
347 *root_thread = th;
348 *thread_pc = deduce_thread_pc(th, where);
349 break;
352 return pin;
354 *root_kind = HEAP;
355 return 0;
358 void free_graph(struct layer* layer)
360 while (layer) {
361 free(layer->nodes);
362 struct layer* next = layer->next;
363 free(layer);
364 layer = next;
368 struct node* find_node(struct layer* layer, lispobj ptr)
370 int i;
371 for(i=layer->count-1; i>=0; --i)
372 if (layer->nodes[i].object == ptr)
373 return &layer->nodes[i];
374 return 0;
377 /// "Compressed" pointers are a huge win - they halve the amount
378 /// of space required to invert the heap.
379 static inline uint32_t encode_pointer(lispobj pointer)
381 uword_t encoding;
382 if (pointer >= DYNAMIC_SPACE_START) {
383 // A dynamic space pointer is stored as a count in doublewords
384 // from the heap base address. A 32GB range is representable.
385 encoding = (pointer - DYNAMIC_SPACE_START) / (2*N_WORD_BYTES);
386 gc_assert(encoding <= 0x7FFFFFFF);
387 return (encoding<<1) | 1; // Low bit signifies compressed ptr.
388 } else {
389 // Non-dynamic-space pointers are stored as-is.
390 gc_assert(pointer <= 0xFFFFFFFF && !(pointer & 1));
391 return pointer; // Low bit 0 signifies literal pointer
395 static inline lispobj decode_pointer(uint32_t encoding)
397 if (encoding & 1) // Compressed ptr
398 return (encoding>>1)*(2*N_WORD_BYTES) + DYNAMIC_SPACE_START;
399 else
400 return encoding; // Literal pointer
403 struct simple_fun* simple_fun_from_pc(char* pc)
405 struct code* code = (struct code*)component_ptr_from_pc((lispobj*)pc);
406 if (!code) return 0;
407 struct simple_fun* prev_fun = (struct simple_fun*)
408 ((char*)code + (code_header_words(code->header)<<WORD_SHIFT)
409 + FIRST_SIMPLE_FUN_OFFSET(code));
410 for_each_simple_fun(i, fun, code, 1, {
411 if (pc < (char*)fun) break;
412 prev_fun = fun;
414 return prev_fun;
417 static void maybe_show_object_name(lispobj obj, FILE* stream)
419 extern void safely_show_lstring(lispobj* string, int quotes, FILE *s);
420 lispobj package, package_name;
421 if (lowtag_of(obj)==OTHER_POINTER_LOWTAG)
422 switch(widetag_of(*native_pointer(obj))) {
423 case SYMBOL_WIDETAG:
424 package = SYMBOL(obj)->package;
425 package_name = ((struct package*)native_pointer(package))->_name;
426 putc(',', stream);
427 safely_show_lstring(native_pointer(package_name), 0, stream);
428 fputs("::", stream);
429 safely_show_lstring(native_pointer(SYMBOL(obj)->name), 0, stream);
430 break;
434 static boolean root_p(lispobj ptr, int criterion)
436 if (ptr <= STATIC_SPACE_END) return 1; // always a root
437 // 0 to 2 are in order of weakest to strongest condition for stopping,
438 // i.e. criterion 0 implies that that largest number of objects
439 // are considered roots.
440 return criterion < 2
441 && (gen_of(ptr) > (criterion ? HIGHEST_NORMAL_GENERATION
442 : gencgc_oldest_gen_to_gc));
445 /// Find any shortest path to 'object' starting at a tenured object or a thread stack.
446 static void trace1(lispobj object,
447 struct hopscotch_table* targets,
448 struct hopscotch_table* visited,
449 struct hopscotch_table* inverted_heap,
450 struct scratchpad* scratchpad,
451 int n_pins, lispobj* pins, void (*context_scanner)(),
452 int criterion)
454 struct node* anchor = 0;
455 lispobj thread_ref;
456 enum ref_kind root_kind;
457 struct thread* root_thread;
458 char* thread_pc = 0;
459 lispobj tls_index;
460 lispobj target;
461 int i;
463 struct layer* top_layer = 0;
464 int layer_capacity = 0;
466 hopscotch_put(targets, object, 1);
467 while ((thread_ref = examine_stacks(targets, context_scanner, n_pins, pins,
468 &root_kind, &root_thread, &thread_pc,
469 &tls_index)) == 0) {
470 // TODO: preallocate layers to avoid possibility of malloc deadlock
471 struct layer* layer = (struct layer*)malloc(sizeof (struct layer));
472 layer->nodes = 0;
473 layer->count = 0;
474 layer->next = top_layer;
475 top_layer = layer;
476 layer_capacity = 0;
477 if (heap_trace_verbose)
478 printf("Next layer: Looking for %d object(s)\n", targets->count);
479 for_each_hopscotch_key(i, target, (*targets)) {
480 uint32_t list = hopscotch_get(inverted_heap, target, 0);
481 if (heap_trace_verbose>1) {
482 uint32_t list1 = list;
483 fprintf(stderr, "target=%p srcs=", (void*)target);
484 while (list1) {
485 uint32_t* cell = (uint32_t*)(scratchpad->base + list1);
486 lispobj* ptr = (lispobj*)decode_pointer(cell[0]);
487 if (hopscotch_containsp(visited, (lispobj)ptr))
488 fprintf(stderr, "%p ", ptr);
489 else {
490 lispobj word = *ptr;
491 int nwords = OBJECT_SIZE(word, ptr);
492 fprintf(stderr, "%p+%d ", ptr, nwords);
494 list1 = cell[1];
496 putc('\n',stderr);
498 while (list && !anchor) {
499 uint32_t* cell = (uint32_t*)(scratchpad->base + list);
500 lispobj ptr = decode_pointer(cell[0]);
501 list = cell[1];
502 if (hopscotch_containsp(visited, ptr))
503 continue;
504 int wordindex = find_ref((lispobj*)ptr, target);
505 if (wordindex == -1) {
506 fprintf(stderr, "Strange: no ref from %p to %p\n",
507 (void*)ptr, (void*)target);
508 continue;
510 hopscotch_insert(visited, ptr, 1);
511 add_to_layer((lispobj*)ptr, wordindex,
512 top_layer, &layer_capacity);
513 // Stop if the object at 'ptr' is tenured.
514 if (root_p(ptr, criterion)) {
515 fprintf(stderr, "Stopping at %p: tenured\n", (void*)ptr);
516 anchor = &top_layer->nodes[top_layer->count-1];
520 if (!top_layer->count) {
521 fprintf(stderr, "Failure tracing from %p. Current targets:\n", (void*)object);
522 for_each_hopscotch_key(i, target, (*targets))
523 fprintf(stderr, "%p ", (void*)target);
524 putc('\n', stderr);
525 free_graph(top_layer);
526 return;
528 if (heap_trace_verbose>1)
529 printf("Found %d object(s)\n", top_layer->count);
530 // The top layer's last object if static or tenured
531 // stops the scan. (And no more objects go in the top layer)
532 if (anchor)
533 break;
534 // Transfer the top layer objects into 'targets'
535 hopscotch_reset(targets);
536 struct node* nodes = top_layer->nodes;
537 for (i=top_layer->count-1 ; i>=0 ; --i) {
538 lispobj ptr = nodes[i].object;
539 hopscotch_put(targets, ptr, 1);
543 FILE *file = stdout;
544 if (thread_ref) {
545 struct vector* lisp_thread_name(os_thread_t os_thread);
546 extern void show_lstring(struct vector*, int, FILE*);
547 struct vector* thread_name;
548 #if 0
549 fprintf(stderr,
550 "%s pointed to by %s: %p\n",
551 top_layer ? "Indirectly" : "Directly",
552 ref_kind_name[root_kind],
553 (void*)thread_ref);
554 #endif
555 if (top_layer) {
556 // The thread indirectly points to a target.
557 // The root object is whatever the thread pointed to,
558 // which must be an object in the top layer. Find that object.
559 anchor = find_node(top_layer, thread_ref);
560 gc_assert(anchor);
562 putc('{', file);
563 if (!root_thread)
564 fprintf(file, "(unknown-thread)");
565 else if ((thread_name = lisp_thread_name(root_thread->os_thread)) != 0)
566 show_lstring(thread_name, 1, file);
567 else
568 fprintf(file, "thread=%p", root_thread);
569 fprintf(file, ":%s:", ref_kind_name[root_kind]);
570 if (root_kind==BINDING_STACK || root_kind==TLS) {
571 #ifdef LISP_FEATURE_SB_THREAD
572 lispobj* symbol = find_sym_by_tls_index(tls_index);
573 #else
574 lispobj* symbol = native_pointer(tls_index);
575 #endif
576 if (symbol)
577 show_lstring(symbol_name(symbol), 0, file);
578 else
579 fprintf(file, "%"OBJ_FMTX, tls_index);
580 } else {
581 struct simple_fun* fun = simple_fun_from_pc(thread_pc);
582 if (fun) {
583 fprintf(file, "fun=%p", (void*)make_lispobj(fun, FUN_POINTER_LOWTAG));
584 if (is_lisp_pointer(fun->name) &&
585 widetag_of(*native_pointer(fun->name)) == SYMBOL_WIDETAG) {
586 fprintf(file, "=");
587 show_lstring(VECTOR(SYMBOL(fun->name)->name), 0, file);
589 } else if (thread_pc)
590 fprintf(file, "pc=%p", thread_pc);
592 fprintf(file, "}->");
593 } else { // Stopped at (pseudo)static object
594 fprintf(stderr, "Anchor object is @ %p. word[%d]\n",
595 native_pointer(anchor->object), anchor->wordindex);
598 target = thread_ref;
599 while (top_layer) {
600 struct node next = *anchor;
601 lispobj ptr = next.object;
602 if (ptr <= STATIC_SPACE_END)
603 fprintf(file, "(static,");
604 else
605 fprintf(file, "(g%d,", gen_of(ptr));
606 fputs(classify_obj(ptr), file);
607 maybe_show_object_name(ptr, file);
608 fprintf(file, ")%p[%d]->", (void*)ptr, next.wordindex);
609 target = native_pointer(ptr)[next.wordindex];
610 // Special-case a few combinations of <type,wordindex>
611 switch (next.wordindex) {
612 case 0:
613 if (lowtag_of(ptr) == INSTANCE_POINTER_LOWTAG ||
614 lowtag_of(ptr) == FUN_POINTER_LOWTAG)
615 target = instance_layout(native_pointer(ptr));
616 break;
617 case 1:
618 if (lowtag_of(ptr) == FUN_POINTER_LOWTAG &&
619 widetag_of(*native_pointer(ptr)) == CLOSURE_WIDETAG)
620 target -= FUN_RAW_ADDR_OFFSET;
621 break;
622 case 3:
623 if (lowtag_of(ptr) == OTHER_POINTER_LOWTAG &&
624 widetag_of(FDEFN(ptr)->header) == FDEFN_WIDETAG)
625 target = fdefn_callee_lispobj((struct fdefn*)native_pointer(ptr));
626 break;
628 target = canonical_obj(target);
629 struct layer* next_layer = top_layer->next;
630 free(top_layer->nodes);
631 free(top_layer);
632 top_layer = next_layer;
633 if (top_layer) {
634 anchor = find_node(top_layer, target);
635 gc_assert(anchor);
636 } else {
637 gc_assert(object == target);
640 fprintf(file, "%p.\n", (void*)target);
641 fflush(file);
644 static void record_ptr(lispobj* source, lispobj target,
645 struct scan_state* ss)
647 // Add 'source' to the list of objects keyed by 'target' in the inverted heap.
648 // Note that 'source' has no lowtag, and 'target' does.
649 // Pointer compression occurs here as well: the linked list of source objects
650 // is built using offsets into the scratchpad rather than absolute addresses.
651 target = canonical_obj(target);
652 uint32_t* new_cell = (uint32_t*)ss->scratchpad.free;
653 uint32_t* next = new_cell + 2;
654 gc_assert((char*)next <= ss->scratchpad.end);
655 ss->scratchpad.free = (char*)next;
656 new_cell[0] = encode_pointer((lispobj)source);
657 new_cell[1] = hopscotch_get(&ss->inverted_heap, target, 0);
658 hopscotch_put(&ss->inverted_heap, target,
659 (sword_t)((char*)new_cell - ss->scratchpad.base));
662 #ifdef LISP_FEATURE_IMMOBILE_SPACE
663 #define relevant_ptr_p(x) find_page_index(x)>=0||find_immobile_page_index(x)>=0
664 #else
665 #define relevant_ptr_p(x) find_page_index(x)>=0
666 #endif
668 #define check_ptr(ptr) { \
669 ++n_scanned_words; \
670 if (!is_lisp_pointer(ptr)) ++n_immediates; \
671 else if (relevant_ptr_p((void*)ptr)) { \
672 ++n_pointers; \
673 if (record_ptrs) record_ptr(where,ptr,ss); \
676 static uword_t build_refs(lispobj* where, lispobj* end,
677 struct scan_state* ss)
679 lispobj layout, bitmap, fun;
680 sword_t nwords, scan_limit, i, j;
681 uword_t n_objects = 0, n_scanned_words = 0,
682 n_immediates = 0, n_pointers = 0;
684 boolean record_ptrs = ss->record_ptrs;
685 for ( ; where < end ; where += nwords ) {
686 ++n_objects;
687 lispobj header = *where;
688 if (is_cons_half(header)) {
689 nwords = 2;
690 check_ptr(header);
691 check_ptr(where[1]);
692 continue;
694 int widetag = widetag_of(header);
695 nwords = scan_limit = sizetab[widetag](where);
696 switch (widetag) {
697 case INSTANCE_WIDETAG:
698 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
699 case FUNCALLABLE_INSTANCE_WIDETAG:
700 #endif
701 // mixed boxed/unboxed objects
702 layout = instance_layout(where);
703 check_ptr(layout);
704 // Partially initialized instance can't have nonzero words yet
705 bitmap = layout ? LAYOUT(layout)->bitmap : make_fixnum(-1);
706 // If no raw slots, just scan without use of the bitmap.
707 if (bitmap == make_fixnum(-1)) break;
708 for(i=1; i<scan_limit; ++i)
709 if (layout_bitmap_logbitp(i-1, bitmap)) check_ptr(where[i]);
710 continue;
711 case CLOSURE_WIDETAG:
712 fun = ((struct closure*)where)->fun - FUN_RAW_ADDR_OFFSET;
713 check_ptr(fun);
714 break;
715 case CODE_HEADER_WIDETAG:
716 for_each_simple_fun(i, function_ptr, (struct code*)where, 0, {
717 int wordindex = &function_ptr->name - where;
718 for (j=0; j<4; ++j) check_ptr(where[wordindex+j]);
720 scan_limit = code_header_words(header);
721 break;
722 case FDEFN_WIDETAG:
723 check_ptr(fdefn_callee_lispobj((struct fdefn*)where));
724 scan_limit = 3;
725 break;
726 default:
727 if (!(other_immediate_lowtag_p(widetag) && lowtag_for_widetag[widetag>>2]))
728 lose("Unknown widetag %x\n", widetag);
729 // Skip irrelevant objects.
730 if (unboxed_obj_widetag_p(widetag) ||
731 (widetag == WEAK_POINTER_WIDETAG) || /* do not follow! */
732 // These numeric types contain pointers, but are uninteresting.
733 (widetag == COMPLEX_WIDETAG) ||
734 (widetag == RATIO_WIDETAG))
735 continue;
737 for(i=1; i<scan_limit; ++i) check_ptr(where[i]);
739 if (!record_ptrs) { // just count them
740 ss->n_objects += n_objects;
741 ss->n_scanned_words += n_scanned_words;
742 ss->n_immediates += n_immediates;
743 ss->n_pointers += n_pointers;
745 return 0;
747 #undef check_ptr
749 static void scan_spaces(struct scan_state* ss)
751 build_refs((lispobj*)STATIC_SPACE_START, static_space_free_pointer, ss);
752 #ifdef LISP_FEATURE_IMMOBILE_SPACE
753 build_refs((lispobj*)IMMOBILE_SPACE_START, immobile_fixedobj_free_pointer, ss);
754 build_refs((lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START,
755 immobile_space_free_pointer, ss);
756 #endif
757 walk_generation((uword_t(*)(lispobj*,lispobj*,uword_t))build_refs,
758 -1, (uword_t)ss);
761 #define HASH_FUNCTION HOPSCOTCH_HASH_FUN_MIX
763 static void compute_heap_inverse(struct hopscotch_table* inverted_heap,
764 struct scratchpad* scratchpad)
766 struct scan_state ss;
767 memset(&ss, 0, sizeof ss);
768 fprintf(stderr, "Pass 1: Counting heap objects... ");
769 scan_spaces(&ss);
770 fprintf(stderr, "%ld objs, %ld ptrs, %ld immediates\n",
771 ss.n_objects, ss.n_pointers,
772 ss.n_scanned_words - ss.n_pointers);
773 // Guess at the initial size of ~ .5 million objects.
774 int size = 1<<19; // flsl(tot_n_objects); this would work if you have it
775 while (ss.n_objects > size) size <<= 1;
776 fprintf(stderr, "Pass 2: Inverting heap. Initial size=%d objects\n", size);
777 hopscotch_create(&ss.inverted_heap, HASH_FUNCTION,
778 4, // XXX: half the word size if 64-bit
779 size /* initial size */, 0 /* default hop range */);
780 // Add one pointer due to inability to use the first
781 // two words of the scratchpad.
782 uword_t scratchpad_min_size = (1 + ss.n_pointers) * 2 * sizeof (uint32_t);
783 int pagesize = getpagesize();
784 uword_t scratchpad_size = ALIGN_UP(scratchpad_min_size, pagesize);
785 ss.scratchpad.base = os_allocate(scratchpad_size);
786 gc_assert(ss.scratchpad.base);
787 ss.scratchpad.free = ss.scratchpad.base + 2 * sizeof(uint32_t);
788 ss.scratchpad.end = ss.scratchpad.base + scratchpad_size;
789 fprintf(stderr, "Scratchpad: %lu bytes\n", (long unsigned)scratchpad_size);
790 #if HAVE_GETRUSAGE
791 struct rusage before, after;
792 getrusage(RUSAGE_SELF, &before);
793 #endif
794 ss.record_ptrs = 1;
795 scan_spaces(&ss);
796 *inverted_heap = ss.inverted_heap;
797 *scratchpad = ss.scratchpad;
798 #if HAVE_GETRUSAGE
799 getrusage(RUSAGE_SELF, &after);
800 // We're done building the necessary structure. Show some memory stats.
801 #define timediff(b,a,field) \
802 ((a.field.tv_sec-b.field.tv_sec)*1000000+(a.field.tv_usec-b.field.tv_usec))
803 fprintf(stderr,
804 "Inverted heap: ct=%d, cap=%d, LF=%f ET=%ld+%ld sys+usr\n",
805 inverted_heap->count,
806 1+hopscotch_max_key_index(*inverted_heap),
807 100*(float)inverted_heap->count / (1+hopscotch_max_key_index(*inverted_heap)),
808 timediff(before, after, ru_stime),
809 timediff(before, after, ru_utime));
810 #endif
813 /* Find any shortest path from a thread or tenured object
814 * to each of the specified objects.
816 static void trace_paths(void (*context_scanner)(),
817 lispobj weak_pointers, int n_pins, lispobj* pins,
818 int criterion)
820 int i;
821 struct hopscotch_table inverted_heap;
822 struct scratchpad scratchpad;
823 // A hashset of all objects in the reverse reachability graph so far
824 struct hopscotch_table visited; // *Without* lowtag
825 // A hashset of objects in the current graph layer
826 struct hopscotch_table targets; // With lowtag
828 if (heap_trace_verbose) {
829 fprintf(stderr, "%d pins:\n", n_pins);
830 for(i=0;i<n_pins;++i)
831 fprintf(stderr, " %p%s", (void*)pins[i],
832 ((i%8)==7||i==n_pins-1)?"\n":"");
834 compute_heap_inverse(&inverted_heap, &scratchpad);
835 hopscotch_create(&visited, HASH_FUNCTION, 0, 32, 0);
836 hopscotch_create(&targets, HASH_FUNCTION, 0, 32, 0);
837 do {
838 lispobj car = CONS(weak_pointers)->car;
839 lispobj value = ((struct weak_pointer*)native_pointer(car))->value;
840 weak_pointers = CONS(weak_pointers)->cdr;
841 if (value != UNBOUND_MARKER_WIDETAG) {
842 if (heap_trace_verbose)
843 fprintf(stderr, "Target=%p (%s)\n", (void*)value, classify_obj(value));
844 hopscotch_reset(&visited);
845 hopscotch_reset(&targets);
846 trace1(value, &targets, &visited,
847 &inverted_heap, &scratchpad,
848 n_pins, pins, context_scanner, criterion);
850 } while (weak_pointers != NIL);
851 os_invalidate(scratchpad.base, scratchpad.end-scratchpad.base);
852 hopscotch_destroy(&inverted_heap);
853 hopscotch_destroy(&visited);
854 hopscotch_destroy(&targets);
857 void gc_prove_liveness(void(*context_scanner)(),
858 lispobj objects,
859 int n_pins, uword_t* pins,
860 int criterion)
862 int n_watched = 0, n_live = 0, n_bad = 0;
863 lispobj list;
864 for ( list = objects ;
865 list != NIL && lowtag_of(list) == LIST_POINTER_LOWTAG ;
866 list = CONS(list)->cdr ) {
867 ++n_watched;
868 lispobj car = CONS(list)->car;
869 if ((lowtag_of(car) != OTHER_POINTER_LOWTAG ||
870 widetag_of(*native_pointer(car)) != WEAK_POINTER_WIDETAG))
871 ++n_bad;
872 else
873 n_live += ((struct weak_pointer*)native_pointer(car))->value
874 != UNBOUND_MARKER_WIDETAG;
876 if (lowtag_of(list) != LIST_POINTER_LOWTAG || n_bad) {
877 fprintf(stderr, "; Bad value in liveness tracker\n");
878 return;
880 fprintf(stderr, "; Liveness tracking: %d/%d live watched objects\n",
881 n_live, n_watched);
882 if (!n_live)
883 return;
884 // Put back lowtags on pinned objects, since wipe_nonpinned_words() removed
885 // them. But first test whether lowtags were already repaired
886 // in case prove_liveness() is called after gc_prove_liveness().
887 if (n_pins>0 && !is_lisp_pointer(pins[0])) {
888 int i;
889 for(i=n_pins-1; i>=0; --i) {
890 pins[i] = compute_lispobj((lispobj*)pins[i]);
893 trace_paths(context_scanner, objects, n_pins, (lispobj*)pins, criterion);
896 /* This should be called inside WITHOUT-GCING so that the set
897 * of pins does not change out from underneath.
899 void prove_liveness(lispobj objects, int criterion)
901 extern struct hopscotch_table pinned_objects;
902 extern int gc_n_stack_pins;
903 gc_prove_liveness(0, objects, gc_n_stack_pins, pinned_objects.keys, criterion);
906 // These are slot offsets in (DEFSTRUCT THREAD),
907 // not the C structure defined in genesis/thread.h
908 #define LISP_THREAD_NAME_SLOT INSTANCE_DATA_START+0
909 #define LISP_THREAD_OS_THREAD_SLOT INSTANCE_DATA_START+3
911 struct vector* lisp_thread_name(os_thread_t os_thread)
913 static unsigned int hint;
914 lispobj* sym = find_symbol("*ALL-THREADS*", "SB-THREAD", &hint);
915 lispobj list = sym ? ((struct symbol*)sym)->value : NIL;
916 while (list != NIL) {
917 struct instance* lisp_thread = (struct instance*)native_pointer(CONS(list)->car);
918 list = CONS(list)->cdr;
919 if ((os_thread_t)lisp_thread->slots[LISP_THREAD_OS_THREAD_SLOT]
920 == os_thread)
921 return VECTOR(lisp_thread->slots[LISP_THREAD_NAME_SLOT]);
923 return 0;