5532d6bc7641009c36e61c3d6414395dd94daa87
[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 #define ENDEBUG(s) { printf("Inside %s()\n", s); fflush(stdout);}
17 #define EXDEBUG(s) {printf("Outside %s()\n", s); fflush(stdout);}
18 /*****************************************
19  *  Coordinator Messages
20  ***************************************/
21 #define READ_REQUEST            1
22 #define READ_MULT_REQUEST       2
23 #define MOVE_REQUEST            3
24 #define MOVE_MULT_REQUEST       4
25 #define TRANS_REQUEST           5
26 #define TRANS_ABORT             6
27 #define TRANS_COMMIT            7
28 #define TRANS_PREFETCH          8
29 #define TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING    9
30
31 /*********************************
32  * Participant Messages
33  *******************************/
34 #define OBJECT_FOUND                    10
35 #define OBJECT_NOT_FOUND                11
36 #define OBJECTS_FOUND                   12
37 #define OBJECTS_NOT_FOUND               13
38 #define TRANS_AGREE                     17
39 #define TRANS_DISAGREE                  18
40 #define TRANS_AGREE_BUT_MISSING_OBJECTS 19
41 #define TRANS_SOFT_ABORT                20
42 #define TRANS_SUCESSFUL                 21
43 #define TRANS_PREFETCH_RESPONSE         22
44 #define START_REMOTE_THREAD             23
45 #define THREAD_NOTIFY_REQUEST           24
46 #define THREAD_NOTIFY_RESPONSE          25
47 #define TRANS_UNSUCESSFUL               26
48 #define CLOSE_CONNECTION  27
49
50 //Max number of objects
51 #define MAX_OBJECTS  20
52 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
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  100 //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
87 //bit designations for status field of objheader
88 #define DIRTY 0x01
89 #define NEW   0x02
90 #define LOCK  0x04
91 #define LOCAL  0x08
92
93 /*******Global statistics *********/
94 extern int numprefetchsites;
95
96 double idForTimeDelay;           /* TODO Remove, necessary to get time delay for starting transRequest for this id */
97 int transCount;                  /* TODO Remove, necessary to the transaction id */
98
99 #ifdef COMPILER
100
101 #include "structdefs.h"
102
103 typedef struct objheader {
104   threadlist_t *notifylist;
105   unsigned short version;
106   unsigned short rcount;
107 } objheader_t;
108
109 #define OID(x) \
110   (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
111
112 #define COMPOID(x) \
113   ((void*)((((void *) x )!=NULL) ? (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___)) : 0))
114
115 #define STATUS(x) \
116   *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
117
118 #define STATUSPTR(x) \
119   ((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
120
121 #define TYPE(x) \
122   ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
123
124 #define GETSIZE(size, x) { \
125     int type=TYPE(x); \
126     if (type<NUMCLASSES) { \
127       size=classsize[type]; \
128     } else { \
129       size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject); \
130     } \
131 }
132
133 #else
134
135 typedef struct objheader {
136   threadlist_t *notifylist;
137   unsigned int oid;
138   unsigned short type;
139   unsigned short version;
140   unsigned short rcount;
141   char status;
142 } objheader_t;
143
144 #define OID(x) x->oid
145 #define TYPE(x) x->type
146 #define STATUS(x) x->status
147 #define STATUSPTR(x) &x->status
148 #define GETSIZE(size, x) size=classsize[TYPE(x)]
149 #endif
150
151 typedef struct objstr {
152   unsigned int size;       //this many bytes are allocated after this header
153   void *top;
154   struct objstr *next;
155 } objstr_t;
156
157 typedef struct oidmidpair {
158   unsigned int oid;
159   unsigned int mid;
160 } oidmidpair_t;
161
162 typedef struct transrecord {
163   objstr_t *cache;
164   chashtable_t *lookupTable;
165 #ifdef COMPILER
166   struct ___Object___ * revertlist;
167 #endif
168 } transrecord_t;
169
170 // Structure is a shared structure that keeps track of responses from the participants
171 typedef struct thread_response {
172   char rcv_status;
173 } thread_response_t;
174
175 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
176 typedef struct fixed_data {
177   char control;                 /* control message */
178   char trans_id[TID_LEN];       /* transaction id */
179   int mcount;                   /* participant count */
180   unsigned int numread;         /* no of objects read */
181   unsigned int nummod;                  /* no of objects modified */
182   unsigned int numcreated;              /* no of objects created */
183   int sum_bytes;                /* total bytes of modified objects in a transaction */
184 } fixed_data_t;
185
186 /* Structure that holds trans request information for each participant */
187 typedef struct trans_req_data {
188   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
189   unsigned int *listmid;        /* Pointer to array holding list of participants */
190   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */
191   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
192   unsigned int *oidcreated;     /* Pointer to array holding oids of objects that are newly created */
193 } trans_req_data_t;
194
195 /* Structure that holds information of objects that are not found in the participant
196  * and objs locked within a transaction during commit process */
197 typedef struct trans_commit_data {
198   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
199   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
200   unsigned int *objvernotmatch;    /* Pointer to array holding oids whose version doesn't match on the participant machine */
201   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
202   int numlocked;                /* no of objects locked */
203   int numnotfound;              /* no of objects not found */
204   int numvernotmatch;           /* no of objects whose version doesn't match */
205 } trans_commit_data_t;
206
207
208 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
209 /* Structure for passing multiple arguments to a thread
210  * spawned to process each transaction on a machine */
211 typedef struct thread_data_array {
212   int thread_id;
213   int mid;
214   trans_req_data_t *buffer;     /* Holds trans request information sent to a participant, based on threadid */
215   thread_response_t *recvmsg;   /* Shared datastructure to keep track of the participants response to a trans request */
216   pthread_cond_t *threshold;    /* Condition var to wake up a thread */
217   pthread_mutex_t *lock;        /* Lock for counting participants response */
218   int *count;                   /* Shared variable to count responses from all participants to the TRANS_REQUEST protocol */
219   char *replyctrl;              /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
220   char *replyretry;             /* Shared variable that keep track if coordinator needs retry */
221   transrecord_t *rec;           /* Shared variable transaction record send to all thread data */
222 } thread_data_array_t;
223
224
225 //Structure for passing arguments to the local m/c thread
226 typedef struct local_thread_data_array {
227   thread_data_array_t *tdata;           /* Holds all the arguments send to a thread that is spawned when transaction commits */
228   trans_commit_data_t *transinfo;       /* Holds information of objects locked and not found in the participant */
229 } local_thread_data_array_t;
230
231 //Structure to store mid and socketid information
232 typedef struct midSocketInfo {
233   unsigned int mid;                     /* To communicate with mid use sockid in this data structure */
234   int sockid;
235 } midSocketInfo_t;
236
237 /* Initialize main object store and lookup tables, start server thread. */
238 int dstmInit(void);
239 void send_data(int fd, void *buf, int buflen);
240 void recv_data(int fd, void *buf, int buflen);
241 int recv_data_errorcode(int fd, void *buf, int buflen);
242
243 /* Prototypes for object header */
244 unsigned int getNewOID(void);
245 /* end object header */
246
247 /* Prototypes for object store */
248 objstr_t *objstrCreate(unsigned int size); //size in bytes
249 void objstrDelete(objstr_t *store); //traverse and free entire list
250 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
251 void clearObjStore(); // TODO:currently only clears the prefetch cache object store
252 /* end object store */
253
254 /* Prototypes for server portion */
255 void *dstmListen(void *);
256 int startlistening();
257 void *dstmAccept(void *);
258 int readClientReq(trans_commit_data_t *, int);
259 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
260 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
261 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
262 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
263 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
264 void getCommitCountForObjMod(unsigned int *, unsigned int *, unsigned int *, int *,
265                              int *, int *, int *, int *, int *, int *, char *, unsigned int, unsigned short);
266 void getCommitCountForObjRead(unsigned int *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *,
267                               int *, int *, char *, unsigned int, unsigned short);
268 /* end server portion */
269
270 /* Prototypes for transactions */
271 /* Function called at beginning. Passes in the first parameter. */
272 /* Returns 1 if this thread should run the main process */
273
274 int dstmStartup(const char *);
275 void transInit();
276 int processConfigFile();
277 void addHost(unsigned int);
278 void mapObjMethod(unsigned short);
279
280 void randomdelay();
281 transrecord_t *transStart();
282 __attribute__((pure)) objheader_t *transRead(transrecord_t *, unsigned int);
283 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid header
284 int transCommit(transrecord_t *record); //return 0 if successful
285 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
286 char decideResponse(char *, char *, transrecord_t *, int); // Coordinator decides what response to send to the participant
287 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int); // returns object header from main object store after object is copied into it from remote machine
288 void handleLocalReq(trans_req_data_t *, trans_commit_data_t *, transrecord_t *, char *);
289 int transComProcess(trans_req_data_t *, trans_commit_data_t *, transrecord_t *);
290 void doLocalProcess(char, trans_req_data_t *tdata, trans_commit_data_t *, transrecord_t *);
291 int transAbortProcess(trans_commit_data_t *);
292 void transAbort(transrecord_t *trans);
293 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size);
294 void prefetch(int, int, unsigned int *, unsigned short *, short*);
295 void *transPrefetch(void *);
296 void *mcqProcess(void *);
297 prefetchpile_t *foundLocal(char *); // returns node with prefetch elements(oids, offsets)
298 int lookupObject(unsigned int * oid, short offset);
299 int checkoid(unsigned int oid);
300 int transPrefetchProcess(transrecord_t *, int **, short);
301 void sendPrefetchReq(prefetchpile_t*, int);
302 void sendPrefetchReqnew(prefetchpile_t*, int);
303 int getPrefetchResponse(int);
304 unsigned short getObjType(unsigned int oid);
305 int startRemoteThread(unsigned int oid, unsigned int mid);
306 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs);
307 void commitCountForObjRead(char *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *, unsigned int, unsigned short);
308 void commitCountForObjMod(char *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *, unsigned int, unsigned short);
309
310 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
311 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
312 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
313 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
314
315 /* Internal functions from signal.c */
316 int getthreadid();
317 double getMax(double *array, int size);
318 double getMin(double *array, int size);
319 double getfast(int siteid, int threadid);
320 double getslowest(int siteid, int threadid);
321 double getavg(int siteid, int threadid);
322 double getavgperthd(int siteid, int threadid);
323 double avgfast(int siteid, int threadid);
324 double avgslow(int siteid, int threadid);
325 void bubblesort();
326 void swap(double *e1, double *e2);
327 double avgofthreads(int siteid, int threadid);
328
329 /* end transactions */
330 #endif