11927ab1f8d7028f62cc8e739e21a58286572562
[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  7 //N
62 #define SHUTDOWNINTERVAL  4  //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 <signal.h>
85 #include "plookup.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 #ifdef COMPILER
97
98 #include "structdefs.h"
99
100 typedef struct objheader {
101         threadlist_t *notifylist;
102         unsigned short version;
103         unsigned short rcount;
104 } objheader_t;
105
106 #define OID(x)\
107     (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
108
109 #define COMPOID(x)\
110     (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___))
111
112 #define STATUS(x)\
113          *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
114
115 #define STATUSPTR(x)\
116          ((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
117
118 #define TYPE(x)\
119         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
120
121 #define GETSIZE(size, x) {\
122   int type=TYPE(x);\
123   if (type<NUMCLASSES) {\
124     size=classsize[type];\
125   } else {\
126     size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject);\
127   }\
128 }
129
130 #else
131
132 typedef struct objheader {
133         threadlist_t *notifylist;
134         unsigned int oid;
135         unsigned short type;
136         unsigned short version;
137         unsigned short rcount;
138         char status;
139 } objheader_t;
140
141 #define OID(x) x->oid
142 #define TYPE(x) x->type
143 #define STATUS(x) x->status
144 #define STATUSPTR(x) &x->status
145 #define GETSIZE(size, x) size=classsize[TYPE(x)]
146 #endif
147
148 typedef struct objstr {
149         unsigned int size; //this many bytes are allocated after this header
150         void *top;
151         struct objstr *next;
152 } objstr_t;
153
154 typedef struct oidmidpair {
155     unsigned int oid;
156     unsigned int mid;
157 } oidmidpair_t;
158
159 typedef struct transrecord {
160   objstr_t *cache;
161   chashtable_t *lookupTable;
162 #ifdef COMPILER
163   struct ___Object___ * revertlist;
164 #endif
165 } transrecord_t;
166
167 // Structure is a shared structure that keeps track of responses from the participants
168 typedef struct thread_response {
169   char rcv_status;
170 } thread_response_t;
171
172 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
173 typedef struct fixed_data {
174   char control;                 /* control message */
175   char trans_id[TID_LEN];       /* transaction id */
176   int mcount;                   /* participant count */
177   unsigned int numread;         /* no of objects read */
178   unsigned int nummod;                  /* no of objects modified */
179   unsigned int numcreated;              /* no of objects created */
180   int sum_bytes;                /* total bytes of modified objects in a transaction */
181 } fixed_data_t;
182
183 /* Structure that holds trans request information for each participant */
184 typedef struct trans_req_data {
185   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
186   unsigned int *listmid;        /* Pointer to array holding list of participants */
187   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */ 
188   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
189   unsigned int *oidcreated;     /* Pointer to array holding oids of objects that are newly created */
190 } trans_req_data_t;             
191
192 /* Structure that holds information of objects that are not found in the participant
193  * and objs locked within a transaction during commit process */
194 typedef struct trans_commit_data{
195   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
196   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
197   unsigned int *objvernotmatch;    /* Pointer to array holding oids whose version doesn't match on the participant machine */
198   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
199   int numlocked;                /* no of objects locked */
200   int numnotfound;              /* no of objects not found */
201   int numvernotmatch;           /* no of objects whose version doesn't match */
202 } trans_commit_data_t;
203
204
205 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
206 /* Structure for passing multiple arguments to a thread
207  * spawned to process each transaction on a machine */
208 typedef struct thread_data_array {
209   int thread_id;        
210   int mid;    
211   trans_req_data_t *buffer;     /* Holds trans request information sent to a participant, based on threadid */  
212   thread_response_t *recvmsg;   /* Shared datastructure to keep track of the participants response to a trans request */
213   pthread_cond_t *threshold;    /* Condition var to waking up a thread */
214   pthread_mutex_t *lock;        /* Lock for counting participants response */
215   int *count;                   /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
216   char *replyctrl;              /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
217   char *replyretry;             /* Shared variable that keep track if coordinator needs retry */
218   transrecord_t *rec;           /* To send modified objects */
219   plistnode_t *pilehead;   /*  Shared variable, ptr to the head of the machine piles for the transaction rec */
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 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs);
300
301 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
302 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
303 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
304 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
305
306 /* end transactions */
307 #endif