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