3 #include "multicoreruntime.h"
4 #include "multicoregarbage.h"
5 #include "multicoregcmark.h"
6 #include "multicoregccompact.h"
7 #include "multicoregcflush.h"
8 #include "multicoregcprofile.h"
10 #include "multicoremem_helper.h"
11 #include "bambooalign.h"
13 #include "bme_perf_counter.h"
17 gc_status_t gc_status_info;
19 unsigned long long gc_output_cache_policy_time=0;
22 // dump whole mem in blocks
31 printf("(%x,%x) Dump shared mem: \n",udn_tile_coord_x(),udn_tile_coord_y());
32 // reserved blocks for sblocktbl
33 printf("(%x,%x) ++++ reserved sblocks ++++ \n", udn_tile_coord_x(),
35 for(i=BAMBOO_BASE_VA; i<gcbaseva; i+= 4*16) {
36 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",
37 udn_tile_coord_x(), udn_tile_coord_y(),
38 *((int *)(i)), *((int *)(i + 4)),
39 *((int *)(i + 4*2)), *((int *)(i + 4*3)),
40 *((int *)(i + 4*4)), *((int *)(i + 4*5)),
41 *((int *)(i + 4*6)), *((int *)(i + 4*7)),
42 *((int *)(i + 4*8)), *((int *)(i + 4*9)),
43 *((int *)(i + 4*10)), *((int *)(i + 4*11)),
44 *((int *)(i + 4*12)), *((int *)(i + 4*13)),
45 *((int *)(i + 4*14)), *((int *)(i + 4*15)));
48 bool advanceblock = false;
50 for(i=gcbaseva; (unsigned int)i<(unsigned int)(gcbaseva+BAMBOO_SHARED_MEM_SIZE); i+=4*16) {
52 // computing sblock # and block #, core coordinate (x,y) also
53 if(j%((BAMBOO_SMEM_SIZE)/(4*16)) == 0) {
55 if(j < ((BAMBOO_LARGE_SMEM_BOUND)/(4*16))) {
56 if((j > 0) && (j%((BAMBOO_SMEM_SIZE_L)/(4*16)) == 0)) {
68 coren = gc_block2core[block%(NUMCORES4GC*2)];
70 // compute core coordinate
71 x = BAMBOO_COORDS_X(coren);
72 y = BAMBOO_COORDS_Y(coren);
73 printf("(%x,%x) ==== %d, %d : core (%d,%d), saddr %x====\n",
74 udn_tile_coord_x(), udn_tile_coord_y(),block, sblock++, x, y,
75 (sblock-1)*(BAMBOO_SMEM_SIZE)+gcbaseva);
78 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",
79 udn_tile_coord_x(), udn_tile_coord_y(),
80 *((int *)(i)), *((int *)(i + 4)),
81 *((int *)(i + 4*2)), *((int *)(i + 4*3)),
82 *((int *)(i + 4*4)), *((int *)(i + 4*5)),
83 *((int *)(i + 4*6)), *((int *)(i + 4*7)),
84 *((int *)(i + 4*8)), *((int *)(i + 4*9)),
85 *((int *)(i + 4*10)), *((int *)(i + 4*11)),
86 *((int *)(i + 4*12)), *((int *)(i + 4*13)),
87 *((int *)(i + 4*14)), *((int *)(i + 4*15)));
89 printf("(%x,%x) \n", udn_tile_coord_x(), udn_tile_coord_y());
93 bool gc_checkCoreStatus() {
94 for(int i = 0; i < NUMCORES4GC; i++) {
102 void gc_resetCoreStatus() {
103 for(int i = 0; i < NUMCORES4GC; i++) {
109 void initmulticoregcdata() {
110 bamboo_smem_zero_top = NULL;
112 gc_status_info.gcprocessing = false;
113 gc_status_info.gcphase = FINISHPHASE;
116 gcforwardobjtbl = allocateMGCHash_I(128);
118 gc_profile_flag = false;
120 #ifdef GC_CACHE_ADAPT
121 gccachestage = false;
124 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
125 allocationinfo.blocktable=RUNMALLOC(sizeof(struct blockrecord)*GCNUMBLOCK);
126 for(int i=0; i<GCNUMBLOCK;i++) {
128 allocationinfo.blocktable[i].corenum=0;
130 allocationinfo.blocktable[i].corenum=gc_block2core[(i%(NUMCORES4GC*2))];
131 allocationinfo.blocktable[i].status=BS_FREE;
132 allocationinfo.blocktable[i].usedspace=0;
133 allocationinfo.blocktable[i].freespace=GLOBALBLOCKSIZE(i);
138 //initialize update structures
140 for(int i=0;i<NUMCORES4GC;i++) {
141 origblockarray[i]=NULL;
144 INIT_MULTICORE_GCPROFILE_DATA();
147 void dismulticoregcdata() {
148 freeMGCHash(gcforwardobjtbl);
152 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
153 for(int i = 0; i < NUMCORES4GC; i++) {
155 gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
156 gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
158 gcrequiredmems[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;
165 gcnumsrobjs_index = 0;
167 gcself_numsendobjs = 0;
168 gcself_numreceiveobjs = 0;
174 update_origblockptr=NULL;
177 MGCHashreset(gcforwardobjtbl);
180 gc_output_cache_policy_time=0;
183 void checkMarkStatus_p2() {
184 // tprintf("Check mark status 2\n");
185 // check if the sum of send objs and receive obj are the same
186 // yes->check if the info is the latest; no->go on executing
187 unsigned int sumsendobj = 0;
188 for(int i = 0; i < NUMCORESACTIVE; i++) {
189 sumsendobj += gcnumsendobjs[gcnumsrobjs_index][i];
191 for(int i = 0; i < NUMCORESACTIVE; i++) {
192 sumsendobj -= gcnumreceiveobjs[gcnumsrobjs_index][i];
194 if(0 == sumsendobj) {
195 // Check if there are changes of the numsendobjs or numreceiveobjs
198 for(i = 0; i < NUMCORESACTIVE; i++) {
199 if((gcnumsendobjs[0][i]!=gcnumsendobjs[1][i])||(gcnumreceiveobjs[0][i]!=gcnumreceiveobjs[1][i]) ) {
203 if(i == NUMCORESACTIVE) {
204 //tprintf("Mark terminated\n");
205 // all the core status info are the latest,stop mark phase
206 gc_status_info.gcphase = COMPACTPHASE;
207 // restore the gcstatus for all cores
208 for(int i = 0; i < NUMCORESACTIVE; i++) {
212 // There were changes between phase 1 and phase 2, can not decide
213 // whether the mark phase has been finished
215 // As it fails in phase 2, flip the entries
216 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
219 // There were changes between phase 1 and phase 2, can not decide
220 // whether the mark phase has been finished
222 // As it fails in phase 2, flip the entries
223 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
227 void checkMarkStatus() {
228 // tprintf("Check mark status\n");
229 if((!waitconfirm)||(waitconfirm && (numconfirm == 0))) {
230 unsigned int entry_index = 0;
233 entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
236 entry_index = gcnumsrobjs_index;
238 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
239 // check the status of all cores
240 if (gc_checkCoreStatus()) {
243 // the first time found all cores stall
244 // send out status confirm msg to all other cores
245 // reset the corestatus array too
247 numconfirm = NUMCORESACTIVE - 1;
248 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
249 GC_SEND_MSG_1_TO_CLIENT(GCMARKCONFIRM);
252 checkMarkStatus_p2();
253 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
256 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
261 // compute load balance for all cores
263 // compute load balance
264 // get the total loads
266 unsigned int tloads = 0;
267 for(int i = 0; i < NUMCORES4GC; i++) {
268 tloads += gcloads[i];
269 //tprintf("load: %d %d \n", gcloads[i], i);
271 heaptop = gcbaseva + tloads;
273 unsigned int topblockindex;
275 BLOCKINDEX(topblockindex, heaptop);
276 // num of blocks per core
277 unsigned int numbpc = (topblockindex+NUMCORES4GC-1)/NUMCORES4GC;
282 void gc_collect(struct garbagelist * stackptr) {
283 gc_status_info.gcprocessing = true;
284 // inform the master that this core is at a gc safe point and is ready to
286 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
288 // core collector routine
289 //wait for init phase
290 WAITFORGCPHASE(INITPHASE);
292 GC_PRINTF("Do initGC\n");
295 //send init finish msg to core coordinator
296 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
298 //wait for mark phase
299 WAITFORGCPHASE(MARKPHASE);
301 GC_PRINTF("Start mark phase\n");
303 GC_PRINTF("Finish mark phase, start compact phase\n");
305 GC_PRINTF("Finish compact phase\n");
307 WAITFORGCPHASE(UPDATEPHASE);
309 GC_PRINTF("Start update phase\n");
310 GCPROFILE_INFO_2_MASTER();
312 GC_PRINTF("Finish update phase\n");
314 CACHEADAPT_PHASE_CLIENT();
316 // invalidate all shared mem pointers
317 bamboo_cur_msp = NULL;
318 bamboo_smem_size = 0;
319 bamboo_smem_zero_top = NULL;
322 WAITFORGCPHASE(FINISHPHASE);
324 GC_PRINTF("Finish gc! \n");
327 void gc_nocollect(struct garbagelist * stackptr) {
328 gc_status_info.gcprocessing = true;
329 // inform the master that this core is at a gc safe point and is ready to
331 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
333 WAITFORGCPHASE(INITPHASE);
335 GC_PRINTF("Do initGC\n");
338 //send init finish msg to core coordinator
339 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
341 WAITFORGCPHASE(MARKPHASE);
343 GC_PRINTF("Start mark phase\n");
345 GC_PRINTF("Finish mark phase, wait for update\n");
347 // non-gc core collector routine
348 WAITFORGCPHASE(UPDATEPHASE);
350 GC_PRINTF("Start update phase\n");
351 GCPROFILE_INFO_2_MASTER();
353 GC_PRINTF("Finish update phase\n");
355 CACHEADAPT_PHASE_CLIENT();
357 // invalidate all shared mem pointers
358 bamboo_cur_msp = NULL;
359 bamboo_smem_size = 0;
360 bamboo_smem_zero_top = NULL;
363 WAITFORGCPHASE(FINISHPHASE);
365 GC_PRINTF("Finish gc! \n");
368 void master_mark(struct garbagelist *stackptr) {
370 GC_PRINTF("Start mark phase \n");
371 gc_status_info.gcphase = MARKPHASE;
372 GC_SEND_MSG_1_TO_CLIENT(GCSTART);
378 void master_getlargeobjs() {
379 // send msgs to all cores requiring large objs info
380 // Note: only need to ask gc cores, non-gc cores do not host any objs
381 numconfirm = NUMCORES4GC - 1;
382 for(int i = 1; i < NUMCORES4GC; i++) {
383 send_msg_1(i,GCLOBJREQUEST);
385 gcloads[BAMBOO_NUM_OF_CORE] = gccurr_heaptop;
386 //spin until we have all responses
387 while(numconfirm!=0) ;
390 GC_PRINTF("prepare to cache large objs \n");
395 void master_updaterefs(struct garbagelist * stackptr) {
396 gc_status_info.gcphase = UPDATEPHASE;
397 GC_SEND_MSG_1_TO_CLIENT(GCSTARTUPDATE);
399 GC_PRINTF("Start update phase \n");
402 GC_CHECK_ALL_CORE_STATUS();
403 GC_PRINTF("Finish update phase \n");
406 void master_finish() {
407 gc_status_info.gcphase = FINISHPHASE;
409 // invalidate all shared mem pointers
410 // put it here as it takes time to inform all the other cores to
411 // finish gc and it might cause problem when some core resumes
412 // mutator earlier than the other cores
413 bamboo_cur_msp = NULL;
414 bamboo_smem_size = 0;
415 bamboo_smem_zero_top = NULL;
418 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
419 CACHEADAPT_OUTPUT_CACHE_POLICY();
420 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
423 GC_SEND_MSG_1_TO_CLIENT(GCFINISH);
424 gc_status_info.gcprocessing = false;
427 // inform other cores to stop and wait for gc
428 GC_PRINTF("Back to Back gc case\n");
430 for(int i = 0; i < NUMCORESACTIVE; i++) {
431 // reuse the gcnumsendobjs & gcnumreceiveobjs
432 gcnumsendobjs[0][i] = 0;
433 gcnumreceiveobjs[0][i] = 0;
435 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
439 void gc_master(struct garbagelist * stackptr) {
440 tprintf("start GC!\n");
441 gc_status_info.gcprocessing = true;
442 gc_status_info.gcphase = INITPHASE;
447 GC_SEND_MSG_1_TO_CLIENT(GCSTARTINIT);
449 //tprintf("Check core status \n");
450 GC_CHECK_ALL_CORE_STATUS();
452 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
453 CACHEADAPT_OUTPUT_CACHE_SAMPLING();
454 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
455 //tprintf("start mark phase\n");
457 master_mark(stackptr);
458 //tprintf("finish mark phase\n");
459 // get large objects from all cores
460 master_getlargeobjs();
461 //tprintf("start compact phase\n");
464 //tprintf("start update phase\n");
465 // update the references
466 master_updaterefs(stackptr);
467 //tprintf("gc master finished update \n");
468 // do cache adaptation
469 CACHEADAPT_PHASE_MASTER();
470 //tprintf("finish cachdapt phase\n");
471 // do finish up stuff
473 for(int i=0;i<GCNUMBLOCK;i++) {
474 struct blockrecord *record=&allocationinfo.blocktable[i];
475 tprintf("%u. used=%u free=%u corenum=%u status=%u, base=%x, ptr=%x\n", i, record->usedspace, record->freespace, record->corenum, record->status, gcbaseva+OFFSET2BASEVA(i), (gcbaseva+OFFSET2BASEVA(i)+record->usedspace));
481 //tprintf("finish GC ! %d \n",gcflag);
486 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
487 gcnumsendobjs[0][BAMBOO_NUM_OF_CORE] = self_numsendobjs;
488 gcnumreceiveobjs[0][BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
490 for(int i = 0; i < NUMCORESACTIVE; i++) {
491 sumsendobj += gcnumsendobjs[0][i];
493 for(int i = 0; i < NUMCORESACTIVE; i++) {
494 sumsendobj -= gcnumreceiveobjs[0][i];
496 if(0 != sumsendobj) {
497 // there were still some msgs on the fly, wait until there
498 // are some update pregc information coming and check it again
500 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
504 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
510 void pregcprocessing() {
511 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
512 // disable the timer interrupt
513 bamboo_mask_timer_intr();
514 // get the sampling data
515 bamboo_output_dtlb_sampling();
519 void postgcprocessing() {
520 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
521 // enable the timer interrupt
522 bamboo_tile_timer_set_next_event(GC_TILE_TIMER_EVENT_SETTING);
523 bamboo_unmask_timer_intr();
524 //turn on sampling again
525 bamboo_dtlb_sampling_init();
529 bool gc(struct garbagelist * stackptr) {
532 gc_status_info.gcprocessing = false;
536 profile_start(GC_REGION);
539 // core coordinator routine
540 if(0 == BAMBOO_NUM_OF_CORE) {
541 GC_PRINTF("Check if we can do gc or not\n");
542 gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
544 //wait for other cores to catch up
545 while(!gc_checkCoreStatus())
550 GC_PRINTF("start gc! \n");
553 } else if(BAMBOO_NUM_OF_CORE < NUMCORES4GC) {
554 GC_PRINTF("Core reporting for gc.\n");
556 gc_collect(stackptr);
559 gc_nocollect(stackptr);
563 profile_start(APP_REGION);