1 /* mm/cacao-gc/rootset.c - GC module for root set management
3 Copyright (C) 2006, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "mm/memory.hpp"
35 #include "threads/threadlist.h"
36 #include "threads/thread.hpp"
38 #include "toolbox/logging.hpp"
40 #include "vm/global.h"
41 #include "vm/jit/replace.hpp"
42 #include "vm/jit/stacktrace.hpp"
45 rootset_t *rootset_create(void)
49 /* allocate memory for rootset */
53 rs->capacity = ROOTSET_INITIAL_CAPACITY;
60 rootset_t *rootset_resize(rootset_t *rs)
65 /* double the capacity of this rootset */
66 size_old = sizeof(rootset_t) + (rs->capacity - ROOTSET_INITIAL_CAPACITY) * sizeof(rootset_entry_t);
68 size_new = sizeof(rootset_t) + (rs->capacity - ROOTSET_INITIAL_CAPACITY) * sizeof(rootset_entry_t);
70 GC_LOG2( printf("Resizing rootset to capacity %d (%d -> %d)\n", rs->capacity, size_old, size_new); );
72 /* reallocate memory for rootset */
73 /* XXX DMREALLOC(ptr,type,num1,num2) */
74 rs = DMREALLOC(rs, u1, size_old, size_new);
80 /* rootset_from_globals ********************************************************
82 Searches global variables to compile the global root set out of references
85 REMEMBER: All threads are stopped, so we don't have to lock the
86 lists in this function.
89 - thread objects (threads.c)
90 - classloader objects (loader.c)
91 - global reference table (jni.c)
92 - finalizer entries (final.c)
94 *******************************************************************************/
96 #define ROOTSET_ADD(adr,mrk,tp) \
97 if (refcount >= rs->capacity) \
98 rs = rootset_resize(rs); \
99 rs->refs[refcount].ref = (adr); \
100 rs->refs[refcount].marks = (mrk); \
101 rs->refs[refcount].reftype = (tp); \
104 static rootset_t *rootset_from_globals(rootset_t *rs)
106 list_final_entry_t *fe;
107 list_gcref_entry_t *re;
110 GC_LOG( dolog("GC: Acquiring Root-Set from globals ..."); );
112 /* initialize the rootset struct */
114 GC_ASSERT(rs->refcount == 0);
115 rs->thread = ROOTSET_DUMMY_THREAD;
117 refcount = rs->refcount;
119 /* walk through all registered strong references */
120 for (re = list_first(gc_reflist_strong); re != NULL; re = list_next(gc_reflist_strong, re)) {
121 GC_LOG2( printf("Found Registered Reference: %p at %p of type %d\n", *(re->ref), re->ref, ref->reftype); );
123 /* add this registered reference to the root set */
124 ROOTSET_ADD(re->ref, true, re->reftype)
127 /* walk through all registered weak references */
128 for (re = list_first(gc_reflist_weak); re != NULL; re = list_next(gc_reflist_weak, re)) {
129 GC_LOG2( printf("Found Registered Weak Reference: %p at %p of type %d\n", *(re->ref), re->ref, ref->reftype); );
131 /* add this registered reference to the root set */
132 ROOTSET_ADD(re->ref, false, re->reftype)
135 /* walk through all finalizer entries */
136 for (fe = list_first(final_list); fe != NULL; fe = list_next(final_list, fe)) {
137 GC_LOG2( printf("Found Finalizer Entry: %p\n", (void *) fe->o); );
139 /* add this object with finalizer to the root set */
140 ROOTSET_ADD(&( fe->o ), false, GC_REFTYPE_FINALIZER)
143 /* remeber how many references there are inside this root set */
144 rs->refcount = refcount;
150 static rootset_t *rootset_from_classes(rootset_t *rs)
154 void *sys_start, *sys_end;
158 GC_LOG( dolog("GC: Acquiring Root-Set from classes ..."); );
160 /* TODO: cleanup!!! */
161 sys_start = heap_region_sys->base;
162 sys_end = heap_region_sys->ptr;
164 refcount = rs->refcount;
166 /* walk through all classinfo blocks */
168 while (c < (classinfo *) sys_end) {
170 GC_LOG2( printf("Searching in class "); class_print(c); printf("\n"); );
172 /* walk through all fields */
174 for (i = 0; i < c->fieldscount; i++, f++) {
176 /* check if this is a static reference */
177 if (!IS_ADR_TYPE(f->type) || !(f->flags & ACC_STATIC))
180 /* check for outside or null pointers */
181 if (f->value->a == NULL)
184 GC_LOG2( printf("Found Static Field Reference: %p\n", (void *) f->value->a);
185 printf("\tfrom field: "); field_print(f); printf("\n");
186 printf("\tto object : "); heap_print_object(f->value->a); printf("\n"); );
188 /* add this static field reference to the root set */
189 ROOTSET_ADD(f->value, true, GC_REFTYPE_CLASSREF);
193 /* skip to next classinfo block */
195 c = (classinfo *) (GC_ALIGN((ptrint) c, GC_ALIGN_SIZE));
199 /* remeber how many references there are inside this root set */
200 rs->refcount = refcount;
206 /* rootset_from_thread *********************************************************
208 Searches the stack of the passed thread for references and compiles a
209 root set out of them.
211 NOTE: uses dump memory!
220 *******************************************************************************/
222 static rootset_t *rootset_from_thread(threadobject *thread, rootset_t *rs)
224 executionstate_t *es;
231 #if defined(ENABLE_THREADS)
232 GC_ASSERT(thread != NULL);
233 GC_LOG( dolog("GC: Acquiring Root-Set from thread (tid=%p) ...", (void *) thread->tid); );
235 GC_ASSERT(thread == NULL);
236 GC_LOG( dolog("GC: Acquiring Root-Set from single-thread ..."); );
239 GC_LOG2( printf("Stacktrace of thread:\n");
240 threads_thread_print_stacktrace(thread); );
242 /* get the sourcestate of the threads */
243 es = GC_EXECUTIONSTATE;
249 /* print our full source state */
250 GC_LOG2( replace_sourcestate_println(ss); );
252 /* initialize the rootset struct */
254 GC_ASSERT(rs->refcount == 0);
257 refcount = rs->refcount;
259 /* now inspect the source state to compile the root set */
260 for (sf = ss->frames; sf != NULL; sf = sf->down) {
262 GC_LOG( printf("Source Frame: localcount=%d, stackdepth=%d, syncslots=%d\n", sf->javalocalcount, sf->javastackdepth, sf->syncslotcount); );
264 for (i = 0; i < sf->javalocalcount; i++) {
266 /* we only need to consider references */
267 if (sf->javalocaltype[i] != TYPE_ADR)
270 GC_LOG2( printf("Found Reference (Java Local): %p\n", (void *) sf->javalocals[i].a); );
272 /* add this reference to the root set */
273 ROOTSET_ADD((java_object_t **) &( sf->javalocals[i] ), true, GC_REFTYPE_STACK);
277 for (i = 0; i < sf->javastackdepth; i++) {
279 /* we only need to consider references */
280 if (sf->javastacktype[i] != TYPE_ADR)
283 GC_LOG2( printf("Found Reference (Java Stack): %p\n", (void *) sf->javastack[i].a); );
285 /* add this reference to the root set */
286 ROOTSET_ADD((java_object_t **) &( sf->javastack[i] ), true, GC_REFTYPE_STACK);
290 for (i = 0; i < sf->syncslotcount; i++) {
292 GC_LOG( printf("Found Reference (Sync Slot): %p\n", (void *) sf->syncslots[i].a); );
294 /* add this reference to the root set */
295 ROOTSET_ADD((java_object_t **) &( sf->syncslots[i] ), true, GC_REFTYPE_STACK);
300 /* now walk through all local references of this thread */
301 #if defined(ENABLE_THREADS)
302 lrt = thread->_localref_table;
308 for (i = 0; i < lrt->used; i++) {
310 /* there should be no null pointers in here */
311 GC_ASSERT(lrt->refs[i] != NULL);
313 GC_LOG2( printf("Found LocalRef: %p\n", (void *) lrt->refs[i]); );
315 /* add this reference to the root set */
316 ROOTSET_ADD(&( lrt->refs[i] ), true, GC_REFTYPE_LOCALREF);
323 /* remeber how many references there are inside this root set */
324 rs->refcount = refcount;
330 rootset_t *rootset_readout()
336 /* find the global rootset ... */
337 rs_top = rootset_create();
338 rs_top = rootset_from_globals(rs_top);
339 rs_top = rootset_from_classes(rs_top);
341 /* ... and the rootsets for the threads */
343 #if defined(ENABLE_THREADS)
344 for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
346 /* ignore threads which are in state NEW */
347 if (t->state == THREAD_STATE_NEW)
350 rs->next = rootset_create();
351 rs->next = rootset_from_thread(t, rs->next);
358 rs->next = rootset_create();
359 rs->next = rootset_from_thread(t, rs->next);
366 void rootset_writeback(rootset_t *rs)
368 threadobject *thread;
370 /* walk through all rootsets */
374 /* does this rootset belong to a thread? */
375 if (thread != ROOTSET_DUMMY_THREAD) {
376 #if defined(ENABLE_THREADS)
377 GC_ASSERT(thread != NULL);
378 GC_LOG( dolog("GC: Writing back Root-Set to thread (tid=%p) ...", (void *) thread->tid); );
380 GC_ASSERT(thread == NULL);
381 GC_LOG( dolog("GC: Writing back Root-Set to single-thread ..."); );
384 /* now rebuild the stack of the thread */
385 replace_gc_into_native(thread);
394 /* Debugging ******************************************************************/
397 static const char* reftype_names[] = {
398 "THREADOBJECT", "CLASSLOADER ", "GLOBAL-REF ",
399 "FINALIZER ", "LOCAL-REF ", "ON-STACK-ADR",
400 "STATIC FIELD", "LOCKRECORD "
403 void rootset_print(rootset_t *rs)
408 /* walk through all rootsets in the chain */
409 printf("Root Set Chain:\n");
412 /* print the thread this rootset belongs to */
413 if (rs->thread == ROOTSET_DUMMY_THREAD) {
414 printf("\tGlobal Root Set:\n");
416 #if defined(ENABLE_THREADS)
417 printf("\tLocal Root Set with Thread-Id %p:\n", (void *) rs->thread->tid);
419 printf("\tLocal Root Set:\n");
423 /* print the references in this rootset */
424 printf("\tReferences (%d / %d):\n", rs->refcount, rs->capacity);
425 for (i = 0; i < rs->refcount; i++) {
427 o = *( rs->refs[i].ref );
429 /*printf("\t\tReference at %p points to ...\n", (void *) rs->refs[i]);*/
431 printf("%s ", reftype_names[rs->refs[i].reftype]);
432 if (rs->refs[i].marks)
437 heap_print_object(o);
451 * These are local overrides for various environment variables in Emacs.
452 * Please do not remove this and leave it at the end of the file, where
453 * Emacs will automagically detect them.
454 * ---------------------------------------------------------------------
457 * indent-tabs-mode: t
461 * vim:noexpandtab:sw=4:ts=4: