3 #include "multicoreruntime.h"
4 #include "GenericHashtable.h"
6 #include "multicoregcmark.h"
10 extern struct parameterwrapper ** objectqueues[][NUMCLASSES];
11 extern int numqueues[][NUMCLASSES];
12 extern struct genhashtable * activetasks;
13 extern struct parameterwrapper ** objectqueues[][NUMCLASSES];
14 extern struct taskparamdescriptor *currtpd;
15 extern struct LockValue runtime_locks[MAXTASKPARAMS];
16 extern int runtime_locklen;
19 extern struct global_defs_t * global_defs_p;
22 extern struct lockvector bamboo_threadlocks;
25 INLINE void gettype_size(void * ptr, int * ttype, unsigned int * tsize) {
26 int type = ((int *)ptr)[0];
27 if(type < NUMCLASSES) {
29 *tsize = classsize[type];
33 struct ArrayObject *ao=(struct ArrayObject *)ptr;
34 unsigned int elementsize=classsize[type];
35 unsigned int length=ao->___length___;
36 *tsize = sizeof(struct ArrayObject)+length*elementsize;
41 INLINE bool isLarge(void * ptr, int * ttype, unsigned int * tsize) {
42 // check if a pointer refers to a large object
43 gettype_size(ptr, ttype, tsize);
44 unsigned INTPTR blocksize = (((unsigned INTPTR)(ptr-gcbaseva)) < BAMBOO_LARGE_SMEM_BOUND)? BAMBOO_SMEM_SIZE_L:BAMBOO_SMEM_SIZE;
46 // ptr is a start of a block OR it crosses the boundary of current block
47 return (*tsize) > blocksize;
50 INLINE unsigned int hostcore(void * ptr) {
51 // check the host core of ptr
52 unsigned int host = 0;
53 if(1 == (NUMCORES4GC)) {
56 unsigned int t = (unsigned int)ptr - (unsigned int)gcbaseva;
57 unsigned int b = (t < BAMBOO_LARGE_SMEM_BOUND) ? t / (BAMBOO_SMEM_SIZE_L) : NUMCORES4GC+((t-(BAMBOO_LARGE_SMEM_BOUND))/(BAMBOO_SMEM_SIZE));
58 host = gc_block2core[(b%(NUMCORES4GC*2))];
63 //push the null check into the mark macro
64 //#define MARKOBJ(objptr, ii) {void * marktmpptr=objptr; if (marktmpptr!=NULL) markObj(marktmpptr, __LINE__, ii);}
65 //#define MARKOBJNONNULL(objptr, ii) {markObj(objptr, __LINE__, ii);}
67 #define MARKOBJ(objptr, ii) {void * marktmpptr=objptr; if (marktmpptr!=NULL) markObj(marktmpptr);}
69 #define MARKOBJNONNULL(objptr, ii) {markObj(objptr);}
71 // NOTE: the objptr should not be NULL and should be a shared obj
72 void markObj(void * objptr) {
73 unsigned int host = hostcore(objptr);
74 if(BAMBOO_NUM_OF_CORE == host) {
76 if(!checkMark(objptr)) {
77 // this is the first time that this object is discovered,
78 // set the flag as DISCOVERED
83 // check if this obj has been forwarded already
84 if(MGCHashadd(gcforwardobjtbl, (unsigned int)objptr)) {
85 // if not, send msg to host informing that objptr is active
86 send_msg_2(host,GCMARKEDOBJ,objptr);
87 GCPROFILE_RECORD_FORWARD_OBJ();
93 void markgarbagelist(struct garbagelist * listptr) {
94 for(;listptr!=NULL;listptr=listptr->next) {
95 int size=listptr->size;
96 for(int i=0; i<size; i++) {
97 MARKOBJ(listptr->array[i], i);
103 void tomark(struct garbagelist * stackptr) {
104 BAMBOO_ASSERT(MARKPHASE == gc_status_info.gcphase);
106 gc_status_info.gcbusystatus = true;
108 // enqueue current stack
109 markgarbagelist(stackptr);
111 // enqueue static pointers global_defs_p
112 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
113 markgarbagelist((struct garbagelist *)global_defs_p);
117 // enqueue objectsets
118 if(BAMBOO_NUM_OF_CORE < NUMCORESACTIVE) {
119 for(int i=0; i<NUMCLASSES; i++) {
120 struct parameterwrapper ** queues = objectqueues[BAMBOO_NUM_OF_CORE][i];
121 int length = numqueues[BAMBOO_NUM_OF_CORE][i];
122 for(int j = 0; j < length; ++j) {
123 struct parameterwrapper * parameter = queues[j];
124 struct ObjectHash * set=parameter->objectset;
125 struct ObjectNode * ptr=set->listhead;
126 for(;ptr!=NULL;ptr=ptr->lnext) {
127 MARKOBJNONNULL((void *)ptr->key, 0);
133 // enqueue current task descriptor
134 if(currtpd != NULL) {
135 for(int i=0; i<currtpd->numParameters; i++) {
136 // currtpd->parameterArray[i] can not be NULL
137 MARKOBJNONNULL(currtpd->parameterArray[i], i);
141 // enqueue active tasks
142 if(activetasks != NULL) {
143 struct genpointerlist * ptr=activetasks->list;
144 for(;ptr!=NULL;ptr=ptr->inext) {
145 struct taskparamdescriptor *tpd=ptr->src;
146 for(int i=0; i<tpd->numParameters; i++) {
147 // the tpd->parameterArray[i] can not be NULL
148 MARKOBJNONNULL(tpd->parameterArray[i], i);
153 // enqueue cached transferred obj
154 struct QueueItem * tmpobjptr = getHead(&objqueue);
155 for(;tmpobjptr != NULL;tmpobjptr=getNextQueueItem(tmpobjptr)) {
156 struct transObjInfo * objInfo=(struct transObjInfo *)(tmpobjptr->objectptr);
157 // the objptr can not be NULL
158 MARKOBJNONNULL(objInfo->objptr, 0);
161 // enqueue cached objs to be transferred
162 struct QueueItem * item = getHead(totransobjqueue);
163 for(;item != NULL;item=getNextQueueItem(item)) {
164 struct transObjInfo * totransobj=(struct transObjInfo *)(item->objectptr);
165 // the objptr can not be NULL
166 MARKOBJNONNULL(totransobj->objptr, 0);
169 // enqueue lock related info
170 for(int i = 0; i < runtime_locklen; i++) {
171 MARKOBJ((void *)(runtime_locks[i].redirectlock), 0);
172 MARKOBJ((void *)(runtime_locks[i].value), i);
177 // enqueue global thread queue
178 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
180 unsigned int thread_counter = *((unsigned int*)(bamboo_thread_queue+1));
181 if(thread_counter > 0) {
182 unsigned int start = *((unsigned int*)(bamboo_thread_queue+2));
183 for(int i = thread_counter; i > 0; i--) {
184 // the thread obj can not be NULL
185 MARKOBJNONNULL((void *)bamboo_thread_queue[4+start], 0);
186 start = (start+1)&bamboo_max_thread_num_mask;
191 // enqueue the bamboo_threadlocks
192 for(int i = 0; i < bamboo_threadlocks.index; i++) {
193 // the locks can not be NULL
194 MARKOBJNONNULL((void *)(bamboo_threadlocks.locks[i].object), i);
197 // enqueue the bamboo_current_thread
198 MARKOBJ(bamboo_current_thread, 0);
202 INLINE void scanPtrsInObj(void * ptr, int type) {
203 // scan all pointers in ptr
204 unsigned int * pointer = pointerarray[type];
206 /* Array of primitives */
207 #ifdef OBJECTHASPOINTERS
208 pointer=pointerarray[OBJECTTYPE];
209 //handle object class
211 for(int i=1; i<=size; i++) {
212 unsigned int offset=pointer[i];
213 void * objptr=*((void **)(((char *)ptr)+offset));
217 } else if (((unsigned int)pointer)==1) {
218 /* Array of pointers */
219 struct ArrayObject *ao=(struct ArrayObject *) ptr;
220 int length=ao->___length___;
221 for(int i=0; i<length; i++) {
222 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
225 #ifdef OBJECTHASPOINTERS
226 pointer=pointerarray[OBJECTTYPE];
227 //handle object class
229 for(int i=1; i<=size; i++) {
230 unsigned int offset=pointer[i];
231 void * objptr=*((void **)(((char *)ptr)+offset));
238 for(int i=1; i<=size; i++) {
239 unsigned int offset=pointer[i];
240 void * objptr=*((void **)(((char *)ptr)+offset));
246 void mark(bool isfirst, struct garbagelist * stackptr) {
250 gccurr_heaptop = 0; // record the size of all active objs in this core
251 // aligned but does not consider block boundaries
252 gcmarkedptrbound = 0;
254 unsigned int isize = 0;
255 bool sendStall = false;
257 while(MARKPHASE == gc_status_info.gcphase) {
260 while(gc_moreItems()) {
262 gc_status_info.gcbusystatus = true;
263 void * ptr = gc_dequeue();
265 unsigned int size = 0;
266 unsigned int type = 0;
267 bool islarge=isLarge(ptr, &type, &size);
268 unsigned int iunits = ALIGNUNITS(size);
270 setLengthMarked(ptr,iunits);
273 // ptr is a large object and not marked or enqueued
274 printf("NEED TO SUPPORT LARGE OBJECTS!\n");
276 // ptr is an unmarked active object on this core
277 unsigned int isize=iunits<<ALIGNMENTSHIFT;
278 gccurr_heaptop += isize;
280 if (top>gcmarkedptrbound)
281 gcmarkedptrbound=top;
284 // scan the pointers in object
285 scanPtrsInObj(ptr, type);
287 gc_status_info.gcbusystatus = false;
288 // send mark finish msg to core coordinator
289 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
290 int entry_index = waitconfirm ? (gcnumsrobjs_index==0) : gcnumsrobjs_index;
291 gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
292 gcnumsendobjs[entry_index][BAMBOO_NUM_OF_CORE]=gcself_numsendobjs;
293 gcnumreceiveobjs[entry_index][BAMBOO_NUM_OF_CORE]=gcself_numreceiveobjs;
296 send_msg_4(STARTUPCORE,GCFINISHMARK,BAMBOO_NUM_OF_CORE,gcself_numsendobjs,gcself_numreceiveobjs);
301 if(BAMBOO_NUM_OF_CORE == STARTUPCORE)
308 #endif // MULTICORE_GC