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 * mallocmem(int tofindb,
15 // find suitable block
16 mem=gcbaseva+bamboo_smemtbl[tofindb]+OFFSET2BASEVA(tofindb);
19 for(int i = tofindb; i <= totest; i++) {
20 bamboo_smemtbl[i]=BLOCKSIZE(i<NUMCORES4GC);
22 if(tofindb == bamboo_free_block) {
23 bamboo_free_block = totest+1;
28 // 06/07/11 add a parameter minremain, it specifies the minimum number of
29 // blocks to leave for each core for local allocation.
30 INLINE void * searchBlock4Mem(int* tofindb,
39 int bound = BAMBOO_SMEM_SIZE_L;
40 int freeblocks=(gcnumblock-bamboo_reserved_smem-1)/NUMCORES4GC+1;
41 while((*totest<(gcnumblock-bamboo_reserved_smem))&&(freeblocks>minremain)) {
42 bound = BLOCKSIZE(*totest<NUMCORES4GC);
43 int nsize = bamboo_smemtbl[*totest];
44 if((nsize==bound)||((nsize != 0)&&(*totest != *tofindb))) {
45 // a fully/partially occupied partition, can not be appended
46 //the last continuous block is not big enough,check the next local block
50 *tofindb=*totest=gc_core2block[2*gccorenum+i]+(NUMCORES4GC*2)*j;
53 // an empty block or a partially occupied block that can be set as the
55 if(*totest == *tofindb) {
56 // the first partition
58 } else if(nsize == 0) {
59 // an empty partition, can be appended
63 // have enough space in the block, malloc
64 return mallocmem(*tofindb, *totest, size, allocsize);
66 // no enough space yet, try to append next continuous block
67 *totest = *totest + 1;
74 INLINE void * searchBlock4Mem_global(int* tofindb,
79 int bound = BAMBOO_SMEM_SIZE_L;
80 while(*totest<(gcnumblock-bamboo_reserved_smem)) {
81 bound = BLOCKSIZE(*totest<NUMCORES4GC);
82 int nsize = bamboo_smemtbl[*totest];
83 if((nsize==bound)||((nsize != 0)&&(*totest != *tofindb))) {
84 // a fully/partially occupied partition, can not be appended
85 // set the next block as a new start
89 // an empty block or a partially occupied block that can be set as the
91 if(*totest == *tofindb) {
92 // the first partition
94 } else if(nsize == 0) {
95 // an empty partition, can be appended
99 // have enough space in the block, malloc
100 return mallocmem(*tofindb, *totest, size, allocsize);
102 // no enough space yet, try to append next continuous block
103 *totest = *totest + 1;
110 // Only allocate local mem chunks to each core.
111 // If a core has used up its local shared memory, start gc.
112 void * localmalloc_I(int coren,
116 int gccorenum=(coren<NUMCORES4GC)?(coren):(coren%NUMCORES4GC);
117 int tofindb=gc_core2block[2*gccorenum];
119 mem=searchBlock4Mem(&tofindb,&totest,gccorenum,isize,allocsize,0);
121 // no more local mem, do not find suitable block
127 #define LOCALMEMRESERVATION 2
130 // Allocate the local shared memory to each core with the highest priority,
131 // if a core has used up its local shared memory, try to allocate the
132 // shared memory that belong to its neighbours, if also failed, start gc.
133 void * fixedmalloc_I(int coren,
138 int gccorenum=(coren<NUMCORES4GC)?(coren):(coren%NUMCORES4GC);
140 int bound=BAMBOO_SMEM_SIZE_L;
143 for(k=0;k<NUM_CORES2TEST;k++) {
144 if(core2test[gccorenum][k]==-1) {
145 // try next neighbour
148 tofindb=totest=gc_core2block[2*core2test[gccorenum][k]];
149 mem=searchBlock4Mem(&tofindb,&totest,core2test[gccorenum][k],isize,allocsize,(k==0)?0:((gcnumblock/NUMCORES4GC)>>LOCALMEMRESERVATION));
154 // no more memory available on either coren or its neighbour cores
161 // Allocate the local shared memory to each core with the highest priority,
162 // if a core has used up its local shared memory, try to allocate the
163 // shared memory that belong to its neighbours first, if failed, check
164 // current memory allocation rate, if it has already reached the threshold,
165 // start gc, otherwise, allocate the shared memory globally. If all the
166 // shared memory has been used up, start gc.
167 void * mixedmalloc_I(int coren,
172 int gccorenum=(coren<NUMCORES4GC)?(coren):(coren%NUMCORES4GC);
175 for(k=0;k<NUM_CORES2TEST;k++) {
176 if(core2test[gccorenum][k]==-1) {
177 // try next neighbour
180 tofindb=totest=gc_core2block[2*core2test[gccorenum][k]];
181 mem=searchBlock4Mem(&tofindb,&totest,core2test[gccorenum][k],isize,allocsize,(k==0)?0:((gcnumblock/NUMCORES4GC)>>LOCALMEMRESERVATION));
183 gcmem_mixed_usedmem+=size;
187 if(gcmem_mixed_usedmem>=gcmem_mixed_threshold) {
188 // no more memory available on either coren or its neighbour cores
192 // try allocate globally
193 mem=globalmalloc_I(coren,isize,allocsize);
195 gcmem_mixed_usedmem+=size;
202 // Allocate all the memory chunks globally, do not consider the host cores
203 // When all the shared memory are used up, start gc.
204 void * globalmalloc_I(int coren,
208 int tofindb = bamboo_free_block;
209 int totest = tofindb;
210 if(tofindb > gcnumblock-1-bamboo_reserved_smem) {
211 // Out of shared memory
215 mem=searchBlock4Mem_global(&tofindb, &totest, isize, allocsize);
222 void * smemalloc(int coren, int isize, int * allocsize) {
223 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
224 void *retval=smemalloc(coren, isize, allocsize);
225 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
229 // malloc from the shared memory
230 void * smemalloc_I(int coren, int isize, int * allocsize) {
232 void *mem = localmalloc_I(coren, isize, allocsize);
234 void *mem = fixedmalloc_I(coren, isize, allocsize);
236 void *mem = mixedmalloc_I(coren, isize, allocsize);
238 void *mem = globalmalloc_I(coren, isize, allocsize);
242 // no enough shared global memory
247 if(!gc_status_info.gcprocessing) {
248 // inform other cores to stop and wait for gc
250 for(int i = 0; i < NUMCORESACTIVE; i++) {
251 // reuse the gcnumsendobjs & gcnumreceiveobjs
252 gcnumsendobjs[0][i] = 0;
253 gcnumreceiveobjs[0][i] = 0;
255 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
263 // malloc from the shared memory
264 void * smemalloc_I(int coren,
268 int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
269 if(toallocate > bamboo_free_smem_size) {
273 mem = (void *)bamboo_free_smemp;
274 bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
275 bamboo_free_smem_size -= toallocate;
277 *allocsize = toallocate;
279 // no enough shared global memory
285 #endif // MULTICORE_GC