2 #include "structdefs.h"
9 #include "methodheaders.h"
12 #include "DSTM/interface_recovery/dstm.h"
13 #include "DSTM/interface_recovery/prelookup.h"
15 #include "DSTM/interface/dstm.h"
16 #include "DSTM/interface/prelookup.h"
17 #include "DSTM/interface/prefetch.h"
25 #define ARRAY_LENGTH 700003
27 __thread int event[ARRAY_LENGTH];
28 __thread unsigned long long clkticks[ARRAY_LENGTH];
29 unsigned long long beginClock=0;
30 #define FILENAME "log"
33 #if defined(THREADS)||defined(STM)
34 /* Global barrier for STM */
35 pthread_barrier_t barrier;
36 pthread_barrierattr_t attr;
41 extern int classsize[];
42 extern int typearray[];
43 extern int typearray2[];
44 jmp_buf error_handler;
49 float failurechance=0;
52 int injectinstructionfailures;
54 float instfailurechance=0;
57 typedef unsigned long long ticks;
62 int instanceof(struct ___Object___ *ptr, int type) {
74 i=typearray2[i-NUMCLASSES];
80 void exithandler(int sig, siginfo_t *info, void * uap) {
84 void initializeexithandler() {
86 sig.sa_sigaction=&exithandler;
87 sig.sa_flags=SA_SIGINFO;
88 sigemptyset(&sig.sa_mask);
89 sigaction(SIGUSR2, &sig, 0);
93 /* This function inject failures */
95 void injectinstructionfailure() {
97 if (injectinstructionfailures) {
100 instructioncount=failurecount;
101 instaccum+=failurecount;
102 if ((((double)random())/RAND_MAX)<instfailurechance) {
105 printf("FAILURE!!! %d\n",numfailures);
106 longjmp(error_handler,11);
111 if (injectinstructionfailures) {
114 instaccum+=failurecount;
115 if ((((double)random())/RAND_MAX)<instfailurechance) {
118 printf("FAILURE!!! %d\n",numfailures);
126 #ifdef D___Double______nativeparsedouble____L___String___
127 double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
128 int length=VAR(___str___)->___count___;
129 int maxlength=(length>60)?60:length;
130 char str[maxlength+1];
131 struct ArrayObject * chararray=VAR(___str___)->___value___;
133 int offset=VAR(___str___)->___offset___;
134 for(i=0; i<maxlength; i++) {
135 str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
143 #ifdef D___Double______nativeparsedouble_____AR_B_I_I
144 double CALL23(___Double______nativeparsedouble_____AR_B_I_I, int start, int length,int start,int length,struct ArrayObject * ___str___) {
145 int maxlength=(length>60)?60:length;
146 char str[maxlength+1];
147 struct ArrayObject * bytearray=VAR(___str___);
149 for(i=0; i<maxlength; i++) {
150 str[i]=(((char *)&bytearray->___length___)+sizeof(int))[i+start];
158 #ifdef D___String______convertdoubletochar____D__AR_C
159 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
160 int length=VAR(___chararray___)->___length___;
163 int num=snprintf(str, length, "%f",___val___);
166 for(i=0; i<length; i++) {
167 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
172 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
173 void deepArrayCopy(struct ___Object___ * dst, struct ___Object___ * src) {
174 int dsttype=((int *)dst)[0];
175 int srctype=((int *)src)[0];
177 src=src->___objlocation___;
179 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
181 struct ArrayObject *aodst=(struct ArrayObject *)dst;
182 struct ArrayObject *aosrc=(struct ArrayObject *)src;
183 int dstlength=aodst->___length___;
184 int srclength=aosrc->___length___;
185 if (dstlength!=srclength)
187 unsigned INTPTR *pointer=pointerarray[srctype];
189 int elementsize=classsize[srctype];
190 int size=srclength*elementsize;
192 memcpy(((char *)&aodst->___length___)+sizeof(int) , ((char *)&aosrc->___length___)+sizeof(int), size);
196 for(i=0;i<srclength;i++) {
197 struct ___Object___ * ptr=((struct ___Object___**)(((char*) &aosrc->___length___)+sizeof(int)))[i];
198 int ptrtype=((int *)ptr)[0];
199 if (ptrtype>=NUMCLASSES) {
200 struct ___Object___ * dstptr=((struct ___Object___**)(((char*) &aodst->___length___)+sizeof(int)))[i];
201 deepArrayCopy(dstptr,ptr);
204 ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
210 void CALL02(___System______deepArrayCopy____L___Object____L___Object___, struct ___Object___ * ___dst___, struct ___Object___ * ___src___) {
211 deepArrayCopy(VAR(___dst___), VAR(___src___));
215 void CALL11(___System______exit____I,int ___status___, int ___status___) {
217 printf("numTransCommit = %d\n", numTransCommit);
218 printf("numTransAbort = %d\n", numTransAbort);
219 printf("nSoftAbort = %d\n", nSoftAbort);
221 printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
222 printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
225 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
226 printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
234 #if defined(__i386__)
236 static __inline__ unsigned long long rdtsc(void)
238 unsigned long long int x;
239 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
242 #elif defined(__x86_64__)
244 static __inline__ unsigned long long rdtsc(void)
247 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
248 return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
251 #elif defined(__powerpc__)
253 typedef unsigned long long int unsigned long long;
255 static __inline__ unsigned long long rdtsc(void)
257 unsigned long long int result=0;
258 unsigned long int upper, lower,tmp;
266 : "=r"(upper),"=r"(lower),"=r"(tmp)
270 result = result|lower;
276 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
278 event[counter] = ___event___;
279 clkticks[counter] = rdtsc();
285 void CALL00(___System______logevent____) {
292 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
297 memset(filename, 0, 20);
298 sprintf(filename, "%s_%d", FILENAME, ___threadid___);
299 if ((fp = fopen(filename, "w+")) == NULL) {
304 for (i = 0; i < counter-1; i++) {
305 fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i]-beginClock, clkticks[i+1]-beginClock);
307 fprintf(fp, "%d %lld\n", event[i], clkticks[i]-beginClock);
314 void CALL00(___System______initLog____) {
318 for(i=0; i<ARRAY_LENGTH; i++) {
327 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
328 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
329 char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
330 memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
334 void CALL11(___System______printI____I,int ___status___, int ___status___) {
335 printf("%d\n",___status___);
338 long long CALL00(___System______currentTimeMillis____) {
339 struct timeval tv; long long retval;
340 gettimeofday(&tv, NULL);
341 retval = tv.tv_sec; /* seconds */
342 retval*=1000; /* milliseconds */
343 retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
347 long long CALL00(___System______microTimes____) {
350 gettimeofday(&tv, NULL);
351 retval = tv.tv_sec; /* seconds */
352 retval*=1000000; /* microsecs */
353 retval+= (tv.tv_usec); /* adjust microseconds & add them in */
357 long long CALL00(___System______getticks____) {
360 asm volatile("rdtsc" : "=a" (a), "=d" (d));
361 return (((ticks)a) | (((ticks)d) << 32));
364 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
365 struct ArrayObject * chararray=VAR(___s___)->___value___;
367 int offset=VAR(___s___)->___offset___;
368 for(i=0; i<VAR(___s___)->___count___; i++) {
369 short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
375 void CALL00(___System______clearPrefetchCache____) {
380 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
381 /* Manual Prefetches to be inserted */
382 //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
383 //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
384 int numoffset=VAR(___offsets___)->___length___;
386 short offArry[numoffset+2];
389 for(i = 2; i<(numoffset+2); i++) {
390 offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
391 //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
394 if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
395 oid = (unsigned int) VAR(___o___); //outside transaction therefore just an oid
397 oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
399 rangePrefetch(oid, (short)(numoffset+2), offArry);
402 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
407 #ifdef D___Task______execution____
408 extern void* virtualtable[];
409 // associated with Task.execution(). finds proper execute method and call it
410 void CALL01(___Task______execution____,struct ___Task___ * ___this___)
413 oid = (unsigned int) VAR(___this___); // object id
414 int type = getObjType(oid); // object type
417 int p[] = {1,0 , oid};
419 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(p);
421 // call the proper execute method
422 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(oid);
429 /* STM Barrier constructs */
430 #ifdef D___Barrier______setBarrier____I
431 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
432 // Barrier initialization
434 if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
435 printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
441 #ifdef D___Barrier______enterBarrier____
442 void CALL00(___Barrier______enterBarrier____) {
443 // Synchronization point
446 stopforgc((struct garbagelist *)___params___);
448 ret = pthread_barrier_wait(&barrier);
452 if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
453 printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
459 /* Object allocation function */
462 __attribute__((malloc)) void * allocate_newglobal(int type) {
463 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
465 //printf("DEBUG %s(), type= %x\n", __func__, type);
474 /* Array allocation function */
476 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
477 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
479 printf("ERROR: negative array\n");
483 v->___length___=length;
495 // STM Versions of allocation functions
497 /* Object allocation function */
498 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
500 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type], 0);
502 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
505 v->___objlocation___=v;
509 /* Array allocation function */
510 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
512 int basesize=length*classsize[type];
513 //round the base size up
514 basesize=(basesize+LOWMASK)&HIGHMASK;
515 int numlocks=basesize>>INDEXSHIFT;
516 int bookkeepsize=numlocks*2*sizeof(int);
517 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+basesize+bookkeepsize, bookkeepsize);
518 unsigned int *intptr=(unsigned int *)(((char *)v)-sizeof(objheader_t));
519 for(;numlocks>0;numlocks--) {
524 v->lowindex=MAXARRAYSIZE;
526 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
529 printf("ERROR: negative array\n");
532 v->___objlocation___=(struct ___Object___*)v;
534 v->___length___=length;
538 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
539 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
540 struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
541 initdsmlocks(&tmp->lock);
543 v->___objlocation___=v;
548 /* Array allocation function */
550 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
552 int basesize=length*classsize[type];
553 //round the base size up
554 basesize=(basesize+LOWMASK)&HIGHMASK;
555 int numlocks=basesize>>INDEXSHIFT;
556 int bookkeepsize=(numlocks)*2*sizeof(int);
557 int *tmpint=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+bookkeepsize);
558 for(;numlocks>0;numlocks--) {
562 objheader_t *tmp=(objheader_t *)tmpint;
563 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
565 v->lowindex=MAXARRAYSIZE;
567 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
568 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
571 tmp->lock=RW_LOCK_BIAS;
573 initdsmlocks(&tmp->lock);
578 printf("ERROR: negative array %d\n", length);
581 v->___objlocation___=(struct ___Object___ *)v;
582 v->___length___=length;
588 #if defined(PRECISE_GC)
589 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
590 struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
603 /* Array allocation function */
605 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
606 struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
609 printf("ERROR: negative array\n");
612 v->___length___=length;
625 __attribute__((malloc)) void * allocate_new(int type) {
626 struct ___Object___ * v=FREEMALLOC(classsize[type]);
634 /* Array allocation function */
636 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
637 __attribute__((malloc)) struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
639 v->___length___=length;
648 /* Converts C character arrays into Java strings */
650 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
652 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
656 struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
657 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
658 struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
659 chararray=(struct ArrayObject *) ptrarray[2];
661 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
662 struct ___String___ * strobj=allocate_new(STRINGTYPE);
664 strobj->___value___=chararray;
665 strobj->___count___=length;
666 strobj->___offset___=0;
668 for(i=0; i<length; i++) {
669 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
674 /* Generated code calls this if we fail a bounds check */
676 void failedboundschk() {
678 printf("Array out of bounds\n");
685 longjmp(error_handler,2);
689 /* Abort task call */
692 longjmp(error_handler,4);
694 printf("Aborting\n");
700 #ifdef D___System______Assert____Z
701 void CALL11(___System______Assert____Z, int ___status___, int ___status___) {
703 printf("Assertion violation\n");
704 *((int *)(NULL)); //force stack trace error