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);
131 gcmem_mixed_threshold=(unsigned int)((BAMBOO_SHARED_MEM_SIZE-bamboo_reserved_smem*BAMBOO_SMEM_SIZE)*0.8);
132 gcmem_mixed_usedmem = 0;
135 gc_profile_flag = false;
137 gc_localheap_s = false;
138 #ifdef GC_CACHE_ADAPT
139 gccachestage = false;
142 INIT_MULTICORE_GCPROFILE_DATA();
145 void dismulticoregcdata() {
146 freeMGCHash(gcforwardobjtbl);
150 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
151 for(int i = 0; i < NUMCORES4GC; i++) {
153 gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
154 gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
156 gcrequiredmems[i] = 0;
157 gcfilledblocks[i] = 0;
160 for(int i = NUMCORES4GC; i < NUMCORESACTIVE; i++) {
162 gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
163 gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
168 gcnumsrobjs_index = 0;
170 gcself_numsendobjs = 0;
171 gcself_numreceiveobjs = 0;
172 gcmarkedptrbound = 0;
182 MGCHashreset(gcforwardobjtbl);
185 gc_output_cache_policy_time=0;
188 bool gc_checkAllCoreStatus() {
189 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
190 for(int i = 0; i < NUMCORESACTIVE; i++) {
191 if(gccorestatus[i] != 0) {
192 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
196 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
200 // NOTE: should be invoked with interrupts turned off
201 bool gc_checkAllCoreStatus_I() {
202 for(int i = 0; i < NUMCORESACTIVE; i++) {
203 if(gccorestatus[i] != 0) {
210 void checkMarkStatus_p2() {
211 // check if the sum of send objs and receive obj are the same
212 // yes->check if the info is the latest; no->go on executing
213 unsigned int sumsendobj = 0;
214 for(int i = 0; i < NUMCORESACTIVE; i++) {
215 sumsendobj += gcnumsendobjs[gcnumsrobjs_index][i];
217 for(int i = 0; i < NUMCORESACTIVE; i++) {
218 sumsendobj -= gcnumreceiveobjs[gcnumsrobjs_index][i];
220 if(0 == sumsendobj) {
221 // Check if there are changes of the numsendobjs or numreceiveobjs
224 for(i = 0; i < NUMCORESACTIVE; i++) {
225 if((gcnumsendobjs[0][i]!=gcnumsendobjs[1][i])||(gcnumreceiveobjs[0][i]!=gcnumreceiveobjs[1][i]) ) {
229 if(i == NUMCORESACTIVE) {
230 // all the core status info are the latest,stop mark phase
231 gc_status_info.gcphase = COMPACTPHASE;
232 // restore the gcstatus for all cores
233 for(int i = 0; i < NUMCORESACTIVE; i++) {
237 // There were changes between phase 1 and phase 2, can not decide
238 // whether the mark phase has been finished
240 // As it fails in phase 2, flip the entries
241 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
244 // There were changes between phase 1 and phase 2, can not decide
245 // whether the mark phase has been finished
247 // As it fails in phase 2, flip the entries
248 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
252 void checkMarkStatus() {
253 if((!waitconfirm)||(waitconfirm && (numconfirm == 0))) {
254 unsigned int entry_index = 0;
257 entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
260 entry_index = gcnumsrobjs_index;
262 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
263 gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
264 gcnumsendobjs[entry_index][BAMBOO_NUM_OF_CORE] = gcself_numsendobjs;
265 gcnumreceiveobjs[entry_index][BAMBOO_NUM_OF_CORE] = gcself_numreceiveobjs;
266 // check the status of all cores
267 if (gc_checkAllCoreStatus_I()) {
270 // the first time found all cores stall
271 // send out status confirm msg to all other cores
272 // reset the corestatus array too
274 numconfirm = NUMCORESACTIVE - 1;
275 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
276 GC_SEND_MSG_1_TO_CLIENT(GCMARKCONFIRM);
279 checkMarkStatus_p2();
280 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
283 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
288 // compute load balance for all cores
289 int loadbalance(void ** heaptop, unsigned int * topblock, unsigned int * topcore) {
290 // compute load balance
291 // get the total loads
292 unsigned int tloads = 0;
293 for(int i = 0; i < NUMCORES4GC; i++) {
294 tloads += gcloads[i];
296 *heaptop = gcbaseva + tloads;
298 unsigned int topblockindex;
300 BLOCKINDEX(topblockindex, *heaptop);
301 // num of blocks per core
302 unsigned int numbpc = (topblockindex+NUMCORES4GC-1)/NUMCORES4GC;
304 *topblock = topblockindex;
305 RESIDECORE(*heaptop, *topcore);
310 // update the bmmboo_smemtbl to record current shared mem usage
311 void updateSmemTbl(unsigned int coren, void * localtop) {
312 unsigned int ltopcore = 0;
313 unsigned int bound = BAMBOO_SMEM_SIZE_L;
314 BLOCKINDEX(ltopcore, localtop);
315 if((unsigned int)localtop>=(unsigned int)(gcbaseva+BAMBOO_LARGE_SMEM_BOUND)){
316 bound = BAMBOO_SMEM_SIZE;
318 unsigned int load = (unsigned INTPTR)(localtop-gcbaseva)%(unsigned int)bound;
319 unsigned int toset = 0;
320 for(int j=0; 1; j++) {
321 for(int i=0; i<2; i++) {
322 toset = gc_core2block[2*coren+i]+(unsigned int)(NUMCORES4GC*2)*j;
323 if(toset < ltopcore) {
324 bamboo_smemtbl[toset]=BLOCKSIZE(toset<NUMCORES4GC);
326 gcmem_mixed_usedmem += bamboo_smemtbl[toset];
328 } else if(toset == ltopcore) {
329 bamboo_smemtbl[toset] = load;
331 gcmem_mixed_usedmem += bamboo_smemtbl[toset];
341 void gc_collect(struct garbagelist * stackptr) {
342 gc_status_info.gcprocessing = true;
343 // inform the master that this core is at a gc safe point and is ready to
345 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
347 // core collector routine
348 //wait for init phase
349 WAITFORGCPHASE(INITPHASE);
351 GC_PRINTF("Do initGC\n");
354 //send init finish msg to core coordinator
355 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
357 //wait for mark phase
358 WAITFORGCPHASE(MARKPHASE);
360 GC_PRINTF("Start mark phase\n");
361 mark(true, stackptr);
362 GC_PRINTF("Finish mark phase, start compact phase\n");
364 GC_PRINTF("Finish compact phase\n");
366 WAITFORGCPHASE(UPDATEPHASE);
368 GC_PRINTF("Start flush phase\n");
369 GCPROFILE_INFO_2_MASTER();
371 GC_PRINTF("Finish flush phase\n");
373 CACHEADAPT_PHASE_CLIENT();
375 // invalidate all shared mem pointers
376 bamboo_cur_msp = NULL;
377 bamboo_smem_size = 0;
378 bamboo_smem_zero_top = NULL;
381 WAITFORGCPHASE(FINISHPHASE);
383 GC_PRINTF("Finish gc! \n");
386 void gc_nocollect(struct garbagelist * stackptr) {
387 gc_status_info.gcprocessing = true;
388 // inform the master that this core is at a gc safe point and is ready to
390 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
392 WAITFORGCPHASE(INITPHASE);
394 GC_PRINTF("Do initGC\n");
397 //send init finish msg to core coordinator
398 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
400 WAITFORGCPHASE(MARKPHASE);
402 GC_PRINTF("Start mark phase\n");
403 mark(true, stackptr);
404 GC_PRINTF("Finish mark phase, wait for flush\n");
406 // non-gc core collector routine
407 WAITFORGCPHASE(UPDATEPHASE);
409 GC_PRINTF("Start flush phase\n");
410 GCPROFILE_INFO_2_MASTER();
412 GC_PRINTF("Finish flush phase\n");
414 CACHEADAPT_PHASE_CLIENT();
416 // invalidate all shared mem pointers
417 bamboo_cur_msp = NULL;
418 bamboo_smem_size = 0;
419 bamboo_smem_zero_top = NULL;
422 WAITFORGCPHASE(FINISHPHASE);
424 GC_PRINTF("Finish gc! \n");
427 void master_mark(struct garbagelist *stackptr) {
430 GC_PRINTF("Start mark phase \n");
431 GC_SEND_MSG_1_TO_CLIENT(GCSTART);
432 gc_status_info.gcphase = MARKPHASE;
435 while(MARKPHASE == gc_status_info.gcphase) {
436 mark(isfirst, stackptr);
443 void master_getlargeobjs() {
444 // send msgs to all cores requiring large objs info
445 // Note: only need to ask gc cores, non-gc cores do not host any objs
446 numconfirm = NUMCORES4GC - 1;
447 for(int i = 1; i < NUMCORES4GC; i++) {
448 send_msg_1(i,GCLOBJREQUEST);
450 gcloads[BAMBOO_NUM_OF_CORE] = gccurr_heaptop;
451 //spin until we have all responses
452 while(numconfirm!=0) ;
455 if(gcheaptop < gcmarkedptrbound) {
456 gcheaptop = gcmarkedptrbound;
459 GC_PRINTF("prepare to cache large objs \n");
464 void master_updaterefs(struct garbagelist * stackptr) {
465 gc_status_info.gcphase = UPDATEPHASE;
466 GC_SEND_MSG_1_TO_CLIENT(GCSTARTUPDATE);
468 GC_PRINTF("Start flush phase \n");
471 GC_CHECK_ALL_CORE_STATUS(UPDATEPHASE==gc_status_info.gcphase);
472 GC_PRINTF("Finish flush phase \n");
475 void master_finish() {
476 gc_status_info.gcphase = FINISHPHASE;
478 // invalidate all shared mem pointers
479 // put it here as it takes time to inform all the other cores to
480 // finish gc and it might cause problem when some core resumes
481 // mutator earlier than the other cores
482 bamboo_cur_msp = NULL;
483 bamboo_smem_size = 0;
484 bamboo_smem_zero_top = NULL;
487 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
488 CACHEADAPT_OUTPUT_CACHE_POLICY();
489 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
491 GC_SEND_MSG_1_TO_CLIENT(GCFINISH);
493 gc_status_info.gcprocessing = false;
495 // inform other cores to stop and wait for gc
497 for(int i = 0; i < NUMCORESACTIVE; i++) {
498 // reuse the gcnumsendobjs & gcnumreceiveobjs
499 gcnumsendobjs[0][i] = 0;
500 gcnumreceiveobjs[0][i] = 0;
502 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
506 void gc_master(struct garbagelist * stackptr) {
507 tprintf("start GC !!!!!!!!!!!!! \n");
508 gc_status_info.gcprocessing = true;
509 gc_status_info.gcphase = INITPHASE;
514 GC_SEND_MSG_1_TO_CLIENT(GCSTARTINIT);
516 GC_PRINTF("Check core status \n");
517 GC_CHECK_ALL_CORE_STATUS(true);
519 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
520 CACHEADAPT_OUTPUT_CACHE_SAMPLING();
521 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
524 master_mark(stackptr);
526 // get large objects from all cores
527 master_getlargeobjs();
532 // update the references
533 master_updaterefs(stackptr);
535 // do cache adaptation
536 CACHEADAPT_PHASE_MASTER();
538 // do finish up stuff
541 GC_PRINTF("gc finished \n");
542 tprintf("finish GC ! %d \n",gcflag);
547 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
548 gcnumsendobjs[0][BAMBOO_NUM_OF_CORE] = self_numsendobjs;
549 gcnumreceiveobjs[0][BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
551 for(int i = 0; i < NUMCORESACTIVE; i++) {
552 sumsendobj += gcnumsendobjs[0][i];
554 for(int i = 0; i < NUMCORESACTIVE; i++) {
555 sumsendobj -= gcnumreceiveobjs[0][i];
557 if(0 != sumsendobj) {
558 // there were still some msgs on the fly, wait until there
559 // are some update pregc information coming and check it again
561 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
565 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
571 void pregcprocessing() {
572 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
573 // disable the timer interrupt
574 bamboo_mask_timer_intr();
576 // Zero out the remaining memory here because for the GC_CACHE_ADAPT version,
577 // we need to make sure during the gcinit phase the shared heap is not
578 // touched. Otherwise, there would be problem when adapt the cache strategy.
579 BAMBOO_CLOSE_CUR_MSP();
580 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
581 // get the sampling data
582 bamboo_output_dtlb_sampling();
586 void postgcprocessing() {
587 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
588 // enable the timer interrupt
589 bamboo_tile_timer_set_next_event(GC_TILE_TIMER_EVENT_SETTING);
590 bamboo_unmask_timer_intr();
594 bool gc(struct garbagelist * stackptr) {
597 gc_status_info.gcprocessing = false;
601 // core coordinator routine
602 if(0 == BAMBOO_NUM_OF_CORE) {
603 GC_PRINTF("Check if we can do gc or not\n");
604 gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
605 if(!gc_checkAllCoreStatus()) {
606 // some of the cores are still executing the mutator and did not reach
607 // some gc safe point, therefore it is not ready to do gc
614 GC_PRINTF("start gc! \n");
617 } else if(BAMBOO_NUM_OF_CORE < NUMCORES4GC) {
619 gc_collect(stackptr);
622 gc_nocollect(stackptr);