helpful progress reporting
[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 #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 //Prefetch tuning paramters
58 #define RETRYINTERVAL  20 //N
59 #define SHUTDOWNINTERVAL  3  //M
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 <signal.h>
82 #include "plookup.h"
83 #include "dsmdebug.h"
84
85 //bit designations for status field of objheader
86 #define DIRTY 0x01
87 #define NEW   0x02
88 #define LOCK  0x04
89 #define LOCAL  0x08
90
91 /*******Global statistics *********/
92 extern int numprefetchsites;
93
94 #ifdef COMPILER
95
96 #include "structdefs.h"
97
98 typedef struct objheader {
99   threadlist_t *notifylist;
100   unsigned short version;
101   unsigned short rcount;
102 } objheader_t;
103
104 #define OID(x) \
105   (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
106
107 #define COMPOID(x) \
108   ((void*)((((void *) x )!=NULL) ? (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___)) : 0))
109
110 #define STATUS(x) \
111   *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
112
113 #define STATUSPTR(x) \
114   ((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
115
116 #define TYPE(x) \
117   ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
118
119 #define GETSIZE(size, x) { \
120     int type=TYPE(x); \
121     if (type<NUMCLASSES) { \
122       size=classsize[type]; \
123     } else { \
124       size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject); \
125     } \
126 }
127
128 #else
129
130 typedef struct objheader {
131   threadlist_t *notifylist;
132   unsigned int oid;
133   unsigned short type;
134   unsigned short version;
135   unsigned short rcount;
136   char status;
137 } objheader_t;
138
139 #define OID(x) x->oid
140 #define TYPE(x) x->type
141 #define STATUS(x) x->status
142 #define STATUSPTR(x) &x->status
143 #define GETSIZE(size, x) size=classsize[TYPE(x)]
144 #endif
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 a participant, based on threadid */
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;           /* Shared variable transaction record send to all thread data */
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 void getCommitCountForObjMod(unsigned int *, unsigned int *, unsigned int *, int *,
260                              int *, int *, int *, int *, int *, int *, char *, unsigned int, unsigned short);
261 void getCommitCountForObjRead(unsigned int *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *,
262                               int *, int *, char *, unsigned int, unsigned short);
263 /* end server portion */
264
265 /* Prototypes for transactions */
266 /* Function called at beginning. Passes in the first parameter. */
267 /* Returns 1 if this thread should run the main process */
268
269 int dstmStartup(const char *);
270 void transInit();
271 int processConfigFile();
272 void addHost(unsigned int);
273 void mapObjMethod(unsigned short);
274
275 void randomdelay();
276 transrecord_t *transStart();
277 objheader_t *transRead(transrecord_t *, unsigned int);
278 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid header
279 int transCommit(transrecord_t *record); //return 0 if successful
280 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
281 void decideResponse(thread_data_array_t *); // Coordinator decides what response to send to the participant
282 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
283 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int); // returns object header from main object store after object is copied into it from remote machine
284 void *handleLocalReq(void *); //handles Local requests
285 int transComProcess(local_thread_data_array_t *);
286 int transAbortProcess(local_thread_data_array_t *);
287 void transAbort(transrecord_t *trans);
288 void sendPrefetchResponse(int sd, char *control, char *sendbuffer, int *size);
289 void prefetch(int, int, unsigned int *, unsigned short *, short*);
290 void *transPrefetch(void *);
291 void *mcqProcess(void *);
292 prefetchpile_t *foundLocal(char *); // returns node with prefetch elements(oids, offsets)
293 int lookupObject(unsigned int * oid, short offset);
294 int transPrefetchProcess(transrecord_t *, int **, short);
295 void sendPrefetchReq(prefetchpile_t*, int);
296 void sendPrefetchReqnew(prefetchpile_t*, int);
297 int getPrefetchResponse(int);
298 unsigned short getObjType(unsigned int oid);
299 int startRemoteThread(unsigned int oid, unsigned int mid);
300 plistnode_t *pInsert(plistnode_t *pile, objheader_t *headeraddr, unsigned int mid, int num_objs);
301 void commitCountForObjRead(local_thread_data_array_t *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *, unsigned int, unsigned short);
302 void commitCountForObjMod(local_thread_data_array_t *, unsigned int *, unsigned int *, int *, int *, int *, int *, int *, unsigned int, unsigned short);
303
304 /* Sends notification request for thread join, if sucessful returns 0 else returns -1 */
305 int reqNotify(unsigned int *oidarry, unsigned short *versionarry, unsigned int numoid);
306 void threadNotify(unsigned int oid, unsigned short version, unsigned int tid);
307 int notifyAll(threadlist_t **head, unsigned int oid, unsigned int version);
308
309 /* end transactions */
310 #endif