Added code to read config file of IP address, and use these for the mostly fair distr...
[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 STATUS(x)\
76          *((unsigned int *) &(((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->___localcopy___))
77
78 #define TYPE(x)\
79         ((struct ___Object___ *)((unsigned int) x + sizeof(objheader_t)))->type
80
81 #else
82
83 typedef struct objheader {
84         unsigned int oid;
85         unsigned short type;
86         unsigned short version;
87         unsigned short rcount;
88         char status;
89 } objheader_t;
90
91 #define OID(x) x->oid
92 #define TYPE(x) x->type
93 #define STATUS(x) x->status
94
95 #endif
96
97
98 typedef struct objstr {
99         unsigned int size; //this many bytes are allocated after this header
100         void *top;
101         struct objstr *next;
102 } objstr_t;
103
104 typedef struct transrecord {
105   objstr_t *cache;
106   chashtable_t *lookupTable;
107 #ifdef COMPILER
108   struct ___Object___ * revertlist;
109 #endif
110 } transrecord_t;
111 // Structure is a shared structure that keeps track of responses from the participants
112 typedef struct thread_response {
113   char rcv_status;
114 } thread_response_t;
115
116 // Structure that holds  fixed data to be sent along with TRANS_REQUEST
117 typedef struct fixed_data {
118   char control;                 /* control message */
119   char trans_id[TID_LEN];       /* transaction id */
120   int mcount;                   /* participant count */
121   short numread;                /* no of objects read */
122   short nummod;                 /* no of objects modified */
123   short numcreated;             /* no of objects created */
124   int sum_bytes;                /* total bytes of modified objects in a transaction */
125 } fixed_data_t;
126
127 /* Structure that holds trans request information for each participant */
128 typedef struct trans_req_data {
129   fixed_data_t f;               /* Holds first few fixed bytes of data sent during TRANS_REQUEST protcol*/
130   unsigned int *listmid;        /* Pointer to array holding list of participants */
131   char *objread;                /* Pointer to array holding oid and version number of objects that are only read */ 
132   unsigned int *oidmod;         /* Pointer to array holding oids of objects that are modified */
133   unsigned int *oidcreated;             /* Pointer to array holding oids of objects that are newly created */
134 } trans_req_data_t;             
135
136 /* Structure that holds information of objects that are not found in the participant
137  * and objs locked within a transaction during commit process */
138 typedef struct trans_commit_data{
139   unsigned int *objlocked;      /* Pointer to array holding oids of objects locked inside a transaction */
140   unsigned int *objnotfound;    /* Pointer to array holding oids of objects not found on the participant machine */
141   void *modptr;                 /* Pointer to the address in the mainobject store of the participant that holds all modified objects */
142   int numlocked;                /* no of objects locked */
143   int numnotfound;              /* no of objects not found */
144 } trans_commit_data_t;
145
146
147 #define PRINT_TID(PTR) printf("DEBUG -> %x %d\n", PTR->mid, PTR->thread_id);
148 /* Structure for passing multiple arguments to a thread
149  * spawned to process each transaction on a machine */
150 typedef struct thread_data_array {
151   int thread_id;        
152   int mid;    
153   int pilecount;                /* No of remote machines involved */
154   trans_req_data_t *buffer;     /* Holds trans request information sent to participants */  
155   thread_response_t *recvmsg;   /* Shared datastructure to keep track of the participants response to a trans request */
156   pthread_cond_t *threshold;    /* Condition var to waking up a thread */
157   pthread_mutex_t *lock;        /* Lock for counting participants response */
158   int *count;                   /* Variable to count responses from all participants to the TRANS_REQUEST protocol */
159   char *replyctrl;              /* Shared ctrl message that stores the reply to be sent to participants, filled by decideResponse() */
160   char *replyretry;             /* Shared variable that keep track if coordinator needs retry */
161   transrecord_t *rec;           /* To send modified objects */
162 } thread_data_array_t;
163
164
165 //Structure for passing arguments to the local m/c thread
166 typedef struct local_thread_data_array {
167         thread_data_array_t *tdata;     /* Holds all the arguments send to a thread that is spawned when transaction commits */ 
168         trans_commit_data_t *transinfo; /* Holds information of objects locked and not found in the participant */ 
169 } local_thread_data_array_t;
170
171 //Structure for members within prefetch tuples
172 typedef struct member {
173         short offset;           /* Holds offset of the ptr field */
174         short index;            /* Holds the array index value */ 
175         struct member *next;    
176 }trans_member_t;
177
178 /* Initialize main object store and lookup tables, start server thread. */
179 int dstmInit(void);
180
181 /* Prototypes for object header */
182 unsigned int getNewOID(void);
183 unsigned int objSize(objheader_t *object);
184 /* end object header */
185
186 /* Prototypes for object store */
187 objstr_t *objstrCreate(unsigned int size); //size in bytes
188 void objstrDelete(objstr_t *store); //traverse and free entire list
189 void *objstrAlloc(objstr_t *store, unsigned int size); //size in bytes
190 /* end object store */
191
192 /* Prototypes for server portion */
193 void *dstmListen();
194 void *dstmAccept(void *);
195 int readClientReq(trans_commit_data_t *, int);
196 int processClientReq(fixed_data_t *, trans_commit_data_t *,unsigned int *, char *, void *, unsigned int *, int);
197 char handleTransReq(fixed_data_t *, trans_commit_data_t *, unsigned int *, char *, void *, int);
198 int decideCtrlMessage(fixed_data_t *, trans_commit_data_t *, int *, int *, int *, int *, int *, void *, unsigned int *, unsigned int *, int);
199 //int transCommitProcess(trans_commit_data_t *, int);
200 int transCommitProcess(void *, unsigned int *, unsigned int *, int, int, int);
201 /* end server portion */
202
203 /* Prototypes for transactions */
204 /* Function called at beginning. Passes in the first parameter. */
205 /* Returns 1 if this thread should run the main process */
206
207 int dstmStartup(const char *);
208 void transInit();
209 int processConfigFile();
210 void addHost(unsigned int);
211
212 void randomdelay(void);
213 transrecord_t *transStart();
214 objheader_t *transRead(transrecord_t *, unsigned int);
215 objheader_t *transCreateObj(transrecord_t *, unsigned int); //returns oid
216 int transCommit(transrecord_t *record); //return 0 if successful
217 void *transRequest(void *);     //the C routine that the thread will execute when TRANS_REQUEST begins
218 void *handleLocalReq(void *);   //the C routine that the local m/c thread will execute 
219 int decideResponse(thread_data_array_t *);// Coordinator decides what response to send to the participant
220 char sendResponse(thread_data_array_t *, int); //Sends control message back to Participants
221 void *getRemoteObj(transrecord_t *, unsigned int, unsigned int);
222 int transAbortProcess(void *, unsigned int *, int, int);
223 int transComProcess(void*, unsigned int *, unsigned int *, unsigned int *, int, int, int);
224 void prefetch(int, unsigned int *, short *, short*);
225 void *transPrefetch(void *);
226 void *mcqProcess(void *);
227 void checkPrefetchTuples(prefetchqelem_t *);
228 prefetchpile_t *foundLocal(prefetchqelem_t *);
229 prefetchpile_t *makePreGroups(prefetchqelem_t *, int *);
230 void checkPreCache(prefetchqelem_t *, int *, int, int, unsigned int, int, int, int);
231 int transPrefetchProcess(transrecord_t *, int **, short);
232 void sendPrefetchReq(prefetchpile_t*, int);
233 void getPrefetchResponse(int, int);
234 unsigned short getObjType(unsigned int oid);
235 int startRemoteThread(unsigned int oid, unsigned int mid);
236 /* end transactions */
237 #endif