bug fixes
[IRC.git] / Robust / src / Runtime / bamboo / multicoregccompact.c
1 #ifdef MULTICORE_GC
2 #include "multicoregccompact.h"
3 #include "runtime_arch.h"
4 #include "multicoreruntime.h"
5 #include "multicoregarbage.h"
6 #include "markbit.h"
7
8 bool gc_checkCoreStatus() {
9   for(int i = 0; i < NUMCORES4GC; ++i) {
10     if(gccorestatus[i] != 0) {
11       return false;
12     }
13   }  
14   return true;
15 }
16
17 void gc_resetCoreStatus() {
18   for(int i = 0; i < NUMCORES4GC; ++i) {
19     gccorestatus[i] = 1;
20   }
21 }
22
23 void initOrig_Dst(struct moveHelper * orig,struct moveHelper * to) {
24   // init the dst ptr
25   to->localblocknum = 0;
26   BASEPTR(to->base, BAMBOO_NUM_OF_CORE, to->localblocknum);
27   to->ptr = to->base;
28   to->bound=to->base+BLOCKSIZE(to->localblocknum);
29   
30   // init the orig ptr
31   orig->localblocknum = 0;
32   orig->ptr=orig->base = to->base;
33   orig->bound = orig->base + BLOCKSIZE(orig->localblocknum);
34 }
35
36 void getSpaceLocally(struct moveHelper *to) {
37   //we have space on our core...just keep going
38   to->localblocknum++;
39   BASEPTR(to->base,BAMBOO_NUM_OF_CORE, to->localblocknum);
40   to->ptr=to->base;
41   to->bound = to->base + BLOCKSIZE(to->localblocknum);
42 }
43
44 void getSpaceRemotely(struct moveHelper *to, unsigned int minimumbytes) {
45   //need to get another block from elsewhere
46   //set flag to wait for memory
47   gctomove=false;
48   //send request for memory
49   send_msg_4(STARTUPCORE,GCFINISHCOMPACT,BAMBOO_NUM_OF_CORE, to->ptr, minimumbytes);
50   //wait for flag to be set that we received message
51   while(!gctomove) ;
52
53   //store pointer
54   to->ptr = gcmovestartaddr;
55
56   //set localblock number to high number to indicate this block isn't local
57   to->localblocknum = MAXBLOCK;
58   unsigned int globalblocknum;
59   BLOCKINDEX(globalblocknum, to->ptr);
60   to->base = gcbaseva + OFFSET2BASEVA(globalblocknum);
61   to->bound = gcbaseva + BOUNDPTR(globalblocknum);
62 }
63
64 void getSpace(struct moveHelper *to, unsigned int minimumbytes) {
65   //need more space to compact into
66   if (to->localblocknum < gcblock2fill) {
67     getSpaceLocally(to);
68   } else {
69     getSpaceRemotely(to, minimumbytes);
70   }
71 }
72
73 void compacthelper(struct moveHelper * orig,struct moveHelper * to) {
74   while(true) {
75     unsigned int minimumbytes=compactblocks(orig, to);
76     if (orig->ptr==orig->bound) {
77       //need more data to compact
78       //increment the core
79       orig->localblocknum++;
80       BASEPTR(orig->base,BAMBOO_NUM_OF_CORE, orig->localblocknum);
81       orig->ptr=orig->base;
82       orig->bound = orig->base + BLOCKSIZE(orig->localblocknum);
83       if (orig->base >= gcbaseva+BAMBOO_SHARED_MEM_SIZE)
84         break;
85     }
86     if (minimumbytes!=0) {
87       getSpace(to, minimumbytes);
88     }
89   }
90
91   send_msg_4(STARTUPCORE,GCFINISHCOMPACT,BAMBOO_NUM_OF_CORE, to->ptr, 0);
92 }
93
94 /* Should be invoked with interrupt turned off. */
95
96 void * assignSpareMem_I(unsigned int sourcecore, unsigned int requiredmem) {
97   return NULL;
98 }
99
100 void * assignSpareMem(unsigned int sourcecore,unsigned int requiredmem) {
101   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
102   void * retval=assignSpareMem_I(sourcecore, requiredmem);
103   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
104   return retval;
105 }
106
107 /* should be invoked with interrupt turned off */
108
109 void * gcfindSpareMem_I(unsigned int requiredmem,unsigned int requiredcore) {
110   void * startaddr;
111   for(int k = 0; k < NUMCORES4GC; k++) {
112     if((gccorestatus[k] == 0) && (gcfilledblocks[k] < gcstopblock[k])) {
113       // check if this stopped core has enough mem
114       startaddr=assignSpareMem_I(k, requiredmem);
115       return startaddr;
116     }
117   }
118   // If we cannot find spare mem right now, hold the request
119   gcrequiredmems[requiredcore] = requiredmem;
120   gcmovepending++;
121   return NULL;
122
123
124 bool gcfindSpareMem(unsigned int requiredmem,unsigned int requiredcore) {
125   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
126   bool retval=gcfindSpareMem_I(requiredmem, requiredcore);
127   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
128   return retval;
129 }
130
131 /* This function is performance critical...  spend more time optimizing it */
132
133 unsigned int compactblocks(struct moveHelper * orig, struct moveHelper * to) {
134   void *toptr=to->ptr;
135   void *tobound=to->bound;
136   void *origptr=orig->ptr;
137   void *origbound=orig->bound;
138   unsigned INTPTR origendoffset=ALIGNTOTABLEINDEX((unsigned INTPTR)(origbound-gcbaseva));
139   unsigned int objlength;
140
141   while(origptr<origbound) {
142     //Try to skip over stuff fast first
143     unsigned INTPTR offset=(unsigned INTPTR) (origptr-gcbaseva);
144     unsigned INTPTR arrayoffset=ALIGNTOTABLEINDEX(offset);
145     if (!gcmarktbl[arrayoffset]) {
146       do {
147         arrayoffset++;
148         if (arrayoffset<origendoffset) {
149           //finished with block...
150           origptr=origbound;
151           to->ptr=toptr;
152           orig->ptr=origptr;
153           return 0;
154         }
155       } while(!gcmarktbl[arrayoffset]);
156       origptr=CONVERTTABLEINDEXTOPTR(arrayoffset);
157     }
158
159     //Scan more carefully next
160     objlength=getMarkedLength(origptr);
161
162     if (objlength!=NOTMARKED) {
163       unsigned int length=ALIGNSIZETOBYTES(objlength);
164       void *endtoptr=toptr+length;
165       if (endtoptr>tobound) {
166         toptr=tobound;
167         to->ptr=toptr;
168         orig->ptr=origptr;
169         return length;
170       }
171       //good to move objects and update pointers
172       gcmappingtbl[OBJMAPPINGINDEX(origptr)]=toptr;
173       origptr+=length;
174       toptr=endtoptr;
175     } else
176       origptr+=ALIGNMENTSIZE;
177   }
178 }
179
180 void compact() {
181   BAMBOO_ASSERT(COMPACTPHASE == gc_status_info.gcphase);
182   BAMBOO_CACHE_MF();
183   
184   // initialize structs for compacting
185   struct moveHelper orig={0,NULL,NULL,0,NULL,0,0,0,0};
186   struct moveHelper to={0,NULL,NULL,0,NULL,0,0,0,0};
187   initOrig_Dst(&orig, &to);
188
189   CACHEADAPT_SAMPLING_DATA_REVISE_INIT(&orig, &to);
190
191   compacthelper(&orig, &to);
192
193
194 void master_compact() {
195   // predict number of blocks to fill for each core
196   void * tmpheaptop = 0;
197   int numblockspercore = loadbalance(&tmpheaptop, &gctopblock, &gctopcore);
198   
199   GC_PRINTF("mark phase finished \n");
200   
201   gc_resetCoreStatus();
202   tmpheaptop = gcbaseva + BAMBOO_SHARED_MEM_SIZE;
203   for(int i = 0; i < NUMCORES4GC; i++) {
204     // init some data strutures for compact phase
205     gcfilledblocks[i] = 0;
206     gcrequiredmems[i] = 0;
207     gccorestatus[i] = 1;
208     //send start compact messages to all cores
209     gcstopblock[i] = numblockspercore;
210     if(i != STARTUPCORE) {
211       send_msg_2(i, GCSTARTCOMPACT, numblockspercore);
212     } else {
213       gcblock2fill = numblockspercore;
214     }
215   }
216   BAMBOO_CACHE_MF();
217   GCPROFILE_ITEM();
218   // compact phase
219   compact();
220   /* wait for all cores to finish compacting */
221
222   while(gc_checkCoreStatus())
223     ;
224
225   GCPROFILE_ITEM();
226
227   GC_PRINTF("compact phase finished \n");
228 }
229
230 #endif // MULTICORE_GC