changes
[IRC.git] / Robust / src / Runtime / bamboo / multicoregarbage.c
1 #ifdef MULTICORE_GC
2 #include "runtime.h"
3 #include "multicoreruntime.h"
4 #include "multicoregarbage.h"
5 #include "multicoregcmark.h"
6 #include "multicoregccompact.h"
7 #include "multicoregcflush.h"
8 #include "multicoregcprofile.h"
9 #include "gcqueue.h"
10 #include "multicoremem_helper.h"
11 #include "bambooalign.h"
12 #ifdef PERFCOUNT
13 #include "bme_perf_counter.h"
14 #endif
15
16 volatile bool gcflag;
17 gc_status_t gc_status_info;
18
19 unsigned long long gc_output_cache_policy_time=0;
20
21 #ifdef GC_DEBUG
22 // dump whole mem in blocks
23 void dumpSMem() {
24   int block = 0;
25   int sblock = 0;
26   unsigned int j = 0;
27   unsigned int i = 0;
28   int coren = 0;
29   int x = 0;
30   int y = 0;
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(),
34          udn_tile_coord_y());
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)));
46   }
47   sblock = 0;
48   bool advanceblock = false;
49   // remaining memory
50   for(i=gcbaseva; (unsigned int)i<(unsigned int)(gcbaseva+BAMBOO_SHARED_MEM_SIZE); i+=4*16) {
51     advanceblock = false;
52     // computing sblock # and block #, core coordinate (x,y) also
53     if(j%((BAMBOO_SMEM_SIZE)/(4*16)) == 0) {
54       // finished a sblock
55       if(j < ((BAMBOO_LARGE_SMEM_BOUND)/(4*16))) {
56         if((j > 0) && (j%((BAMBOO_SMEM_SIZE_L)/(4*16)) == 0)) {
57           // finished a block
58           block++;
59           advanceblock = true;  
60         }
61       } else {
62         // finished a block
63         block++;
64         advanceblock = true;
65       }
66       // compute core #
67       if(advanceblock) {
68         coren = gc_block2core[block%(NUMCORES4GC*2)];
69       }
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);
76     }
77     j++;
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)));
88   }
89   printf("(%x,%x) \n", udn_tile_coord_x(), udn_tile_coord_y());
90 }
91 #endif
92
93 bool gc_checkCoreStatus() {
94   for(int i = 0; i < NUMCORES4GC; i++) {
95     if(gccorestatus[i]) {
96       return false;
97     }
98   }
99   return true;
100 }
101
102 void gc_resetCoreStatus() {
103   for(int i = 0; i < NUMCORES4GC; i++) {
104     gccorestatus[i] = 1;
105   }
106 }
107
108
109 void initmulticoregcdata() {
110   bamboo_smem_zero_top = NULL;
111   gcflag = false;
112   gc_status_info.gcprocessing = false;
113   gc_status_info.gcphase = FINISHPHASE;
114
115   gcprecheck = true;
116   gcforwardobjtbl = allocateMGCHash_I(128);
117 #ifdef MGC_SPEC
118   gc_profile_flag = false;
119 #endif
120
121   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
122     allocationinfo.blocktable=RUNMALLOC(sizeof(struct blockrecord)*GCNUMBLOCK);
123     for(int i=0; i<GCNUMBLOCK;i++) {
124       if (1==NUMCORES4GC)
125         allocationinfo.blocktable[i].corenum=0;
126       else
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);
131     }
132     buildCore2Test();
133   }
134
135   //initialize update structures
136   origarraycount=0;
137   for(int i=0;i<NUMCORES4GC;i++) {
138     origblockarray[i]=NULL;
139   }
140
141   INIT_MULTICORE_GCPROFILE_DATA();
142 }
143
144 void dismulticoregcdata() {
145   freeMGCHash(gcforwardobjtbl);
146 }
147
148 void initGC() {
149   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
150     for(int i = 0; i < NUMCORES4GC; i++) {
151       gccorestatus[i] = 1;
152       gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
153       gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
154       gcloads[i] = 0;
155       gcrequiredmems[i] = 0;
156     } 
157     for(int i = NUMCORES4GC; i < NUMCORESACTIVE; i++) {
158       gccorestatus[i] = 1;
159       gcnumsendobjs[0][i] = gcnumsendobjs[1][i] = 0;
160       gcnumreceiveobjs[0][i] = gcnumreceiveobjs[1][i] = 0;
161     }
162     gcnumsrobjs_index = 0;
163   } 
164   gcself_numsendobjs = 0;
165   gcself_numreceiveobjs = 0;
166   gcmovestartaddr = 0;
167   gctomove = false;
168   gcblock2fill = 0;
169   gcmovepending = 0;
170   gccurr_heaptop = 0;
171   update_origblockptr=NULL;
172   gc_queueinit();
173
174   MGCHashreset(gcforwardobjtbl);
175
176   GCPROFILE_INIT();
177   gc_output_cache_policy_time=0;
178
179
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];
187   } 
188   for(int i = 0; i < NUMCORESACTIVE; i++) {
189     sumsendobj -= gcnumreceiveobjs[gcnumsrobjs_index][i];
190   } 
191   if(0 == sumsendobj) {
192     // Check if there are changes of the numsendobjs or numreceiveobjs 
193     // on each core
194     int i = 0;
195     for(i = 0; i < NUMCORESACTIVE; i++) {
196       if((gcnumsendobjs[0][i]!=gcnumsendobjs[1][i])||(gcnumreceiveobjs[0][i]!=gcnumreceiveobjs[1][i]) ) {
197         break;
198       }
199     }  
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++) {
206         gccorestatus[i] = 1;
207       }  
208     } else {
209       // There were changes between phase 1 and phase 2, can not decide 
210       // whether the mark phase has been finished
211       waitconfirm = false;
212       // As it fails in phase 2, flip the entries
213       gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
214     } 
215   } else {
216     // There were changes between phase 1 and phase 2, can not decide 
217     // whether the mark phase has been finished
218     waitconfirm = false;
219     // As it fails in phase 2, flip the entries
220     gcnumsrobjs_index = (gcnumsrobjs_index == 0) ? 1 : 0;
221   }
222 }
223
224 void checkMarkStatus() {
225   //  tprintf("Check mark status\n");
226   if((!waitconfirm)||(waitconfirm && (numconfirm == 0))) {
227     unsigned int entry_index = 0;
228     if(waitconfirm) {
229       // phase 2
230       entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
231     } else {
232       // phase 1
233       entry_index = gcnumsrobjs_index;
234     }
235     BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
236     // check the status of all cores
237     if (gc_checkCoreStatus()) {
238       // ask for confirm
239       if(!waitconfirm) {
240         // the first time found all cores stall
241         // send out status confirm msg to all other cores
242         // reset the corestatus array too    
243         waitconfirm = true;
244         numconfirm = NUMCORESACTIVE - 1;
245         BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
246         GC_SEND_MSG_1_TO_CLIENT(GCMARKCONFIRM);
247       } else {
248         // Phase 2
249         checkMarkStatus_p2(); 
250         BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
251       }
252     } else {
253       BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
254     } 
255   } 
256
257
258 // compute load balance for all cores
259 int loadbalance() {
260   // compute load balance
261   // get the total loads
262   void * heaptop;
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);
267   }
268   heaptop = gcbaseva + tloads;
269
270   unsigned int topblockindex;
271   
272   BLOCKINDEX(topblockindex, heaptop);
273   // num of blocks per core
274   unsigned int numbpc = (topblockindex+NUMCORES4GC-1)/NUMCORES4GC;
275   
276   return numbpc;
277 }
278
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 
282   // do gc
283   send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
284
285   // invalidate all shared mem pointers
286   bamboo_cur_msp = NULL;
287   bamboo_smem_size = 0;
288   bamboo_smem_zero_top = NULL;
289   gcflag = false;
290
291
292   // core collector routine
293   //wait for init phase
294   WAITFORGCPHASE(INITPHASE);
295
296   GC_PRINTF("Do initGC\n");
297   initGC();
298   CACHEADAPT_GC(true);
299   //send init finish msg to core coordinator
300   send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
301
302   //wait for mark phase
303   WAITFORGCPHASE(MARKPHASE);
304
305   GC_PRINTF("Start mark phase\n");
306   mark(stackptr);
307   GC_PRINTF("Finish mark phase, start compact phase\n");
308   compact();
309   GC_PRINTF("Finish compact phase\n");
310
311   WAITFORGCPHASE(UPDATEPHASE);
312
313   GC_PRINTF("Start update phase\n");
314   GCPROFILE_INFO_2_MASTER();
315   update(stackptr);
316   GC_PRINTF("Finish update phase\n");
317
318   CACHEADAPT_PHASE_CLIENT();
319
320   WAITFORGCPHASE(FINISHPHASE);
321
322   GC_PRINTF("Finish gc! \n");
323
324
325 void gc_nocollect(struct garbagelist * stackptr) {
326   gc_status_info.gcprocessing = true;
327   // inform the master that this core is at a gc safe point and is ready to 
328   // do gc
329   send_msg_4(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
330
331   // invalidate all shared mem pointers
332   bamboo_cur_msp = NULL;
333   bamboo_smem_size = 0;
334   bamboo_smem_zero_top = NULL;
335   gcflag = false;
336   
337   WAITFORGCPHASE(INITPHASE);
338
339   GC_PRINTF("Do initGC\n");
340   initGC();
341   CACHEADAPT_GC(true);
342
343   //send init finish msg to core coordinator
344   send_msg_2(STARTUPCORE,GCFINISHINIT,BAMBOO_NUM_OF_CORE);
345
346
347   WAITFORGCPHASE(MARKPHASE);
348
349   GC_PRINTF("Start mark phase\n"); 
350   mark(stackptr);
351   GC_PRINTF("Finish mark phase, wait for update\n");
352
353   // non-gc core collector routine
354   WAITFORGCPHASE(UPDATEPHASE);
355
356   GC_PRINTF("Start update phase\n");
357   GCPROFILE_INFO_2_MASTER();
358   update(stackptr);
359   GC_PRINTF("Finish update phase\n"); 
360
361   CACHEADAPT_PHASE_CLIENT();
362
363   WAITFORGCPHASE(FINISHPHASE);
364
365   GC_PRINTF("Finish gc! \n");
366 }
367
368 void master_mark(struct garbagelist *stackptr) {
369
370   GC_PRINTF("Start mark phase \n");
371   gc_status_info.gcphase = MARKPHASE;
372   GC_SEND_MSG_1_TO_CLIENT(GCSTART);
373   // mark phase
374
375   mark(stackptr);
376 }
377
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);
384   }
385   gcloads[BAMBOO_NUM_OF_CORE] = gccurr_heaptop;
386   //spin until we have all responses
387   while(numconfirm!=0) ;
388
389   GCPROFILE_ITEM_MASTER();
390   GC_PRINTF("prepare to cache large objs \n");
391
392 }
393
394
395 void master_updaterefs(struct garbagelist * stackptr) {
396   gc_status_info.gcphase = UPDATEPHASE;
397   GC_SEND_MSG_1_TO_CLIENT(GCSTARTUPDATE);
398   GC_PRINTF("Start update phase \n");
399   // update phase
400   update(stackptr);
401   GC_CHECK_ALL_CORE_STATUS();
402   GC_PRINTF("Finish update phase \n");
403 }
404
405 void master_finish() {
406   gc_status_info.gcphase = FINISHPHASE;
407   
408   // invalidate all shared mem pointers
409   // put it here as it takes time to inform all the other cores to
410   // finish gc and it might cause problem when some core resumes
411   // mutator earlier than the other cores
412   bamboo_cur_msp = NULL;
413   bamboo_smem_size = 0;
414   bamboo_smem_zero_top = NULL;
415   
416   GCPROFILE_END_MASTER();
417   unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
418   CACHEADAPT_OUTPUT_CACHE_POLICY();
419   gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
420   gcflag = false;
421
422   GC_SEND_MSG_1_TO_CLIENT(GCFINISH);
423   gc_status_info.gcprocessing = false;  
424
425   if(gcflag) {
426     // inform other cores to stop and wait for gc
427     GC_PRINTF("Back to Back gc case\n");
428     gcprecheck = true;
429     for(int i = 0; i < NUMCORESACTIVE; i++) {
430       // reuse the gcnumsendobjs & gcnumreceiveobjs
431       gcnumsendobjs[0][i] = 0;
432       gcnumreceiveobjs[0][i] = 0;
433     }
434     GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
435   }
436 }
437
438 void gc_master(struct garbagelist * stackptr) {
439   tprintf("start GC!\n");
440   gc_status_info.gcprocessing = true;
441   gc_status_info.gcphase = INITPHASE;
442   GC_SEND_MSG_1_TO_CLIENT(GCSTARTINIT);
443
444   waitconfirm = false;
445   numconfirm = 0;
446   initGC();
447   CACHEADAPT_GC(true);
448   //tprintf("Check core status \n");
449   GC_CHECK_ALL_CORE_STATUS();
450   unsigned long long tmpt = BAMBOO_GET_EXE_TIME();
451   CACHEADAPT_OUTPUT_CACHE_SAMPLING();
452   gc_output_cache_policy_time += (BAMBOO_GET_EXE_TIME()-tmpt);
453   //tprintf("start mark phase\n");
454   // do mark phase
455   GCPROFILE_ITEM_MASTER();
456   master_mark(stackptr);
457   GCPROFILE_ITEM_MASTER();
458   //tprintf("finish mark phase\n");
459   // get large objects from all cores
460   master_getlargeobjs();
461   //tprintf("start compact phase\n");
462   // compact the heap
463   master_compact();
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
472 #ifdef GC_DEBUG
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));
476   }
477 #endif
478
479   master_finish();
480
481   //tprintf("finish GC ! %d \n",gcflag);
482
483
484 void pregccheck() {
485   while(true) {
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;
489     int sumsendobj = 0;
490     for(int i = 0; i < NUMCORESACTIVE; i++) {
491       sumsendobj += gcnumsendobjs[0][i];
492     }  
493     for(int i = 0; i < NUMCORESACTIVE; i++) {
494       sumsendobj -= gcnumreceiveobjs[0][i];
495     } 
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
499       gcprecheck = false;
500       BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
501
502       while(!gcprecheck) ;
503     } else {
504       BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
505       return;
506     }
507   }
508 }
509
510 void pregcprocessing() {
511 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)&&(defined(GC_CACHE_ADAPT_POLICY4)||defined(GC_CACHE_ADAPT_POLICY3))
512   // disable the timer interrupt
513   bamboo_mask_timer_intr();
514   // get the sampling data 
515   bamboo_output_dtlb_sampling();
516 #endif
517 }
518
519 void postgcprocessing() {
520 #if defined(GC_CACHE_ADAPT)&&defined(GC_CACHE_SAMPLING)&&(defined(GC_CACHE_ADAPT_POLICY4)||defined(GC_CACHE_ADAPT_POLICY3))
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();
526 #endif
527 }
528
529 bool gc(struct garbagelist * stackptr) {
530   // check if do gc
531   if(!gcflag) {
532     gc_status_info.gcprocessing = false;
533     return false;
534   }
535 #ifdef PERFCOUNT
536   profile_start(GC_REGION);
537 #endif
538
539   // core coordinator routine
540   if(0 == BAMBOO_NUM_OF_CORE) {
541     GC_PRINTF("start gc! \n");
542     GCPROFILE_START_MASTER();
543
544     GC_PRINTF("Check if we can do gc or not\n");
545     gccorestatus[BAMBOO_NUM_OF_CORE] = 0;
546     pregcprocessing();
547
548     //wait for other cores to catch up
549     while(!gc_checkCoreStatus())
550       ;
551
552     //pregccheck();
553     gc_master(stackptr);
554   } else if(BAMBOO_NUM_OF_CORE < NUMCORES4GC) {
555     GC_PRINTF("Core reporting for gc.\n");
556     pregcprocessing();
557     gc_collect(stackptr);
558   } else {
559     pregcprocessing();
560     gc_nocollect(stackptr);
561   }
562   postgcprocessing();
563 #ifdef PERFCOUNT
564   profile_start(APP_REGION);
565 #endif
566   return true;
567
568
569 #endif