2 #include "structdefs.h"
9 #include "methodheaders.h"
12 #include "prelookup.h"
21 __thread int event[100000*7+3];
22 __thread unsigned long long clkticks[100000*7+3];
23 #define FILENAME "log"
26 #if defined(THREADS)||defined(STM)
27 /* Global barrier for STM */
28 pthread_barrier_t barrier;
29 pthread_barrierattr_t attr;
34 extern int classsize[];
35 extern int typearray[];
36 extern int typearray2[];
37 jmp_buf error_handler;
42 float failurechance=0;
45 int injectinstructionfailures;
47 float instfailurechance=0;
50 typedef unsigned long long ticks;
55 int instanceof(struct ___Object___ *ptr, int type) {
67 i=typearray2[i-NUMCLASSES];
73 void exithandler(int sig, siginfo_t *info, void * uap) {
77 void initializeexithandler() {
79 sig.sa_sigaction=&exithandler;
80 sig.sa_flags=SA_SIGINFO;
81 sigemptyset(&sig.sa_mask);
82 sigaction(SIGUSR2, &sig, 0);
86 /* This function inject failures */
88 void injectinstructionfailure() {
90 if (injectinstructionfailures) {
93 instructioncount=failurecount;
94 instaccum+=failurecount;
95 if ((((double)random())/RAND_MAX)<instfailurechance) {
98 printf("FAILURE!!! %d\n",numfailures);
99 longjmp(error_handler,11);
104 if (injectinstructionfailures) {
107 instaccum+=failurecount;
108 if ((((double)random())/RAND_MAX)<instfailurechance) {
111 printf("FAILURE!!! %d\n",numfailures);
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___;
126 int offset=VAR(___str___)->___offset___;
127 for(i=0; i<maxlength; i++) {
128 str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
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___;
141 int num=snprintf(str, length, "%f",___val___);
144 for(i=0; i<length; i++) {
145 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
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)
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)
162 unsigned INTPTR *pointer=pointerarray[srctype];
164 int elementsize=classsize[srctype];
165 int size=srclength*elementsize;
167 memcpy(((char *)&aodst->___length___)+sizeof(int) , ((char *)&aosrc->___length___)+sizeof(int), size);
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);
179 ((struct ___Object___ **)(((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
185 void CALL02(___System______deepArrayCopy____L___Object____L___Object___, struct ___Object___ * ___dst___, struct ___Object___ * ___src___) {
186 deepArrayCopy(VAR(___dst___), VAR(___src___));
190 void CALL11(___System______exit____I,int ___status___, int ___status___) {
192 printf("numTransCommit = %d\n", numTransCommit);
193 printf("numTransAbort = %d\n", numTransAbort);
194 printf("nSoftAbort = %d\n", nSoftAbort);
196 printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
197 printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
200 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
201 printf("typesCausingAbort[%2d] numaccess= %5d numabort= %3d\n", i, typesCausingAbort[i].numaccess, typesCausingAbort[i].numabort);
209 #if defined(__i386__)
211 static __inline__ unsigned long long rdtsc(void)
213 unsigned long long int x;
214 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
217 #elif defined(__x86_64__)
219 static __inline__ unsigned long long rdtsc(void)
222 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
223 return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
226 #elif defined(__powerpc__)
228 typedef unsigned long long int unsigned long long;
230 static __inline__ unsigned long long rdtsc(void)
232 unsigned long long int result=0;
233 unsigned long int upper, lower,tmp;
241 : "=r"(upper),"=r"(lower),"=r"(tmp)
245 result = result|lower;
251 void CALL11(___System______logevent____I,int ___event___, int ___event___) {
253 event[counter] = ___event___;
254 clkticks[counter] = rdtsc();
260 void CALL11(___System______flushToFile____I, int ___threadid___, int ___threadid___) {
265 memset(filename, 0, 20);
266 sprintf(filename, "%s_%d", FILENAME, ___threadid___);
267 if ((fp = fopen(filename, "w+")) == NULL) {
272 for (i = 0; i < counter-1; i++) {
273 fprintf(fp, "%d %lld %lld\n", event[i], clkticks[i], clkticks[i+1]);
275 fprintf(fp, "%d %lld\n", event[i], clkticks[i]);
282 void CALL00(___System______initLog____) {
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 *));
296 void CALL11(___System______printI____I,int ___status___, int ___status___) {
297 printf("%d\n",___status___);
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 */
309 long long CALL00(___System______microTimes____) {
312 gettimeofday(&tv, NULL);
313 retval = tv.tv_sec; /* seconds */
314 retval*=1000000; /* microsecs */
315 retval+= (tv.tv_usec); /* adjust microseconds & add them in */
319 long long CALL00(___System______getticks____) {
322 asm volatile("rdtsc" : "=a" (a), "=d" (d));
323 return (((ticks)a) | (((ticks)d) << 32));
326 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
327 struct ArrayObject * chararray=VAR(___s___)->___value___;
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];
337 void CALL00(___System______clearPrefetchCache____) {
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___;
348 short offArry[numoffset+2];
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]);
356 if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
357 oid = (unsigned int) VAR(___o___); //outside transaction therefore just an oid
359 oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
361 rangePrefetch(oid, (short)(numoffset+2), offArry);
364 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
371 /* STM Barrier constructs */
372 #ifdef D___Barrier______setBarrier____I
373 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
374 // Barrier initialization
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__);
383 #ifdef D___Barrier______enterBarrier____
384 void CALL00(___Barrier______enterBarrier____) {
385 // Synchronization point
388 stopforgc((struct garbagelist *)___params___);
390 ret = pthread_barrier_wait(&barrier);
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__);
401 /* Object allocation function */
404 __attribute__((malloc)) void * allocate_newglobal(int type) {
405 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
415 /* Array allocation function */
417 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
418 struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
420 printf("ERROR: negative array\n");
424 v->___length___=length;
436 // STM Versions of allocation functions
438 /* Object allocation function */
439 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
440 struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
442 v->___objlocation___=v;
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]);
450 printf("ERROR: negative array\n");
453 v->___objlocation___=(struct ___Object___*)v;
455 v->___length___=length;
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);
463 v->___objlocation___=v;
468 /* Array allocation function */
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);
477 printf("ERROR: negative array %d\n", length);
480 v->___objlocation___=(struct ___Object___ *)v;
481 v->___length___=length;
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]);
502 /* Array allocation function */
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]);
508 printf("ERROR: negative array\n");
511 v->___length___=length;
524 __attribute__((malloc)) void * allocate_new(int type) {
525 struct ___Object___ * v=FREEMALLOC(classsize[type]);
533 /* Array allocation function */
535 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
536 __attribute__((malloc)) struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
538 v->___length___=length;
547 /* Converts C character arrays into Java strings */
549 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
551 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
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];
560 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
561 struct ___String___ * strobj=allocate_new(STRINGTYPE);
563 strobj->___value___=chararray;
564 strobj->___count___=length;
565 strobj->___offset___=0;
567 for(i=0; i<length; i++) {
568 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
573 /* Generated code calls this if we fail a bounds check */
575 void failedboundschk() {
577 printf("Array out of bounds\n");
584 longjmp(error_handler,2);
588 /* Abort task call */
591 longjmp(error_handler,4);
593 printf("Aborting\n");