1715d86d248799bc383de6b27c9a0d8aa2547388
[IRC.git] / Robust / src / Runtime / multicoreruntime.h
1 #ifndef MULTICORE_RUNTIME
2 #define MULTICORE_RUNTIME
3
4 ////////////////////////////////////////////////////////////////
5 // global variables                                          //
6 ///////////////////////////////////////////////////////////////
7
8 // data structures for msgs
9 #define BAMBOO_OUT_BUF_LENGTH 300
10 int msgdata[30];
11 int msgtype;
12 int msgdataindex;
13 int msglength;
14 int outmsgdata[BAMBOO_OUT_BUF_LENGTH];
15 int outmsgindex;
16 int outmsglast;
17 int outmsgleft;
18 bool isMsgHanging;
19 volatile bool isMsgSending;
20
21 /* Message format:
22  *      type + Msgbody
23  * type: 0 -- transfer object
24  *       1 -- transfer stall msg
25  *       2 -- lock request
26  *       3 -- lock grount
27  *       4 -- lock deny
28  *       5 -- lock release
29  *       // add for profile info
30  *       6 -- transfer profile output msg
31  *       7 -- transfer profile output finish msg
32  *       // add for alias lock strategy
33  *       8 -- redirect lock request
34  *       9 -- lock grant with redirect info
35  *       a -- lock deny with redirect info
36  *       b -- lock release with redirect info
37  *       c -- status confirm request
38  *       d -- status report msg
39  *       e -- terminate
40  *       f -- requiring for new memory
41  *      10 -- response for new memory request
42  *      11 -- GC start
43  *      12 -- compact phase start
44  *      13 -- flush phase start
45  *      14 -- mark phase finish
46  *      15 -- compact phase finish
47  *      16 -- flush phase finish
48  *      17 -- GC finish
49  *      18 -- marked phase finish confirm request
50  *      19 -- marked phase finish confirm response
51  *      1a -- markedObj msg
52  *      1b -- start moving objs msg
53  *      1c -- ask for mapping info of a markedObj
54  *      1d -- mapping info of a markedObj
55  *      1e -- large objs info request
56  *      1f -- large objs info response
57  *
58  * ObjMsg: 0 + size of msg + obj's address + (task index + param index)+
59  * StallMsg: 1 + corenum + sendobjs + receiveobjs (size is always 4 * sizeof(int))
60  * LockMsg: 2 + lock type + obj pointer + lock + request core (size is always 5 * sizeof(int))
61  *          3/4/5 + lock type + obj pointer + lock (size is always 4 * sizeof(int))
62  *          8 + lock type + obj pointer +  redirect lock + root request core + request core (size is always 6 * sizeof(int))
63  *          9/a + lock type + obj pointer + redirect lock (size is always 4 * sizeof(int))
64  *          b + lock type + lock + redirect lock (size is always 4 * sizeof(int))
65  *          lock type: 0 -- read; 1 -- write
66  * ProfileMsg: 6 + totalexetime (size is always 2 * sizeof(int))
67  *             7 + corenum (size is always 2 * sizeof(int))
68  * StatusMsg: c (size is always 1 * sizeof(int))
69  *            d + status + corenum + sendobjs + receiveobjs (size is always 5 * sizeof(int))
70  *            status: 0 -- stall; 1 -- busy
71  * TerminateMsg: e (size is always 1 * sizeof(int)
72  * MemoryMsg: f + size + corenum (size is always 3 * sizeof(int))
73  *           10 + base_va + size (size is always 3 * sizeof(int))
74  * GCMsg: 11 (size is always 1 * sizeof(int))
75  *        12 + size of msg + (num of objs to move + (start address + end address + dst core + start dst)+)? + (num of incoming objs + (start dst + orig core)+)? + (num of large obj lists + (start address + lenght + start dst)+)?
76  *        13 (size is always 1 * sizeof(int))
77  *        14 + corenum + gcsendobjs + gcreceiveobjs (size if always 4 * sizeof(int))
78  *        15/16 + corenum (size is always 2 * sizeof(int))
79  *        17 (size is always 1 * sizeof(int))
80  *        18 (size if always 1 * sizeof(int))
81  *        19 + size of msg + corenum + gcsendobjs + gcreceiveobjs (size is always 5 * sizeof(int))
82  *        1a + obj's address (size is always 2 * sizeof(int))
83  *        1b + corenum ( size is always 2 * sizeof(int))
84  *        1c + obj's address + corenum (size is always 3 * sizeof(int))
85  *        1d + obj's address + dst address (size if always 3 * sizeof(int))
86  *        1e (size is always 1 * sizeof(int))
87  *        1f + size of msg + corenum + current heap size + (num of large obj lists + (start address + length)+)?
88  */
89 enum MSGTYPE {
90         TRANSOBJ = 0x0,  // 0x0
91         TRANSTALL,       // 0x1
92         LOCKREQUEST,     // 0x2
93         LOCKGROUNT,      // 0x3
94         LOCKDENY,        // 0x4
95         LOCKRELEASE,     // 0x5
96         PROFILEOUTPUT,   // 0x6
97         PROFILEFINISH,   // 0x7
98         REDIRECTLOCK,    // 0x8
99         REDIRECTGROUNT,  // 0x9
100         REDIRECTDENY,    // 0xa
101         REDIRECTRELEASE, // 0xb
102         STATUSCONFIRM,   // 0xc
103         STATUSREPORT,    // 0xd
104         TERMINATE,       // 0xe
105         MEMREQUEST,      // 0xf
106         MEMRESPONSE,     // 0x10
107 #ifdef MULTICORE_GC
108         GCSTART,         // 0x11
109         GCSTARTCOMPACT,  // 0x12
110         GCSTARTFLUSH,    // 0x13
111         GCFINISHMARK,    // 0x14
112         GCFINISHCOMPACT, // 0x15
113         GCFINISHFLUSH,   // 0x16
114         GCFINISH,        // 0x17
115         GCMARKCONFIRM,   // 0x18
116         GCMARKREPORT,    // 0x19
117         GCMARKEDOBJ,     // 0x1a
118         GCMOVESTART,     // 0x1b
119         GCMAPREQUEST,    // 0x1c
120         GCMAPINFO,       // 0x1d
121         GCLOBJREQUEST,   // 0x1e
122         GCLOBJINFO,      // 0x1f
123 #endif
124         MSGEND
125 };
126
127 // data structures of status for termination
128 int corestatus[NUMCORES]; // records status of each core
129                           // 1: running tasks
130                           // 0: stall
131 int numsendobjs[NUMCORES]; // records how many objects a core has sent out
132 int numreceiveobjs[NUMCORES]; // records how many objects a core has received
133 int numconfirm;
134 bool waitconfirm;
135 bool busystatus;
136 int self_numsendobjs;
137 int self_numreceiveobjs;
138
139 // get rid of lock msgs for GC version
140 #ifndef MULTICORE_GC
141 // data structures for locking
142 struct RuntimeHash * objRedirectLockTbl;
143 int lockobj;
144 int lock2require;
145 int lockresult;
146 bool lockflag;
147 #endif
148
149 // data structures for waiting objs
150 struct Queue objqueue;
151
152 // data structures for shared memory allocation
153 #ifdef MULTICORE_GC
154 #include "multicoregarbage.h"
155 #else
156 #define BAMBOO_NUM_PAGES 1024 * 512
157 #define BAMBOO_PAGE_SIZE 4096
158 #define BAMBOO_SHARED_MEM_SIZE BAMBOO_PAGE_SIZE * BAMBOO_PAGE_SIZE
159 #define BAMBOO_BASE_VA 0xd000000
160 #define BAMBOO_SMEM_SIZE 16 * BAMBOO_PAGE_SIZE
161
162 bool smemflag;
163 mspace bamboo_free_msp;
164 mspace bamboo_cur_msp;
165 int bamboo_smem_size;
166 #endif
167
168 // for test TODO
169 int total_num_t6;
170
171 // data structures for profile mode
172 #ifdef PROFILE
173
174 #define TASKINFOLENGTH 30000
175 //#define INTERRUPTINFOLENGTH 500
176
177 bool stall;
178 //bool isInterrupt;
179 int totalexetime;
180
181 typedef struct task_info {
182   char* taskName;
183   unsigned long long startTime;
184   unsigned long long endTime;
185   unsigned long long exitIndex;
186   struct Queue * newObjs; 
187 } TaskInfo;
188
189 /*typedef struct interrupt_info {
190    int startTime;
191    int endTime;
192    } InterruptInfo;*/
193
194 TaskInfo * taskInfoArray[TASKINFOLENGTH];
195 int taskInfoIndex;
196 bool taskInfoOverflow;
197 /*InterruptInfo * interruptInfoArray[INTERRUPTINFOLENGTH];
198    int interruptInfoIndex;
199    bool interruptInfoOverflow;*/
200 int profilestatus[NUMCORES]; // records status of each core
201                              // 1: running tasks
202                              // 0: stall
203 #endif // #ifdef PROFILE
204
205 #ifndef INTERRUPT
206 bool reside;
207 #endif
208 /////////////////////////////////////////////////////////////
209
210 ////////////////////////////////////////////////////////////
211 // these are functions should be implemented in           //
212 // multicore runtime for any multicore processors         //
213 ////////////////////////////////////////////////////////////
214 #ifdef TASK
215 #ifdef MULTICORE
216 inline void initialization(void) __attribute__((always_inline));
217 inline void initCommunication(void) __attribute__((always_inline));
218 inline void fakeExecution(void) __attribute__((always_inline));
219 inline void terminate(void) __attribute__((always_inline));
220
221 // lock related functions
222 bool getreadlock(void* ptr);
223 void releasereadlock(void* ptr);
224 bool getwritelock(void* ptr);
225 void releasewritelock(void* ptr);
226 bool getwritelock_I(void* ptr);
227 void releasewritelock_I(void * ptr);
228 /* this function is to process lock requests. 
229  * can only be invoked in receiveObject() */
230 // if return -1: the lock request is redirected
231 //            0: the lock request is approved
232 //            1: the lock request is denied
233 inline int processlockrequest(int locktype, int lock, int obj, int requestcore, int rootrequestcore, bool cache) __attribute_((always_inline));
234 inline void processlockrelease(int locktype, int lock, int redirectlock, bool isredirect) __attribute_((always_inline));
235
236 // msg related functions
237 inline void send_msg_1(int targetcore, unsigned long n0) __attribute__((always_inline));
238 inline void send_msg_2(int targetcore, unsigned long n0, unsigned long n1) __attribute__((always_inline));
239 inline void send_msg_3(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2) __attribute__((always_inline));
240 inline void send_msg_4(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2, unsigned long n3) __attribute__((always_inline));
241 inline void send_msg_5(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4) __attribute__((always_inline));
242 inline void send_msg_6(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4, unsigned long n5) __attribute__((always_inline));
243 inline void cache_msg_2(int targetcore, unsigned long n0, unsigned long n1) __attribute__((always_inline));
244 inline void cache_msg_3(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2) __attribute__((always_inline));
245 inline void cache_msg_4(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2, unsigned long n3) __attribute__((always_inline));
246 inline void cache_msg_5(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4) __attribute__((always_inline));
247 inline void cache_msg_6(int targetcore, unsigned long n0, unsigned long n1, unsigned long n2, unsigned long n3, unsigned long n4, unsigned long n5) __attribute__((always_inline));
248 inline void transferObject(struct transObjInfo * transObj);
249 inline int receiveMsg(void) __attribute__((always_inline));
250 inline int receiveGCMsg(void) __attribute__((always_inline));
251
252 #ifdef PROFILE
253 inline void profileTaskStart(char * taskname) __attribute__((always_inline));
254 inline void profileTaskEnd(void) __attribute__((always_inline));
255 void outputProfileData();
256 #endif  // #ifdef PROFILE
257 ///////////////////////////////////////////////////////////
258
259 //////////////////////////////////////////////////////////////////////////////////////
260 //  For each version of BAMBOO runtime, there should be a header file named        //
261 //  runtim_arch.h defining following MARCOS:                                       //
262 //  BAMBOO_TOTALCORE: the total # of cores available in the processor              //
263 //  BAMBOO_NUM_OF_CORE: the # of current residing core                             //
264 //  BAMBOO_GET_NUM_OF_CORE(): compute the # of current residing core               //
265 //  BAMBOO_DEBUGPRINT(x): print out integer x                                      //
266 //  BAMBOO_DEBUGPRINT_REG(x): print out value of variable x                        //
267 //  BAMBOO_LOCAL_MEM_CALLOC(x, y): allocate an array of x elements each of whose   //
268 //                                 size in bytes is y on local memory              //
269 //  BAMBOO_LOCAL_MEM_FREE(x): free space with ptr x on local memory                //
270 //  BAMBOO_SHARE_MEM_CALLOC(x, y): allocate an array of x elements each of whose   //
271 //                                 size in bytes is y on shared memory             //
272 //  BAMBOO_START_CRITICAL_SECTION_OBJ_QUEUE()                                      //
273 //  BAMBOO_CLOSE_CRITICAL_SECTION_OBJ_QUEUE(): locks for global data structures    //
274 //                                             related to obj queue                //
275 //  BAMBOO_START_CRITICAL_SECTION_STATUS()                                         //
276 //  BAMBOO_CLOSE_CRITICAL_SECTION_STATUS(): locks for global data structures       //
277 //                                          related to status data                 //
278 //  BAMBOO_START_CRITICAL_SECTION_MSG()                                            //
279 //  BAMBOO_CLOSE_CRITICAL_SECTION_MSG(): locks for global data structures related  //
280 //                                       to msg data                               //
281 //  BAMBOO_START_CRITICAL_SECTION_LOCK()                                           //
282 //  BAMBOO_CLOSE_CRITICAL_SECTION_LOCK(): locks for global data structures related //
283 //                                        to lock table                            //
284 //  BAMBOO_START_CRITICAL_SECTION_MEM()                                            //
285 //  BAMBOO_CLOSE_CRITICAL_SECTION_MEM(): locks for allocating memory               //
286 //  BAMBOO_START_CRITICAL_SECTION()                                                //
287 //  BAMBOO_CLOSE_CRITICAL_SECTION(): locks for all global data structures          //
288 //  BAMBOO_WAITING_FOR_LOCK(): routine executed while waiting for lock request     //
289 //                             response                                            //
290 //  BAMBOO_CACHE_LINE_SIZE: the cache line size                                    //
291 //  BAMBOO_CACHE_LINE_MASK: mask for a cache line                                  //
292 //  BAMBOO_CACHE_FLUSH_RANGE(x, y): flush cache lines started at x with length y   //
293 //  BAMBOO_CACHE_FLUSH_ALL(): flush the whole cache of a core if necessary         //
294 //  BAMBOO_EXIT(x): exit routine                                                   //
295 //  BAMBOO_MSG_AVAIL(): checking if there are msgs coming in                       //
296 //  BAMBOO_GCMSG_AVAIL(): checking if there are gcmsgs coming in                   //
297 //  BAMBOO_GET_EXE_TIME(): rountine to get current clock cycle number              //
298 /////////////////////////////////////////////////////////////////////////////////////
299
300 #endif  // #ifdef MULTICORE
301 #endif  // #ifdef TASK
302 #endif  // #ifndef MULTICORE_RUNTIME