2 #include "runtime_arch.h"
3 #include "multicoreruntime.h"
6 #include "multicoregarbage.h"
7 #include "multicorehelper.h"
8 #include "multicoremem_helper.h"
10 INLINE void setupsmemmode(void) {
12 // Only allocate local mem chunks to each core.
13 // If a core has used up its local shared memory, start gc.
14 bamboo_smem_mode = SMEMLOCAL;
16 // Allocate the local shared memory to each core with the highest priority,
17 // if a core has used up its local shared memory, try to allocate the
18 // shared memory that belong to its neighbours, if also failed, start gc.
19 bamboo_smem_mode = SMEMFIXED;
21 // Allocate the local shared memory to each core with the highest priority,
22 // if a core has used up its local shared memory, try to allocate the
23 // shared memory that belong to its neighbours first, if failed, check
24 // current memory allocation rate, if it has already reached the threshold,
25 // start gc, otherwise, allocate the shared memory globally. If all the
26 // shared memory has been used up, start gc.
27 bamboo_smem_mode = SMEMMIXED;
29 // Allocate all the memory chunks globally, do not consider the host cores
30 // When all the shared memory are used up, start gc.
31 bamboo_smem_mode = SMEMGLOBAL;
33 // defaultly using local mode
34 bamboo_smem_mode = SMEMLOCAL;
38 INLINE void * mallocmem(int tofindb,
43 // find suitable block
44 mem=gcbaseva+bamboo_smemtbl[tofindb]+OFFSET2BASEVA(tofindb);
47 for(int i = tofindb; i <= totest; i++) {
48 bamboo_smemtbl[i]=BLOCKSIZE(i<NUMCORES4GC);
50 if(tofindb == bamboo_free_block) {
51 bamboo_free_block = totest+1;
56 INLINE void * searchBlock4Mem(int* tofindb,
64 int bound = BAMBOO_SMEM_SIZE_L;
65 while(*totest<(gcnumblock-bamboo_reserved_smem)) {
66 bound = BLOCKSIZE(*totest<NUMCORES4GC);
67 int nsize = bamboo_smemtbl[*totest];
68 if((nsize==bound)||((nsize != 0)&&(*totest != *tofindb))) {
69 // a fully/partially occupied partition, can not be appended
70 //the last continuous block is not big enough,check the next local block
76 *tofindb=*totest=gc_core2block[2*gccorenum+i]+(NUMCORES4GC*2)*j;
78 // an empty block or a partially occupied block that can be set as the
80 if(*totest == *tofindb) {
81 // the first partition
83 } else if(nsize == 0) {
84 // an empty partition, can be appended
88 // have enough space in the block, malloc
89 return mallocmem(*tofindb, *totest, size, allocsize);
92 // no enough space yet, try to append next continuous block
93 *totest = *totest + 1;
100 INLINE void * searchBlock4Mem_global(int* tofindb,
107 int bound = BAMBOO_SMEM_SIZE_L;
108 while(*totest<(gcnumblock-bamboo_reserved_smem)) {
109 bound = BLOCKSIZE(*totest<NUMCORES4GC);
110 int nsize = bamboo_smemtbl[*totest];
111 if((nsize==bound)||((nsize != 0)&&(*totest != *tofindb))) {
112 // a fully/partially occupied partition, can not be appended
113 // set the next block as a new start
117 // an empty block or a partially occupied block that can be set as the
119 if(*totest == *tofindb) {
120 // the first partition
121 size = bound - nsize;
122 } else if(nsize == 0) {
123 // an empty partition, can be appended
127 // have enough space in the block, malloc
128 return mallocmem(*tofindb, *totest, size, allocsize);
131 // no enough space yet, try to append next continuous block
132 *totest = *totest + 1;
139 // Only allocate local mem chunks to each core.
140 // If a core has used up its local shared memory, start gc.
141 void * localmalloc_I(int coren,
145 int gccorenum = (coren<NUMCORES4GC)?(coren):(coren%NUMCORES4GC);
146 int tofindb = gc_core2block[2*gccorenum];
147 int totest = tofindb;
148 mem = searchBlock4Mem(&tofindb, &totest, gccorenum, isize, allocsize);
150 // no more local mem, do not find suitable block
157 // Allocate the local shared memory to each core with the highest priority,
158 // if a core has used up its local shared memory, try to allocate the
159 // shared memory that belong to its neighbours, if also failed, start gc.
160 void * fixedmalloc_I(int coren,
165 int gccorenum = (coren<NUMCORES4GC)?(coren):(coren%NUMCORES4GC);
167 int bound = BAMBOO_SMEM_SIZE_L;
170 for(k=0; k<NUM_CORES2TEST; k++) {
171 if(core2test[gccorenum][k] == -1) {
172 // try next neighbour
175 tofindb=totest=gc_core2block[2*core2test[gccorenum][k]];
176 mem=searchBlock4Mem(&tofindb,&totest,core2test[gccorenum][k],
182 // no more memory available on either coren or its neighbour cores
189 // Allocate the local shared memory to each core with the highest priority,
190 // if a core has used up its local shared memory, try to allocate the
191 // shared memory that belong to its neighbours first, if failed, check
192 // current memory allocation rate, if it has already reached the threshold,
193 // start gc, otherwise, allocate the shared memory globally. If all the
194 // shared memory has been used up, start gc.
195 void * mixedmalloc_I(int coren,
200 int gccorenum = (coren < NUMCORES4GC) ? (coren) : (coren % NUMCORES4GC);
202 int bound = BAMBOO_SMEM_SIZE_L;
205 for(k=0; k<NUM_CORES2TEST; k++) {
206 if(core2test[gccorenum][k] == -1) {
207 // try next neighbour
210 tofindb=totest=gc_core2block[2*core2test[gccorenum][k]];
211 mem=searchBlock4Mem(&tofindb,&totest,core2test[gccorenum][k],
214 gcmem_mixed_usedmem += size;
218 if(gcmem_mixed_usedmem >= gcmem_mixed_threshold) {
219 // no more memory available on either coren or its neighbour cores
223 // try allocate globally
224 mem = globalmalloc_I(coren, isize, allocsize);
226 gcmem_mixed_usedmem += size;
233 // Allocate all the memory chunks globally, do not consider the host cores
234 // When all the shared memory are used up, start gc.
235 void * globalmalloc_I(int coren,
239 int tofindb = bamboo_free_block;
240 int totest = tofindb;
241 int bound = BAMBOO_SMEM_SIZE_L;
244 if(tofindb > gcnumblock-1-bamboo_reserved_smem) {
245 // Out of shared memory
249 mem=searchBlock4Mem_global(&tofindb, &totest, isize, allocsize);
256 // malloc from the shared memory
257 void * smemalloc_I(int coren,
261 int isize = size+(BAMBOO_CACHE_LINE_SIZE);
263 // go through the bamboo_smemtbl for suitable partitions
264 switch(bamboo_smem_mode) {
266 mem = localmalloc_I(coren, isize, allocsize);
272 mem = fixedmalloc_I(coren, isize, allocsize);
282 mem = mixedmalloc_I(coren, isize, allocsize);
291 mem = globalmalloc_I(coren, isize, allocsize);
300 // no enough shared global memory
304 if(!gc_status_info.gcprocessing) {
305 // inform other cores to stop and wait for gc
307 for(int i = 0; i < NUMCORESACTIVE; i++) {
308 // reuse the gcnumsendobjs & gcnumreceiveobjs
309 gcnumsendobjs[0][i] = 0;
310 gcnumreceiveobjs[0][i] = 0;
312 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
320 // malloc from the shared memory
321 void * smemalloc_I(int coren,
325 int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
326 if(toallocate > bamboo_free_smem_size) {
330 mem = (void *)bamboo_free_smemp;
331 bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
332 bamboo_free_smem_size -= toallocate;
334 *allocsize = toallocate;
336 // no enough shared global memory
342 #endif // MULTICORE_GC