2 #include "runtime_arch.h"
3 #include "multicoreruntime.h"
6 #include "multicorehelper.h"
7 #include "multicoremem_helper.h"
9 INLINE void setupsmemmode(void) {
11 // Only allocate local mem chunks to each core.
12 // If a core has used up its local shared memory, start gc.
13 bamboo_smem_mode = SMEMLOCAL;
15 // Allocate the local shared memory to each core with the highest priority,
16 // if a core has used up its local shared memory, try to allocate the
17 // shared memory that belong to its neighbours, if also failed, start gc.
18 bamboo_smem_mode = SMEMFIXED;
20 // Allocate the local shared memory to each core with the highest priority,
21 // if a core has used up its local shared memory, try to allocate the
22 // shared memory that belong to its neighbours first, if failed, check
23 // current memory allocation rate, if it has already reached the threshold,
24 // start gc, otherwise, allocate the shared memory globally. If all the
25 // shared memory has been used up, start gc.
26 bamboo_smem_mode = SMEMMIXED;
28 // Allocate all the memory chunks globally, do not consider the host cores
29 // When all the shared memory are used up, start gc.
30 bamboo_smem_mode = SMEMGLOBAL;
32 // defaultly using local mode
33 bamboo_smem_mode = SMEMLOCAL;
37 INLINE void * mallocmem(int tofindb,
42 // find suitable block
43 mem=gcbaseva+bamboo_smemtbl[tofindb]+OFFSET2BASEVA(tofindb);
46 for(int i = tofindb; i <= totest; i++) {
47 bamboo_smemtbl[i]=BLOCKSIZE(i<NUMCORES4GC);
49 if(tofindb == bamboo_free_block) {
50 bamboo_free_block = totest+1;
55 INLINE void * searchBlock4Mem(int* tofindb,
63 int bound = BAMBOO_SMEM_SIZE_L;
64 while(*totest<(gcnumblock-bamboo_reserved_smem)) {
65 bound = BLOCKSIZE(*totest<NUMCORES4GC);
66 int nsize = bamboo_smemtbl[*totest];
67 if((nsize==bound)||((nsize != 0)&&(*totest != *tofindb))) {
68 // a fully/partially occupied partition, can not be appended
69 //the last continuous block is not big enough,check the next local block
75 *tofindb=*totest=gc_core2block[2*gccorenum+i]+(NUMCORES4GC*2)*j;
77 // an empty block or a partially occupied block that can be set as the
79 if(*totest == *tofindb) {
80 // the first partition
82 } else if(nsize == 0) {
83 // an empty partition, can be appended
87 // have enough space in the block, malloc
88 return mallocmem(*tofindb, *totest, size, allocsize);
91 // no enough space yet, try to append next continuous block
92 *totest = *totest + 1;
99 INLINE void * searchBlock4Mem_global(int* tofindb,
106 int bound = BAMBOO_SMEM_SIZE_L;
107 while(*totest<(gcnumblock-bamboo_reserved_smem)) {
108 bound = BLOCKSIZE(*totest<NUMCORES4GC);
109 int nsize = bamboo_smemtbl[*totest];
110 if((nsize==bound)||((nsize != 0)&&(*totest != *tofindb))) {
111 // a fully/partially occupied partition, can not be appended
112 // set the next block as a new start
116 // an empty block or a partially occupied block that can be set as the
118 if(*totest == *tofindb) {
119 // the first partition
120 size = bound - nsize;
121 } else if(nsize == 0) {
122 // an empty partition, can be appended
126 // have enough space in the block, malloc
127 return mallocmem(*tofindb, *totest, size, allocsize);
130 // no enough space yet, try to append next continuous block
131 *totest = *totest + 1;
138 // Only allocate local mem chunks to each core.
139 // If a core has used up its local shared memory, start gc.
140 void * localmalloc_I(int coren,
144 int gccorenum = (coren<NUMCORES4GC)?(coren):(coren%NUMCORES4GC);
145 int tofindb = gc_core2block[2*gccorenum];
146 int totest = tofindb;
147 mem = searchBlock4Mem(&tofindb, &totest, gccorenum, isize, allocsize);
149 // no more local mem, do not find suitable block
156 // Allocate the local shared memory to each core with the highest priority,
157 // if a core has used up its local shared memory, try to allocate the
158 // shared memory that belong to its neighbours, if also failed, start gc.
159 void * fixedmalloc_I(int coren,
164 int gccorenum = (coren<NUMCORES4GC)?(coren):(coren%NUMCORES4GC);
166 int bound = BAMBOO_SMEM_SIZE_L;
169 for(k=0; k<NUM_CORES2TEST; k++) {
170 if(core2test[gccorenum][k] == -1) {
171 // try next neighbour
174 tofindb=totest=gc_core2block[2*core2test[gccorenum][k]];
175 mem=searchBlock4Mem(&tofindb,&totest,core2test[gccorenum][k],
181 // no more memory available on either coren or its neighbour cores
188 // Allocate the local shared memory to each core with the highest priority,
189 // if a core has used up its local shared memory, try to allocate the
190 // shared memory that belong to its neighbours first, if failed, check
191 // current memory allocation rate, if it has already reached the threshold,
192 // start gc, otherwise, allocate the shared memory globally. If all the
193 // shared memory has been used up, start gc.
194 void * mixedmalloc_I(int coren,
199 int gccorenum = (coren < NUMCORES4GC) ? (coren) : (coren % NUMCORES4GC);
201 int bound = BAMBOO_SMEM_SIZE_L;
204 for(k=0; k<NUM_CORES2TEST; k++) {
205 if(core2test[gccorenum][k] == -1) {
206 // try next neighbour
209 tofindb=totest=gc_core2block[2*core2test[gccorenum][k]];
210 mem=searchBlock4Mem(&tofindb,&totest,core2test[gccorenum][k],
213 gcmem_mixed_usedmem += size;
217 if(gcmem_mixed_usedmem >= gcmem_mixed_threshold) {
218 // no more memory available on either coren or its neighbour cores
222 // try allocate globally
223 mem = globalmalloc_I(coren, isize, allocsize);
225 gcmem_mixed_usedmem += size;
232 // Allocate all the memory chunks globally, do not consider the host cores
233 // When all the shared memory are used up, start gc.
234 void * globalmalloc_I(int coren,
238 int tofindb = bamboo_free_block;
239 int totest = tofindb;
240 int bound = BAMBOO_SMEM_SIZE_L;
243 if(tofindb > gcnumblock-1-bamboo_reserved_smem) {
244 // Out of shared memory
248 mem=searchBlock4Mem_global(&tofindb, &totest, isize, allocsize);
255 // malloc from the shared memory
256 void * smemalloc_I(int coren,
260 int isize = size+(BAMBOO_CACHE_LINE_SIZE);
262 // go through the bamboo_smemtbl for suitable partitions
263 switch(bamboo_smem_mode) {
265 mem = localmalloc_I(coren, isize, allocsize);
271 mem = fixedmalloc_I(coren, isize, allocsize);
281 mem = mixedmalloc_I(coren, isize, allocsize);
290 mem = globalmalloc_I(coren, isize, allocsize);
299 // no enough shared global memory
304 // inform other cores to stop and wait for gc
306 for(int i = 0; i < NUMCORESACTIVE; i++) {
307 // reuse the gcnumsendobjs & gcnumreceiveobjs
308 gcnumsendobjs[0][i] = 0;
309 gcnumreceiveobjs[0][i] = 0;
311 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
319 // malloc from the shared memory
320 void * smemalloc_I(int coren,
324 int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
325 if(toallocate > bamboo_free_smem_size) {
329 mem = (void *)bamboo_free_smemp;
330 bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
331 bamboo_free_smem_size -= toallocate;
333 *allocsize = toallocate;
335 // no enough shared global memory
341 #endif // MULTICORE_GC