2 #include "multicoregccompact.h"
3 #include "runtime_arch.h"
4 #include "multicoreruntime.h"
5 #include "multicoregarbage.h"
7 #include "multicoremem_helper.h"
9 int gc_countRunningCores() {
11 for(int i = 0; i < NUMCORES4GC; i++) {
19 void initOrig_Dst(struct moveHelper * orig,struct moveHelper * to) {
21 to->localblocknum = 0;
22 BASEPTR(to->base, BAMBOO_NUM_OF_CORE, to->localblocknum);
24 to->bound=to->base+BLOCKSIZE(to->localblocknum);
27 orig->localblocknum = 0;
28 orig->ptr=orig->base = to->base;
29 orig->bound = orig->base + BLOCKSIZE(orig->localblocknum);
32 void getSpaceLocally(struct moveHelper *to) {
33 //we have space on our core...just keep going
35 BASEPTR(to->base,BAMBOO_NUM_OF_CORE, to->localblocknum);
37 to->bound = to->base + BLOCKSIZE(to->localblocknum);
40 //This function is called on the master core only...and typically by
41 //the message interrupt handler
43 void handleReturnMem_I(unsigned int cnum, void *heaptop) {
44 unsigned int blockindex;
45 BLOCKINDEX(blockindex, heaptop);
46 unsigned INTPTR localblocknum=GLOBALBLOCK2LOCAL(blockindex);
48 //this core is done as far as memory usage is concerned
51 struct blockrecord * blockrecord=&allocationinfo.blocktable[blockindex];
53 blockrecord->status=BS_FREE;
54 blockrecord->usedspace=(unsigned INTPTR)(heaptop-OFFSET2BASEVA(blockindex)-gcbaseva);
55 blockrecord->freespace=BLOCKSIZE(localblocknum)-blockrecord->usedspace;
56 /* Update the lowest free block */
57 if (blockindex < allocationinfo.lowestfreeblock) {
58 allocationinfo.lowestfreeblock=blockindex;
61 /* This is our own block...means we should mark other blocks above us as free*/
62 if (cnum==blockrecord->corenum) {
63 unsigned INTPTR nextlocalblocknum=localblocknum+1;
64 for(;nextlocalblocknum<numblockspercore;nextlocalblocknum++) {
65 unsigned INTPTR blocknum=BLOCKINDEX2(cnum, nextlocalblocknum);
66 struct blockrecord * nextblockrecord=&allocationinfo.blocktable[blockindex];
67 nextblockrecord->status=BS_FREE;
68 nextblockrecord->usedspace=0;
69 //this is true because this cannot be the lowest block
70 nextblockrecord->freespace=BLOCKSIZE(1);
74 //this could be the last one....
75 int count=gc_countRunningCores();
76 if (gcmovepending==count) {
77 // All cores have stopped...hand out memory as necessary to handle all requests
78 handleMemoryRequests_I();
80 //see if returned memory blocks let us resolve requests
81 useReturnedMem(cnum, allocationinfo.lowestfreeblock);
85 void useReturnedMem(unsigned int corenum, block_t localblockindex) {
86 for(int i=0;i<NUMCORES4GC;i++) {
87 unsigned INTPTR requiredmem=gcrequiredmems[i];
89 unsigned INTPTR desiredmem=maxusefulmems[i];
90 unsigned INTPTR threshold=(desiredmem<MINMEMORYCHUNKSIZE)? desiredmem: MINMEMORYCHUNKSIZE;
91 unsigned INTPTR memcheck=requiredmem>threshold?requiredmem:threshold;
94 for(block_t nextlocalblocknum=localblockindex;nextlocalblocknum<numblockspercore;nextlocalblocknum++) {
95 unsigned INTPTR blocknum=BLOCKINDEX2(corenum, nextlocalblocknum);
96 struct blockrecord * nextblockrecord=&allocationinfo.blocktable[blocknum];
97 if (nextblockrecord->status==BS_FREE) {
98 unsigned INTPTR freespace=nextblockrecord->freespace&~BAMBOO_CACHE_LINE_MASK;
100 if (freespace>=memcheck) {
101 nextblockrecord->status=BS_USED;
102 void *blockptr=OFFSET2BASEVA(blocknum)+gcbaseva;
103 unsigned INTPTR usedspace=((nextblockrecord->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
104 //taken care of one block
106 void *startaddr=blockptr+usedspace;
109 if(BAMBOO_CHECK_SEND_MODE()) {
110 cache_msg_2_I(corenum,GCMOVESTART,startaddr);
112 send_msg_2_I(corenum,GCMOVESTART,startaddr);
121 void handleReturnMem(unsigned int cnum, void *heaptop) {
122 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
123 handleReturnMem_I(cnum, heaptop);
124 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
127 void getSpaceRemotely(struct moveHelper *to, unsigned int minimumbytes) {
128 //need to get another block from elsewhere
129 //set flag to wait for memory
130 if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
132 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
133 void *startaddr=handlegcfinishcompact_I(BAMBOO_NUM_OF_CORE, minimumbytes, gccurr_heaptop);
134 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
136 gcmovestartaddr=startaddr;
142 //send request for memory
143 send_msg_4(STARTUPCORE,GCFINISHCOMPACT,BAMBOO_NUM_OF_CORE, minimumbytes, gccurr_heaptop);
144 //wait for flag to be set that we received message
150 to->ptr = gcmovestartaddr;
152 //set localblock number to high number to indicate this block isn't local
153 to->localblocknum = MAXBLOCK;
154 unsigned int globalblocknum;
155 BLOCKINDEX(globalblocknum, to->ptr);
156 to->base = gcbaseva + OFFSET2BASEVA(globalblocknum);
157 to->bound = gcbaseva + BOUNDPTR(globalblocknum);
160 void getSpace(struct moveHelper *to, unsigned int minimumbytes) {
161 //need more space to compact into
162 if (to->localblocknum < gcblock2fill) {
165 getSpaceRemotely(to, minimumbytes);
169 void compacthelper(struct moveHelper * orig,struct moveHelper * to) {
170 bool senttopmessage=false;
172 if ((gccurr_heaptop < ((unsigned INTPTR)(to->bound-to->ptr)))&&!senttopmessage) {
173 //This block is the last for this core...let the startup know
174 if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
175 handleReturnMem(BAMBOO_NUM_OF_CORE, to->ptr+gccurr_heaptop);
177 send_msg_3(STARTUPCORE, GCRETURNMEM, BAMBOO_NUM_OF_CORE, to->ptr+gccurr_heaptop);
179 //Only send the message once
182 unsigned int minimumbytes=compactblocks(orig, to);
184 if (orig->ptr==orig->bound) {
185 //need more data to compact
187 orig->localblocknum++;
188 BASEPTR(orig->base,BAMBOO_NUM_OF_CORE, orig->localblocknum);
189 orig->ptr=orig->base;
190 orig->bound = orig->base + BLOCKSIZE(orig->localblocknum);
191 if (orig->base >= gcbaseva+BAMBOO_SHARED_MEM_SIZE)
194 if (minimumbytes!=0) {
195 getSpace(to, minimumbytes);
199 if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
200 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
201 handlegcfinishcompact_I(BAMBOO_NUM_OF_CORE, 0, 0);
202 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
204 send_msg_4(STARTUPCORE,GCFINISHCOMPACT,BAMBOO_NUM_OF_CORE, 0, 0);
208 void * checkNeighbors_I(int corenum, unsigned INTPTR requiredmem, unsigned INTPTR desiredmem) {
209 int minblockindex=allocationinfo.lowestfreeblock/NUMCORES4GC;
210 unsigned INTPTR threshold=(desiredmem<MINMEMORYCHUNKSIZE)? desiredmem: MINMEMORYCHUNKSIZE;
211 unsigned INTPTR memcheck=requiredmem>threshold?requiredmem:threshold;
213 for(int i=0;i<NUM_CORES2TEST;i++) {
214 int neighborcore=core2test[corenum][i];
215 if (neighborcore!=-1) {
216 for(block_t lblock=minblockindex;lblock<numblockspercore;lblock++) {
217 block_t globalblockindex=BLOCKINDEX2(neighborcore, lblock);
218 struct blockrecord * block=&allocationinfo.blocktable[globalblockindex];
219 if (block->status==BS_FREE) {
220 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
221 if (memcheck<=freespace) {
224 block->status=BS_USED;
225 void *blockptr=OFFSET2BASEVA(globalblockindex)+gcbaseva;
226 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
227 return blockptr+usedspace;
236 void * globalSearch_I(unsigned int topblock, unsigned INTPTR requiredmem, unsigned INTPTR desiredmem) {
237 unsigned int firstfree=NOFREEBLOCK;
238 unsigned INTPTR threshold=(desiredmem<MINMEMORYCHUNKSIZE)? desiredmem: MINMEMORYCHUNKSIZE;
239 unsigned INTPTR memcheck=requiredmem>threshold?requiredmem:threshold;
241 for(block_t i=allocationinfo.lowestfreeblock;i<topblock;i++) {
242 struct blockrecord * block=&allocationinfo.blocktable[i];
243 if (block->status==BS_FREE) {
244 if(firstfree==NOFREEBLOCK)
246 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
247 if (memcheck<=freespace) {
250 block->status=BS_USED;
251 void *blockptr=OFFSET2BASEVA(i)+gcbaseva;
252 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
253 allocationinfo.lowestfreeblock=firstfree;
254 return blockptr+usedspace;
258 allocationinfo.lowestfreeblock=firstfree;
262 void handleOneMemoryRequest(int core, unsigned int lowestblock) {
263 unsigned INTPTR requiredmem=gcrequiredmems[core];
264 unsigned INTPTR desiredmem=maxusefulmems[core];
265 block_t firstfree=NOFREEBLOCK;
266 unsigned INTPTR threshold=(desiredmem<MINMEMORYCHUNKSIZE)? desiredmem: MINMEMORYCHUNKSIZE;
267 unsigned INTPTR memcheck=requiredmem>threshold?requiredmem:threshold;
269 for(block_t searchblock=lowestblock;searchblock<GCNUMBLOCK;searchblock++) {
270 struct blockrecord * block=&allocationinfo.blocktable[searchblock];
271 if (block->status==BS_FREE) {
272 if(firstfree==NOFREEBLOCK)
273 firstfree=searchblock;
274 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
275 if (freespace>=memcheck) {
276 //TODO: should check memory block at same level on our own core...if that works, use it to preserve locality
280 block->status=BS_USED;
281 void *blockptr=OFFSET2BASEVA(searchblock)+gcbaseva;
282 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
283 allocationinfo.lowestfreeblock=firstfree;
284 //taken care of one block
286 void *startaddr=blockptr+usedspace;
287 if(BAMBOO_CHECK_SEND_MODE()) {
288 cache_msg_2_I(core,GCMOVESTART,startaddr);
290 send_msg_2_I(core,GCMOVESTART,startaddr);
296 //this is bad...ran out of memory
300 void handleMemoryRequests_I() {
301 unsigned int lowestblock=allocationinfo.lowestfreeblock;
302 if (lowestblock==NOFREEBLOCK) {
303 lowestblock=numblockspercore*NUMCORES4GC;
306 for(int i=0;i < NUMCORES4GC; i++) {
307 if (gcrequiredmems[i]) {
308 handleOneMemoryRequest(i, lowestblock);
309 lowestblock=allocationinfo.lowestfreeblock;
314 /* should be invoked with interrupt turned off */
316 void * gcfindSpareMem_I(unsigned INTPTR requiredmem, unsigned INTPTR desiredmem,unsigned int requiredcore) {
317 if (allocationinfo.lowestfreeblock!=NOFREEBLOCK) {
318 //There are spare blocks
319 unsigned int topblock=numblockspercore*NUMCORES4GC;
322 if (memblock=checkNeighbors_I(requiredcore, requiredmem, desiredmem)) {
324 } else if (memblock=globalSearch_I(topblock, requiredmem, desiredmem)) {
329 // If we cannot find spare mem right now, hold the request
330 gcrequiredmems[requiredcore] = requiredmem;
331 maxusefulmems[requiredcore]=desiredmem;
334 int count=gc_countRunningCores();
335 if (gcmovepending==count) {
336 // All cores have stopped...hand out memory as necessary to handle all requests
337 handleMemoryRequests_I();
343 /* This function is performance critical... spend more time optimizing it */
345 unsigned int compactblocks(struct moveHelper * orig, struct moveHelper * to) {
346 void *toptrinit=to->ptr;
347 void *toptr=toptrinit;
348 void *tobound=to->bound;
349 void *origptr=orig->ptr;
350 void *origbound=orig->bound;
351 unsigned INTPTR origendoffset=ALIGNTOTABLEINDEX((unsigned INTPTR)(origbound-gcbaseva));
352 unsigned int objlength;
353 while(origptr<origbound) {
354 //Try to skip over stuff fast first
355 unsigned INTPTR offset=(unsigned INTPTR) (origptr-gcbaseva);
356 unsigned INTPTR arrayoffset=ALIGNTOTABLEINDEX(offset);
357 if (!gcmarktbl[arrayoffset]) {
360 if (arrayoffset>=origendoffset) {
361 //finished with block...
365 gccurr_heaptop-=(unsigned INTPTR)(toptr-toptrinit);
368 } while(!gcmarktbl[arrayoffset]);
369 origptr=CONVERTTABLEINDEXTOPTR(arrayoffset);
372 //Scan more carefully next
373 objlength=getMarkedLength(origptr);
375 if (objlength!=NOTMARKED) {
376 unsigned int length=ALIGNSIZETOBYTES(objlength);
378 /* unsigned int size;
380 gettype_size(origptr, &type, &size);
381 size=((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
384 tprintf("BAD SIZE IN BITMAP: type=%u object=%x size=%u length=%u\n", type, origptr, size, length);
387 void *endtoptr=toptr+length;
388 if (endtoptr>tobound) {
389 gccurr_heaptop-=(unsigned INTPTR)(toptr-toptrinit);
394 //good to move objects and update pointers
395 //tprintf("Decided to compact obj %x to %x\n", origptr, toptr);
397 gcmappingtbl[OBJMAPPINGINDEX(origptr)]=toptr;
401 origptr+=ALIGNMENTSIZE;
405 gccurr_heaptop-=(unsigned INTPTR)(toptr-toptrinit);
410 BAMBOO_ASSERT(COMPACTPHASE == gc_status_info.gcphase);
412 // initialize structs for compacting
413 struct moveHelper orig={0,NULL,NULL,0,NULL,0,0,0,0};
414 struct moveHelper to={0,NULL,NULL,0,NULL,0,0,0,0};
415 initOrig_Dst(&orig, &to);
417 CACHEADAPT_SAMPLING_DATA_REVISE_INIT(&orig, &to);
419 compacthelper(&orig, &to);
422 void master_compact() {
423 // predict number of blocks to fill for each core
424 numblockspercore = loadbalance()+1;
426 GC_PRINTF("mark phase finished \n");
428 gc_resetCoreStatus();
429 //initialize local data structures first....we don't want remote requests messing data up
430 unsigned int initblocks=numblockspercore*NUMCORES4GC;
431 allocationinfo.lowestfreeblock=NOFREEBLOCK;
434 for(int i=0;i<initblocks;i++) {
435 allocationinfo.blocktable[i].status=BS_USED;
439 for(int i=initblocks;i<GCNUMBLOCK;i++) {
440 allocationinfo.blocktable[i].status=BS_FREE;
441 allocationinfo.blocktable[i].usedspace=0;
442 //this is true because all cores have at least one block already...
443 allocationinfo.blocktable[i].freespace=BLOCKSIZE(1);
446 //start all of the cores
447 for(int i = 0; i < NUMCORES4GC; i++) {
448 // init some data strutures for compact phase
449 gcrequiredmems[i] = 0;
452 //send start compact messages to all cores
453 if(i != STARTUPCORE) {
454 send_msg_2(i, GCSTARTCOMPACT, numblockspercore);
456 gcblock2fill = numblockspercore;
462 /* wait for all cores to finish compacting */
463 tprintf("MASTER WAITING\n");
466 while(!gc_checkCoreStatus())
469 tprintf("POST_WAIT\n");
472 //just in case we didn't get blocks back...
473 if (allocationinfo.lowestfreeblock==NOFREEBLOCK)
474 allocationinfo.lowestfreeblock=numblockspercore*NUMCORES4GC;
476 GC_PRINTF("compact phase finished \n");
479 #endif // MULTICORE_GC