99a21fbd347aa643363a901748cd7de8d1141b31
[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 #define GET_NTUPLES(x)  ((int *)(x + sizeof(prefetchqelem_t)))
9 #define GET_PTR_OID(x)  ((unsigned int *)(x + sizeof(prefetchqelem_t) + sizeof(int)))
10 #define GET_PTR_EOFF(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int))))
11 #define GET_PTR_ARRYFLD(x,n) ((short *)(x + sizeof(prefetchqelem_t) + sizeof(int) + (n*sizeof(unsigned int)) + (n*sizeof(short))))
12
13
14 //Coordinator Messages
15 #define READ_REQUEST            1
16 #define READ_MULT_REQUEST       2
17 #define MOVE_REQUEST            3
18 #define MOVE_MULT_REQUEST       4
19 #define TRANS_REQUEST           5
20 #define TRANS_ABORT             6
21 #define TRANS_COMMIT            7
22 #define TRANS_PREFETCH          8
23 #define TRANS_ABORT_BUT_RETRY_COMMIT_WITH_RELOCATING    9
24
25 //Participant Messages
26 #define OBJECT_FOUND                    10
27 #define OBJECT_NOT_FOUND                11
28 #define OBJECTS_FOUND                   12
29 #define OBJECTS_NOT_FOUND               13
30 #define TRANS_AGREE                     17
31 #define TRANS_DISAGREE                  18
32 #define TRANS_AGREE_BUT_MISSING_OBJECTS 19
33 #define TRANS_SOFT_ABORT                20
34 #define TRANS_SUCESSFUL                 21
35 #define TRANS_PREFETCH_RESPONSE         22
36 #define START_REMOTE_THREAD             23
37
38 //Control bits for status of objects in Machine pile
39 #define OBJ_LOCKED_BUT_VERSION_MATCH    14
40 #define OBJ_UNLOCK_BUT_VERSION_MATCH    15
41 #define VERSION_NO_MATCH                16
42
43 //Max number of objects 
44 #define MAX_OBJECTS  20
45
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <pthread.h>
51 #include "clookup.h"
52 #include "queue.h"
53 #include "mcpileq.h"
54
55 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
56 #define TID_LEN 20
57 //bit designations for status field of objheader
58 #define DIRTY 0x01
59 #define NEW   0x02
60 #define LOCK  0x04
61 #define LOCAL  0x08
62
63 #ifdef COMPILER
64
65 #include "structdefs.h"
66
67 typedef struct objheader {
68         unsigned short version;
69         unsigned short rcount;
70 } objheader_t;
71
72 #define OID(x)\
73     (*((unsigned int *)&((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___nextobject___))
74
75 #define COMPOID(x)\
76     (*((unsigned int *)&((struct ___Object___ *) x)->___nextobject___))
77
78 #define STATUS(x)\
79          *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
80
81 #define TYPE(x)\
82         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
83
84 #define GETSIZE(size, x) {\
85   int type=TYPE(x);\
86   if (type<NUMCLASSES) {\
87     size=classsize[type];\
88   } else {\
89     size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject);\
90   }\
91 }
92
93 #else
94
95 typedef struct objheader {
96         unsigned int oid;
97         unsigned short type;
98         unsigned short version;
99         unsigned short rcount;
100         char status;
101 } objheader_t;
102
103 #define OID(x) x->oid
104 #define TYPE(x) x->type
105 #define STATUS(x) x->status
106 #define GETSIZE(size, x) size=classsize[TYPE(x)]
107 #endif
108
109
110 typedef struct objstr {
111         unsigned int size; //this many bytes are allocated after this header
112         void *top;
113         struct objstr *next;
114 } objstr_t;
115
116 typedef struct transrecord {
117   objstr_t *cache;
118   chashtable_t *lookupTable;
119 #ifdef COMPILER
120   struct ___Object___ * revertlist;
121 #endif
122 } transrecord_t;
123 // Structure is a shared structure that keeps track of responses from the participants
124 typedef struct thread_response {
125   char rcv_status;
126 } thread_response_t;
127
128 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
129 typedef struct fixed_data {
130   char control;                 /* control message */
131   char trans_id[TID_LEN];       /* transaction id */
132   int mcount;                   /* participant count */
133   short numread;                /* no of objects read */
134   short nummod;                 /* no of objects modified */
135   short numcreated;             /* no of objects created */
136   int sum_bytes;                /* total bytes of modified objects in a transaction */
137 } fixed_data_t;
138
139 /* Structure that holds trans request information for each participant */
140 typedef struct trans_req_data {
141   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
142   unsigned int *listmid;        /* Pointer to array holding list of participants */
143   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */ 
144   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
145   unsigned int *oidcreated;             /* Pointer to array holding oids of objects that are newly created */
146 } trans_req_data_t;             
147
148 /* Structure that holds information of objects that are not found in the participant
149  * and objs locked within a transaction during commit process */
150 typedef struct trans_commit_data{
151   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
152   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
153   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
154   int numlocked;                /* no of objects locked */
155   int numnotfound;              /* no of objects not found */
156 } trans_commit_data_t;
157
158
159 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
160 /* Structure for passing multiple arguments to a thread
161  * spawned to process each transaction on a machine */
162 typedef struct thread_data_array {
163   int thread_id;        
164   int mid;    
165   trans_req_data_t *buffer;     /* Holds trans request information sent to participants */  
166   thread_response_t *recvmsg;   /* Shared datastructure to keep track of the participants response to a trans request */
167   pthread_cond_t *threshold;    /* Condition var to waking up a thread */
168   pthread_mutex_t *lock;        /* Lock for counting participants response */
169   int *count;                   /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
170   char *replyctrl;              /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
171   char *replyretry;             /* Shared variable that keep track if coordinator needs retry */
172   transrecord_t *rec;           /* To send modified objects */
173 } thread_data_array_t;
174
175
176 //Structure for passing arguments to the local m/c thread
177 typedef struct local_thread_data_array {
178         thread_data_array_t *tdata;     /* Holds all the arguments send to a thread that is spawned when transaction commits */ 
179         trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */ 
180 } local_thread_data_array_t;
181
182 //Structure for members within prefetch tuples
183 typedef struct member {
184         short offset;           /* Holds offset of the ptr field */
185         short index;            /* Holds the array index value */ 
186         struct member *next;    
187 }trans_member_t;
188
189 /* Initialize main object store and lookup tables, start server thread. */
190 int dstmInit(void);
191
192 /* Prototypes for object header */
193 unsigned int getNewOID(void);
194 /* end object header */
195
196 /* Prototypes for object store */
197 objstr_t *objstrCreate(unsigned int size); //size in bytes
198 void objstrDelete(objstr_t *store); //traverse and free entire list
199 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
200 /* end object store */
201
202 /* Prototypes for server portion */
203 void *dstmListen();
204 void *dstmAccept(void *);
205 int readClientReq(trans_commit_data_t *, int);
206 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
207 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
208 int decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
209 //int transCommitProcess(trans_commit_data_t *, int);
210 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
211 /* end server portion */
212
213 /* Prototypes for transactions */
214 /* Function called at beginning. Passes in the first parameter. */
215 /* Returns 1 if this thread should run the main process */
216
217 int dstmStartup(const char *);
218 void transInit();
219 int processConfigFile();
220 void addHost(unsigned int);
221 void mapObjMethod(unsigned short);
222
223 void randomdelay(void);
224 transrecord_t *transStart();
225 objheader_t *transRead(transrecord_t *, unsigned int);
226 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid
227 int transCommit(transrecord_t *record); //return 0 if successful
228 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
229 void decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
230 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
231 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);
232  
233 void *handleLocalReq(void *);
234 int transComProcess(local_thread_data_array_t *);
235 int transAbortProcess(local_thread_data_array_t *);
236
237 void prefetch(int, unsigned int *, unsigned short *, short*);
238 void *transPrefetch(void *);
239 void *mcqProcess(void *);
240 void checkPrefetchTuples(prefetchqelem_t *);
241 prefetchpile_t *foundLocal(prefetchqelem_t *);
242 prefetchpile_t *makePreGroups(prefetchqelem_t *, int *);
243 void checkPreCache(prefetchqelem_t *, int *, unsigned int, int);
244 int transPrefetchProcess(transrecord_t *, int **, short);
245 void sendPrefetchReq(prefetchpile_t*, int);
246 void getPrefetchResponse(int, int);
247 unsigned short getObjType(unsigned int oid);
248 int startRemoteThread(unsigned int oid, unsigned int mid);
249 /* end transactions */
250 #endif