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