changes for reading input files
[IRC.git] / Robust / src / Runtime / bamboo / multicoreruntime.c
index 3d9ddb31c14086d376f6a53a1fd6131033ba4f10..57c971af4d1506f33e9b5ee0514499d70791ec4d 100644 (file)
@@ -1,9 +1,21 @@
 #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
+#ifdef MEMPERFCOUNT
+#include "memprof.h"
+#endif
 
 extern int classsize[];
 extern int typearray[];
@@ -165,13 +177,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 +291,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 +338,34 @@ long long CALL00(___System______currentTimeMillis____) {
 }
 #endif
 
+#ifdef D___System______numGCs____
+long long ___System______numGCs____(struct ___System______numGCs_____params * ___params___) {
+#ifdef MULTICORE_GC
+  return numGCs;
+#else
+  return 0;
+#endif
+}
+#endif
+
+#ifdef D___System______milliGcTime____
+long long CALL00(___System______milliGcTime____) {
+#ifdef MULTICORE_GC
+  return GCtime/700000;
+#else
+  return 0;
+#endif
+}
+#endif
+
+#ifdef D___System______nanoTime____ 
+long long CALL00(___System______nanoTime____) {
+  //TilePro64 is 700mHz
+  return ((unsigned long long)BAMBOO_GET_EXE_TIME())/700;
+}
+#endif
+
+#ifdef D___System______setgcprofileflag____
 void CALL00(___System______setgcprofileflag____) {
 #ifdef GC_PROFILE
 #ifdef MGC_SPEC
@@ -336,7 +374,9 @@ void CALL00(___System______setgcprofileflag____) {
 #endif
 #endif
 }
+#endif
 
+#ifdef D___System______resetgcprofileflag____
 void CALL00(___System______resetgcprofileflag____) {
 #ifdef GC_PROFILE
 #ifdef MGC_SPEC
@@ -345,12 +385,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,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 +425,33 @@ void CALL01(___System______printString____L___String___,
       ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
     printf("%c", sc);
   }
-#endif // TILERA_BME
 #endif // MGC
 }
 #endif
 
+#ifdef D___Scanner______next____ 
+struct ArrayObject * CALL01(___Scanner______next____, struct ___Scanner___ * ___this___) {
+  int pos = VAR(___this___)->___currentpos___;
+#if defined(MULTICORE_GC)||defined(PMC_GC)
+  struct ArrayObject * result= allocate_newarray(NULL, CHARARRAYTYPE, 10);
+#else
+  struct ArrayObject * result=allocate_newarray(CHARARRAYTYPE, 10);
+#endif
+  int i = 0;
+  while(true) { //(VAR(___this___)->___sourcename___[pos]==' ')||(VAR(___this___)->___sourcename___[pos]=='\n')){
+         pos++;
+  }
+  do {
+         ((short *)(((char *)&result->___length___)+sizeof(int)))[i++]='\0';//(short)VAR(___this___)->___sourcename___[pos++]; // TODO
+  }while(true);//(VAR(___this___)->___sourcename___[pos]!=' ')&&(VAR(___this___)->___sourcename___[pos]!='\n'));
+  VAR(___this___)->___currentpos___ = pos;
+  return result;
+}
+#endif
+
 /* Object allocation function */
 
-#ifdef MULTICORE_GC
+#if defined(MULTICORE_GC)||defined(PMC_GC)
 void * allocate_new(void * ptr, 
                     int type) {
   struct ___Object___ * v=
@@ -379,10 +463,6 @@ void * allocate_new(void * ptr,
   v->lockcount = 0;
 #endif
   initlock(v);
-#ifdef GC_PROFILE
-  extern unsigned int gc_num_obj;
-  gc_num_obj++;
-#endif
   return v;
 }
 
@@ -403,11 +483,7 @@ struct ArrayObject * allocate_newarray(void * ptr,
     return NULL;
   }
   v->___length___=length;
-  initlock(v);
-#ifdef GC_PROFILE
-  extern unsigned int gc_num_obj;
-  gc_num_obj++;
-#endif
+  initlock((struct ___Object___ *)v);
   return v;
 }
 
@@ -435,13 +511,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 +526,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 +548,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 +557,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 +580,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 +603,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 +655,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 +672,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;
@@ -628,25 +704,26 @@ INLINE void initruntimedata() {
   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 +755,7 @@ INLINE void getprofiledata_I() {
 #endif
 }
 
-INLINE void checkCoreStatus() {
+void checkCoreStatus() {
   int i = 0;
   int sumsendobj = 0;
   if((!waitconfirm) ||
@@ -726,7 +803,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,19 +828,44 @@ INLINE void checkCoreStatus() {
 }
 
 // main function for each core
-inline void run(int argc, char** argv) {
+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();
 
@@ -775,28 +882,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
@@ -818,7 +928,7 @@ inline void run(int argc, char** argv) {
         sendStall = false;
       }
 #endif
-
+      
       if(!tocontinue) {
         // check if stop
         if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
@@ -831,20 +941,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
@@ -856,6 +966,6 @@ inline void run(int argc, char** argv) {
       }
     }
   }
-} 
-
+}
 #endif // MULTICORE