e2ad79cf66f61a94f155bdd7a3c5d1c73cea9d62
[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 //Max remote-machine connections
53 #define NUM_MACHINES 2
54 #define LOADFACTOR 0.5
55 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
56 //Transaction id per machine
57 #define TID_LEN 20
58 #define LISTEN_PORT 2156
59 #define UDP_PORT 2158
60 //Prefetch tuning paramters
61 #define RETRYINTERVAL  100  //N
62 #define SHUTDOWNINTERVAL  1  //M
63
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <pthread.h>
68 #include "clookup.h"
69 #include "queue.h"
70 #include "mcpileq.h"
71 #include "threadnotify.h"
72 #include <arpa/inet.h>
73 #include <sys/types.h>
74 #include <sys/socket.h>
75 #include <sys/ioctl.h>
76 #include <sys/time.h>
77 #include <netdb.h>
78 #include <netinet/in.h>
79 #include <sys/types.h>
80 #include <unistd.h>
81 #include <errno.h>
82 #include <time.h>
83 #include "sockpool.h"
84 #include "prelookup.h"
85 #include <signal.h>
86 #include "addPrefetchEnhance.h"
87
88 //bit designations for status field of objheader
89 #define DIRTY 0x01
90 #define NEW   0x02
91 #define LOCK  0x04
92 #define LOCAL  0x08
93
94 /*******Global statistics *********/
95 extern int numprefetchsites;
96
97 #ifdef COMPILER
98
99 #include "structdefs.h"
100
101 typedef struct objheader {
102         threadlist_t *notifylist;
103         unsigned short version;
104         unsigned short rcount;
105 } objheader_t;
106
107 #define OID(x)\
108     (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
109
110 #define COMPOID(x)\
111     (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___))
112
113 #define STATUS(x)\
114          *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
115
116 #define STATUSPTR(x)\
117          ((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
118
119 #define TYPE(x)\
120         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
121
122 #define GETSIZE(size, x) {\
123   int type=TYPE(x);\
124   if (type<NUMCLASSES) {\
125     size=classsize[type];\
126   } else {\
127     size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject);\
128   }\
129 }
130
131 #else
132
133 typedef struct objheader {
134         threadlist_t *notifylist;
135         unsigned int oid;
136         unsigned short type;
137         unsigned short version;
138         unsigned short rcount;
139         char status;
140 } objheader_t;
141
142 #define OID(x) x->oid
143 #define TYPE(x) x->type
144 #define STATUS(x) x->status
145 #define STATUSPTR(x) &x->status
146 #define GETSIZE(size, x) size=classsize[TYPE(x)]
147 #endif
148
149 typedef struct objstr {
150         unsigned int size; //this many bytes are allocated after this header
151         void *top;
152         struct objstr *next;
153 } objstr_t;
154
155 typedef struct oidmidpair {
156     unsigned int oid;
157     unsigned int mid;
158 } oidmidpair_t;
159
160 typedef struct transrecord {
161   objstr_t *cache;
162   chashtable_t *lookupTable;
163 #ifdef COMPILER
164   struct ___Object___ * revertlist;
165 #endif
166 } transrecord_t;
167
168 // Structure is a shared structure that keeps track of responses from the participants
169 typedef struct thread_response {
170   char rcv_status;
171 } thread_response_t;
172
173 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
174 typedef struct fixed_data {
175   char control;                 /* control message */
176   char trans_id[TID_LEN];       /* transaction id */
177   int mcount;                   /* participant count */
178   unsigned int numread;         /* no of objects read */
179   unsigned int nummod;                  /* no of objects modified */
180   unsigned int numcreated;              /* no of objects created */
181   int sum_bytes;                /* total bytes of modified objects in a transaction */
182 } fixed_data_t;
183
184 /* Structure that holds trans request information for each participant */
185 typedef struct trans_req_data {
186   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
187   unsigned int *listmid;        /* Pointer to array holding list of participants */
188   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */ 
189   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
190   unsigned int *oidcreated;     /* Pointer to array holding oids of objects that are newly created */
191 } trans_req_data_t;             
192
193 /* Structure that holds information of objects that are not found in the participant
194  * and objs locked within a transaction during commit process */
195 typedef struct trans_commit_data{
196   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
197   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
198   unsigned int *objvernotmatch;    /* Pointer to array holding oids whose version doesn't match on the participant machine */
199   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
200   int numlocked;                /* no of objects locked */
201   int numnotfound;              /* no of objects not found */
202   int numvernotmatch;           /* no of objects whose version doesn't match */
203 } trans_commit_data_t;
204
205
206 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
207 /* Structure for passing multiple arguments to a thread
208  * spawned to process each transaction on a machine */
209 typedef struct thread_data_array {
210   int thread_id;        
211   int mid;    
212   trans_req_data_t *buffer;     /* Holds trans request information sent to participants */  
213   thread_response_t *recvmsg;   /* Shared datastructure to keep track of the participants response to a trans request */
214   pthread_cond_t *threshold;    /* Condition var to waking up a thread */
215   pthread_mutex_t *lock;        /* Lock for counting participants response */
216   int *count;                   /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
217   char *replyctrl;              /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
218   char *replyretry;             /* Shared variable that keep track if coordinator needs retry */
219   transrecord_t *rec;           /* To send modified objects */
220 } thread_data_array_t;
221
222
223 //Structure for passing arguments to the local m/c thread
224 typedef struct local_thread_data_array {
225         thread_data_array_t *tdata;     /* Holds all the arguments send to a thread that is spawned when transaction commits */ 
226         trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */ 
227 } local_thread_data_array_t;
228
229 //Structure to store mid and socketid information
230 typedef struct midSocketInfo {
231         unsigned int mid;               /* To communicate with mid use sockid in this data structure */
232         int sockid;
233 } midSocketInfo_t;
234
235 /* Initialize main object store and lookup tables, start server thread. */
236 int dstmInit(void);
237 void send_data(int fd, void *buf, int buflen);
238 void recv_data(int fd, void *buf, int buflen);
239 int recv_data_errorcode(int fd, void *buf, int buflen);
240
241 /* Prototypes for object header */
242 unsigned int getNewOID(void);
243 /* end object header */
244
245 /* Prototypes for object store */
246 objstr_t *objstrCreate(unsigned int size); //size in bytes
247 void objstrDelete(objstr_t *store); //traverse and free entire list
248 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
249 void clearObjStore(); // TODO:currently only clears the prefetch cache object store
250 /* end object store */
251
252 /* Prototypes for server portion */
253 void *dstmListen(void *);
254 int startlistening();
255 void *dstmAccept(void *);
256 int readClientReq(trans_commit_data_t *, int);
257 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
258 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
259 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
260 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
261 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
262 /* end server portion */
263
264 /* Prototypes for transactions */
265 /* Function called at beginning. Passes in the first parameter. */
266 /* Returns 1 if this thread should run the main process */
267
268 int dstmStartup(const char *);
269 void transInit();
270 int processConfigFile();
271 void addHost(unsigned int);
272 void mapObjMethod(unsigned short);
273
274 void randomdelay();
275 transrecord_t *transStart();
276 objheader_t *transRead(transrecord_t *, unsigned int);
277 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid header
278 int transCommit(transrecord_t *record); //return 0 if successful
279 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
280 void decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
281 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
282 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);// returns object header from main object store after object is copied into it from remote machine
283 void *handleLocalReq(void *);//handles Local requests 
284 int transComProcess(local_thread_data_array_t *);
285 int transAbortProcess(local_thread_data_array_t *);
286 void transAbort(transrecord_t *trans);
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 *);// returns node with prefetch elements(oids, offsets)
292 int lookupObject(unsigned int * oid, short offset);
293 int transPrefetchProcess(transrecord_t *, int **, short);
294 void sendPrefetchReq(prefetchpile_t*, int);
295 void sendPrefetchReqnew(prefetchpile_t*, int);
296 int getPrefetchResponse(int);
297 unsigned short getObjType(unsigned int oid);
298 int startRemoteThread(unsigned int oid, unsigned int mid);
299 int updatePrefetchCache(thread_data_array_t *, int, char);
300
301
302
303 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
304 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
305 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
306 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
307
308 /* end transactions */
309 #endif