1 #ifndef BAMBOO_MULTICORE_GARBAGE_H
2 #define BAMBOO_MULTICORE_GARBAGE_H
5 #include "multicoregc.h"
6 #include "multicorehelper.h" // for mappings between core # and block #
7 #include "structdefs.h"
8 #include "multicoregcprofile.h"
12 #define GC_PRINTF tprintf
14 #define GC_PRINTF if(0) tprintf
18 #define TR() tprintf("%u\n",__LINE__)
23 // data structures for GC
24 #define BAMBOO_SMEM_SIZE_L (BAMBOO_SMEM_SIZE * 2)
25 #define BAMBOO_LARGE_SMEM_BOUND (BAMBOO_SMEM_SIZE_L*NUMCORES4GC)
26 // let each gc core to have one big block, this is very important
27 // for the computation of NUMBLOCKS(s, n), DO NOT change this!
30 INITPHASE = 0x0, // 0x0
33 SUBTLECOMPACTPHASE, // 0x3
36 CACHEPOLICYPHASE, // 0x6
37 PREFINISHPHASE, // 0x7
41 typedef struct gc_status {
42 volatile bool gcprocessing;
43 volatile GCPHASETYPE gcphase; // indicating GC phase
44 volatile bool gcbusystatus;
47 extern volatile bool gcflag;
48 extern gc_status_t gc_status_info;
49 volatile bool gcprecheck; // indicates if there are updated pregc information
51 unsigned INTPTR gccurr_heaptop;
52 struct MGCHash * gcforwardobjtbl; // cache forwarded objs in mark phase
53 // for mark phase termination
54 volatile unsigned int gccorestatus[NUMCORESACTIVE];//records status of each core
58 volatile unsigned int returnedmem[NUMCORESACTIVE];//records status of each core
61 volatile unsigned int gcnumsendobjs[2][NUMCORESACTIVE];//# of objects sent out
62 volatile unsigned int gcnumreceiveobjs[2][NUMCORESACTIVE];//# of objects received
63 volatile unsigned int gcnumsrobjs_index;//indicates which entry to record the
64 // info received before phase 1 of the mark finish
66 // the info received in phase 2 must be
67 // recorded in the other entry
69 unsigned int gcself_numsendobjs;
70 unsigned int gcself_numreceiveobjs;
73 unsigned INTPTR gcloads[NUMCORES4GC];
74 block_t numblockspercore;
76 //Top of each core's heap
77 void * topptrs[NUMCORES4GC];
79 // compact instruction
80 //keep track of what block we can fill to
81 unsigned int gcblock2fill;
84 //this points to memory handed to core from master
85 volatile unsigned int gcmovestartaddr;
86 //this flag tells core that it is okay to start compacting
87 volatile bool gctomove;
89 //keeps track of memory request master was not able to serve
90 volatile unsigned int maxusefulmems[NUMCORES4GC]; //record pending mem requests
91 volatile unsigned int gcrequiredmems[NUMCORES4GC]; //record pending mem requests
92 volatile unsigned int gcmovepending;
94 //keep track of current base block pointer for orig block
95 volatile void * update_origblockptr;
96 volatile void * origblockarray[NUMCORES4GC];
97 volatile int origarraycount;
98 volatile bool blockgranted;
101 // shared memory pointer for pointer mapping tbls
102 // In GC version, this block of memory is located at the bottom of the
103 // shared memory, right on the top of the smem tbl.
104 // The bottom of the shared memory = sbstart tbl + smemtbl + bamboo_rmsp
105 // These three types of table are always reside at the bottom of the shared
106 // memory and will never be moved or garbage collected
107 //gcmappingtable gives new pointer location
108 void ** gcmappingtbl;
109 //number of bytes in mapping table
110 unsigned int bamboo_rmsp_size;
112 //mark table....keep track of mark bits
113 volatile unsigned int * gcmarktbl;
115 bool gc_checkCoreStatus();
117 void gc_resetCoreStatus();
121 /* Structure to keep track of free space in block */
123 /* BS_USED indicates that we don't have information for this block yet */
125 /* BS_LARGEOBJECT indicates that the beginning of this block has a large object*/
127 /* BS_FREE indicates that the block is at least partially free */
132 enum blockstatus status;
133 unsigned INTPTR usedspace;
134 unsigned INTPTR freespace;
135 unsigned int corenum;
138 #define NOFREEBLOCK 0xffffffff
140 unsigned int lowestfreeblock;
141 struct blockrecord * blocktable;
144 struct allocrecord allocationinfo;
146 #ifdef GC_CACHE_ADAPT
147 volatile bool gccachestage;
148 // table recording the sampling data collected for cache adaption
149 unsigned int * gccachesamplingtbl;
150 unsigned int * gccachesamplingtbl_local;// for zeroing memory only
151 unsigned int size_cachesamplingtbl_local; // for zeroing memory only
152 unsigned int * gccachesamplingtbl_r;
153 unsigned int * gccachesamplingtbl_local_r; // for zeroing memory only
154 unsigned int size_cachesamplingtbl_local_r; // for zeroing memory only
155 unsigned int * gccachepolicytbl;
156 unsigned int size_cachepolicytbl;
159 /* Total number of blocks in heap */
161 #define GCNUMBLOCK (NUMCORES4GC+(BAMBOO_SHARED_MEM_SIZE-BAMBOO_LARGE_SMEM_BOUND)/BAMBOO_SMEM_SIZE)
162 #define GCNUMLOCALBLOCK (GCNUMBLOCK/NUMCORES4GC)
164 /* Leave the neighboring cores with at least two blocks */
165 #define MAXNEIGHBORALLOC (GCNUMLOCALBLOCK-2)
167 /* This macro defines the smallest memoy chunk the master will hand out to another core during compacting */
169 #define MINMEMORYCHUNKSIZE 32768
171 #define ISVALIDPTR(x) ((((unsigned INTPTR)x)>=((unsigned INTPTR)gcbaseva))&&(((unsigned INTPTR)x)<((unsigned INTPTR)(gcbaseva+BAMBOO_SHARED_MEM_SIZE))))
173 /* This macro waits for the given gc phase */
174 #define WAITFORGCPHASE(phase) while(gc_status_info.gcphase != phase) ;
176 /* Local block number that can never be reached...*/
177 #define MAXBLOCK 0x4fffffff
179 //Takes in pointer to heap object and converts to offset in alignment units
180 #define OBJMAPPINGINDEX(p) ALIGNOBJSIZE((unsigned INTPTR)(p-gcbaseva))
182 //Converts size of object into alignment units (need to round up)
183 #define ALIGNUNITS(s) (((s-1)>>ALIGNMENTSHIFT)+1)
185 //Rounds object size up to next alignment unit size
186 #define ALIGNSIZE(s) ((((unsigned int)(s-1))&~(ALIGNMENTBYTES-1))+ALIGNMENTBYTES)
188 #define GLOBALBLOCK2LOCAL(s) (s/NUMCORES4GC)
190 // mapping of pointer to block # (start from 0), here the block # is
192 #define BLOCKINDEX(b, p) \
194 unsigned INTPTR t = (unsigned INTPTR)(p - gcbaseva); \
195 if(t < BAMBOO_LARGE_SMEM_BOUND) { \
196 b = t / BAMBOO_SMEM_SIZE_L; \
198 b = NUMCORES4GC+((t-BAMBOO_LARGE_SMEM_BOUND)/BAMBOO_SMEM_SIZE); \
202 #define RESIDECORE(c, p) { \
203 if(1 == (NUMCORES4GC)) { \
208 c = gc_block2core[(b%(NUMCORES4GC*2))]; \
212 #define BLOCK2CORE(c, b) { \
213 if(1 == (NUMCORES4GC)) { \
216 c = gc_block2core[(b%(NUMCORES4GC*2))]; \
220 INLINE static unsigned int hostcore(void * ptr) {
221 // check the host core of ptr
223 RESIDECORE(host, ptr);
227 /*This macro takes in a number of bytes (the current offset into the
228 heap) and returns the number of local blocks needed for that many
231 #define NUMBLOCKS(s, n) \
232 if(s < (BAMBOO_SMEM_SIZE_L)) { \
235 (n) = 1 + ((s) - (BAMBOO_SMEM_SIZE_L)) / (BAMBOO_SMEM_SIZE); \
238 //this macro takes in a global block identifier and returns the base
239 //offset into the heap
240 #define OFFSET2BASEVA(i) \
241 (((i)<NUMCORES4GC)?(BAMBOO_SMEM_SIZE_L*(i)):(BAMBOO_SMEM_SIZE*((i)-NUMCORES4GC)+BAMBOO_LARGE_SMEM_BOUND))
244 //This macro takes in a local block number and returns the size of the block
245 #define BLOCKSIZE(c) \
246 ((c)?BAMBOO_SMEM_SIZE:BAMBOO_SMEM_SIZE_L)
248 //This macro takes in a local block number and returns the size of the block
249 #define GLOBALBLOCKSIZE(c) \
250 ((c<NUMCORES4GC)?BAMBOO_SMEM_SIZE_L:BAMBOO_SMEM_SIZE)
252 //Takes as input the core number c and the local block index n and
253 //returns the global block index
255 #define BLOCKINDEX2(c, n) \
256 (gc_core2block[2*(c)+((n)&1)]+(NUMCORES4GC*2)*((n)>>1))
258 //This macro takes in a global block number and returns the base
259 //pointer of the next block
260 #define BOUNDPTR(b) \
261 (((b)<NUMCORES4GC)?(((b)+1)*BAMBOO_SMEM_SIZE_L):(BAMBOO_LARGE_SMEM_BOUND+((b)-NUMCORES4GC+1)*BAMBOO_SMEM_SIZE))
263 //This macro takes in the core number c and the local block number and
264 //sets p to the base pointer
266 #define BASEPTR(p, c, n) { \
267 unsigned int b = BLOCKINDEX2((c), (n)); \
268 if(b < (NUMCORES4GC)) { \
269 p = gcbaseva + b * (BAMBOO_SMEM_SIZE_L); \
271 p = gcbaseva+(BAMBOO_LARGE_SMEM_BOUND)+ \
272 (b-(NUMCORES4GC))*(BAMBOO_SMEM_SIZE); \
276 // the next core in the top of the heap
277 #define NEXTTOPCORE(b) (gc_block2core[((b)+1)%(NUMCORES4GC*2)])
279 // check if all cores are stall now
280 #define GC_CHECK_ALL_CORE_STATUS() gccorestatus[BAMBOO_NUM_OF_CORE] = 0; \
281 while(!gc_checkCoreStatus()) ;
283 // send a 1-word msg to all clients
284 #define GC_SEND_MSG_1_TO_CLIENT(m) \
286 for(int i = 0; i < NUMCORESACTIVE; ++i) { \
287 gccorestatus[i] = 1; \
288 if(BAMBOO_NUM_OF_CORE != i) { \
289 send_msg_1(i, (m)); \
294 #define ISLOCAL(p) (hostcore(p)==BAMBOO_NUM_OF_CORE)
296 void initmulticoregcdata();
297 void dismulticoregcdata();
298 bool gc(struct garbagelist * stackptr); // core coordinator routine
299 void gc_collect(struct garbagelist* stackptr); //core collector routine
300 void gc_nocollect(struct garbagelist* stackptr); //non-gc core collector routine
301 void master_mark(struct garbagelist *stackptr);
302 void master_getlargeobjs();
303 void master_compact();
304 void master_updaterefs();
305 void master_finish();
306 void gc_master(struct garbagelist * stackptr);
309 void transferMarkResults_I();
310 void * gcfindSpareMem_I(unsigned INTPTR requiredmem, unsigned INTPTR maxbytesneeded, unsigned int requiredcore);
312 #define INITMULTICOREGCDATA() initmulticoregcdata()
313 #define DISMULTICOREGCDATA() dismulticoregcdata()
314 #else // MULTICORE_GC
315 #define INITMULTICOREGCDATA()
316 #define DISMULTICOREGCDATA()
317 #endif // MULTICORE_GC
318 #endif // BAMBOO_MULTICORE_GARBAGE_H