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