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