add finish printfs
[IRC.git] / Robust / src / Runtime / bamboo / multicoreruntime.c
index 47a68cf8060b7f9391875dc17d87b52787231f21..defc97092dec9e0e4a7f94168091fa5a17840875 100644 (file)
@@ -1,9 +1,18 @@
 #ifdef MULTICORE
-
 #include "runtime.h"
 #include "multicoreruntime.h"
 #include "methodheaders.h"
 #include "multicoregarbage.h"
+#ifdef PMC_GC
+#include "multicoregcprofile.h"
+#include "multicoregc.h"
+#include "pmc_garbage.h"
+#endif
+#include "multicore_arch.h"
+#include <stdio.h>
+#ifdef PERFCOUNT
+#include "bme_perf_counter.h"
+#endif
 
 extern int classsize[];
 extern int typearray[];
@@ -165,13 +174,6 @@ int CALL12(___String______convertdoubletochar____D__AR_C,
   }
   return num;
 }
-#else
-int CALL12(___String______convertdoubletochar____D__AR_C, 
-           double ___val___, 
-           double ___val___, 
-           struct ArrayObject ___chararray___) {
-  return 0;
-}
 #endif
 
 #ifdef D___System______deepArrayCopy____L___Object____L___Object___
@@ -286,12 +288,17 @@ void CALL11(___System______exit____I,
             int ___status___, 
             int ___status___) {
 // gc_profile mode, output gc prfiling data
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
     BAMBOO_PRINT(BAMBOO_GET_EXE_TIME());
     BAMBOO_PRINT(0xbbbbbbbb);
     CACHEADAPT_DISABLE_TIMER();
     GC_OUTPUT_PROFILE_DATA();
+#ifdef PERFCOUNT
+    print_statistics();
+#endif
+    gc_outputProfileDataReadable();
+    tprintf("FINISH_EXECUTION\n");
   }
 #endif 
   BAMBOO_EXIT_APP(___status___);
@@ -328,6 +335,7 @@ long long CALL00(___System______currentTimeMillis____) {
 }
 #endif
 
+#ifdef D___System______setgcprofileflag____
 void CALL00(___System______setgcprofileflag____) {
 #ifdef GC_PROFILE
 #ifdef MGC_SPEC
@@ -336,7 +344,9 @@ void CALL00(___System______setgcprofileflag____) {
 #endif
 #endif
 }
+#endif
 
+#ifdef D___System______resetgcprofileflag____
 void CALL00(___System______resetgcprofileflag____) {
 #ifdef GC_PROFILE
 #ifdef MGC_SPEC
@@ -345,12 +355,37 @@ void CALL00(___System______resetgcprofileflag____) {
 #endif
 #endif
 }
+#endif
+
+#ifdef D___System______gc____
+void CALL00(___System______gc____) {
+#ifdef MULTICORE_GC
+  if(BAMBOO_NUM_OF_CORE == STARTUPCORE) {
+    if(!gc_status_info.gcprocessing && !gcflag) {
+      gcflag = true;
+      gcprecheck = true;
+      for(int i = 0; i < NUMCORESACTIVE; i++) {
+        // reuse the gcnumsendobjs & gcnumreceiveobjs
+        gcnumsendobjs[0][i] = 0;
+        gcnumreceiveobjs[0][i] = 0;
+      }
+      for(int i = 0; i < NUMCORES4GC; i++) {
+        if(i != STARTUPCORE) {
+          send_msg_1_I(i,GCSTARTPRE);
+        }
+      }
+    }
+  } else {
+    // send msg to the startup core to start gc
+    send_msg_1(STARTUPCORE, GCINVOKE);
+  }
+#endif
+}
+#endif
 
 #ifdef D___System______printString____L___String___
-void CALL01(___System______printString____L___String___,
-            struct ___String___ * ___s___) {
-#ifdef MGC
-#ifdef TILERA_BME
+void CALL01(___System______printString____L___String___, struct ___String___ * ___s___) {
+#if defined(MGC)&&defined(TILERA_BME)
   struct ArrayObject * chararray=VAR(___s___)->___value___;
   int i;
   int offset=VAR(___s___)->___offset___;
@@ -360,14 +395,13 @@ void CALL01(___System______printString____L___String___,
       ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
     printf("%c", sc);
   }
-#endif // TILERA_BME
 #endif // MGC
 }
 #endif
 
 /* Object allocation function */
 
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
 void * allocate_new(void * ptr, 
                     int type) {
   struct ___Object___ * v=
@@ -403,7 +437,7 @@ struct ArrayObject * allocate_newarray(void * ptr,
     return NULL;
   }
   v->___length___=length;
-  initlock(v);
+  initlock((struct ___Object___ *)v);
 #ifdef GC_PROFILE
   extern unsigned int gc_num_obj;
   gc_num_obj++;
@@ -435,13 +469,13 @@ struct ArrayObject * allocate_newarray(int type,
   v->lock = NULL;
 #endif
   v->___length___=length;
-  initlock(v);
+  initlock((struct ___Object___ *) v);
   return v;
 }
 #endif
 
 /* Converts C character arrays into Java strings */
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
 __attribute__((malloc)) struct ___String___ * NewStringShort(void * ptr, 
                                                              const short *str,
                                                              int length) {
@@ -450,7 +484,7 @@ __attribute__((malloc)) struct ___String___ * NewStringShort(const short *str,
                                                              int length) {
 #endif
   int i;
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
   struct ArrayObject * chararray=
     allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
   INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
@@ -472,7 +506,7 @@ __attribute__((malloc)) struct ___String___ * NewStringShort(const short *str,
 }
 
 /* Converts C character arrays into Java strings */
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
 struct ___String___ * NewString(void * ptr, 
                                 const char *str,
                                 int length) {
@@ -481,7 +515,7 @@ struct ___String___ * NewString(const char *str,
                                 int length) {
 #endif
   int i;
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
   struct ArrayObject * chararray=
     allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
   int ptrarray[]={1, (int) ptr, (int) chararray};
@@ -504,9 +538,10 @@ struct ___String___ * NewString(const char *str,
 
 /* Generated code calls this if we fail a bounds check */
 
-void failedboundschk(int num) {
+ void failedboundschk(int num, int index, struct ArrayObject * ao) {
 #ifndef TASK
-  printf("Array out of bounds, %d \n", num);
+  printf("Array out of bounds at line %u with index %u of object %x with lengt\
+h %u\n", num, index, ao, ao->___length___);
 #ifdef THREADS
   threadexit();
 #elif defined MGC
@@ -526,7 +561,7 @@ void failedboundschk(int num) {
 
 /* Generated code calls this if we fail null ptr chk */
 void failednullptr(void * ptr) {
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
 #ifndef RAW
   //print out current stack
   int i,j;
@@ -578,12 +613,11 @@ void abort_task() {
 #endif
 }
 
-INLINE void initruntimedata() {
-  int i;
+void initruntimedata() {
   // initialize the arrays
   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
     // startup core to initialize corestatus[]
-    for(i = 0; i < NUMCORESACTIVE; ++i) {
+    for(int i = 0; i < NUMCORESACTIVE; ++i) {
       corestatus[i] = 1;
       numsendobjs[i] = 0;
       numreceiveobjs[i] = 0;
@@ -596,21 +630,21 @@ INLINE void initruntimedata() {
   self_numsendobjs = 0;
   self_numreceiveobjs = 0;
 
-  for(i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
+  for(int i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
     msgdata[i] = -1;
   }
   msgdataindex = 0;
   msgdatalast = 0;
   //msglength = BAMBOO_MSG_BUF_LENGTH;
   msgdatafull = false;
-  for(i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
+  for(int i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
     outmsgdata[i] = -1;
   }
   outmsgindex = 0;
   outmsglast = 0;
   outmsgleft = 0;
   isMsgHanging = false;
-
+  
   smemflag = true;
   bamboo_cur_msp = NULL;
   bamboo_smem_size = 0;
@@ -622,31 +656,32 @@ INLINE void initruntimedata() {
 
 #ifdef MGC
   initializethreads();
-  bamboo_current_thread = 0;
+  bamboo_current_thread = NULL;
 #endif // MGC
 
   INITTASKDATA();
 }
 
-INLINE void disruntimedata() {
+void disruntimedata() {
   DISMULTICOREGCDATA();
   DISTASKDATA();
   BAMBOO_LOCAL_MEM_CLOSE();
   BAMBOO_SHARE_MEM_CLOSE();
 }
 
-INLINE void recordtotalexetime() {
+void recordtotalexetime() {
 #ifdef USEIO
   totalexetime = BAMBOO_GET_EXE_TIME()-bamboo_start_time;
 #else // USEIO
-  BAMBOO_PRINT(BAMBOO_GET_EXE_TIME()-bamboo_start_time);
+  unsigned long long timediff=BAMBOO_GET_EXE_TIME()-bamboo_start_time;
+  BAMBOO_PRINT(timediff);
 #ifndef BAMBOO_MEMPROF
   BAMBOO_PRINT(0xbbbbbbbb);
 #endif
 #endif // USEIO
 }
 
-INLINE void getprofiledata_I() {
+void getprofiledata_I() {
   //profile mode, send msgs to other cores to request pouring out progiling data
 #ifdef PROFILE
   // use numconfirm to check if all cores have finished output task profiling 
@@ -678,7 +713,7 @@ INLINE void getprofiledata_I() {
 #endif
 }
 
-INLINE void checkCoreStatus() {
+void checkCoreStatus() {
   int i = 0;
   int sumsendobj = 0;
   if((!waitconfirm) ||
@@ -726,7 +761,12 @@ INLINE void checkCoreStatus() {
           getprofiledata_I();
           CACHEADAPT_DISABLE_TIMER();
           GC_OUTPUT_PROFILE_DATA();
+#ifdef PERFCOUNT
+         print_statistics();
+#endif
+         gc_outputProfileDataReadable();
           disruntimedata();
+         tprintf("FINISH_EXECUTION\n");
           BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
           terminate();  // All done.
         }
@@ -746,20 +786,44 @@ INLINE void checkCoreStatus() {
 }
 
 // main function for each core
-inline void run(int argc, char** argv) {
-  int i = 0;
+void run(int argc, char** argv) {
   bool sendStall = false;
   bool isfirst = true;
   bool tocontinue = false;
-
+  startflag = false;
   corenum = BAMBOO_GET_NUM_OF_CORE();
-
   // initialize runtime data structures
   initruntimedata();
-
-  // other architecture related initialization
-  initialization();
   initCommunication();
+#ifdef PMC_GC
+  pmc_onceInit();
+#endif
+#ifdef PERFCOUNT
+  if (BAMBOO_NUM_OF_CORE==STARTUPCORE)
+    profile_init(_LOCAL_DRD_CNT,_LOCAL_WR_CNT, _REMOTE_DRD_CNT, _REMOTE_WR_CNT);
+  else {
+    int offcore=4*(BAMBOO_NUM_OF_CORE-1);
+    profile_init(validevents[(offcore)%87], validevents[(offcore+1)%87], validevents[(offcore+2)%87], validevents[(offcore+3)%87]);
+  }
+#endif
+  if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
+    numconfirm=NUMCORES-1;
+    for(int i=0;i<NUMCORES;i++) {
+      if (i!=STARTUPCORE) {
+       send_msg_1(i,REQNOTIFYSTART);
+      }
+    }
+    while(numconfirm!=0)
+      ;
+    tprintf("START_EXECUTION\n");
+    bamboo_start_time = BAMBOO_GET_EXE_TIME();
+  } else {
+    while(!startflag)
+      ;
+  }
+#ifdef PERFCOUNT
+  bme_performance_counter_start();
+#endif
 
   CACHEADAPT_ENABLE_TIMER();
 
@@ -776,28 +840,31 @@ inline void run(int argc, char** argv) {
 #ifdef TASK
     /* Create queue of active tasks */
     activetasks= genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
-        (int (*)(void *,void *)) &comparetpd);
-
+                                     (int (*)(void *,void *)) &comparetpd);
+    
     /* Process task information */
     processtasks();
-
+    
     if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
       /* Create startup object */
       createstartupobject(argc, argv);
     }
 #endif
-
-       if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
+    
+#ifdef PERFCOUNT
+      profile_start(APP_REGION);
+#endif
+    if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
 #ifdef TASK
-         // run the initStaticAndGlobal method to initialize the static blocks and
-         // global fields
-         initStaticAndGlobal();
+      // run the initStaticAndGlobal method to initialize the static blocks and
+      // global fields
+      initStaticAndGlobal();
 #elif defined MGC
-         // run the main method in the specified mainclass
-         mgc_main(argc, argv);
+      // run the main method in the specified mainclass
+      mgc_main(argc, argv);
 #endif // TASK
-       }
-
+    }
+    
     while(true) {
       GCCHECK(NULL);
 #ifdef TASK
@@ -819,7 +886,7 @@ inline void run(int argc, char** argv) {
         sendStall = false;
       }
 #endif
-
+      
       if(!tocontinue) {
         // check if stop
         if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
@@ -832,20 +899,20 @@ inline void run(int argc, char** argv) {
 #ifdef PROFILE
             if(!stall) {
 #endif
-            if(isfirst) {
-              // wait for some time
-              int halt = 10000;
-              while(halt--) {
-              }
-              isfirst = false;
-            } else {
-              // send StallMsg to startup core
-              // send stall msg
-              send_msg_4(STARTUPCORE,TRANSTALL,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
-              sendStall = true;
-              isfirst = true;
-              busystatus = false;
-            }
+             if(isfirst) {
+               // wait for some time
+               int halt = 10000;
+               while(halt--) {
+               }
+               isfirst = false;
+             } else {
+               // send StallMsg to startup core
+               // send stall msg
+               send_msg_4(STARTUPCORE,TRANSTALL,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
+               sendStall = true;
+               isfirst = true;
+               busystatus = false;
+             }
 #ifdef PROFILE
             }
 #endif
@@ -857,6 +924,6 @@ inline void run(int argc, char** argv) {
       }
     }
   }
-} 
-
+}
 #endif // MULTICORE