typos
[IRC.git] / Robust / src / Runtime / runtime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <signal.h>
4 #include "mem.h"
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include "option.h"
9 #include "methodheaders.h"
10 #ifdef DSTM
11 #include "dstm.h"
12 #include "prelookup.h"
13 #include "prefetch.h"
14 #endif
15 #ifdef STM
16 #include "tm.h"
17 #include <pthread.h>
18 /* Global barrier for STM */
19 pthread_barrier_t barrier; 
20 pthread_barrierattr_t attr;
21 #endif
22 #include <string.h>
23
24 extern int classsize[];
25 extern int typearray[];
26 extern int typearray2[];
27 jmp_buf error_handler;
28 int instructioncount;
29
30 char *options;
31 int injectfailures=0;
32 float failurechance=0;
33 int errors=0;
34 int debugtask=0;
35 int injectinstructionfailures;
36 int failurecount;
37 float instfailurechance=0;
38 int numfailures;
39 int instaccum=0;
40 #ifdef DMALLOC
41 #include "dmalloc.h"
42 #endif
43
44 int instanceof(struct ___Object___ *ptr, int type) {
45   int i=ptr->type;
46   do {
47     if (i==type)
48       return 1;
49     i=typearray[i];
50   } while(i!=-1);
51   i=ptr->type;
52   if (i>NUMCLASSES) {
53     do {
54       if (i==type)
55         return 1;
56       i=typearray2[i-NUMCLASSES];
57     } while(i!=-1);
58   }
59   return 0;
60 }
61
62 void exithandler(int sig, siginfo_t *info, void * uap) {
63   exit(0);
64 }
65
66 void initializeexithandler() {
67   struct sigaction sig;
68   sig.sa_sigaction=&exithandler;
69   sig.sa_flags=SA_SIGINFO;
70   sigemptyset(&sig.sa_mask);
71   sigaction(SIGUSR2, &sig, 0);
72 }
73
74
75 /* This function inject failures */
76
77 void injectinstructionfailure() {
78 #ifdef TASK
79   if (injectinstructionfailures) {
80     if (numfailures==0)
81       return;
82     instructioncount=failurecount;
83     instaccum+=failurecount;
84     if ((((double)random())/RAND_MAX)<instfailurechance) {
85       if (numfailures>0)
86         numfailures--;
87       printf("FAILURE!!! %d\n",numfailures);
88       longjmp(error_handler,11);
89     }
90   }
91 #else
92 #ifdef THREADS
93   if (injectinstructionfailures) {
94     if (numfailures==0)
95       return;
96     instaccum+=failurecount;
97     if ((((double)random())/RAND_MAX)<instfailurechance) {
98       if (numfailures>0)
99         numfailures--;
100       printf("FAILURE!!! %d\n",numfailures);
101       threadexit();
102     }
103   }
104 #endif
105 #endif
106 }
107
108 void CALL11(___System______exit____I,int ___status___, int ___status___) {
109 #ifdef TRANSSTATS
110   printf("numTransCommit = %d\n", numTransCommit);
111   printf("numTransAbort = %d\n", numTransAbort);
112   printf("nSoftAbort = %d\n", nSoftAbort);
113 #ifdef STM
114   printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
115   printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
116 #endif
117 #endif
118   exit(___status___);
119 }
120
121 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
122 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
123   char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
124   memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
125 }
126 #endif
127
128 void CALL11(___System______printI____I,int ___status___, int ___status___) {
129   printf("%d\n",___status___);
130 }
131
132 long long CALL00(___System______currentTimeMillis____) {
133   struct timeval tv; long long retval;
134   gettimeofday(&tv, NULL);
135   retval = tv.tv_sec; /* seconds */
136   retval*=1000; /* milliseconds */
137   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
138   return retval;
139 }
140
141 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
142   struct ArrayObject * chararray=VAR(___s___)->___value___;
143   int i;
144   int offset=VAR(___s___)->___offset___;
145   for(i=0; i<VAR(___s___)->___count___; i++) {
146     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
147     putchar(sc);
148   }
149 }
150
151 #ifdef DSTM
152 void CALL00(___System______clearPrefetchCache____) {
153   prehashClear();
154 }
155
156 #ifdef RANGEPREFETCH
157 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
158   /* Manual Prefetches to be inserted */
159   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
160   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
161   int numoffset=VAR(___offsets___)->___length___;
162   int i;
163   short offArry[numoffset+2];
164   offArry[0] = 0;
165   offArry[1] = 0;
166   for(i = 2; i<(numoffset+2); i++) {
167     offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
168     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
169   }
170   unsigned int oid;
171   if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
172     oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
173   } else { //even
174     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
175   }
176   rangePrefetch(oid, (short)(numoffset+2), offArry);
177 }
178 #else
179 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
180   return;
181 }
182 #endif
183
184 #endif
185
186 #ifdef STM
187 /* STM Barrier constructs */
188 #ifdef D___Barrier______setBarrier____I
189 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
190 #ifdef PRECISE_GC
191   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
192 #endif
193   // Barrier initialization
194   int ret; 
195   if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
196     printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
197     exit(-1);
198   }
199 #ifdef PRECISE_GC
200   restartaftergc(tmp);
201 #endif
202
203 #endif
204
205 #ifdef D___Barrier______enterBarrier____
206 void CALL00(___Barrier______enterBarrier____) {
207   // Synchronization point
208   int ret;
209   ret = pthread_barrier_wait(&barrier);
210   if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
211     printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
212     exit(-1);
213   }
214 }
215 #endif
216 #endif
217
218 /* Object allocation function */
219
220 #ifdef DSTM
221 __attribute__((malloc)) void * allocate_newglobal(int type) {
222   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
223   v->type=type;
224 #ifdef THREADS
225   v->tid=0;
226   v->lockentry=0;
227   v->lockcount=0;
228 #endif
229   return v;
230 }
231
232 /* Array allocation function */
233
234 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
235   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
236   if (length<0) {
237     printf("ERROR: negative array\n");
238     return NULL;
239   }
240   v->type=type;
241   v->___length___=length;
242 #ifdef THREADS
243   v->tid=0;
244   v->lockentry=0;
245   v->lockcount=0;
246 #endif
247   return v;
248 }
249 #endif
250
251
252 #ifdef STM
253 // STM Versions of allocation functions
254
255 /* Object allocation function */
256 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
257   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
258   v->type=type;
259   v->___objlocation___=v;
260   return v;
261 }
262
263 /* Array allocation function */
264 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
265   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
266   if (length<0) {
267     printf("ERROR: negative array\n");
268     return NULL;
269   }
270   v->___objlocation___=(struct ___Object___*)v;
271   v->type=type;
272   v->___length___=length;
273   return v;
274 }
275 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
276   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
277   struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
278   initdsmlocks(&tmp->lock);
279   tmp->version = 1;
280   v->___objlocation___=v;
281   v->type = type;
282   return v;
283 }
284
285 /* Array allocation function */
286
287 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
288   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
289   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
290   initdsmlocks(&tmp->lock);
291   tmp->version=1;
292   v->type=type;
293   if (length<0) {
294     printf("ERROR: negative array %d\n", length);
295     return NULL;
296   }
297   v->___objlocation___=(struct ___Object___ *)v;
298   v->___length___=length;
299   return v;
300 }
301 #endif
302
303 #ifndef STM
304 #if defined(PRECISE_GC)
305 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
306   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
307   v->type=type;
308 #ifdef THREADS
309   v->tid=0;
310   v->lockentry=0;
311   v->lockcount=0;
312 #endif
313 #ifdef OPTIONAL
314   v->fses=0;
315 #endif
316   return v;
317 }
318
319 /* Array allocation function */
320
321 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
322   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
323   v->type=type;
324   if (length<0) {
325     printf("ERROR: negative array\n");
326     return NULL;
327   }
328   v->___length___=length;
329 #ifdef THREADS
330   v->tid=0;
331   v->lockentry=0;
332   v->lockcount=0;
333 #endif
334 #ifdef OPTIONAL
335   v->fses=0;
336 #endif
337   return v;
338 }
339
340 #else
341 __attribute__((malloc)) void * allocate_new(int type) {
342   struct ___Object___ * v=FREEMALLOC(classsize[type]);
343   v->type=type;
344 #ifdef OPTIONAL
345   v->fses=0;
346 #endif
347   return v;
348 }
349
350 /* Array allocation function */
351
352 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
353   __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
354   v->type=type;
355   v->___length___=length;
356 #ifdef OPTIONAL
357   v->fses=0;
358 #endif
359   return v;
360 }
361 #endif
362 #endif
363
364
365 /* Converts C character arrays into Java strings */
366 #ifdef PRECISE_GC
367 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
368 #else
369 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
370 #endif
371   int i;
372 #ifdef PRECISE_GC
373   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
374   INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
375   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
376   chararray=(struct ArrayObject *) ptrarray[2];
377 #else
378   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
379   struct ___String___ * strobj=allocate_new(STRINGTYPE);
380 #endif
381   strobj->___value___=chararray;
382   strobj->___count___=length;
383   strobj->___offset___=0;
384
385   for(i=0; i<length; i++) {
386     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
387   }
388   return strobj;
389 }
390
391 /* Generated code calls this if we fail a bounds check */
392
393 void failedboundschk() {
394 #ifndef TASK
395   printf("Array out of bounds\n");
396 #ifdef THREADS
397   threadexit();
398 #else
399   exit(-1);
400 #endif
401 #else
402   longjmp(error_handler,2);
403 #endif
404 }
405
406 /* Abort task call */
407 void abort_task() {
408 #ifdef TASK
409   longjmp(error_handler,4);
410 #else
411   printf("Aborting\n");
412   exit(-1);
413 #endif
414 }