2 #include "structdefs.h"
9 #include "methodheaders.h"
12 #include "DSTM/interface_recovery/dstm.h"
13 #include "DSTM/interface_recovery/prelookup.h"
16 extern int numRecovery;
17 extern unsigned int deadMachine[8];
18 extern unsigned int sizeOfRedupedData[8];
19 extern double elapsedTime[8];
23 #include "DSTM/interface/dstm.h"
24 #include "DSTM/interface/altprelookup.h"
25 #include "DSTM/interface/prefetch.h"
33 #define ARRAY_LENGTH 700003
35 __thread int event[ARRAY_LENGTH];
36 __thread unsigned long long clkticks[ARRAY_LENGTH];
37 unsigned long long beginClock=0;
38 #define FILENAME "log"
42 __thread int objcount=0;
43 #define ASSIGNUID(x) { \
44 int number=((objcount++)<<EVTHREADSHIFT)|threadnum; \
51 #if defined(THREADS)||defined(STM)
52 /* Global barrier for STM */
53 pthread_barrier_t barrier;
54 pthread_barrierattr_t attr;
59 extern int classsize[];
60 extern int typearray[];
61 extern int typearray2[];
62 jmp_buf error_handler;
67 float failurechance=0;
70 int injectinstructionfailures;
72 float instfailurechance=0;
75 typedef unsigned long long ticks;
80 int instanceof(struct ___Object___ *ptr, int type) {
92 i=typearray2[i-NUMCLASSES];
98 void exithandler(int sig, siginfo_t *info, void * uap) {
102 void initializeexithandler() {
103 struct sigaction sig;
104 sig.sa_sigaction=&exithandler;
105 sig.sa_flags=SA_SIGINFO;
106 sigemptyset(&sig.sa_mask);
107 sigaction(SIGUSR2, &sig, 0);
111 /* This function inject failures */
113 void injectinstructionfailure() {
115 if (injectinstructionfailures) {
118 instructioncount=failurecount;
119 instaccum+=failurecount;
120 if ((((double)random())/RAND_MAX)<instfailurechance) {
123 printf("FAILURE!!! %d\n",numfailures);
124 longjmp(error_handler,11);
129 if (injectinstructionfailures) {
132 instaccum+=failurecount;
133 if ((((double)random())/RAND_MAX)<instfailurechance) {
136 printf("FAILURE!!! %d\n",numfailures);
144 #ifdef D___Double______nativeparsedouble____L___String___
145 double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
146 int length=VAR(___str___)->___count___;
147 int maxlength=(length>60)?60:length;
148 char str[maxlength+1];
149 struct ArrayObject * chararray=VAR(___str___)->___value___;
151 int offset=VAR(___str___)->___offset___;
152 for(i=0; i<maxlength; i++) {
153 str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
161 #ifdef D___Double______nativeparsedouble_____AR_B_I_I
162 double CALL23(___Double______nativeparsedouble_____AR_B_I_I, int start, int length,int start,int length,struct ArrayObject * ___str___) {
163 int maxlength=(length>60)?60:length;
164 char str[maxlength+1];
165 struct ArrayObject * bytearray=VAR(___str___);
167 for(i=0; i<maxlength; i++) {
168 str[i]=(((char *)&bytearray->___length___)+sizeof(int))[i+start];
176 #ifdef D___String______convertdoubletochar____D__AR_C
177 int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
178 int length=VAR(___chararray___)->___length___;
181 int num=snprintf(str, length, "%f",___val___);
184 for(i=0; i<length; i++) {
185 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
190 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
191 void deepArrayCopy(struct ___Object___ * dst, struct ___Object___ * src) {
192 int dsttype=((int *)dst)[0];
193 int srctype=((int *)src)[0];
195 src=src->___objlocation___;
197 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
199 struct ArrayObject *aodst=(struct ArrayObject *)dst;
200 struct ArrayObject *aosrc=(struct ArrayObject *)src;
201 int dstlength=aodst->___length___;
202 int srclength=aosrc->___length___;
203 if (dstlength!=srclength)
205 unsigned INTPTR *pointer=pointerarray[srctype];
207 int elementsize=classsize[srctype];
208 int size=srclength*elementsize;
210 memcpy(((char *)&aodst->___length___)+sizeof(int) , ((char *)&aosrc->___length___)+sizeof(int), size);
214 for(i=0;i<srclength;i++) {
215 struct ___Object___ * ptr=((struct ___Object___**)(((char*) &aosrc->___length___)+sizeof(int)))[i];
216 int ptrtype=((int *)ptr)[0];
217 if (ptrtype>=NUMCLASSES) {
218 struct ___Object___ * dstptr=((struct ___Object___**)(((char*) &aodst->___length___)+sizeof(int)))[i];
219 deepArrayCopy(dstptr,ptr);
222 ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
228 void CALL02(___System______deepArrayCopy____L___Object____L___Object___, struct ___Object___ * ___dst___, struct ___Object___ * ___src___) {
229 deepArrayCopy(VAR(___dst___), VAR(___src___));
233 void CALL11(___System______exit____I,int ___status___, int ___status___) {
235 printf("numTransCommit = %d\n", numTransCommit);
236 printf("numTransAbort = %d\n", numTransAbort);
237 printf("nSoftAbort = %d\n", nSoftAbort);
239 printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
240 printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
243 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
244 printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
255 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
257 event[counter] = ___event___;
258 clkticks[counter] = rdtsc();
264 void CALL00(___System______logevent____) {
271 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
276 memset(filename, 0, 20);
277 sprintf(filename, "%s_%d", FILENAME, ___threadid___);
278 if ((fp = fopen(filename, "w+")) == NULL) {
283 for (i = 0; i < counter-1; i++) {
284 fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i]-beginClock, clkticks[i+1]-beginClock);
286 fprintf(fp, "%d %lld\n", event[i], clkticks[i]-beginClock);
293 void CALL00(___System______initLog____) {
297 for(i=0; i<ARRAY_LENGTH; i++) {
306 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
307 void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
308 char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
309 memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
313 void CALL11(___System______printI____I,int ___status___, int ___status___) {
314 printf("%d\n",___status___);
317 long long CALL00(___System______currentTimeMillis____) {
318 struct timeval tv; long long retval;
319 gettimeofday(&tv, NULL);
320 retval = tv.tv_sec; /* seconds */
321 retval*=1000; /* milliseconds */
322 retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
326 long long CALL00(___System______microTimes____) {
329 gettimeofday(&tv, NULL);
330 retval = tv.tv_sec; /* seconds */
331 retval*=1000000; /* microsecs */
332 retval+= (tv.tv_usec); /* adjust microseconds & add them in */
336 long long CALL00(___System______getticks____) {
339 asm volatile("rdtsc" : "=a" (a), "=d" (d));
340 return (((ticks)a) | (((ticks)d) << 32));
343 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
344 struct ArrayObject * chararray=VAR(___s___)->___value___;
346 int offset=VAR(___s___)->___offset___;
347 for(i=0; i<VAR(___s___)->___count___; i++) {
348 short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
357 #ifdef D___RecoveryStat______printRecoveryStat____
359 void CALL00(___RecoveryStat______printRecoveryStat____) {
361 printf("***** Recovery Stats *****\n");
362 printf("numRecovery = %d\n",numRecovery);
364 for(i=0; i < numRecovery;i++) {
365 printf("Dead Machine = %s\n",midtoIPString(deadMachine[i]));
366 printf("Recovery Time = %.2f\n",elapsedTime[i]);
368 printf("**************************\n\n");
371 void CALL00(___RecoveryStat______printRecoveryStat____) {
380 void CALL00(___System______clearPrefetchCache____) {
385 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
386 /* Manual Prefetches to be inserted */
387 //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
388 //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
389 int numoffset=VAR(___offsets___)->___length___;
391 short offArry[numoffset+2];
394 for(i = 2; i<(numoffset+2); i++) {
395 offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
396 //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
399 if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
400 oid = (unsigned int) VAR(___o___); //outside transaction therefore just an oid
402 oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
404 rangePrefetch(oid, (short)(numoffset+2), offArry);
407 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
412 #ifdef D___Task______execution____
413 extern void* virtualtable[];
414 // associated with Task.execution(). finds proper execute method and call it
415 void CALL01(___Task______execution____,struct ___Task___ * ___this___)
418 oid = (unsigned int) VAR(___this___); // object id
419 int type = getObjType(oid); // object type
422 int p[] = {1,0 , oid};
424 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(p);
426 // call the proper execute method
427 ((void(*) (void *))virtualtable[type*MAXCOUNT + EXECUTEMETHOD])(oid);
434 /* STM Barrier constructs */
435 #ifdef D___Barrier______setBarrier____I
436 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
437 // Barrier initialization
439 if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
440 printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
446 #ifdef D___Barrier______enterBarrier____
447 void CALL00(___Barrier______enterBarrier____) {
448 // Synchronization point
451 EVLOGEVENT(EV_ENTERBARRIER);
454 stopforgc((struct garbagelist *)___params___);
456 ret = pthread_barrier_wait(&barrier);
460 if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
461 printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
465 EVLOGEVENT(EV_EXITBARRIER);
470 /* Object allocation function */
473 __attribute__((malloc)) void * allocate_newglobal(int type) {
474 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
476 //printf("DEBUG %s(), type= %x\n", __func__, type);
485 /* Array allocation function */
487 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
488 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
490 printf("ERROR: negative array\n");
494 v->___length___=length;
506 // STM Versions of allocation functions
508 /* Object allocation function */
509 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
511 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type], 0);
513 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
517 v->___objlocation___=v;
521 /* Array allocation function */
522 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
524 int basesize=length*classsize[type];
525 //round the base size up
526 basesize=(basesize+LOWMASK)&HIGHMASK;
527 int numlocks=basesize>>INDEXSHIFT;
528 int bookkeepsize=numlocks*2*sizeof(int);
529 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+basesize+bookkeepsize, bookkeepsize);
530 unsigned int *intptr=(unsigned int *)(((char *)v)-sizeof(objheader_t));
531 for(;numlocks>0;numlocks--) {
536 v->lowindex=MAXARRAYSIZE;
538 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
542 printf("ERROR: negative array\n");
545 v->___objlocation___=(struct ___Object___*)v;
547 v->___length___=length;
551 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
552 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
553 struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
555 initdsmlocks(&tmp->lock);
557 v->___objlocation___=v;
562 /* Array allocation function */
564 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
566 int basesize=length*classsize[type];
567 //round the base size up
568 basesize=(basesize+LOWMASK)&HIGHMASK;
569 int numlocks=basesize>>INDEXSHIFT;
570 int bookkeepsize=(numlocks)*2*sizeof(int);
571 int *tmpint=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+bookkeepsize);
572 for(;numlocks>0;numlocks--) {
576 objheader_t *tmp=(objheader_t *)tmpint;
577 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
579 v->lowindex=MAXARRAYSIZE;
581 objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
582 struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
585 tmp->lock=RW_LOCK_BIAS;
587 initdsmlocks(&tmp->lock);
593 printf("ERROR: negative array %d\n", length);
596 v->___objlocation___=(struct ___Object___ *)v;
597 v->___length___=length;
603 #if defined(PRECISE_GC)
604 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
605 struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
618 /* Array allocation function */
620 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
621 struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
624 printf("ERROR: negative array\n");
627 v->___length___=length;
640 __attribute__((malloc)) void * allocate_new(int type) {
641 struct ___Object___ * v=FREEMALLOC(classsize[type]);
649 /* Array allocation function */
651 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
652 __attribute__((malloc)) struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
654 v->___length___=length;
663 /* Converts C character arrays into Java strings */
665 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
667 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
671 struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
672 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
673 struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
674 chararray=(struct ArrayObject *) ptrarray[2];
676 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
677 struct ___String___ * strobj=allocate_new(STRINGTYPE);
679 strobj->___value___=chararray;
680 strobj->___count___=length;
681 strobj->___offset___=0;
683 for(i=0; i<length; i++) {
684 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
689 /* Generated code calls this if we fail a bounds check */
691 void failedboundschk() {
693 printf("Array out of bounds\n");
700 longjmp(error_handler,2);
704 /* Abort task call */
707 longjmp(error_handler,4);
709 printf("Aborting\n");
715 #ifdef D___System______Assert____Z
716 void CALL11(___System______Assert____Z, int ___status___, int ___status___) {
718 printf("Assertion violation\n");
719 *((int *)(NULL)); //force stack trace error