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