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