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"
17 extern int classsize[];
18 extern int typearray[];
19 extern int typearray2[];
20 extern int* supertypes[];
23 extern struct genhashtable * activetasks;
30 int instanceofif(int otype, int type) {
37 int num = supertypes[otype][0];
38 for(int i = 1; i < num + 1; i++) {
39 int t = supertypes[otype][i];
40 if(instanceofif(t, type) == 1) {
47 int instanceof(struct ___Object___ *ptr, int type) {
52 if(instanceofif(i, type) == 1) {
59 i=typearray2[i-NUMCLASSES];
65 void initializeexithandler() {
68 /* This function inject failures */
69 void injectinstructionfailure() {
70 // not supported in MULTICORE version
74 #ifdef D___Double______nativeparsedouble____L___String___
75 double CALL01(___Double______nativeparsedouble____L___String___,
76 struct ___String___ * ___str___) {
77 int length=VAR(___str___)->___count___;
78 int maxlength=(length>60) ? 60 : length;
79 char str[maxlength+1];
80 struct ArrayObject * chararray=VAR(___str___)->___value___;
82 int offset=VAR(___str___)->___offset___;
83 for(i=0; i<maxlength; i++) {
85 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
88 double d=0.0; //atof(str); TODO Unimplemented nativeparsedoulbe
93 #ifdef D___Double______nativeparsedouble_____AR_B_I_I
94 double CALL23(___Double______nativeparsedouble_____AR_B_I_I,
99 struct ArrayObject * ___str___) {
100 int maxlength=(length>60)?60:length;
101 char str[maxlength+1];
102 struct ArrayObject * bytearray=VAR(___str___);
104 for(i=0; i<maxlength; i++) {
105 str[i]=(((char *)&bytearray->___length___)+sizeof(int))[i+start];
108 double d=0.0; //atof(str); TODO Unimplemented nativeparsedouble
113 typedef union jvalue {
123 #ifdef D___Double______doubleToRawLongBits____D
124 long long CALL11(___Double______doubleToRawLongBits____D,
126 double ___value___) {
130 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
131 /* On little endian ARM processors when using FPA, word order of
132 doubles is still big endian. So take that into account here. When
133 using VFP, word order of doubles follows byte order. */
134 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
135 val.j = SWAP_DOUBLE(val.j);
142 #ifdef D___Double______longBitsToDouble____J
143 double CALL11(___Double______longBitsToDouble____J,
144 long long ___bits___,
145 long long ___bits___) {
149 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
151 #define SWAP_DOUBLE(a) (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
153 val.j = SWAP_DOUBLE(val.j);
160 #ifdef D___String______convertdoubletochar____D__AR_C
161 int CALL12(___String______convertdoubletochar____D__AR_C,
164 struct ArrayObject * ___chararray___) {
165 int length=VAR(___chararray___)->___length___;
168 int num=snprintf(str, length, "%f",___val___);
171 for(i=0; i<length; i++) {
172 ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=
179 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
180 void deepArrayCopy(struct ___Object___ * dst,
181 struct ___Object___ * src) {
182 int dsttype=((int *)dst)[0];
183 int srctype=((int *)src)[0];
184 if (dsttype<NUMCLASSES||srctype<NUMCLASSES||srctype!=dsttype)
186 struct ArrayObject *aodst=(struct ArrayObject *)dst;
187 struct ArrayObject *aosrc=(struct ArrayObject *)src;
188 int dstlength=aodst->___length___;
189 int srclength=aosrc->___length___;
190 if (dstlength!=srclength)
192 unsigned INTPTR *pointer=pointerarray[srctype];
194 int elementsize=classsize[srctype];
195 int size=srclength*elementsize;
197 memcpy(((char *)&aodst->___length___)+sizeof(int) ,
198 ((char *)&aosrc->___length___)+sizeof(int), size);
202 for(i=0;i<srclength;i++) {
203 struct ___Object___ * ptr=
204 ((struct ___Object___**)(((char*)&aosrc->___length___)+sizeof(int)))[i];
205 int ptrtype=((int *)ptr)[0];
206 if (ptrtype>=NUMCLASSES) {
207 struct ___Object___ * dstptr=((struct ___Object___**)
208 (((char*)&aodst->___length___)+sizeof(int)))[i];
209 deepArrayCopy(dstptr,ptr);
212 ((struct ___Object___ **)
213 (((char*) &aodst->___length___)+sizeof(int)))[i]=ptr;
219 void CALL02(___System______deepArrayCopy____L___Object____L___Object___,
220 struct ___Object___ * ___dst___,
221 struct ___Object___ * ___src___) {
222 deepArrayCopy(VAR(___dst___), VAR(___src___));
226 #ifdef D___System______arraycopy____L___Object____I_L___Object____I_I
227 void arraycopy(struct ___Object___ *src,
229 struct ___Object___ *dst,
232 int dsttype=((int *)dst)[0];
233 int srctype=((int *)src)[0];
235 //not an array or type mismatch
236 if (dsttype<NUMCLASSES||srctype<NUMCLASSES/*||srctype!=dsttype*/)
239 struct ArrayObject *aodst=(struct ArrayObject *)dst;
240 struct ArrayObject *aosrc=(struct ArrayObject *)src;
241 int dstlength=aodst->___length___;
242 int srclength=aosrc->___length___;
246 if (srcPos+length>srclength)
248 if (destPos+length>dstlength)
251 unsigned INTPTR *pointer=pointerarray[srctype];
253 int elementsize=classsize[srctype];
254 int size=length*elementsize;
256 memcpy(((char *)&aodst->___length___)+sizeof(int)+destPos*elementsize,
257 ((char *)&aosrc->___length___)+sizeof(int)+srcPos*elementsize, size);
261 for(i=0;i<length;i++) {
262 struct ___Object___ * ptr=((struct ___Object___**)
263 (((char*)&aosrc->___length___)+sizeof(int)))[i+srcPos];
264 int ptrtype=((int *)ptr)[0];
266 ((struct ___Object___ **)
267 (((char*) &aodst->___length___)+sizeof(int)))[i+destPos]=ptr;
272 void CALL35(___System______arraycopy____L___Object____I_L___Object____I_I,
276 struct ___Object___ * ___src___,
278 struct ___Object___ * ___dst___,
281 arraycopy(VAR(___src___), ___srcPos___, VAR(___dst___), ___destPos___,
286 #ifdef D___System______exit____I
287 void CALL11(___System______exit____I,
290 // gc_profile mode, output gc prfiling data
291 #if defined(MULTICORE_GC)||defined(PMC_GC)
292 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
293 BAMBOO_PRINT(BAMBOO_GET_EXE_TIME());
294 BAMBOO_PRINT(0xbbbbbbbb);
295 CACHEADAPT_DISABLE_TIMER();
296 GC_OUTPUT_PROFILE_DATA();
300 gc_outputProfileDataReadable();
301 tprintf("FINISH_EXECUTION\n");
304 BAMBOO_EXIT_APP(___status___);
308 #ifdef D___Vector______removeElement_____AR_L___Object____I_I
309 void CALL23(___Vector______removeElement_____AR_L___Object____I_I,
312 struct ArrayObject * ___array___,
315 char* offset=((char *)(&VAR(___array___)->___length___))
316 +sizeof(unsigned int)+sizeof(void *)*___index___;
317 memmove(offset, offset+sizeof(void *),
318 (___size___-___index___-1)*sizeof(void *));
322 #ifdef D___System______printI____I
323 void CALL11(___System______printI____I,
326 BAMBOO_PRINT(0x1111);
327 BAMBOO_PRINT_REG(___status___);
331 #ifdef D___System______currentTimeMillis____
332 long long CALL00(___System______currentTimeMillis____) {
333 //TilePro64 is 700mHz
334 return ((unsigned long long)BAMBOO_GET_EXE_TIME())/700000;
338 #ifdef D___System______setgcprofileflag____
339 void CALL00(___System______setgcprofileflag____) {
342 extern volatile bool gc_profile_flag;
343 gc_profile_flag = true;
349 #ifdef D___System______resetgcprofileflag____
350 void CALL00(___System______resetgcprofileflag____) {
353 extern volatile bool gc_profile_flag;
354 gc_profile_flag = false;
360 #ifdef D___System______gc____
361 void CALL00(___System______gc____) {
363 if(BAMBOO_NUM_OF_CORE == STARTUPCORE) {
364 if(!gc_status_info.gcprocessing && !gcflag) {
367 for(int i = 0; i < NUMCORESACTIVE; i++) {
368 // reuse the gcnumsendobjs & gcnumreceiveobjs
369 gcnumsendobjs[0][i] = 0;
370 gcnumreceiveobjs[0][i] = 0;
372 for(int i = 0; i < NUMCORES4GC; i++) {
373 if(i != STARTUPCORE) {
374 send_msg_1(i,GCSTARTPRE);
379 // send msg to the startup core to start gc
380 send_msg_1(STARTUPCORE, GCINVOKE);
386 #ifdef D___System______printString____L___String___
387 void CALL01(___System______printString____L___String___, struct ___String___ * ___s___) {
388 #if defined(MGC)&&defined(TILERA_BME)
389 struct ArrayObject * chararray=VAR(___s___)->___value___;
391 int offset=VAR(___s___)->___offset___;
393 for(i=0; i<VAR(___s___)->___count___; i++) {
395 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
402 /* Object allocation function */
404 #if defined(MULTICORE_GC)||defined(PMC_GC)
405 void * allocate_new(void * ptr,
407 struct ___Object___ * v=
408 (struct ___Object___*)FREEMALLOC((struct garbagelist*) ptr,classsize[type]);
419 /* Array allocation function */
421 struct ArrayObject * allocate_newarray(void * ptr,
424 struct ArrayObject * v=(struct ArrayObject *)FREEMALLOC(
425 (struct garbagelist*)ptr,
426 sizeof(struct ArrayObject)+length*classsize[type]);
435 v->___length___=length;
436 initlock((struct ___Object___ *)v);
441 void * allocate_new(int type) {
442 struct ___Object___ * v=FREEMALLOC(classsize[type]);
452 /* Array allocation function */
454 struct ArrayObject * allocate_newarray(int type,
456 struct ArrayObject * v=FREEMALLOC(
457 sizeof(struct ArrayObject)+length*classsize[type]);
463 v->___length___=length;
464 initlock((struct ___Object___ *) v);
469 /* Converts C character arrays into Java strings */
470 #if defined(MULTICORE_GC)||defined(PMC_GC)
471 __attribute__((malloc)) struct ___String___ * NewStringShort(void * ptr,
475 __attribute__((malloc)) struct ___String___ * NewStringShort(const short *str,
479 #if defined(MULTICORE_GC)||defined(PMC_GC)
480 struct ArrayObject * chararray=
481 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
482 INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
483 struct ___String___ * strobj=
484 allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
485 chararray=(struct ArrayObject *) ptrarray[2];
487 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
488 struct ___String___ * strobj=allocate_new(STRINGTYPE);
490 strobj->___value___=chararray;
491 strobj->___count___=length;
492 strobj->___offset___=0;
494 for(i=0; i<length; i++) {
495 ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=str[i];
500 /* Converts C character arrays into Java strings */
501 #if defined(MULTICORE_GC)||defined(PMC_GC)
502 struct ___String___ * NewString(void * ptr,
506 struct ___String___ * NewString(const char *str,
510 #if defined(MULTICORE_GC)||defined(PMC_GC)
511 struct ArrayObject * chararray=
512 allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
513 int ptrarray[]={1, (int) ptr, (int) chararray};
514 struct ___String___ * strobj=
515 allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
516 chararray=(struct ArrayObject *) ptrarray[2];
518 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
519 struct ___String___ * strobj=allocate_new(STRINGTYPE);
521 strobj->___value___=chararray;
522 strobj->___count___=length;
523 strobj->___offset___=0;
525 for(i=0; i<length; i++) {
526 ((short*)(((char*)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
531 /* Generated code calls this if we fail a bounds check */
533 void failedboundschk(int num, int index, struct ArrayObject * ao) {
535 printf("Array out of bounds at line %u with index %u of object %x with lengt\
536 h %u\n", num, index, ao, ao->___length___);
546 printf("Array out of bounds\n");
547 longjmp(error_handler,2);
554 /* Generated code calls this if we fail null ptr chk */
555 void failednullptr(void * ptr) {
556 #if defined(MULTICORE_GC)||defined(PMC_GC)
558 //print out current stack
561 struct garbagelist * stackptr = (struct garbagelist *)ptr;
562 while(stackptr!=NULL) {
563 tprintf("Stack %d: \n\t", j);
564 for(i=0; i<stackptr->size; i++) {
565 if(stackptr->array[i] != NULL) {
566 tprintf("%x, ", stackptr->array[i]);
572 stackptr=stackptr->next;
577 printf("NULL ptr\n");
587 printf("NULL ptr\n");
588 longjmp(error_handler,2);
595 /* Abort task call */
599 printf("Aborting\n");
600 longjmp(error_handler,4);
603 printf("Aborting\n");
608 void initruntimedata() {
609 // initialize the arrays
610 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
611 // startup core to initialize corestatus[]
612 for(int i = 0; i < NUMCORESACTIVE; ++i) {
615 numreceiveobjs[i] = 0;
622 self_numsendobjs = 0;
623 self_numreceiveobjs = 0;
625 for(int i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
630 //msglength = BAMBOO_MSG_BUF_LENGTH;
632 for(int i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
638 isMsgHanging = false;
641 bamboo_cur_msp = NULL;
642 bamboo_smem_size = 0;
647 INITMULTICOREGCDATA();
651 bamboo_current_thread = NULL;
657 void disruntimedata() {
658 DISMULTICOREGCDATA();
660 BAMBOO_LOCAL_MEM_CLOSE();
661 BAMBOO_SHARE_MEM_CLOSE();
664 void recordtotalexetime() {
666 totalexetime = BAMBOO_GET_EXE_TIME()-bamboo_start_time;
668 unsigned long long timediff=BAMBOO_GET_EXE_TIME()-bamboo_start_time;
669 BAMBOO_PRINT(timediff);
670 #ifndef BAMBOO_MEMPROF
671 BAMBOO_PRINT(0xbbbbbbbb);
676 void getprofiledata_I() {
677 //profile mode, send msgs to other cores to request pouring out progiling data
679 // use numconfirm to check if all cores have finished output task profiling
680 // information. This is safe as when the execution reaches this phase there
681 // should have no other msgs except the PROFILEFINISH msg, there should be
683 numconfirm=NUMCORESACTIVE-1;
684 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
685 for(i = 1; i < NUMCORESACTIVE; ++i) {
686 // send profile request msg to core i
687 send_msg_2(i, PROFILEOUTPUT, totalexetime);
690 // pour profiling data on startup core
694 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
695 if(numconfirm != 0) {
697 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
701 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
708 void checkCoreStatus() {
712 (waitconfirm && (numconfirm == 0))) {
713 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
714 corestatus[BAMBOO_NUM_OF_CORE] = 0;
715 numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
716 numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
717 // check the status of all cores
718 for(i = 0; i < NUMCORESACTIVE; ++i) {
719 if(corestatus[i] != 0) {
723 if(i == NUMCORESACTIVE) {
724 // check if the sum of send objs and receive obj are the same
725 // yes->check if the info is the latest; no->go on executing
727 for(i = 0; i < NUMCORESACTIVE; ++i) {
728 sumsendobj += numsendobjs[i];
730 for(i = 0; i < NUMCORESACTIVE; ++i) {
731 sumsendobj -= numreceiveobjs[i];
733 if(0 == sumsendobj) {
735 // the first time found all cores stall
736 // send out status confirm msg to all other cores
737 // reset the corestatus array too
738 corestatus[BAMBOO_NUM_OF_CORE] = 1;
740 numconfirm = NUMCORESACTIVE - 1;
741 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
742 for(i = 1; i < NUMCORESACTIVE; ++i) {
744 // send status confirm msg to core i
745 send_msg_1(i, STATUSCONFIRM);
749 // all the core status info are the latest
750 // terminate; for profiling mode, send request to all
751 // other cores to pour out profiling data
752 recordtotalexetime();
754 CACHEADAPT_DISABLE_TIMER();
755 GC_OUTPUT_PROFILE_DATA();
759 gc_outputProfileDataReadable();
761 tprintf("FINISH_EXECUTION\n");
762 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
763 terminate(); // All done.
766 // still some objects on the fly on the network
767 // reset the waitconfirm and numconfirm
772 // not all cores are stall, keep on waiting
776 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
780 // main function for each core
781 void run(int argc, char** argv) {
782 bool sendStall = false;
784 bool tocontinue = false;
786 corenum = BAMBOO_GET_NUM_OF_CORE();
787 // initialize runtime data structures
794 if (BAMBOO_NUM_OF_CORE==STARTUPCORE)
795 profile_init(_LOCAL_DRD_CNT,_LOCAL_WR_CNT, _REMOTE_DRD_CNT, _REMOTE_WR_CNT);
797 int offcore=4*(BAMBOO_NUM_OF_CORE-1);
798 profile_init(validevents[(offcore)%87], validevents[(offcore+1)%87], validevents[(offcore+2)%87], validevents[(offcore+3)%87]);
801 if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
802 numconfirm=NUMCORES-1;
803 for(int i=0;i<NUMCORES;i++) {
804 if (i!=STARTUPCORE) {
805 send_msg_1(i,REQNOTIFYSTART);
810 tprintf("START_EXECUTION\n");
811 bamboo_start_time = BAMBOO_GET_EXE_TIME();
817 bme_performance_counter_start();
820 CACHEADAPT_ENABLE_TIMER();
822 initializeexithandler();
824 // main process of the execution module
825 if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
827 // non-executing cores, only processing communications
833 /* Create queue of active tasks */
834 activetasks= genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
835 (int (*)(void *,void *)) &comparetpd);
837 /* Process task information */
840 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
841 /* Create startup object */
842 createstartupobject(argc, argv);
847 profile_start(APP_REGION);
849 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
851 // run the initStaticAndGlobal method to initialize the static blocks and
853 initStaticAndGlobal();
855 // run the main method in the specified mainclass
856 mgc_main(argc, argv);
863 // check if there are new active tasks can be executed
869 while(receiveObject_I() != -1) {
872 // check if there are some pending objects,
873 // if yes, enqueue them and executetasks again
874 tocontinue = checkObjQueue();
876 tocontinue = trystartthread();
884 if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
895 // wait for some time
901 // send StallMsg to startup core
903 send_msg_4(STARTUPCORE,TRANSTALL,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);