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