1 // TODO: DO NOT support tag!!!
4 #include "multicoreruntime.h"
5 #include "multicoregarbage.h"
6 #include "multicoregcmark.h"
8 #include "multicoregccompact.h"
9 #include "multicoregcflush.h"
10 #include "multicoregcprofile.h"
14 extern unsigned int gcmem_mixed_threshold;
15 extern unsigned int gcmem_mixed_usedmem;
19 gc_status_t gc_status_info;
21 unsigned long long gc_output_cache_policy_time=0;
24 // dump whole mem in blocks
33 printf("(%x,%x) Dump shared mem: \n",udn_tile_coord_x(),udn_tile_coord_y());
34 // reserved blocks for sblocktbl
35 printf("(%x,%x) ++++ reserved sblocks ++++ \n", udn_tile_coord_x(),
37 for(i=BAMBOO_BASE_VA; (unsinged int)i<(unsigned int)gcbaseva; i+= 4*16) {
38 printf("(%x,%x) 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x \n",
39 udn_tile_coord_x(), udn_tile_coord_y(),
40 *((int *)(i)), *((int *)(i + 4)),
41 *((int *)(i + 4*2)), *((int *)(i + 4*3)),
42 *((int *)(i + 4*4)), *((int *)(i + 4*5)),
43 *((int *)(i + 4*6)), *((int *)(i + 4*7)),
44 *((int *)(i + 4*8)), *((int *)(i + 4*9)),
45 *((int *)(i + 4*10)), *((int *)(i + 4*11)),
46 *((int *)(i + 4*12)), *((int *)(i + 4*13)),
47 *((int *)(i + 4*14)), *((int *)(i + 4*15)));
50 bool advanceblock = false;
52 for(i=gcbaseva; (unsigned int)i<(unsigned int)(gcbaseva+BAMBOO_SHARED_MEM_SIZE); i+=4*16) {
54 // computing sblock # and block #, core coordinate (x,y) also
55 if(j%((BAMBOO_SMEM_SIZE)/(4*16)) == 0) {
57 if(j < ((BAMBOO_LARGE_SMEM_BOUND)/(4*16))) {
58 if((j > 0) && (j%((BAMBOO_SMEM_SIZE_L)/(4*16)) == 0)) {
70 coren = gc_block2core[block%(NUMCORES4GC*2)];
72 // compute core coordinate
73 x = BAMBOO_COORDS_X(coren);
74 y = BAMBOO_COORDS_Y(coren);
75 printf("(%x,%x) ==== %d, %d : core (%d,%d), saddr %x====\n",
76 udn_tile_coord_x(), udn_tile_coord_y(),block, sblock++, x, y,
77 (sblock-1)*(BAMBOO_SMEM_SIZE)+gcbaseva);
80 printf("(%x,%x) 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x \n",
81 udn_tile_coord_x(), udn_tile_coord_y(),
82 *((int *)(i)), *((int *)(i + 4)),
83 *((int *)(i + 4*2)), *((int *)(i + 4*3)),
84 *((int *)(i + 4*4)), *((int *)(i + 4*5)),
85 *((int *)(i + 4*6)), *((int *)(i + 4*7)),
86 *((int *)(i + 4*8)), *((int *)(i + 4*9)),
87 *((int *)(i + 4*10)), *((int *)(i + 4*11)),
88 *((int *)(i + 4*12)), *((int *)(i + 4*13)),
89 *((int *)(i + 4*14)), *((int *)(i + 4*15)));
91 printf("(%x,%x) \n", udn_tile_coord_x(), udn_tile_coord_y());
95 void initmulticoregcdata() {
96 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
97 // startup core to initialize corestatus[]
98 for(int i = 0; i < NUMCORESACTIVE; i++) {
100 gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
101 gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
103 for(int i = 0; i < NUMCORES4GC; i++) {
105 gcrequiredmems[i] = 0;
107 gcfilledblocks[i] = 0;
111 bamboo_smem_zero_top = NULL;
113 gc_status_info.gcprocessing = false;
114 gc_status_info.gcphase = FINISHPHASE;
118 gcself_numsendobjs = 0;
119 gcself_numreceiveobjs = 0;
120 gcmarkedptrbound = 0;
121 gcforwardobjtbl = allocateMGCHash_I(128);
128 gcmem_mixed_threshold=(unsigned int)((BAMBOO_SHARED_MEM_SIZE-bamboo_reserved_smem*BAMBOO_SMEM_SIZE)*0.8);
129 gcmem_mixed_usedmem = 0;
132 gc_profile_flag = false;
134 gc_localheap_s = false;
135 #ifdef GC_CACHE_ADAPT
136 gccachestage = false;
139 INIT_MULTICORE_GCPROFILE_DATA();
142 void dismulticoregcdata() {
143 freeMGCHash(gcforwardobjtbl);
147 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
148 for(int i = 0; i < NUMCORES4GC; i++) {
150 gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
151 gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
153 gcrequiredmems[i] = 0;
154 gcfilledblocks[i] = 0;
157 for(int i = NUMCORES4GC; i < NUMCORESACTIVE; i++) {
159 gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
160 gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
163 gcnumsrobjs_index = 0;
165 gcself_numsendobjs = 0;
166 gcself_numreceiveobjs = 0;
167 gcmarkedptrbound = 0;
176 MGCHashreset(gcforwardobjtbl);
179 gc_output_cache_policy_time=0;
182 bool gc_checkAllCoreStatus() {
183 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
184 for(int i = 0; i < NUMCORESACTIVE; i++) {
185 if(gccorestatus[i] != 0) {
186 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
190 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
194 // NOTE: should be invoked with interrupts turned off
195 bool gc_checkAllCoreStatus_I() {
196 for(int i = 0; i < NUMCORESACTIVE; i++) {
197 if(gccorestatus[i] != 0) {
204 void checkMarkStatus_p2() {
205 // check if the sum of send objs and receive obj are the same
206 // yes->check if the info is the latest; no->go on executing
207 unsigned int sumsendobj = 0;
208 for(int i = 0; i < NUMCORESACTIVE; i++) {
209 sumsendobj += gcnumsendobjs[gcnumsrobjs_index][i];
211 for(int i = 0; i < NUMCORESACTIVE; i++) {
212 sumsendobj -= gcnumreceiveobjs[gcnumsrobjs_index][i];
214 if(0 == sumsendobj) {
215 // Check if there are changes of the numsendobjs or numreceiveobjs
218 for(i = 0; i < NUMCORESACTIVE; i++) {
219 if((gcnumsendobjs[0][i]!=gcnumsendobjs[1][i])||(gcnumreceiveobjs[0][i]!=gcnumreceiveobjs[1][i]) ) {
223 if(i == NUMCORESACTIVE) {
224 // all the core status info are the latest,stop mark phase
225 gc_status_info.gcphase = COMPACTPHASE;
226 // restore the gcstatus for all cores
227 for(int i = 0; i < NUMCORESACTIVE; i++) {
231 // There were changes between phase 1 and phase 2, can not decide
232 // whether the mark phase has been finished
234 // As it fails in phase 2, flip the entries
235 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
238 // There were changes between phase 1 and phase 2, can not decide
239 // whether the mark phase has been finished
241 // As it fails in phase 2, flip the entries
242 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
246 void checkMarkStatus() {
247 if((!waitconfirm)||(waitconfirm && (numconfirm == 0))) {
248 unsigned int entry_index = 0;
251 entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
254 entry_index = gcnumsrobjs_index;
256 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
257 gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
258 gcnumsendobjs[entry_index][BAMBOO_NUM_OF_CORE] = gcself_numsendobjs;
259 gcnumreceiveobjs[entry_index][BAMBOO_NUM_OF_CORE] = gcself_numreceiveobjs;
260 // check the status of all cores
261 if (gc_checkAllCoreStatus_I()) {
264 // the first time found all cores stall
265 // send out status confirm msg to all other cores
266 // reset the corestatus array too
268 numconfirm = NUMCORESACTIVE - 1;
269 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
270 GC_SEND_MSG_1_TO_CLIENT(GCMARKCONFIRM);
273 checkMarkStatus_p2();
274 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
277 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
282 // compute load balance for all cores
283 int loadbalance(void ** heaptop) {
284 // compute load balance
285 // get the total loads
286 unsigned int tloads = 0;
287 for(int i = 0; i < NUMCORES4GC; i++) {
288 tloads += gcloads[i];
290 *heaptop = gcbaseva + tloads;
292 unsigned int topblockindex;
294 BLOCKINDEX(topblockindex, *heaptop);
295 // num of blocks per core
296 unsigned int numbpc = (topblockindex+NUMCORES4GC-1)/NUMCORES4GC;
302 // update the bmmboo_smemtbl to record current shared mem usage
303 void updateSmemTbl(unsigned int coren, void * localtop) {
304 unsigned int ltopcore = 0;
305 unsigned int bound = BAMBOO_SMEM_SIZE_L;
306 BLOCKINDEX(ltopcore, localtop);
307 if((unsigned int)localtop>=(unsigned int)(gcbaseva+BAMBOO_LARGE_SMEM_BOUND)){
308 bound = BAMBOO_SMEM_SIZE;
310 unsigned int load = (unsigned INTPTR)(localtop-gcbaseva)%(unsigned int)bound;
311 unsigned int toset = 0;
312 for(int j=0; 1; j++) {
313 for(int i=0; i<2; i++) {
314 toset = gc_core2block[2*coren+i]+(unsigned int)(NUMCORES4GC*2)*j;
315 if(toset < ltopcore) {
316 bamboo_smemtbl[toset]=BLOCKSIZE(toset<NUMCORES4GC);
318 gcmem_mixed_usedmem += bamboo_smemtbl[toset];
320 } else if(toset == ltopcore) {
321 bamboo_smemtbl[toset] = load;
323 gcmem_mixed_usedmem += bamboo_smemtbl[toset];
333 void gc_collect(struct garbagelist * stackptr) {
334 gc_status_info.gcprocessing = true;
335 // inform the master that this core is at a gc safe point and is ready to
337 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
339 // core collector routine
340 //wait for init phase
341 WAITFORGCPHASE(INITPHASE);
343 GC_PRINTF("Do initGC\n");
346 //send init finish msg to core coordinator
347 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
349 //wait for mark phase
350 WAITFORGCPHASE(MARKPHASE);
352 GC_PRINTF("Start mark phase\n");
353 mark(true, stackptr);
354 GC_PRINTF("Finish mark phase, start compact phase\n");
356 GC_PRINTF("Finish compact phase\n");
358 WAITFORGCPHASE(UPDATEPHASE);
360 GC_PRINTF("Start flush phase\n");
361 GCPROFILE_INFO_2_MASTER();
363 GC_PRINTF("Finish flush phase\n");
365 CACHEADAPT_PHASE_CLIENT();
367 // invalidate all shared mem pointers
368 bamboo_cur_msp = NULL;
369 bamboo_smem_size = 0;
370 bamboo_smem_zero_top = NULL;
373 WAITFORGCPHASE(FINISHPHASE);
375 GC_PRINTF("Finish gc! \n");
378 void gc_nocollect(struct garbagelist * stackptr) {
379 gc_status_info.gcprocessing = true;
380 // inform the master that this core is at a gc safe point and is ready to
382 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
384 WAITFORGCPHASE(INITPHASE);
386 GC_PRINTF("Do initGC\n");
389 //send init finish msg to core coordinator
390 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
392 WAITFORGCPHASE(MARKPHASE);
394 GC_PRINTF("Start mark phase\n");
395 mark(true, stackptr);
396 GC_PRINTF("Finish mark phase, wait for flush\n");
398 // non-gc core collector routine
399 WAITFORGCPHASE(UPDATEPHASE);
401 GC_PRINTF("Start flush phase\n");
402 GCPROFILE_INFO_2_MASTER();
404 GC_PRINTF("Finish flush phase\n");
406 CACHEADAPT_PHASE_CLIENT();
408 // invalidate all shared mem pointers
409 bamboo_cur_msp = NULL;
410 bamboo_smem_size = 0;
411 bamboo_smem_zero_top = NULL;
414 WAITFORGCPHASE(FINISHPHASE);
416 GC_PRINTF("Finish gc! \n");
419 void master_mark(struct garbagelist *stackptr) {
422 GC_PRINTF("Start mark phase \n");
423 GC_SEND_MSG_1_TO_CLIENT(GCSTART);
424 gc_status_info.gcphase = MARKPHASE;
427 while(MARKPHASE == gc_status_info.gcphase) {
428 mark(isfirst, stackptr);
435 void master_getlargeobjs() {
436 // send msgs to all cores requiring large objs info
437 // Note: only need to ask gc cores, non-gc cores do not host any objs
438 numconfirm = NUMCORES4GC - 1;
439 for(int i = 1; i < NUMCORES4GC; i++) {
440 send_msg_1(i,GCLOBJREQUEST);
442 gcloads[BAMBOO_NUM_OF_CORE] = gccurr_heaptop;
443 //spin until we have all responses
444 while(numconfirm!=0) ;
447 if(gcheaptop < gcmarkedptrbound) {
448 gcheaptop = gcmarkedptrbound;
451 GC_PRINTF("prepare to cache large objs \n");
456 void master_updaterefs(struct garbagelist * stackptr) {
457 gc_status_info.gcphase = UPDATEPHASE;
458 GC_SEND_MSG_1_TO_CLIENT(GCSTARTUPDATE);
460 GC_PRINTF("Start flush phase \n");
463 GC_CHECK_ALL_CORE_STATUS(UPDATEPHASE==gc_status_info.gcphase);
464 GC_PRINTF("Finish flush phase \n");
467 void master_finish() {
468 gc_status_info.gcphase = FINISHPHASE;
470 // invalidate all shared mem pointers
471 // put it here as it takes time to inform all the other cores to
472 // finish gc and it might cause problem when some core resumes
473 // mutator earlier than the other cores
474 bamboo_cur_msp = NULL;
475 bamboo_smem_size = 0;
476 bamboo_smem_zero_top = NULL;
479 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
480 CACHEADAPT_OUTPUT_CACHE_POLICY();
481 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
483 GC_SEND_MSG_1_TO_CLIENT(GCFINISH);
485 gc_status_info.gcprocessing = false;
487 // inform other cores to stop and wait for gc
489 for(int i = 0; i < NUMCORESACTIVE; i++) {
490 // reuse the gcnumsendobjs & gcnumreceiveobjs
491 gcnumsendobjs[0][i] = 0;
492 gcnumreceiveobjs[0][i] = 0;
494 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
498 void gc_master(struct garbagelist * stackptr) {
499 tprintf("start GC !!!!!!!!!!!!! \n");
500 gc_status_info.gcprocessing = true;
501 gc_status_info.gcphase = INITPHASE;
506 GC_SEND_MSG_1_TO_CLIENT(GCSTARTINIT);
508 GC_PRINTF("Check core status \n");
509 GC_CHECK_ALL_CORE_STATUS(true);
511 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
512 CACHEADAPT_OUTPUT_CACHE_SAMPLING();
513 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
516 master_mark(stackptr);
518 // get large objects from all cores
519 master_getlargeobjs();
524 // update the references
525 master_updaterefs(stackptr);
527 // do cache adaptation
528 CACHEADAPT_PHASE_MASTER();
530 // do finish up stuff
533 GC_PRINTF("gc finished \n");
534 tprintf("finish GC ! %d \n",gcflag);
539 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
540 gcnumsendobjs[0][BAMBOO_NUM_OF_CORE] = self_numsendobjs;
541 gcnumreceiveobjs[0][BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
543 for(int i = 0; i < NUMCORESACTIVE; i++) {
544 sumsendobj += gcnumsendobjs[0][i];
546 for(int i = 0; i < NUMCORESACTIVE; i++) {
547 sumsendobj -= gcnumreceiveobjs[0][i];
549 if(0 != sumsendobj) {
550 // there were still some msgs on the fly, wait until there
551 // are some update pregc information coming and check it again
553 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
557 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
563 void pregcprocessing() {
564 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
565 // disable the timer interrupt
566 bamboo_mask_timer_intr();
568 // Zero out the remaining memory here because for the GC_CACHE_ADAPT version,
569 // we need to make sure during the gcinit phase the shared heap is not
570 // touched. Otherwise, there would be problem when adapt the cache strategy.
571 BAMBOO_CLOSE_CUR_MSP();
572 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
573 // get the sampling data
574 bamboo_output_dtlb_sampling();
578 void postgcprocessing() {
579 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
580 // enable the timer interrupt
581 bamboo_tile_timer_set_next_event(GC_TILE_TIMER_EVENT_SETTING);
582 bamboo_unmask_timer_intr();
586 bool gc(struct garbagelist * stackptr) {
589 gc_status_info.gcprocessing = false;
593 // core coordinator routine
594 if(0 == BAMBOO_NUM_OF_CORE) {
595 GC_PRINTF("Check if we can do gc or not\n");
596 gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
597 if(!gc_checkAllCoreStatus()) {
598 // some of the cores are still executing the mutator and did not reach
599 // some gc safe point, therefore it is not ready to do gc
606 GC_PRINTF("start gc! \n");
609 } else if(BAMBOO_NUM_OF_CORE < NUMCORES4GC) {
611 gc_collect(stackptr);
614 gc_nocollect(stackptr);