2cb3cebe3fe3fc2514beb8a0d62759ee76713cc5
[IRC.git] / Robust / src / Runtime / bamboo / multicoregarbage.h
1 #ifndef MULTICORE_GARBAGE_H
2 #define MULTICORE_GARBAGE_H
3 #include "multicoregc.h"
4 #include "multicorehelper.h"  // for mappins between core # and block #
5 #include "structdefs.h"
6 #include "MGCHash.h"
7 #include "GCSharedHash.h"
8 #ifdef GC_CACHE_ADAPT
9 #include "multicorecache.h"
10 #endif // GC_CACHE_ADAPT
11
12 #ifndef bool
13 #define bool int
14 #endif
15
16 // data structures for GC
17 #ifdef GC_DEBUG
18 #define BAMBOO_SMEM_SIZE_L (BAMBOO_SMEM_SIZE * 2)
19 #else
20 #define BAMBOO_SMEM_SIZE_L (BAMBOO_SMEM_SIZE * 2)
21 #endif
22 #define BAMBOO_LARGE_SMEM_BOUND (BAMBOO_SMEM_SIZE_L*NUMCORES4GC)
23 // let each gc core to have one big block, this is very important
24 // for the computation of NUMBLOCKS(s, n), DO NOT change this!
25
26 #ifdef GC_FLUSH_DTLB
27 #define GC_NUM_FLUSH_DTLB 1
28 int gc_num_flush_dtlb;
29 #endif
30
31 #define NUMPTRS 100
32
33 // for GC profile
34 #ifdef GC_PROFILE
35 #define GCINFOLENGTH 100
36
37 #ifdef GC_CACHE_ADAPT
38 #define GC_PROFILE_NUM_FIELD 16
39 #else
40 #define GC_PROFILE_NUM_FIELD 15
41 #endif // GC_CACHE_ADAPT
42
43 typedef struct gc_info {
44   unsigned long long time[GC_PROFILE_NUM_FIELD];
45   int index;
46 } GCInfo;
47
48 GCInfo * gc_infoArray[GCINFOLENGTH];
49 int gc_infoIndex;
50 bool gc_infoOverflow;
51 unsigned long long gc_num_livespace;
52 unsigned long long gc_num_freespace;
53 unsigned long long gc_num_lobjspace;
54 unsigned int gc_num_lobj;
55
56 unsigned int gc_num_liveobj;
57 unsigned int gc_num_obj;
58 unsigned int gc_num_forwardobj;
59 int gc_num_profiles;
60
61 #endif // GC_PROFILE
62
63 typedef enum {
64   INIT = 0,           // 0
65   DISCOVERED = 2,     // 2
66   REMOTEM = 4,        // 4
67   MARKED = 8,         // 8
68   COMPACTED = 16,     // 16
69   FLUSHED = 32,       // 32
70   END = 33            // 33
71 } GCOBJFLAG;
72
73 typedef enum {
74   INITPHASE = 0x0,         // 0x0
75   MARKPHASE,               // 0x1
76   COMPACTPHASE,            // 0x2
77   SUBTLECOMPACTPHASE,      // 0x3
78   MAPPHASE,                // 0x4
79   FLUSHPHASE,              // 0x5
80 #ifdef GC_CACHE_ADAPT
81   PREFINISHPHASE,          // 0x6
82 #endif // GC_CACHE_ADAPT
83   FINISHPHASE              // 0x6/0x7
84 } GCPHASETYPE;
85
86 volatile bool gcflag;
87 volatile bool gcprocessing;
88 volatile GCPHASETYPE gcphase; // indicating GC phase
89
90 volatile bool gcpreinform; // counter for stopped cores
91 volatile bool gcprecheck; // indicates if there are updated pregc information
92
93 int gccurr_heaptop;
94 struct MGCHash * gcforwardobjtbl; // cache forwarded objs in mark phase
95 // for mark phase termination
96 volatile int gccorestatus[NUMCORESACTIVE]; // records status of each core
97                                            // 1: running gc
98                                            // 0: stall
99 volatile int gcnumsendobjs[2][NUMCORESACTIVE]; // the # of objects sent out
100 volatile int gcnumreceiveobjs[2][NUMCORESACTIVE]; // the # of objects received
101 volatile int gcnumsrobjs_index;  // indicates which entry to record the info 
102                                          // received before phase 1 of the mark finish 
103                                                          // checking process
104                                                                  // the info received in phase 2 must be 
105                                                                  // recorded in the other entry
106 volatile bool gcbusystatus;
107 int gcself_numsendobjs;
108 int gcself_numreceiveobjs;
109
110 // for load balancing
111 INTPTR gcheaptop;
112 int gcloads[NUMCORES4GC];
113 int gctopcore; // the core host the top of the heap
114 int gctopblock; // the number of current top block
115
116 int gcnumlobjs;
117
118 // compact instruction
119 INTPTR gcmarkedptrbound;
120 int gcblock2fill;
121 int gcstopblock[NUMCORES4GC]; // indicate when to stop compact phase
122 int gcfilledblocks[NUMCORES4GC]; //indicate how many blocks have been fulfilled
123 // move instruction;
124 INTPTR gcmovestartaddr;
125 int gcdstcore;
126 volatile bool gctomove;
127 int gcrequiredmems[NUMCORES4GC]; //record pending mem requests
128 volatile int gcmovepending;
129
130 // shared memory pointer for shared pointer mapping tbls
131 // In GC version, this block of memory is located at the bottom of the 
132 // shared memory, right on the top of the smem tbl.
133 // The bottom of the shared memory = sbstart tbl + smemtbl 
134 //                                  + NUMCORES4GC bamboo_rmsp
135 // These three types of table are always reside at the bottom of the shared 
136 // memory and will never be moved or garbage collected
137 #ifdef GC_SMALLPAGESIZE
138 #define BAMBOO_RMSP_SIZE (1024 * 1024)
139 #else
140 #define BAMBOO_RMSP_SIZE (BAMBOO_SMEM_SIZE) // (45 * 16 * 1024)
141 #endif
142 mspace bamboo_rmsp;
143 // shared pointer mapping tbl
144 mgcsharedhashtbl_t * gcsharedptbl;
145 // remote shared pointer tbls
146 mgcsharedhashtbl_t * gcrpointertbls[NUMCORES4GC];
147
148 #ifdef LOCALHASHTBL_TEST
149 struct RuntimeHash * gcpointertbl;
150 #else
151 mgchashtable_t * gcpointertbl;
152 #endif
153 int gcobj2map;
154 int gcmappedobj;
155 volatile bool gcismapped;
156
157 // table recording the starting address of each small block
158 // (size is BAMBOO_SMEM_SIZE)
159 // Note: 1. this table always resides on the very bottom of the shared memory
160 //       2. it is not counted in the shared heap, would never be garbage 
161 //          collected
162 INTPTR * gcsbstarttbl;
163 int gcreservedsb;  // number of reserved sblock for sbstarttbl
164 int gcnumblock; // number of total blocks in the shared mem
165 int gcbaseva; // base va for shared memory without reserved sblocks
166 #ifdef GC_CACHE_ADAPT
167 int gctopva; // top va for shared memory without reserved sblocks
168 volatile bool gccachestage;
169 // table recording the sampling data collected for cache adaption 
170 unsigned int * gccachesamplingtbl;
171 unsigned int * gccachesamplingtbl_local;
172 unsigned int size_cachesamplingtbl_local;
173 unsigned int * gccachesamplingtbl_r;
174 unsigned int * gccachesamplingtbl_local_r;
175 unsigned int size_cachesamplingtbl_local_r;
176 int * gccachepolicytbl;
177 unsigned int size_cachepolicytbl;
178 #endif // GC_CACHE_ADAPT
179
180 #define ISSHAREDOBJ(p) \
181   ((((int)p)>gcbaseva)&&(((int)p)<(gcbaseva+(BAMBOO_SHARED_MEM_SIZE))))
182
183 #define ALIGNSIZE(s, as) \
184   (*((int*)as)) = (((s) & (~(BAMBOO_CACHE_LINE_MASK))) + (BAMBOO_CACHE_LINE_SIZE))
185
186 // mapping of pointer to block # (start from 0), here the block # is
187 // the global index
188 #define BLOCKINDEX(p, b) \
189   { \
190     int t = (p) - gcbaseva; \
191     if(t < (BAMBOO_LARGE_SMEM_BOUND)) { \
192       (*((int*)b)) = t / (BAMBOO_SMEM_SIZE_L); \
193     } else { \
194       (*((int*)b)) = NUMCORES4GC+((t-(BAMBOO_LARGE_SMEM_BOUND))/(BAMBOO_SMEM_SIZE)); \
195     } \
196   }
197
198 // mapping of pointer to core #
199 #define RESIDECORE(p, c) \
200   { \
201     if(1 == (NUMCORES4GC)) { \
202       (*((int*)c)) = 0; \
203     } else { \
204       int b; \
205       BLOCKINDEX((p), &b); \
206       (*((int*)c)) = gc_block2core[(b%(NUMCORES4GC*2))]; \
207     } \
208   }
209
210 // NOTE: n starts from 0
211 // mapping of heaptop (how many bytes there are in the local heap) to
212 // the number of the block
213 // the number of the block indicates that the block is the xth block on
214 // the local heap
215 #define NUMBLOCKS(s, n) \
216   if(s < (BAMBOO_SMEM_SIZE_L)) { \
217     (*((int*)(n))) = 0; \
218   } else { \
219     (*((int*)(n))) = 1 + ((s) - (BAMBOO_SMEM_SIZE_L)) / (BAMBOO_SMEM_SIZE); \
220   }
221
222 #define OFFSET(s, o) \
223   if(s < BAMBOO_SMEM_SIZE_L) { \
224     (*((int*)(o))) = (s); \
225   } else { \
226     (*((int*)(o))) = ((s) - (BAMBOO_SMEM_SIZE_L)) % (BAMBOO_SMEM_SIZE); \
227   }
228
229 // mapping of (core #, index of the block) to the global block index
230 #define BLOCKINDEX2(c, n) (gc_core2block[(2*(c))+((n)%2)]+((NUMCORES4GC*2)*((n)/2)))
231
232 // mapping of (core #, number of the block) to the base pointer of the block
233 #define BASEPTR(c, n, p) \
234   { \
235     int b = BLOCKINDEX2((c), (n)); \
236     if(b < (NUMCORES4GC)) { \
237       (*((int*)p)) = gcbaseva + b * (BAMBOO_SMEM_SIZE_L); \
238     } else { \
239       (*((int*)p)) = gcbaseva+(BAMBOO_LARGE_SMEM_BOUND)+ \
240                      (b-(NUMCORES4GC))*(BAMBOO_SMEM_SIZE); \
241     } \
242   }
243
244 // the next core in the top of the heap
245 #define NEXTTOPCORE(b) (gc_block2core[((b)+1)%(NUMCORES4GC*2)])
246
247 inline bool gc(struct garbagelist * stackptr); // core coordinator routine
248 inline void gc_collect(struct garbagelist* stackptr); //core collector routine
249 inline void gc_nocollect(struct garbagelist* stackptr); //non-gc core collector routine
250 inline void transferMarkResults_I();
251 inline void gc_enqueue_I(void *ptr);
252 inline void gc_lobjenqueue_I(void *ptr, int length, int host);
253 inline bool gcfindSpareMem_I(int * startaddr,
254                              int * tomove,
255                              int * dstcore,
256                              int requiredmem,
257                              int requiredcore);
258
259 inline void * gc_lobjdequeue4(int * length, int * host);
260 inline int gc_lobjmoreItems4();
261 inline void gc_lobjqueueinit4();
262
263 #ifdef GC_PROFILE
264 INLINE void gc_profileStart(void);
265 INLINE void gc_profileItem(void);
266 INLINE void gc_profileEnd(void);
267 void gc_outputProfileData();
268 #endif
269
270 #endif
271