9858b07009a64aad5c22b8a9f5d9c14b190edbc1
[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 #endif
19 #ifdef STMLOG
20 __thread int counter;
21 __thread int event[100000*7+3];
22 __thread unsigned long long clkticks[100000*7+3];
23 #define FILENAME  "log"
24 #endif
25
26 #if defined(THREADS)||defined(STM)
27 /* Global barrier for STM */
28 pthread_barrier_t barrier;
29 pthread_barrierattr_t attr;
30 #endif
31
32 #include <string.h>
33
34 extern int classsize[];
35 extern int typearray[];
36 extern int typearray2[];
37 jmp_buf error_handler;
38 int instructioncount;
39
40 char *options;
41 int injectfailures=0;
42 float failurechance=0;
43 int errors=0;
44 int debugtask=0;
45 int injectinstructionfailures;
46 int failurecount;
47 float instfailurechance=0;
48 int numfailures;
49 int instaccum=0;
50 typedef unsigned long long ticks;
51 #ifdef DMALLOC
52 #include "dmalloc.h"
53 #endif
54
55 int instanceof(struct ___Object___ *ptr, int type) {
56   int i=ptr->type;
57   do {
58     if (i==type)
59       return 1;
60     i=typearray[i];
61   } while(i!=-1);
62   i=ptr->type;
63   if (i>NUMCLASSES) {
64     do {
65       if (i==type)
66         return 1;
67       i=typearray2[i-NUMCLASSES];
68     } while(i!=-1);
69   }
70   return 0;
71 }
72
73 void exithandler(int sig, siginfo_t *info, void * uap) {
74   exit(0);
75 }
76
77 void initializeexithandler() {
78   struct sigaction sig;
79   sig.sa_sigaction=&exithandler;
80   sig.sa_flags=SA_SIGINFO;
81   sigemptyset(&sig.sa_mask);
82   sigaction(SIGUSR2, &sig, 0);
83 }
84
85
86 /* This function inject failures */
87
88 void injectinstructionfailure() {
89 #ifdef TASK
90   if (injectinstructionfailures) {
91     if (numfailures==0)
92       return;
93     instructioncount=failurecount;
94     instaccum+=failurecount;
95     if ((((double)random())/RAND_MAX)<instfailurechance) {
96       if (numfailures>0)
97         numfailures--;
98       printf("FAILURE!!! %d\n",numfailures);
99       longjmp(error_handler,11);
100     }
101   }
102 #else
103 #ifdef THREADS
104   if (injectinstructionfailures) {
105     if (numfailures==0)
106       return;
107     instaccum+=failurecount;
108     if ((((double)random())/RAND_MAX)<instfailurechance) {
109       if (numfailures>0)
110         numfailures--;
111       printf("FAILURE!!! %d\n",numfailures);
112       threadexit();
113     }
114   }
115 #endif
116 #endif
117 }
118
119 #ifdef D___Double______nativeparsedouble____L___String___
120 double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
121   int length=VAR(___str___)->___count___;
122   int maxlength=(length>60)?60:length;
123   char str[maxlength+1];
124   struct ArrayObject * chararray=VAR(___str___)->___value___;
125   int i;
126   int offset=VAR(___str___)->___offset___;
127   for(i=0; i<maxlength; i++) {
128     str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
129   }
130   str[i]=0;
131   double d=atof(str);
132   return d;
133 }
134 #endif
135
136 #ifdef D___String______convertdoubletochar____D__AR_C
137 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
138   int length=VAR(___chararray___)->___length___;
139   char str[length];
140   int i;
141   int num=snprintf(str, length, "%f",___val___);
142   if (num>=length)
143     num=length-1;
144   for(i=0; i<length; i++) {
145     ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
146   }
147   return num;
148 }
149 #endif
150 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
151 void deepArrayCopy(struct ___Object___ * dst, struct ___Object___ * src) {
152   int dsttype=((int *)dst)[0];
153   int srctype=((int *)src)[0];
154   if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
155     return;
156   struct ArrayObject *aodst=(struct ArrayObject *)dst;
157   struct ArrayObject *aosrc=(struct ArrayObject *)src;
158   int dstlength=aodst->___length___;
159   int srclength=aosrc->___length___;
160   if (dstlength!=srclength)
161     return;
162   unsigned INTPTR *pointer=pointerarray[srctype];
163   if (pointer==0) {
164     int elementsize=classsize[srctype];
165     int size=srclength*elementsize;
166     //primitives
167     memcpy(((char *)&aodst->___length___)+sizeof(int) , ((char *)&aosrc->___length___)+sizeof(int), size);
168   } else {
169     //objects
170     int i;
171     for(i=0;i<srclength;i++) {
172       struct ___Object___ * ptr=((struct ___Object___**)(((char*) &aosrc->___length___)+sizeof(int)))[i];
173       int ptrtype=((int *)ptr)[0];
174       if (ptrtype>=NUMCLASSES) {
175         struct ___Object___ * dstptr=((struct ___Object___**)(((char*) &aodst->___length___)+sizeof(int)))[i];
176         deepArrayCopy(dstptr,ptr);
177       } else {
178         //hit an object
179         ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
180       }
181     }
182   }
183 }
184
185 void CALL02(___System______deepArrayCopy____L___Object____L___Object___, struct ___Object___ * ___dst___, struct ___Object___ * ___src___) {
186   deepArrayCopy(VAR(___dst___), VAR(___src___));
187 }
188 #endif
189
190 void CALL11(___System______exit____I,int ___status___, int ___status___) {
191 #ifdef TRANSSTATS
192   printf("numTransCommit = %d\n", numTransCommit);
193   printf("numTransAbort = %d\n", numTransAbort);
194   printf("nSoftAbort = %d\n", nSoftAbort);
195 #ifdef STM
196   printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
197   printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
198 #ifdef STMSTATS
199   int i;
200   for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
201     printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
202   }
203 #endif
204 #endif
205 #endif
206   exit(___status___);
207 }
208
209 #if defined(__i386__)
210
211 static __inline__ unsigned long long rdtsc(void)
212 {
213   unsigned long long int x;
214   __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
215   return x;
216 }
217 #elif defined(__x86_64__)
218
219 static __inline__ unsigned long long rdtsc(void)
220 {
221   unsigned hi, lo;
222   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
223   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
224 }
225
226 #elif defined(__powerpc__)
227
228 typedef unsigned long long int unsigned long long;
229
230 static __inline__ unsigned long long rdtsc(void)
231 {
232   unsigned long long int result=0;
233   unsigned long int upper, lower,tmp;
234   __asm__ volatile(
235       "0:                  \n"
236       "\tmftbu   %0           \n"
237       "\tmftb    %1           \n"
238       "\tmftbu   %2           \n"
239       "\tcmpw    %2,%0        \n"
240       "\tbne     0b         \n"
241       : "=r"(upper),"=r"(lower),"=r"(tmp)
242       );
243   result = upper;
244   result = result<<32;
245   result = result|lower;
246
247   return(result);
248 }
249 #endif
250
251 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
252 #ifdef STMLOG
253   event[counter] = ___event___;
254   clkticks[counter] = rdtsc();
255   counter++;
256 #endif
257   return;
258 }
259
260 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
261 #ifdef STMLOG
262   FILE *fp;
263   /* Flush to file */
264   char filename[20];
265   memset(filename, 0, 20);
266   sprintf(filename, "%s_%d", FILENAME, ___threadid___);
267   if ((fp = fopen(filename, "w+")) == NULL) {
268     perror("fopen");
269     return;
270   }
271   int i;
272   for (i = 0; i < counter-1; i++) {
273     fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i], clkticks[i+1]);
274   }
275   fprintf(fp, "%d %lld\n", event[i], clkticks[i]);
276
277   fclose(fp);
278 #endif
279   return;
280 }
281
282 void CALL00(___System______initLog____) {
283 #ifdef STMLOG
284   counter=0;
285 #endif
286   return;
287 }
288
289 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
290 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
291   char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
292   memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
293 }
294 #endif
295
296 void CALL11(___System______printI____I,int ___status___, int ___status___) {
297   printf("%d\n",___status___);
298 }
299
300 long long CALL00(___System______currentTimeMillis____) {
301   struct timeval tv; long long retval;
302   gettimeofday(&tv, NULL);
303   retval = tv.tv_sec; /* seconds */
304   retval*=1000; /* milliseconds */
305   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
306   return retval;
307 }
308
309 long long CALL00(___System______microTimes____) {
310   struct timeval tv; 
311   long long retval;
312   gettimeofday(&tv, NULL);
313   retval = tv.tv_sec; /* seconds */
314   retval*=1000000; /* microsecs */
315   retval+= (tv.tv_usec); /* adjust microseconds & add them in */
316   return retval;
317 }
318
319 long long CALL00(___System______getticks____) {
320   unsigned a, d;
321   asm("cpuid");
322   asm volatile("rdtsc" : "=a" (a), "=d" (d));
323   return (((ticks)a) | (((ticks)d) << 32));
324 }
325
326 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
327   struct ArrayObject * chararray=VAR(___s___)->___value___;
328   int i;
329   int offset=VAR(___s___)->___offset___;
330   for(i=0; i<VAR(___s___)->___count___; i++) {
331     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
332     putchar(sc);
333   }
334 }
335
336 #ifdef DSTM
337 void CALL00(___System______clearPrefetchCache____) {
338   prehashClear();
339 }
340
341 #ifdef RANGEPREFETCH
342 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
343   /* Manual Prefetches to be inserted */
344   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
345   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
346   int numoffset=VAR(___offsets___)->___length___;
347   int i;
348   short offArry[numoffset+2];
349   offArry[0] = 0;
350   offArry[1] = 0;
351   for(i = 2; i<(numoffset+2); i++) {
352     offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
353     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
354   }
355   unsigned int oid;
356   if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
357     oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
358   } else { //even
359     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
360   }
361   rangePrefetch(oid, (short)(numoffset+2), offArry);
362 }
363 #else
364 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
365   return;
366 }
367 #endif
368
369 #endif
370
371 /* STM Barrier constructs */
372 #ifdef D___Barrier______setBarrier____I
373 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
374   // Barrier initialization
375   int ret;
376   if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
377     printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
378     exit(-1);
379   }
380 }
381 #endif
382
383 #ifdef D___Barrier______enterBarrier____
384 void CALL00(___Barrier______enterBarrier____) {
385   // Synchronization point
386   int ret;
387 #ifdef PRECISE_GC
388   stopforgc((struct garbagelist *)___params___);
389 #endif
390   ret = pthread_barrier_wait(&barrier);
391 #ifdef PRECISE_GC
392   restartaftergc();
393 #endif
394   if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
395     printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
396     exit(-1);
397   }
398 }
399 #endif
400
401 /* Object allocation function */
402
403 #ifdef DSTM
404 __attribute__((malloc)) void * allocate_newglobal(int type) {
405   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
406   v->type=type;
407 #ifdef THREADS
408   v->tid=0;
409   v->lockentry=0;
410   v->lockcount=0;
411 #endif
412   return v;
413 }
414
415 /* Array allocation function */
416
417 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
418   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
419   if (length<0) {
420     printf("ERROR: negative array\n");
421     return NULL;
422   }
423   v->type=type;
424   v->___length___=length;
425 #ifdef THREADS
426   v->tid=0;
427   v->lockentry=0;
428   v->lockcount=0;
429 #endif
430   return v;
431 }
432 #endif
433
434
435 #ifdef STM
436 // STM Versions of allocation functions
437
438 /* Object allocation function */
439 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
440   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
441   v->type=type;
442   v->___objlocation___=v;
443   return v;
444 }
445
446 /* Array allocation function */
447 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
448   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
449   if (length<0) {
450     printf("ERROR: negative array\n");
451     return NULL;
452   }
453   v->___objlocation___=(struct ___Object___*)v;
454   v->type=type;
455   v->___length___=length;
456   return v;
457 }
458 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
459   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
460   struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
461   initdsmlocks(&tmp->lock);
462   tmp->version = 1;
463   v->___objlocation___=v;
464   v->type = type;
465   return v;
466 }
467
468 /* Array allocation function */
469
470 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
471   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
472   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
473   initdsmlocks(&tmp->lock);
474   tmp->version=1;
475   v->type=type;
476   if (length<0) {
477     printf("ERROR: negative array %d\n", length);
478     return NULL;
479   }
480   v->___objlocation___=(struct ___Object___ *)v;
481   v->___length___=length;
482   return v;
483 }
484 #endif
485
486 #ifndef STM
487 #if defined(PRECISE_GC)
488 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
489   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
490   v->type=type;
491 #ifdef THREADS
492   v->tid=0;
493   v->lockentry=0;
494   v->lockcount=0;
495 #endif
496 #ifdef OPTIONAL
497   v->fses=0;
498 #endif
499   return v;
500 }
501
502 /* Array allocation function */
503
504 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
505   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
506   v->type=type;
507   if (length<0) {
508     printf("ERROR: negative array\n");
509     return NULL;
510   }
511   v->___length___=length;
512 #ifdef THREADS
513   v->tid=0;
514   v->lockentry=0;
515   v->lockcount=0;
516 #endif
517 #ifdef OPTIONAL
518   v->fses=0;
519 #endif
520   return v;
521 }
522
523 #else
524 __attribute__((malloc)) void * allocate_new(int type) {
525   struct ___Object___ * v=FREEMALLOC(classsize[type]);
526   v->type=type;
527 #ifdef OPTIONAL
528   v->fses=0;
529 #endif
530   return v;
531 }
532
533 /* Array allocation function */
534
535 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
536   __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
537   v->type=type;
538   v->___length___=length;
539 #ifdef OPTIONAL
540   v->fses=0;
541 #endif
542   return v;
543 }
544 #endif
545 #endif
546
547 /* Converts C character arrays into Java strings */
548 #ifdef PRECISE_GC
549 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
550 #else
551 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
552 #endif
553   int i;
554 #ifdef PRECISE_GC
555   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
556   INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
557   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
558   chararray=(struct ArrayObject *) ptrarray[2];
559 #else
560   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
561   struct ___String___ * strobj=allocate_new(STRINGTYPE);
562 #endif
563   strobj->___value___=chararray;
564   strobj->___count___=length;
565   strobj->___offset___=0;
566
567   for(i=0; i<length; i++) {
568     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
569   }
570   return strobj;
571 }
572
573 /* Generated code calls this if we fail a bounds check */
574
575 void failedboundschk() {
576 #ifndef TASK
577   printf("Array out of bounds\n");
578 #ifdef THREADS
579   threadexit();
580 #else
581   exit(-1);
582 #endif
583 #else
584   longjmp(error_handler,2);
585 #endif
586 }
587
588 /* Abort task call */
589 void abort_task() {
590 #ifdef TASK
591   longjmp(error_handler,4);
592 #else
593   printf("Aborting\n");
594   exit(-1);
595 #endif
596 }