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];
176 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
178 struct ArrayObject *aodst=(struct ArrayObject *)dst;
179 struct ArrayObject *aosrc=(struct ArrayObject *)src;
180 int dstlength=aodst->___length___;
181 int srclength=aosrc->___length___;
182 if (dstlength!=srclength)
184 unsigned INTPTR *pointer=pointerarray[srctype];
186 int elementsize=classsize[srctype];
187 int size=srclength*elementsize;
189 memcpy(((char *)&aodst->___length___)+sizeof(int) , ((char *)&aosrc->___length___)+sizeof(int), size);
193 for(i=0;i<srclength;i++) {
194 struct ___Object___ * ptr=((struct ___Object___**)(((char*) &aosrc->___length___)+sizeof(int)))[i];
195 int ptrtype=((int *)ptr)[0];
196 if (ptrtype>=NUMCLASSES) {
197 struct ___Object___ * dstptr=((struct ___Object___**)(((char*) &aodst->___length___)+sizeof(int)))[i];
198 deepArrayCopy(dstptr,ptr);
201 ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
207 void CALL02(___System______deepArrayCopy____L___Object____L___Object___, struct ___Object___ * ___dst___, struct ___Object___ * ___src___) {
208 deepArrayCopy(VAR(___dst___), VAR(___src___));
212 void CALL11(___System______exit____I,int ___status___, int ___status___) {
214 printf("numTransCommit = %d\n", numTransCommit);
215 printf("numTransAbort = %d\n", numTransAbort);
216 printf("nSoftAbort = %d\n", nSoftAbort);
218 printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
219 printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
222 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
223 printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
231 #if defined(__i386__)
233 static __inline__ unsigned long long rdtsc(void)
235 unsigned long long int x;
236 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
239 #elif defined(__x86_64__)
241 static __inline__ unsigned long long rdtsc(void)
244 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
245 return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
248 #elif defined(__powerpc__)
250 typedef unsigned long long int unsigned long long;
252 static __inline__ unsigned long long rdtsc(void)
254 unsigned long long int result=0;
255 unsigned long int upper, lower,tmp;
263 : "=r"(upper),"=r"(lower),"=r"(tmp)
267 result = result|lower;
273 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
275 event[counter] = ___event___;
276 clkticks[counter] = rdtsc();
282 void CALL00(___System______logevent____) {
289 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
294 memset(filename, 0, 20);
295 sprintf(filename, "%s_%d", FILENAME, ___threadid___);
296 if ((fp = fopen(filename, "w+")) == NULL) {
301 for (i = 0; i < counter-1; i++) {
302 fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i]-beginClock, clkticks[i+1]-beginClock);
304 fprintf(fp, "%d %lld\n", event[i], clkticks[i]-beginClock);
311 void CALL00(___System______initLog____) {
315 for(i=0; i<ARRAY_LENGTH; i++) {
324 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
325 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
326 char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
327 memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
331 void CALL11(___System______printI____I,int ___status___, int ___status___) {
332 printf("%d\n",___status___);
335 long long CALL00(___System______currentTimeMillis____) {
336 struct timeval tv; long long retval;
337 gettimeofday(&tv, NULL);
338 retval = tv.tv_sec; /* seconds */
339 retval*=1000; /* milliseconds */
340 retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
344 long long CALL00(___System______microTimes____) {
347 gettimeofday(&tv, NULL);
348 retval = tv.tv_sec; /* seconds */
349 retval*=1000000; /* microsecs */
350 retval+= (tv.tv_usec); /* adjust microseconds & add them in */
354 long long CALL00(___System______getticks____) {
357 asm volatile("rdtsc" : "=a" (a), "=d" (d));
358 return (((ticks)a) | (((ticks)d) << 32));
361 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
362 struct ArrayObject * chararray=VAR(___s___)->___value___;
364 int offset=VAR(___s___)->___offset___;
365 for(i=0; i<VAR(___s___)->___count___; i++) {
366 short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
372 void CALL00(___System______clearPrefetchCache____) {
377 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
378 /* Manual Prefetches to be inserted */
379 //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
380 //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
381 int numoffset=VAR(___offsets___)->___length___;
383 short offArry[numoffset+2];
386 for(i = 2; i<(numoffset+2); i++) {
387 offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
388 //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
391 if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
392 oid = (unsigned int) VAR(___o___); //outside transaction therefore just an oid
394 oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
396 rangePrefetch(oid, (short)(numoffset+2), offArry);
399 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
404 #ifdef D___Task______execution____
405 extern void* virtualtable[];
406 // associated with Task.execution(). finds proper execute method and call it
407 void CALL01(___Task______execution____,struct ___Task___ * ___this___)
410 oid = (unsigned int) VAR(___this___); // object id
411 int type = getObjType(oid); // object type
414 int p[] = {1,0 , oid};
416 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(p);
418 // call the proper execute method
419 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(oid);
426 /* STM Barrier constructs */
427 #ifdef D___Barrier______setBarrier____I
428 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
429 // Barrier initialization
431 if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
432 printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
438 #ifdef D___Barrier______enterBarrier____
439 void CALL00(___Barrier______enterBarrier____) {
440 // Synchronization point
443 stopforgc((struct garbagelist *)___params___);
445 ret = pthread_barrier_wait(&barrier);
449 if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
450 printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
456 /* Object allocation function */
459 __attribute__((malloc)) void * allocate_newglobal(int type) {
460 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
462 //printf("DEBUG %s(), type= %x\n", __func__, type);
471 /* Array allocation function */
473 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
474 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
476 printf("ERROR: negative array\n");
480 v->___length___=length;
492 // STM Versions of allocation functions
494 /* Object allocation function */
495 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
497 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type], 0);
499 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
502 v->___objlocation___=v;
506 /* Array allocation function */
507 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
509 int basesize=length*classsize[type];
510 //round the base size up
511 basesize=(basesize+LOWMASK)&HIGHMASK;
512 int numlocks=basesize>>INDEXSHIFT;
513 int bookkeepsize=numlocks*2*sizeof(int);
514 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+basesize+bookkeepsize, bookkeepsize);
515 unsigned int *intptr=(unsigned int *)(((char *)v)-sizeof(objheader_t));
516 for(;numlocks>0;numlocks--) {
521 v->lowindex=MAXARRAYSIZE;
523 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
526 printf("ERROR: negative array\n");
529 v->___objlocation___=(struct ___Object___*)v;
531 v->___length___=length;
535 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
536 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
537 struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
538 initdsmlocks(&tmp->lock);
540 v->___objlocation___=v;
545 /* Array allocation function */
547 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
549 int basesize=length*classsize[type];
550 //round the base size up
551 basesize=(basesize+LOWMASK)&HIGHMASK;
552 int numlocks=basesize>>INDEXSHIFT;
553 int bookkeepsize=(numlocks)*2*sizeof(int);
554 int *tmpint=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+bookkeepsize);
555 for(;numlocks>0;numlocks--) {
559 objheader_t *tmp=(objheader_t *)tmpint;
560 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
562 v->lowindex=MAXARRAYSIZE;
564 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
565 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
567 initdsmlocks(&tmp->lock);
571 printf("ERROR: negative array %d\n", length);
574 v->___objlocation___=(struct ___Object___ *)v;
575 v->___length___=length;
581 #if defined(PRECISE_GC)
582 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
583 struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
596 /* Array allocation function */
598 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
599 struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
602 printf("ERROR: negative array\n");
605 v->___length___=length;
618 __attribute__((malloc)) void * allocate_new(int type) {
619 struct ___Object___ * v=FREEMALLOC(classsize[type]);
627 /* Array allocation function */
629 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
630 __attribute__((malloc)) struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
632 v->___length___=length;
641 /* Converts C character arrays into Java strings */
643 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
645 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
649 struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
650 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
651 struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
652 chararray=(struct ArrayObject *) ptrarray[2];
654 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
655 struct ___String___ * strobj=allocate_new(STRINGTYPE);
657 strobj->___value___=chararray;
658 strobj->___count___=length;
659 strobj->___offset___=0;
661 for(i=0; i<length; i++) {
662 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
667 /* Generated code calls this if we fail a bounds check */
669 void failedboundschk() {
671 printf("Array out of bounds\n");
678 longjmp(error_handler,2);
682 /* Abort task call */
685 longjmp(error_handler,4);
687 printf("Aborting\n");
693 #ifdef D___System______Assert____Z
694 void CALL11(___System______Assert____Z, int ___status___, int ___status___) {
696 printf("Assertion violation\n");
697 *((int *)(NULL)); //force stack trace error