3 #include "multicoreruntime.h"
4 #include "methodheaders.h"
5 #include "multicoregarbage.h"
7 #include "multicoregcprofile.h"
8 #include "multicoregc.h"
9 #include "pmc_garbage.h"
11 #include "multicore_arch.h"
14 #include "bme_perf_counter.h"
20 extern int classsize[];
21 extern int typearray[];
22 extern int typearray2[];
23 extern int* supertypes[];
26 extern struct genhashtable * activetasks;
33 int instanceofif(int otype, int type) {
40 int num = supertypes[otype][0];
41 for(int i = 1; i < num + 1; i++) {
42 int t = supertypes[otype][i];
43 if(instanceofif(t, type) == 1) {
50 int instanceof(struct ___Object___ *ptr, int type) {
55 if(instanceofif(i, type) == 1) {
62 i=typearray2[i-NUMCLASSES];
68 void initializeexithandler() {
71 /* This function inject failures */
72 void injectinstructionfailure() {
73 // not supported in MULTICORE version
77 #ifdef D___Double______nativeparsedouble____L___String___
78 double CALL01(___Double______nativeparsedouble____L___String___,
79 struct ___String___ * ___str___) {
80 int length=VAR(___str___)->___count___;
81 int maxlength=(length>60) ? 60 : length;
82 char str[maxlength+1];
83 struct ArrayObject * chararray=VAR(___str___)->___value___;
85 int offset=VAR(___str___)->___offset___;
86 for(i=0; i<maxlength; i++) {
88 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
91 double d=0.0; //atof(str); TODO Unimplemented nativeparsedoulbe
96 #ifdef D___Double______nativeparsedouble_____AR_B_I_I
97 double CALL23(___Double______nativeparsedouble_____AR_B_I_I,
102 struct ArrayObject * ___str___) {
103 int maxlength=(length>60)?60:length;
104 char str[maxlength+1];
105 struct ArrayObject * bytearray=VAR(___str___);
107 for(i=0; i<maxlength; i++) {
108 str[i]=(((char *)&bytearray->___length___)+sizeof(int))[i+start];
111 double d=0.0; //atof(str); TODO Unimplemented nativeparsedouble
116 typedef union jvalue {
126 #ifdef D___Double______doubleToRawLongBits____D
127 long long CALL11(___Double______doubleToRawLongBits____D,
129 double ___value___) {
133 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
134 /* On little endian ARM processors when using FPA, word order of
135 doubles is still big endian. So take that into account here. When
136 using VFP, word order of doubles follows byte order. */
137 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
138 val.j = SWAP_DOUBLE(val.j);
145 #ifdef D___Double______longBitsToDouble____J
146 double CALL11(___Double______longBitsToDouble____J,
147 long long ___bits___,
148 long long ___bits___) {
152 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
154 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
156 val.j = SWAP_DOUBLE(val.j);
163 #ifdef D___String______convertdoubletochar____D__AR_C
164 int CALL12(___String______convertdoubletochar____D__AR_C,
167 struct ArrayObject * ___chararray___) {
168 int length=VAR(___chararray___)->___length___;
171 int num=snprintf(str, length, "%f",___val___);
174 for(i=0; i<length; i++) {
175 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=
182 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
183 void deepArrayCopy(struct ___Object___ * dst,
184 struct ___Object___ * src) {
185 int dsttype=((int *)dst)[0];
186 int srctype=((int *)src)[0];
187 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
189 struct ArrayObject *aodst=(struct ArrayObject *)dst;
190 struct ArrayObject *aosrc=(struct ArrayObject *)src;
191 int dstlength=aodst->___length___;
192 int srclength=aosrc->___length___;
193 if (dstlength!=srclength)
195 unsigned INTPTR *pointer=pointerarray[srctype];
197 int elementsize=classsize[srctype];
198 int size=srclength*elementsize;
200 memcpy(((char *)&aodst->___length___)+sizeof(int) ,
201 ((char *)&aosrc->___length___)+sizeof(int), size);
205 for(i=0;i<srclength;i++) {
206 struct ___Object___ * ptr=
207 ((struct ___Object___**)(((char*)&aosrc->___length___)+sizeof(int)))[i];
208 int ptrtype=((int *)ptr)[0];
209 if (ptrtype>=NUMCLASSES) {
210 struct ___Object___ * dstptr=((struct ___Object___**)
211 (((char*)&aodst->___length___)+sizeof(int)))[i];
212 deepArrayCopy(dstptr,ptr);
215 ((struct ___Object___ **)
216 (((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
222 void CALL02(___System______deepArrayCopy____L___Object____L___Object___,
223 struct ___Object___ * ___dst___,
224 struct ___Object___ * ___src___) {
225 deepArrayCopy(VAR(___dst___), VAR(___src___));
229 #ifdef D___System______arraycopy____L___Object____I_L___Object____I_I
230 void arraycopy(struct ___Object___ *src,
232 struct ___Object___ *dst,
235 int dsttype=((int *)dst)[0];
236 int srctype=((int *)src)[0];
238 //not an array or type mismatch
239 if (dsttype<NUMCLASSES||srctype<NUMCLASSES/*||srctype!=dsttype*/)
242 struct ArrayObject *aodst=(struct ArrayObject *)dst;
243 struct ArrayObject *aosrc=(struct ArrayObject *)src;
244 int dstlength=aodst->___length___;
245 int srclength=aosrc->___length___;
249 if (srcPos+length>srclength)
251 if (destPos+length>dstlength)
254 unsigned INTPTR *pointer=pointerarray[srctype];
256 int elementsize=classsize[srctype];
257 int size=length*elementsize;
259 memcpy(((char *)&aodst->___length___)+sizeof(int)+destPos*elementsize,
260 ((char *)&aosrc->___length___)+sizeof(int)+srcPos*elementsize, size);
264 for(i=0;i<length;i++) {
265 struct ___Object___ * ptr=((struct ___Object___**)
266 (((char*)&aosrc->___length___)+sizeof(int)))[i+srcPos];
267 int ptrtype=((int *)ptr)[0];
269 ((struct ___Object___ **)
270 (((char*) &aodst->___length___)+sizeof(int)))[i+destPos]=ptr;
275 void CALL35(___System______arraycopy____L___Object____I_L___Object____I_I,
279 struct ___Object___ * ___src___,
281 struct ___Object___ * ___dst___,
284 arraycopy(VAR(___src___), ___srcPos___, VAR(___dst___), ___destPos___,
289 #ifdef D___System______exit____I
290 void CALL11(___System______exit____I,
293 // gc_profile mode, output gc prfiling data
294 #if defined(MULTICORE_GC)||defined(PMC_GC)
295 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
296 BAMBOO_PRINT(BAMBOO_GET_EXE_TIME());
297 BAMBOO_PRINT(0xbbbbbbbb);
298 CACHEADAPT_DISABLE_TIMER();
299 GC_OUTPUT_PROFILE_DATA();
303 gc_outputProfileDataReadable();
304 tprintf("FINISH_EXECUTION\n");
307 BAMBOO_EXIT_APP(___status___);
311 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
312 void CALL23(___Vector______removeElement_____AR_L___Object____I_I,
315 struct ArrayObject * ___array___,
318 char* offset=((char *)(&VAR(___array___)->___length___))
319 +sizeof(unsigned int)+sizeof(void *)*___index___;
320 memmove(offset, offset+sizeof(void *),
321 (___size___-___index___-1)*sizeof(void *));
325 #ifdef D___System______printI____I
326 void CALL11(___System______printI____I,
329 BAMBOO_PRINT(0x1111);
330 BAMBOO_PRINT_REG(___status___);
334 #ifdef D___System______currentTimeMillis____
335 long long CALL00(___System______currentTimeMillis____) {
336 //TilePro64 is 700mHz
337 return ((unsigned long long)BAMBOO_GET_EXE_TIME())/700000;
341 #ifdef D___System______numGCs____
342 long long ___System______numGCs____(struct ___System______numGCs_____params * ___params___) {
351 #ifdef D___System______milliGcTime____
352 long long CALL00(___System______milliGcTime____) {
354 return GCtime/700000;
361 #ifdef D___System______nanoTime____
362 long long CALL00(___System______nanoTime____) {
363 //TilePro64 is 700mHz
364 return ((unsigned long long)BAMBOO_GET_EXE_TIME())/700;
368 #ifdef D___System______setgcprofileflag____
369 void CALL00(___System______setgcprofileflag____) {
372 extern volatile bool gc_profile_flag;
373 gc_profile_flag = true;
379 #ifdef D___System______resetgcprofileflag____
380 void CALL00(___System______resetgcprofileflag____) {
383 extern volatile bool gc_profile_flag;
384 gc_profile_flag = false;
390 #ifdef D___System______gc____
391 void CALL00(___System______gc____) {
393 if(BAMBOO_NUM_OF_CORE == STARTUPCORE) {
394 if(!gc_status_info.gcprocessing && !gcflag) {
397 for(int i = 0; i < NUMCORESACTIVE; i++) {
398 // reuse the gcnumsendobjs & gcnumreceiveobjs
399 gcnumsendobjs[0][i] = 0;
400 gcnumreceiveobjs[0][i] = 0;
402 for(int i = 0; i < NUMCORES4GC; i++) {
403 if(i != STARTUPCORE) {
404 send_msg_1(i,GCSTARTPRE);
409 // send msg to the startup core to start gc
410 send_msg_1(STARTUPCORE, GCINVOKE);
416 #ifdef D___System______printString____L___String___
417 void CALL01(___System______printString____L___String___, struct ___String___ * ___s___) {
418 #if defined(MGC)&&defined(TILERA_BME)
419 struct ArrayObject * chararray=VAR(___s___)->___value___;
421 int offset=VAR(___s___)->___offset___;
423 for(i=0; i<VAR(___s___)->___count___; i++) {
425 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
432 #ifdef D___Scanner______next____
433 struct ArrayObject * CALL01(___Scanner______next____, struct ___Scanner___ * ___this___) {
434 int pos = VAR(___this___)->___currentpos___;
435 #if defined(MULTICORE_GC)||defined(PMC_GC)
436 struct ArrayObject * result= allocate_newarray(NULL, CHARARRAYTYPE, 10);
438 struct ArrayObject * result=allocate_newarray(CHARARRAYTYPE, 10);
441 while(true) { //(VAR(___this___)->___sourcename___[pos]==' ')||(VAR(___this___)->___sourcename___[pos]=='\n')){
445 ((short *)(((char *)&result->___length___)+sizeof(int)))[i++]='\0';//(short)VAR(___this___)->___sourcename___[pos++]; // TODO
446 }while(true);//(VAR(___this___)->___sourcename___[pos]!=' ')&&(VAR(___this___)->___sourcename___[pos]!='\n'));
447 VAR(___this___)->___currentpos___ = pos;
452 /* Object allocation function */
454 #if defined(MULTICORE_GC)||defined(PMC_GC)
455 void * allocate_new(void * ptr,
457 struct ___Object___ * v=
458 (struct ___Object___*)FREEMALLOC((struct garbagelist*) ptr,classsize[type]);
469 /* Array allocation function */
471 struct ArrayObject * allocate_newarray(void * ptr,
474 struct ArrayObject * v=(struct ArrayObject *)FREEMALLOC(
475 (struct garbagelist*)ptr,
476 sizeof(struct ArrayObject)+length*classsize[type]);
485 v->___length___=length;
486 initlock((struct ___Object___ *)v);
491 void * allocate_new(int type) {
492 struct ___Object___ * v=FREEMALLOC(classsize[type]);
502 /* Array allocation function */
504 struct ArrayObject * allocate_newarray(int type,
506 struct ArrayObject * v=FREEMALLOC(
507 sizeof(struct ArrayObject)+length*classsize[type]);
513 v->___length___=length;
514 initlock((struct ___Object___ *) v);
519 /* Converts C character arrays into Java strings */
520 #if defined(MULTICORE_GC)||defined(PMC_GC)
521 __attribute__((malloc)) struct ___String___ * NewStringShort(void * ptr,
525 __attribute__((malloc)) struct ___String___ * NewStringShort(const short *str,
529 #if defined(MULTICORE_GC)||defined(PMC_GC)
530 struct ArrayObject * chararray=
531 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
532 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
533 struct ___String___ * strobj=
534 allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
535 chararray=(struct ArrayObject *) ptrarray[2];
537 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
538 struct ___String___ * strobj=allocate_new(STRINGTYPE);
540 strobj->___value___=chararray;
541 strobj->___count___=length;
542 strobj->___offset___=0;
544 for(i=0; i<length; i++) {
545 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=str[i];
550 /* Converts C character arrays into Java strings */
551 #if defined(MULTICORE_GC)||defined(PMC_GC)
552 struct ___String___ * NewString(void * ptr,
556 struct ___String___ * NewString(const char *str,
560 #if defined(MULTICORE_GC)||defined(PMC_GC)
561 struct ArrayObject * chararray=
562 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
563 int ptrarray[]={1, (int) ptr, (int) chararray};
564 struct ___String___ * strobj=
565 allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
566 chararray=(struct ArrayObject *) ptrarray[2];
568 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
569 struct ___String___ * strobj=allocate_new(STRINGTYPE);
571 strobj->___value___=chararray;
572 strobj->___count___=length;
573 strobj->___offset___=0;
575 for(i=0; i<length; i++) {
576 ((short*)(((char*)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
581 /* Generated code calls this if we fail a bounds check */
583 void failedboundschk(int num, int index, struct ArrayObject * ao) {
585 printf("Array out of bounds at line %u with index %u of object %x with lengt\
586 h %u\n", num, index, ao, ao->___length___);
596 printf("Array out of bounds\n");
597 longjmp(error_handler,2);
604 /* Generated code calls this if we fail null ptr chk */
605 void failednullptr(void * ptr) {
606 #if defined(MULTICORE_GC)||defined(PMC_GC)
608 //print out current stack
611 struct garbagelist * stackptr = (struct garbagelist *)ptr;
612 while(stackptr!=NULL) {
613 tprintf("Stack %d: \n\t", j);
614 for(i=0; i<stackptr->size; i++) {
615 if(stackptr->array[i] != NULL) {
616 tprintf("%x, ", stackptr->array[i]);
622 stackptr=stackptr->next;
627 printf("NULL ptr\n");
637 printf("NULL ptr\n");
638 longjmp(error_handler,2);
645 /* Abort task call */
649 printf("Aborting\n");
650 longjmp(error_handler,4);
653 printf("Aborting\n");
658 void initruntimedata() {
659 // initialize the arrays
660 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
661 // startup core to initialize corestatus[]
662 for(int i = 0; i < NUMCORESACTIVE; ++i) {
665 numreceiveobjs[i] = 0;
672 self_numsendobjs = 0;
673 self_numreceiveobjs = 0;
675 for(int i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
680 //msglength = BAMBOO_MSG_BUF_LENGTH;
682 for(int i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
688 isMsgHanging = false;
691 bamboo_cur_msp = NULL;
692 bamboo_smem_size = 0;
697 INITMULTICOREGCDATA();
701 bamboo_current_thread = NULL;
707 void disruntimedata() {
708 DISMULTICOREGCDATA();
710 BAMBOO_LOCAL_MEM_CLOSE();
711 BAMBOO_SHARE_MEM_CLOSE();
714 void recordtotalexetime() {
716 totalexetime = BAMBOO_GET_EXE_TIME()-bamboo_start_time;
718 unsigned long long timediff=BAMBOO_GET_EXE_TIME()-bamboo_start_time;
719 BAMBOO_PRINT(timediff);
720 #ifndef BAMBOO_MEMPROF
721 BAMBOO_PRINT(0xbbbbbbbb);
726 void getprofiledata_I() {
727 //profile mode, send msgs to other cores to request pouring out progiling data
729 // use numconfirm to check if all cores have finished output task profiling
730 // information. This is safe as when the execution reaches this phase there
731 // should have no other msgs except the PROFILEFINISH msg, there should be
733 numconfirm=NUMCORESACTIVE-1;
734 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
735 for(i = 1; i < NUMCORESACTIVE; ++i) {
736 // send profile request msg to core i
737 send_msg_2(i, PROFILEOUTPUT, totalexetime);
740 // pour profiling data on startup core
744 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
745 if(numconfirm != 0) {
747 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
751 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
758 void checkCoreStatus() {
762 (waitconfirm && (numconfirm == 0))) {
763 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
764 corestatus[BAMBOO_NUM_OF_CORE] = 0;
765 numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
766 numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
767 // check the status of all cores
768 for(i = 0; i < NUMCORESACTIVE; ++i) {
769 if(corestatus[i] != 0) {
773 if(i == NUMCORESACTIVE) {
774 // check if the sum of send objs and receive obj are the same
775 // yes->check if the info is the latest; no->go on executing
777 for(i = 0; i < NUMCORESACTIVE; ++i) {
778 sumsendobj += numsendobjs[i];
780 for(i = 0; i < NUMCORESACTIVE; ++i) {
781 sumsendobj -= numreceiveobjs[i];
783 if(0 == sumsendobj) {
785 // the first time found all cores stall
786 // send out status confirm msg to all other cores
787 // reset the corestatus array too
788 corestatus[BAMBOO_NUM_OF_CORE] = 1;
790 numconfirm = NUMCORESACTIVE - 1;
791 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
792 for(i = 1; i < NUMCORESACTIVE; ++i) {
794 // send status confirm msg to core i
795 send_msg_1(i, STATUSCONFIRM);
799 // all the core status info are the latest
800 // terminate; for profiling mode, send request to all
801 // other cores to pour out profiling data
802 recordtotalexetime();
804 CACHEADAPT_DISABLE_TIMER();
805 GC_OUTPUT_PROFILE_DATA();
809 gc_outputProfileDataReadable();
811 tprintf("FINISH_EXECUTION\n");
812 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
813 terminate(); // All done.
816 // still some objects on the fly on the network
817 // reset the waitconfirm and numconfirm
822 // not all cores are stall, keep on waiting
826 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
830 // main function for each core
831 void run(int argc, char** argv) {
832 bool sendStall = false;
834 bool tocontinue = false;
836 corenum = BAMBOO_GET_NUM_OF_CORE();
837 // initialize runtime data structures
844 if (BAMBOO_NUM_OF_CORE==STARTUPCORE)
845 profile_init(_LOCAL_DRD_CNT,_LOCAL_WR_CNT, _REMOTE_DRD_CNT, _REMOTE_WR_CNT);
847 int offcore=4*(BAMBOO_NUM_OF_CORE-1);
848 profile_init(validevents[(offcore)%87], validevents[(offcore+1)%87], validevents[(offcore+2)%87], validevents[(offcore+3)%87]);
851 if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
852 numconfirm=NUMCORES-1;
853 for(int i=0;i<NUMCORES;i++) {
854 if (i!=STARTUPCORE) {
855 send_msg_1(i,REQNOTIFYSTART);
860 tprintf("START_EXECUTION\n");
861 bamboo_start_time = BAMBOO_GET_EXE_TIME();
867 bme_performance_counter_start();
870 CACHEADAPT_ENABLE_TIMER();
872 initializeexithandler();
874 // main process of the execution module
875 if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
877 // non-executing cores, only processing communications
883 /* Create queue of active tasks */
884 activetasks= genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
885 (int (*)(void *,void *)) &comparetpd);
887 /* Process task information */
890 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
891 /* Create startup object */
892 createstartupobject(argc, argv);
897 profile_start(APP_REGION);
899 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
901 // run the initStaticAndGlobal method to initialize the static blocks and
903 initStaticAndGlobal();
905 // run the main method in the specified mainclass
906 mgc_main(argc, argv);
913 // check if there are new active tasks can be executed
919 while(receiveObject_I() != -1) {
922 // check if there are some pending objects,
923 // if yes, enqueue them and executetasks again
924 tocontinue = checkObjQueue();
926 tocontinue = trystartthread();
934 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
945 // wait for some time
951 // send StallMsg to startup core
953 send_msg_4(STARTUPCORE,TRANSTALL,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);