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"
14 gc_status_t gc_status_info;
16 unsigned long long gc_output_cache_policy_time=0;
19 // dump whole mem in blocks
28 printf("(%x,%x) Dump shared mem: \n",udn_tile_coord_x(),udn_tile_coord_y());
29 // reserved blocks for sblocktbl
30 printf("(%x,%x) ++++ reserved sblocks ++++ \n", udn_tile_coord_x(),
32 for(i=BAMBOO_BASE_VA; i<gcbaseva; i+= 4*16) {
33 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",
34 udn_tile_coord_x(), udn_tile_coord_y(),
35 *((int *)(i)), *((int *)(i + 4)),
36 *((int *)(i + 4*2)), *((int *)(i + 4*3)),
37 *((int *)(i + 4*4)), *((int *)(i + 4*5)),
38 *((int *)(i + 4*6)), *((int *)(i + 4*7)),
39 *((int *)(i + 4*8)), *((int *)(i + 4*9)),
40 *((int *)(i + 4*10)), *((int *)(i + 4*11)),
41 *((int *)(i + 4*12)), *((int *)(i + 4*13)),
42 *((int *)(i + 4*14)), *((int *)(i + 4*15)));
45 bool advanceblock = false;
47 for(i=gcbaseva; (unsigned int)i<(unsigned int)(gcbaseva+BAMBOO_SHARED_MEM_SIZE); i+=4*16) {
49 // computing sblock # and block #, core coordinate (x,y) also
50 if(j%((BAMBOO_SMEM_SIZE)/(4*16)) == 0) {
52 if(j < ((BAMBOO_LARGE_SMEM_BOUND)/(4*16))) {
53 if((j > 0) && (j%((BAMBOO_SMEM_SIZE_L)/(4*16)) == 0)) {
65 coren = gc_block2core[block%(NUMCORES4GC*2)];
67 // compute core coordinate
68 x = BAMBOO_COORDS_X(coren);
69 y = BAMBOO_COORDS_Y(coren);
70 printf("(%x,%x) ==== %d, %d : core (%d,%d), saddr %x====\n",
71 udn_tile_coord_x(), udn_tile_coord_y(),block, sblock++, x, y,
72 (sblock-1)*(BAMBOO_SMEM_SIZE)+gcbaseva);
75 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",
76 udn_tile_coord_x(), udn_tile_coord_y(),
77 *((int *)(i)), *((int *)(i + 4)),
78 *((int *)(i + 4*2)), *((int *)(i + 4*3)),
79 *((int *)(i + 4*4)), *((int *)(i + 4*5)),
80 *((int *)(i + 4*6)), *((int *)(i + 4*7)),
81 *((int *)(i + 4*8)), *((int *)(i + 4*9)),
82 *((int *)(i + 4*10)), *((int *)(i + 4*11)),
83 *((int *)(i + 4*12)), *((int *)(i + 4*13)),
84 *((int *)(i + 4*14)), *((int *)(i + 4*15)));
86 printf("(%x,%x) \n", udn_tile_coord_x(), udn_tile_coord_y());
90 bool gc_checkCoreStatus() {
91 for(int i = 0; i < NUMCORES4GC; i++) {
99 void gc_resetCoreStatus() {
100 for(int i = 0; i < NUMCORES4GC; i++) {
106 void initmulticoregcdata() {
107 bamboo_smem_zero_top = NULL;
109 gc_status_info.gcprocessing = false;
110 gc_status_info.gcphase = FINISHPHASE;
113 gcforwardobjtbl = allocateMGCHash_I(128);
115 gc_profile_flag = false;
117 #ifdef GC_CACHE_ADAPT
118 gccachestage = false;
121 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
122 allocationinfo.blocktable=RUNMALLOC(sizeof(struct blockrecord)*GCNUMBLOCK);
123 for(int i=0; i<GCNUMBLOCK;i++) {
125 allocationinfo.blocktable[i].corenum=0;
127 allocationinfo.blocktable[i].corenum=gc_block2core[(i%(NUMCORES4GC*2))];
128 allocationinfo.blocktable[i].status=BS_FREE;
129 allocationinfo.blocktable[i].usedspace=0;
130 allocationinfo.blocktable[i].freespace=GLOBALBLOCKSIZE(i);
135 //initialize update structures
137 for(int i=0;i<NUMCORES4GC;i++) {
138 origblockarray[i]=NULL;
141 INIT_MULTICORE_GCPROFILE_DATA();
144 void dismulticoregcdata() {
145 freeMGCHash(gcforwardobjtbl);
149 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
150 for(int i = 0; i < NUMCORES4GC; i++) {
152 gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
153 gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
155 gcrequiredmems[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;
162 gcnumsrobjs_index = 0;
164 gcself_numsendobjs = 0;
165 gcself_numreceiveobjs = 0;
171 update_origblockptr=NULL;
174 MGCHashreset(gcforwardobjtbl);
177 gc_output_cache_policy_time=0;
180 void checkMarkStatus_p2() {
181 // tprintf("Check mark status 2\n");
182 // check if the sum of send objs and receive obj are the same
183 // yes->check if the info is the latest; no->go on executing
184 unsigned int sumsendobj = 0;
185 for(int i = 0; i < NUMCORESACTIVE; i++) {
186 sumsendobj += gcnumsendobjs[gcnumsrobjs_index][i];
188 for(int i = 0; i < NUMCORESACTIVE; i++) {
189 sumsendobj -= gcnumreceiveobjs[gcnumsrobjs_index][i];
191 if(0 == sumsendobj) {
192 // Check if there are changes of the numsendobjs or numreceiveobjs
195 for(i = 0; i < NUMCORESACTIVE; i++) {
196 if((gcnumsendobjs[0][i]!=gcnumsendobjs[1][i])||(gcnumreceiveobjs[0][i]!=gcnumreceiveobjs[1][i]) ) {
200 if(i == NUMCORESACTIVE) {
201 //tprintf("Mark terminated\n");
202 // all the core status info are the latest,stop mark phase
203 gc_status_info.gcphase = COMPACTPHASE;
204 // restore the gcstatus for all cores
205 for(int i = 0; i < NUMCORESACTIVE; i++) {
209 // There were changes between phase 1 and phase 2, can not decide
210 // whether the mark phase has been finished
212 // As it fails in phase 2, flip the entries
213 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
216 // There were changes between phase 1 and phase 2, can not decide
217 // whether the mark phase has been finished
219 // As it fails in phase 2, flip the entries
220 gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
224 void checkMarkStatus() {
225 // tprintf("Check mark status\n");
226 if((!waitconfirm)||(waitconfirm && (numconfirm == 0))) {
227 unsigned int entry_index = 0;
230 entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
233 entry_index = gcnumsrobjs_index;
235 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
236 // check the status of all cores
237 if (gc_checkCoreStatus()) {
240 // the first time found all cores stall
241 // send out status confirm msg to all other cores
242 // reset the corestatus array too
244 numconfirm = NUMCORESACTIVE - 1;
245 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
246 GC_SEND_MSG_1_TO_CLIENT(GCMARKCONFIRM);
249 checkMarkStatus_p2();
250 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
253 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
258 // compute load balance for all cores
260 // compute load balance
261 // get the total loads
263 unsigned int tloads = 0;
264 for(int i = 0; i < NUMCORES4GC; i++) {
265 tloads += gcloads[i];
266 //tprintf("load: %d %d \n", gcloads[i], i);
268 heaptop = gcbaseva + tloads;
270 unsigned int topblockindex;
272 BLOCKINDEX(topblockindex, heaptop);
273 // num of blocks per core
274 unsigned int numbpc = (topblockindex+NUMCORES4GC-1)/NUMCORES4GC;
279 void gc_collect(struct garbagelist * stackptr) {
280 gc_status_info.gcprocessing = true;
281 // inform the master that this core is at a gc safe point and is ready to
283 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
285 // core collector routine
286 //wait for init phase
287 WAITFORGCPHASE(INITPHASE);
289 GC_PRINTF("Do initGC\n");
292 //send init finish msg to core coordinator
293 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
295 //wait for mark phase
296 WAITFORGCPHASE(MARKPHASE);
298 GC_PRINTF("Start mark phase\n");
300 GC_PRINTF("Finish mark phase, start compact phase\n");
302 GC_PRINTF("Finish compact phase\n");
304 WAITFORGCPHASE(UPDATEPHASE);
306 GC_PRINTF("Start update phase\n");
307 GCPROFILE_INFO_2_MASTER();
309 GC_PRINTF("Finish update phase\n");
311 CACHEADAPT_PHASE_CLIENT();
313 // invalidate all shared mem pointers
314 bamboo_cur_msp = NULL;
315 bamboo_smem_size = 0;
316 bamboo_smem_zero_top = NULL;
319 WAITFORGCPHASE(FINISHPHASE);
321 GC_PRINTF("Finish gc! \n");
324 void gc_nocollect(struct garbagelist * stackptr) {
325 gc_status_info.gcprocessing = true;
326 // inform the master that this core is at a gc safe point and is ready to
328 send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
330 WAITFORGCPHASE(INITPHASE);
332 GC_PRINTF("Do initGC\n");
335 //send init finish msg to core coordinator
336 send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
338 WAITFORGCPHASE(MARKPHASE);
340 GC_PRINTF("Start mark phase\n");
342 GC_PRINTF("Finish mark phase, wait for update\n");
344 // non-gc core collector routine
345 WAITFORGCPHASE(UPDATEPHASE);
347 GC_PRINTF("Start update phase\n");
348 GCPROFILE_INFO_2_MASTER();
350 GC_PRINTF("Finish update phase\n");
352 CACHEADAPT_PHASE_CLIENT();
354 // invalidate all shared mem pointers
355 bamboo_cur_msp = NULL;
356 bamboo_smem_size = 0;
357 bamboo_smem_zero_top = NULL;
360 WAITFORGCPHASE(FINISHPHASE);
362 GC_PRINTF("Finish gc! \n");
365 void master_mark(struct garbagelist *stackptr) {
367 GC_PRINTF("Start mark phase \n");
368 gc_status_info.gcphase = MARKPHASE;
369 GC_SEND_MSG_1_TO_CLIENT(GCSTART);
375 void master_getlargeobjs() {
376 // send msgs to all cores requiring large objs info
377 // Note: only need to ask gc cores, non-gc cores do not host any objs
378 numconfirm = NUMCORES4GC - 1;
379 for(int i = 1; i < NUMCORES4GC; i++) {
380 send_msg_1(i,GCLOBJREQUEST);
382 gcloads[BAMBOO_NUM_OF_CORE] = gccurr_heaptop;
383 //spin until we have all responses
384 while(numconfirm!=0) ;
387 GC_PRINTF("prepare to cache large objs \n");
392 void master_updaterefs(struct garbagelist * stackptr) {
393 gc_status_info.gcphase = UPDATEPHASE;
394 GC_SEND_MSG_1_TO_CLIENT(GCSTARTUPDATE);
396 GC_PRINTF("Start update phase \n");
399 GC_CHECK_ALL_CORE_STATUS();
400 GC_PRINTF("Finish update phase \n");
403 void master_finish() {
404 gc_status_info.gcphase = FINISHPHASE;
406 // invalidate all shared mem pointers
407 // put it here as it takes time to inform all the other cores to
408 // finish gc and it might cause problem when some core resumes
409 // mutator earlier than the other cores
410 bamboo_cur_msp = NULL;
411 bamboo_smem_size = 0;
412 bamboo_smem_zero_top = NULL;
415 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
416 CACHEADAPT_OUTPUT_CACHE_POLICY();
417 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
420 GC_SEND_MSG_1_TO_CLIENT(GCFINISH);
421 gc_status_info.gcprocessing = false;
424 // inform other cores to stop and wait for gc
425 GC_PRINTF("Back to Back gc case\n");
427 for(int i = 0; i < NUMCORESACTIVE; i++) {
428 // reuse the gcnumsendobjs & gcnumreceiveobjs
429 gcnumsendobjs[0][i] = 0;
430 gcnumreceiveobjs[0][i] = 0;
432 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
436 void gc_master(struct garbagelist * stackptr) {
437 tprintf("start GC!\n");
438 gc_status_info.gcprocessing = true;
439 gc_status_info.gcphase = INITPHASE;
444 GC_SEND_MSG_1_TO_CLIENT(GCSTARTINIT);
446 //tprintf("Check core status \n");
447 GC_CHECK_ALL_CORE_STATUS();
449 unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
450 CACHEADAPT_OUTPUT_CACHE_SAMPLING();
451 gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
452 //tprintf("start mark phase\n");
454 master_mark(stackptr);
455 //tprintf("finish mark phase\n");
456 // get large objects from all cores
457 master_getlargeobjs();
458 //tprintf("start compact phase\n");
461 //tprintf("start update phase\n");
462 // update the references
463 master_updaterefs(stackptr);
464 //tprintf("gc master finished update \n");
465 // do cache adaptation
466 CACHEADAPT_PHASE_MASTER();
467 //tprintf("finish cachdapt phase\n");
468 // do finish up stuff
470 for(int i=0;i<GCNUMBLOCK;i++) {
471 struct blockrecord *record=&allocationinfo.blocktable[i];
472 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));
478 //tprintf("finish GC ! %d \n",gcflag);
483 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
484 gcnumsendobjs[0][BAMBOO_NUM_OF_CORE] = self_numsendobjs;
485 gcnumreceiveobjs[0][BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
487 for(int i = 0; i < NUMCORESACTIVE; i++) {
488 sumsendobj += gcnumsendobjs[0][i];
490 for(int i = 0; i < NUMCORESACTIVE; i++) {
491 sumsendobj -= gcnumreceiveobjs[0][i];
493 if(0 != sumsendobj) {
494 // there were still some msgs on the fly, wait until there
495 // are some update pregc information coming and check it again
497 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
501 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
507 void pregcprocessing() {
508 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
509 // disable the timer interrupt
510 bamboo_mask_timer_intr();
512 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
513 // get the sampling data
514 bamboo_output_dtlb_sampling();
518 void postgcprocessing() {
519 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)
520 // enable the timer interrupt
521 bamboo_tile_timer_set_next_event(GC_TILE_TIMER_EVENT_SETTING);
522 bamboo_unmask_timer_intr();
526 bool gc(struct garbagelist * stackptr) {
529 gc_status_info.gcprocessing = false;
533 // core coordinator routine
534 if(0 == BAMBOO_NUM_OF_CORE) {
535 GC_PRINTF("Check if we can do gc or not\n");
536 gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
538 //wait for other cores to catch up
539 while(!gc_checkCoreStatus())
544 GC_PRINTF("start gc! \n");
547 } else if(BAMBOO_NUM_OF_CORE < NUMCORES4GC) {
548 GC_PRINTF("Core reporting for gc.\n");
550 gc_collect(stackptr);
553 gc_nocollect(stackptr);