bug fixes in gCollect.c and gCollect.h
[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 typedef struct objstr {
146         unsigned int size; //this many bytes are allocated after this header
147         void *top;
148         struct objstr *next;
149 } objstr_t;
150
151 typedef struct oidmidpair {
152     unsigned int oid;
153     unsigned int mid;
154 } oidmidpair_t;
155
156 typedef struct transrecord {
157   objstr_t *cache;
158   chashtable_t *lookupTable;
159 #ifdef COMPILER
160   struct ___Object___ * revertlist;
161 #endif
162 } transrecord_t;
163
164 // Structure is a shared structure that keeps track of responses from the participants
165 typedef struct thread_response {
166   char rcv_status;
167 } thread_response_t;
168
169 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
170 typedef struct fixed_data {
171   char control;                 /* control message */
172   char trans_id[TID_LEN];       /* transaction id */
173   int mcount;                   /* participant count */
174   unsigned int numread;         /* no of objects read */
175   unsigned int nummod;                  /* no of objects modified */
176   unsigned int numcreated;              /* no of objects created */
177   int sum_bytes;                /* total bytes of modified objects in a transaction */
178 } fixed_data_t;
179
180 /* Structure that holds trans request information for each participant */
181 typedef struct trans_req_data {
182   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
183   unsigned int *listmid;        /* Pointer to array holding list of participants */
184   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */ 
185   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
186   unsigned int *oidcreated;     /* Pointer to array holding oids of objects that are newly created */
187 } trans_req_data_t;             
188
189 /* Structure that holds information of objects that are not found in the participant
190  * and objs locked within a transaction during commit process */
191 typedef struct trans_commit_data{
192   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
193   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
194   unsigned int *objvernotmatch;    /* Pointer to array holding oids whose version doesn't match on the participant machine */
195   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
196   int numlocked;                /* no of objects locked */
197   int numnotfound;              /* no of objects not found */
198   int numvernotmatch;           /* no of objects whose version doesn't match */
199 } trans_commit_data_t;
200
201
202 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
203 /* Structure for passing multiple arguments to a thread
204  * spawned to process each transaction on a machine */
205 typedef struct thread_data_array {
206   int thread_id;        
207   int mid;    
208   trans_req_data_t *buffer;     /* Holds trans request information sent to participants */  
209   thread_response_t *recvmsg;   /* Shared datastructure to keep track of the participants response to a trans request */
210   pthread_cond_t *threshold;    /* Condition var to waking up a thread */
211   pthread_mutex_t *lock;        /* Lock for counting participants response */
212   int *count;                   /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
213   char *replyctrl;              /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
214   char *replyretry;             /* Shared variable that keep track if coordinator needs retry */
215   transrecord_t *rec;           /* To send modified objects */
216 } thread_data_array_t;
217
218
219 //Structure for passing arguments to the local m/c thread
220 typedef struct local_thread_data_array {
221         thread_data_array_t *tdata;     /* Holds all the arguments send to a thread that is spawned when transaction commits */ 
222         trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */ 
223 } local_thread_data_array_t;
224
225 //Structure to store mid and socketid information
226 typedef struct midSocketInfo {
227         unsigned int mid;               /* To communicate with mid use sockid in this data structure */
228         int sockid;
229 } midSocketInfo_t;
230
231 /* Initialize main object store and lookup tables, start server thread. */
232 int dstmInit(void);
233 void send_data(int fd, void *buf, int buflen);
234 void recv_data(int fd, void *buf, int buflen);
235 int recv_data_errorcode(int fd, void *buf, int buflen);
236
237 /* Prototypes for object header */
238 unsigned int getNewOID(void);
239 /* end object header */
240
241 /* Prototypes for object store */
242 objstr_t *objstrCreate(unsigned int size); //size in bytes
243 void objstrDelete(objstr_t *store); //traverse and free entire list
244 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
245 void clearObjStore(); // TODO:currently only clears the prefetch cache object store
246 /* end object store */
247
248 /* Prototypes for server portion */
249 void *dstmListen(void *);
250 int startlistening();
251 void *dstmAccept(void *);
252 int readClientReq(trans_commit_data_t *, int);
253 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
254 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
255 char decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
256 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
257 void processReqNotify(unsigned int numoid, unsigned int *oid, unsigned short *version, unsigned int mid, unsigned int threadid);
258 /* end server portion */
259
260 /* Prototypes for transactions */
261 /* Function called at beginning. Passes in the first parameter. */
262 /* Returns 1 if this thread should run the main process */
263
264 int dstmStartup(const char *);
265 void transInit();
266 int processConfigFile();
267 void addHost(unsigned int);
268 void mapObjMethod(unsigned short);
269
270 void randomdelay();
271 transrecord_t *transStart();
272 objheader_t *transRead(transrecord_t *, unsigned int);
273 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid header
274 int transCommit(transrecord_t *record); //return 0 if successful
275 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
276 void decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
277 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
278 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);// returns object header from main object store after object is copied into it from remote machine
279 void *handleLocalReq(void *);//handles Local requests 
280 int transComProcess(local_thread_data_array_t *);
281 int transAbortProcess(local_thread_data_array_t *);
282 void transAbort(transrecord_t *trans);
283 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size);
284 void prefetch(int, unsigned int *, unsigned short *, short*);
285 void *transPrefetch(void *);
286 void *mcqProcess(void *);
287 prefetchpile_t *foundLocal(char *);// returns node with prefetch elements(oids, offsets)
288 int lookupObject(unsigned int * oid, short offset);
289 int transPrefetchProcess(transrecord_t *, int **, short);
290 void sendPrefetchReq(prefetchpile_t*, int);
291 void sendPrefetchReqnew(prefetchpile_t*, int);
292 int getPrefetchResponse(int);
293 unsigned short getObjType(unsigned int oid);
294 int startRemoteThread(unsigned int oid, unsigned int mid);
295 int updatePrefetchCache(thread_data_array_t *, int, char);
296
297
298
299 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
300 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
301 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
302 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
303
304 /* end transactions */
305 #endif