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___String______convertdoubletochar____D__AR_C
144 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
145 int length=VAR(___chararray___)->___length___;
148 int num=snprintf(str, length, "%f",___val___);
151 for(i=0; i<length; i++) {
152 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
157 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
158 void deepArrayCopy(struct ___Object___ * dst, struct ___Object___ * src) {
159 int dsttype=((int *)dst)[0];
160 int srctype=((int *)src)[0];
161 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
163 struct ArrayObject *aodst=(struct ArrayObject *)dst;
164 struct ArrayObject *aosrc=(struct ArrayObject *)src;
165 int dstlength=aodst->___length___;
166 int srclength=aosrc->___length___;
167 if (dstlength!=srclength)
169 unsigned INTPTR *pointer=pointerarray[srctype];
171 int elementsize=classsize[srctype];
172 int size=srclength*elementsize;
174 memcpy(((char *)&aodst->___length___)+sizeof(int) , ((char *)&aosrc->___length___)+sizeof(int), size);
178 for(i=0;i<srclength;i++) {
179 struct ___Object___ * ptr=((struct ___Object___**)(((char*) &aosrc->___length___)+sizeof(int)))[i];
180 int ptrtype=((int *)ptr)[0];
181 if (ptrtype>=NUMCLASSES) {
182 struct ___Object___ * dstptr=((struct ___Object___**)(((char*) &aodst->___length___)+sizeof(int)))[i];
183 deepArrayCopy(dstptr,ptr);
186 ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
192 void CALL02(___System______deepArrayCopy____L___Object____L___Object___, struct ___Object___ * ___dst___, struct ___Object___ * ___src___) {
193 deepArrayCopy(VAR(___dst___), VAR(___src___));
197 void CALL11(___System______exit____I,int ___status___, int ___status___) {
199 printf("numTransCommit = %d\n", numTransCommit);
200 printf("numTransAbort = %d\n", numTransAbort);
201 printf("nSoftAbort = %d\n", nSoftAbort);
203 printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
204 printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
207 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
208 printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
216 #if defined(__i386__)
218 static __inline__ unsigned long long rdtsc(void)
220 unsigned long long int x;
221 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
224 #elif defined(__x86_64__)
226 static __inline__ unsigned long long rdtsc(void)
229 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
230 return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
233 #elif defined(__powerpc__)
235 typedef unsigned long long int unsigned long long;
237 static __inline__ unsigned long long rdtsc(void)
239 unsigned long long int result=0;
240 unsigned long int upper, lower,tmp;
248 : "=r"(upper),"=r"(lower),"=r"(tmp)
252 result = result|lower;
258 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
260 event[counter] = ___event___;
261 clkticks[counter] = rdtsc();
267 void CALL00(___System______logevent____) {
274 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
279 memset(filename, 0, 20);
280 sprintf(filename, "%s_%d", FILENAME, ___threadid___);
281 if ((fp = fopen(filename, "w+")) == NULL) {
286 for (i = 0; i < counter-1; i++) {
287 fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i]-beginClock, clkticks[i+1]-beginClock);
289 fprintf(fp, "%d %lld\n", event[i], clkticks[i]-beginClock);
296 void CALL00(___System______initLog____) {
300 for(i=0; i<ARRAY_LENGTH; i++) {
309 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
310 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
311 char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
312 memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
316 void CALL11(___System______printI____I,int ___status___, int ___status___) {
317 printf("%d\n",___status___);
320 long long CALL00(___System______currentTimeMillis____) {
321 struct timeval tv; long long retval;
322 gettimeofday(&tv, NULL);
323 retval = tv.tv_sec; /* seconds */
324 retval*=1000; /* milliseconds */
325 retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
329 long long CALL00(___System______microTimes____) {
332 gettimeofday(&tv, NULL);
333 retval = tv.tv_sec; /* seconds */
334 retval*=1000000; /* microsecs */
335 retval+= (tv.tv_usec); /* adjust microseconds & add them in */
339 long long CALL00(___System______getticks____) {
342 asm volatile("rdtsc" : "=a" (a), "=d" (d));
343 return (((ticks)a) | (((ticks)d) << 32));
346 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
347 struct ArrayObject * chararray=VAR(___s___)->___value___;
349 int offset=VAR(___s___)->___offset___;
350 for(i=0; i<VAR(___s___)->___count___; i++) {
351 short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
357 void CALL00(___System______clearPrefetchCache____) {
362 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
363 /* Manual Prefetches to be inserted */
364 //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
365 //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
366 int numoffset=VAR(___offsets___)->___length___;
368 short offArry[numoffset+2];
371 for(i = 2; i<(numoffset+2); i++) {
372 offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
373 //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
376 if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
377 oid = (unsigned int) VAR(___o___); //outside transaction therefore just an oid
379 oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
381 rangePrefetch(oid, (short)(numoffset+2), offArry);
384 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
389 #ifdef D___Task______execution____
390 extern void* virtualtable[];
391 // associated with Task.execution(). finds proper execute method and call it
392 void CALL01(___Task______execution____,struct ___Task___ * ___this___)
395 oid = (unsigned int) VAR(___this___); // object id
396 int type = getObjType(oid); // object type
399 int p[] = {1,0 , oid};
401 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(p);
403 // call the proper execute method
404 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(oid);
411 /* STM Barrier constructs */
412 #ifdef D___Barrier______setBarrier____I
413 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
414 // Barrier initialization
416 if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
417 printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
423 #ifdef D___Barrier______enterBarrier____
424 void CALL00(___Barrier______enterBarrier____) {
425 // Synchronization point
428 stopforgc((struct garbagelist *)___params___);
430 ret = pthread_barrier_wait(&barrier);
434 if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
435 printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
441 /* Object allocation function */
444 __attribute__((malloc)) void * allocate_newglobal(int type) {
445 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
447 //printf("DEBUG %s(), type= %x\n", __func__, type);
456 /* Array allocation function */
458 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
459 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
461 printf("ERROR: negative array\n");
465 v->___length___=length;
477 // STM Versions of allocation functions
479 /* Object allocation function */
480 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
482 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type], 0);
484 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
487 v->___objlocation___=v;
491 /* Array allocation function */
492 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
494 int basesize=length*classsize[type];
495 //round the base size up
496 basesize=(basesize+LOWMASK)&HIGHMASK;
497 int numlocks=basesize>>INDEXSHIFT;
498 int bookkeepsize=numlocks*2*sizeof(int);
499 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+basesize+bookkeepsize, bookkeepsize);
500 unsigned int *intptr=(unsigned int *)(((char *)v)-sizeof(objheader_t));
501 for(;numlocks>0;numlocks--) {
506 v->lowindex=MAXARRAYSIZE;
508 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
511 printf("ERROR: negative array\n");
514 v->___objlocation___=(struct ___Object___*)v;
516 v->___length___=length;
520 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
521 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
522 struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
523 initdsmlocks(&tmp->lock);
525 v->___objlocation___=v;
530 /* Array allocation function */
532 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
534 int basesize=length*classsize[type];
535 //round the base size up
536 basesize=(basesize+LOWMASK)&HIGHMASK;
537 int numlocks=basesize>>INDEXSHIFT;
538 int bookkeepsize=(numlocks)*2*sizeof(int);
539 int *tmpint=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+bookkeepsize);
540 for(;numlocks>0;numlocks--) {
544 objheader_t *tmp=(objheader_t *)tmpint;
545 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
547 v->lowindex=MAXARRAYSIZE;
549 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
550 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
552 initdsmlocks(&tmp->lock);
556 printf("ERROR: negative array %d\n", length);
559 v->___objlocation___=(struct ___Object___ *)v;
560 v->___length___=length;
566 #if defined(PRECISE_GC)
567 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
568 struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
581 /* Array allocation function */
583 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
584 struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
587 printf("ERROR: negative array\n");
590 v->___length___=length;
603 __attribute__((malloc)) void * allocate_new(int type) {
604 struct ___Object___ * v=FREEMALLOC(classsize[type]);
612 /* Array allocation function */
614 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
615 __attribute__((malloc)) struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
617 v->___length___=length;
626 /* Converts C character arrays into Java strings */
628 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
630 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
634 struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
635 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
636 struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
637 chararray=(struct ArrayObject *) ptrarray[2];
639 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
640 struct ___String___ * strobj=allocate_new(STRINGTYPE);
642 strobj->___value___=chararray;
643 strobj->___count___=length;
644 strobj->___offset___=0;
646 for(i=0; i<length; i++) {
647 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
652 /* Generated code calls this if we fail a bounds check */
654 void failedboundschk() {
656 printf("Array out of bounds\n");
663 longjmp(error_handler,2);
667 /* Abort task call */
670 longjmp(error_handler,4);
672 printf("Aborting\n");