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