Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[IRC.git] / Robust / src / Runtime / DSTM / interface / dstm.h
1 #ifndef _DSTM_H_
2 #define _DSTM_H_
3
4 #ifdef MAC
5 #define MSG_NOSIGNAL 0
6 #endif
7
8 #define CFENCE   asm volatile ("" ::: "memory");
9 /***********************************************************
10  *       Macros
11  **********************************************************/
12 #define GET_SITEID(x) ((int *)(x))
13 #define GET_NTUPLES(x)  ((int *)(x + sizeof(int)))
14 #define GET_PTR_OID(x)  ((unsigned int *)(x + 2*sizeof(int)))
15 #define GET_PTR_EOFF(x,n) ((short *)(x + 2*sizeof(int) + (n*sizeof(unsigned int))))
16 #define GET_PTR_ARRYFLD(x,n) ((short *)(x + 2*sizeof(int) + (n*sizeof(unsigned int)) + (n*sizeof(short))))
17
18 #define ENDEBUG(s) { printf("Inside %s()\n", s); fflush(stdout); }
19 #define EXDEBUG(s) {printf("Outside %s()\n", s); fflush(stdout); }
20 /*****************************************
21  *  Coordinator Messages
22  ***************************************/
23 #define READ_REQUEST            1
24 #define READ_MULT_REQUEST       2
25 #define MOVE_REQUEST            3
26 #define MOVE_MULT_REQUEST       4
27 #define TRANS_REQUEST           5
28 #define TRANS_ABORT             6
29 #define TRANS_COMMIT            7
30 #define TRANS_PREFETCH          8
31 #define TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING    9
32
33 /*********************************
34  * Participant Messages
35  *******************************/
36 #define OBJECT_FOUND                    10
37 #define OBJECT_NOT_FOUND                11
38 #define OBJECTS_FOUND                   12
39 #define OBJECTS_NOT_FOUND               13
40 #define TRANS_AGREE                     17
41 #define TRANS_DISAGREE                  18
42 #define TRANS_AGREE_BUT_MISSING_OBJECTS 19
43 #define TRANS_SOFT_ABORT                20
44 #define TRANS_SUCESSFUL                 21
45 #define TRANS_PREFETCH_RESPONSE         22
46 #define START_REMOTE_THREAD             23
47 #define THREAD_NOTIFY_REQUEST           24
48 #define THREAD_NOTIFY_RESPONSE          25
49 #define TRANS_UNSUCESSFUL               26
50 #define CLOSE_CONNECTION  27
51
52 //Max number of objects
53 #define MAX_OBJECTS  20
54 //Transaction id per machine
55 #define TID_LEN 20
56 #define LISTEN_PORT 2156
57 #define UDP_PORT 2158
58 //Prefetch tuning paramters
59 //#define RETRYINTERVAL  20 //N (For Em3d, SOR, Moldyn benchmarks)
60 //#define SHUTDOWNINTERVAL  3 //M
61 #define RETRYINTERVAL 1  //N  (For MatrixMultiply, 2DFFT, 2DConv benchmarks)
62 #define SHUTDOWNINTERVAL 75 //M
63 #define NUM_TRY_TO_COMMIT 2
64 #define MEM_ALLOC_THRESHOLD 20485760 //20MB
65
66 #include <stdlib.h>
67 #include <stdio.h>
68 #include <string.h>
69 #include <pthread.h>
70 #include "clookup.h"
71 #include "queue.h"
72 #include "mcpileq.h"
73 #include "threadnotify.h"
74 #include <arpa/inet.h>
75 #include <sys/types.h>
76 #include <sys/socket.h>
77 #include <sys/ioctl.h>
78 #include <sys/time.h>
79 #include <netdb.h>
80 #include <netinet/in.h>
81 #include <sys/types.h>
82 #include <unistd.h>
83 #include <errno.h>
84 #include <time.h>
85 #include "sockpool.h"
86 #include <signal.h>
87 #include "plookup.h"
88 #include "dsmdebug.h"
89 #include "readstruct.h"
90 #ifdef ABORTREADERS
91 #include <setjmp.h>
92 #endif
93
94 //bit designations for status field of objheader
95 #define DIRTY 0x01
96 #define NEW   0x02
97 #define LOCK  0x04
98 #define LOCAL  0x08
99
100 /*******Global statistics *********/
101 extern int numprefetchsites;
102
103 /***********************************
104  * Global Variables for statistics
105  **********************************/
106 #ifdef TRANSSTATS
107 extern int numTransCommit;
108 extern int numTransAbort;
109 extern int nSoftAbort;
110 extern int nSoftAbortAbort;
111 extern int nSoftAbortCommit;
112 #endif
113
114 double idForTimeDelay;           /* TODO Remove, necessary to get time delay for starting transRequest for this id */
115 int transCount;                  /* TODO Remove, necessary to the transaction id */
116
117 #ifdef COMPILER
118
119 #include "structdefs.h"
120
121 typedef struct objheader {
122   threadlist_t *notifylist;
123   unsigned short version;
124   unsigned short rcount;
125 } objheader_t;
126
127 #define OID(x) \
128   (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
129
130 #define COMPOID(x) \
131   ((void*)((((void *) x )!=NULL) ? (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___)) : 0))
132
133 #define STATUS(x) \
134   *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
135
136 #define STATUSPTR(x) \
137   ((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
138
139 #define TYPE(x) \
140   ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
141
142 #define GETSIZE(size, x) { \
143     int type=TYPE(x); \
144     if (type<NUMCLASSES) { \
145       size=classsize[type]; \
146     } else { \
147       size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject); \
148     } \
149 }
150
151 #else
152
153 typedef struct objheader {
154   threadlist_t *notifylist;
155   unsigned int oid;
156   unsigned short type;
157   unsigned short version;
158   unsigned short rcount;
159   char status;
160 } objheader_t;
161
162 #define OID(x) x->oid
163 #define TYPE(x) x->type
164 #define STATUS(x) x->status
165 #define STATUSPTR(x) &x->status
166 #define GETSIZE(size, x) size=classsize[TYPE(x)]
167 #endif
168
169 typedef struct objstr {
170   unsigned int size;       //this many bytes are allocated after this header
171   void *top;
172   struct objstr *next;
173   struct objstr *prev;
174 } objstr_t;
175
176 typedef struct oidmidpair {
177   unsigned int oid;
178   unsigned int mid;
179 } oidmidpair_t;
180
181 // Structure is a shared structure that keeps track of responses from the participants
182 typedef struct thread_response {
183   char rcv_status;
184 } thread_response_t;
185
186 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
187 typedef struct fixed_data {
188   char control;                 /* control message */
189   char trans_id[TID_LEN];       /* transaction id */
190   int mcount;                   /* participant count */
191   unsigned int numread;         /* no of objects read */
192   unsigned int nummod;                  /* no of objects modified */
193   unsigned int numcreated;              /* no of objects created */
194   int sum_bytes;                /* total bytes of modified objects in a transaction */
195 } fixed_data_t;
196
197 /* Structure that holds trans request information for each participant */
198 typedef struct trans_req_data {
199   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
200   unsigned int *listmid;        /* Pointer to array holding list of participants */
201   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */
202   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
203   unsigned int *oidcreated;     /* Pointer to array holding oids of objects that are newly created */
204 } trans_req_data_t;
205
206 /* Structure that holds information of objects that are not found in the participant
207  * and objs locked within a transaction during commit process */
208 typedef struct trans_commit_data {
209   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
210   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
211   unsigned int *objvernotmatch;    /* Pointer to array holding oids whose version doesn't match on the participant machine */
212   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
213   int numlocked;                /* no of objects locked */
214   int numnotfound;              /* no of objects not found */
215   int numvernotmatch;           /* no of objects whose version doesn't match */
216 } trans_commit_data_t;
217
218
219 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
220
221 /* Initialize main object store and lookup tables, start server thread. */
222 int dstmInit(void);
223 void send_data(int fd, void *buf, int buflen);
224 void recv_data(int fd, void *buf, int buflen);
225 int recv_data_errorcode(int fd, void *buf, int buflen);
226 void recv_data_buf(int fd, struct readstruct *, void *, int);
227
228 /* Prototypes for object header */
229 unsigned int getNewOID(void);
230 /* end object header */
231
232 /* Prototypes for object store */
233 objstr_t *objstrCreate(unsigned int size); //size in bytes
234 void objstrDelete(objstr_t *store); //traverse and free entire list
235 void *objstrAlloc(objstr_t **store, unsigned int size); //size in bytes
236 void clearObjStore(); // TODO:currently only clears the prefetch cache object store
237 /* end object store */
238
239 /* Prototypes for server portion */
240 void *dstmListen(void *);
241 int startlistening();
242 void *dstmAccept(void *);
243 int readClientReq(trans_commit_data_t *, int, struct readstruct * readbuffer);
244 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int, struct readstruct *);
245 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
246 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
247 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
248 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
249 char getCommitCountForObjMod(unsigned int *, unsigned int *, unsigned int *, int *,
250                              int *, int *, int *, int *, int *, int *, char *, unsigned int, unsigned short);
251 char getCommitCountForObjRead(unsigned int *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *,
252                               int *, int *, char *, unsigned int, unsigned short);
253
254 void procRestObjs(char *, char *, int, int, int, unsigned int *, unsigned int *, int *, int *, int *, int *);
255 void processVerNoMatch(unsigned int *, unsigned int *, int *, int *, int *, int *, unsigned int, unsigned short);
256
257 /* end server portion */
258
259 /* Prototypes for transactions */
260 /* Function called at beginning. Passes in the first parameter. */
261 /* Returns 1 if this thread should run the main process */
262
263 int dstmStartup(const char *);
264 void transInit();
265 int processConfigFile();
266 void addHost(unsigned int);
267 void mapObjMethod(unsigned short);
268
269 void randomdelay();
270 void transStart();
271 //#define TRANSREAD(x,y,z) /* Use "z" which is program point at which TRANSREAD is generated, use this as transRead2(inputvalue,z) */
272 #define TRANSREAD(x,y) { \
273     unsigned int inputvalue; \
274     if ((inputvalue=(unsigned int)y)==0) x=NULL;                                                                                              \
275          else { \
276            chashlistnode_t * cnodetmp=&c_table[(inputvalue&c_mask)>>1];    \
277            do { \
278              if (cnodetmp->key==inputvalue) {x=(void *)& ((objheader_t*)cnodetmp->val)[1]; break; } \
279              cnodetmp=cnodetmp->next; \
280              if (cnodetmp==NULL) {x=(void *)transRead2(inputvalue); asm volatile ("" : "=m" (c_table),"=m" (c_mask)); break; } \
281            } while(1); \
282          }}
283
284 __attribute__((pure)) objheader_t *transRead(unsigned int);
285 __attribute__((pure)) objheader_t *transRead2(unsigned int);
286 //__attribute__((pure)) objheader_t *transRead2(unsigned int, char ptr[]);
287 objheader_t *transCreateObj(unsigned int); //returns oid header
288 int transCommit(); //return 0 if successful
289 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
290 char decideResponse(char *, char *,  int); // Coordinator decides what response to send to the participant
291 void *getRemoteObj(unsigned int, unsigned int); // returns object header from main object store after object is copied into it from remote machine
292 void handleLocalReq(trans_req_data_t *, trans_commit_data_t *, char *);
293 int transComProcess(trans_req_data_t *, trans_commit_data_t *);
294 void doLocalProcess(char, trans_req_data_t *tdata, trans_commit_data_t *);
295 int transAbortProcess(trans_commit_data_t *);
296 void transAbort();
297 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size);
298 void prefetch(int, int, unsigned int *, unsigned short *, short*);
299 void *transPrefetch(void *);
300 void *mcqProcess(void *);
301 prefetchpile_t *foundLocal(char *, int, int); // returns node with prefetch elements(oids, offsets, siteid)
302 int lookupObject(unsigned int * oid, short offset, int *);
303 int checkoid(unsigned int oid, int isLastOffset);
304 int transPrefetchProcess(int **, short);
305 void sendPrefetchReq(prefetchpile_t*, int, int);
306 void sendPrefetchReqnew(prefetchpile_t*, int);
307 int getPrefetchResponse(int, struct readstruct *);
308 unsigned short getObjType(unsigned int oid);
309 int startRemoteThread(unsigned int oid, unsigned int mid);
310 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs);
311 void commitCountForObjRead(char *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *, unsigned int, unsigned short);
312 void commitCountForObjMod(char *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *, unsigned int, unsigned short);
313
314 long long myrdtsc(void);
315 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
316 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
317 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
318 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
319
320 /* Internal functions from signal.c */
321 int getthreadid();
322 double getMax(double *array, int size);
323 double getMin(double *array, int size);
324 double getfast(int siteid, int threadid);
325 double getslowest(int siteid, int threadid);
326 double getavg(int siteid, int threadid);
327 double getavgperthd(int siteid, int threadid);
328 double avgfast(int siteid, int threadid);
329 double avgslow(int siteid, int threadid);
330 void bubblesort();
331 void swap(double *e1, double *e2);
332 double avgofthreads(int siteid, int threadid);
333
334 /* end transactions */
335
336 #include "trans.h"
337 #endif