My changes
[IRC.git] / Robust / src / Runtime / bamboo / multicoremsg.c
index 1e8d85a6b626d65db5ced0884ab16c720dc66a07..7be4c17b619969fe430bc8b123a62479e4ab221a 100644 (file)
 #include "multicoremsg.h"
 #include "runtime.h"
 #include "multicoreruntime.h"
+#include "multicoregarbage.h"
 #include "multicoretaskprofile.h"
-
-INLINE int checkMsgLength_I(int size) {
-  int type = msgdata[msgdataindex];
-  switch(type) {
-  case STATUSCONFIRM:
-  case TERMINATE:
+#include "gcqueue.h"
+
+int msgsizearray[] = {
+  0, //MSGSTART,
+ -1, //TRANSOBJ,              // 0xD1
+  4, //TRANSTALL,             // 0xD2
+  5, //LOCKREQUEST,           // 0xD3
+  4, //LOCKGROUNT,            // 0xD4
+  4, //LOCKDENY,              // 0xD5
+  4, //LOCKRELEASE,           // 0xD6
+  2, //PROFILEOUTPUT,         // 0xD7
+  1, //PROFILEFINISH,         // 0xD8
+  6, //REDIRECTLOCK,          // 0xD9
+  4, //REDIRECTGROUNT,        // 0xDa
+  4, //REDIRECTDENY,          // 0xDb
+  4, //REDIRECTRELEASE,       // 0xDc
+  1, //STATUSCONFIRM,         // 0xDd
+  5, //STATUSREPORT,          // 0xDe
+  1, //TERMINATE,             // 0xDf
+  3, //MEMREQUEST,            // 0xE0
+  3, //MEMRESPONSE,           // 0xE1
 #ifdef MULTICORE_GC
-  case GCSTARTPRE:
-  case GCSTART:
-  case GCSTARTINIT:
-  case GCSTARTFLUSH:
-  case GCFINISH:
-  case GCMARKCONFIRM:
-  case GCLOBJREQUEST:
+  1, //GCSTARTPRE,            // 0xE2
+  1, //GCSTARTINIT,           // 0xE3
+  1, //GCSTART,               // 0xE4
+  2, //GCSTARTCOMPACT,        // 0xE5
+  1, //GCSTARTFLUSH,          // 0xE6
+  4, //GCFINISHPRE,           // 0xE7
+  2, //GCFINISHINIT,          // 0xE8
+  4, //GCFINISHMARK,          // 0xE9
+  6, //GCFINISHCOMPACT,       // 0xEa
+  2, //GCFINISHFLUSH,         // 0xEb
+  1, //GCFINISH,              // 0xEc
+  1, //GCMARKCONFIRM,         // 0xEd
+  5, //GCMARKREPORT,          // 0xEe
+  2, //GCMARKEDOBJ,           // 0xEf
+  4, //GCMOVESTART,           // 0xF0
+  1, //GCLOBJREQUEST,         // 0xF1   
+ -1, //GCLOBJINFO,            // 0xF2
+#ifdef GC_PROFILE
+  4, //GCPROFILES,            // 0xF3
+#endif // GC_PROFILE
 #ifdef GC_CACHE_ADAPT
-  case GCSTARTPREF:
-#endif 
-#endif 
-  {
-    msglength = 1;
-    break;
-  }
+  1, //GCSTARTCACHEPOLICY     // 0xF4
+  2, //GCFINISHCACHEPOLICY    // 0xF5
+  1, //GCSTARTPREF,           // 0xF6
+  2, //GCFINISHPREF,          // 0xF7
+#endif // GC_CACHE_ADAPT
+#endif // MULTICORE_GC
+  -1 //MSGEND
+};
 
-#ifdef TASK
-  case PROFILEOUTPUT:
-  case PROFILEFINISH:
+INLINE unsigned int checkMsgLength_I(unsigned int realtype) {
+#if (defined(TASK)||defined(MULTICORE_GC))
+  unsigned int type = realtype & 0xff;
+#else
+  unsigned int type = realtype;
 #endif
-#ifdef MULTICORE_GC
-  case GCSTARTCOMPACT:
-  case GCMARKEDOBJ:
-  case GCFINISHINIT:
-  case GCFINISHFLUSH:
-#ifdef GC_CACHE_ADAPT
-  case GCFINISHPREF:
-#endif 
-#endif 
-  {
-    msglength = 2;
-    break;
-  }
-
-  case MEMREQUEST:
-  case MEMRESPONSE:
-  {
-    msglength = 3;
-    break;
-  }
-
-  case TRANSTALL:
+  BAMBOO_ASSERT(type<=MSGEND);
 #ifdef TASK
-  case LOCKGROUNT:
-  case LOCKDENY:
-  case LOCKRELEASE:
-  case REDIRECTGROUNT:
-  case REDIRECTDENY:
-  case REDIRECTRELEASE:
-#endif
 #ifdef MULTICORE_GC
-  case GCFINISHPRE:
-  case GCFINISHMARK:
-  case GCMOVESTART:
-#ifdef GC_PROFILE
-  case GCPROFILES:
-#endif
-#endif
-  {
-    msglength = 4;
-    break;
-  }
-
-#ifdef TASK
-  case LOCKREQUEST:
+  if(type==TRANSOBJ||type==GCLOBJINFO) {
+#else
+  if(type==TRANSOBJ) {
 #endif
-  case STATUSREPORT:
-#ifdef MULTICORE_GC
-  case GCFINISHCOMPACT:
-  case GCMARKREPORT:
+#elif MULTICORE_GC
+  if (type==GCLOBJINFO) {
 #endif
-  {
-    msglength = 5;
-    break;
+#if (defined(TASK)||defined(MULTICORE_GC))
+    return realtype>>8;
   }
-
-#ifdef TASK
-  case REDIRECTLOCK:
-  {
-    msglength = 6;
-    break;
-  }
-#endif
-
-#ifdef TASK
-  case TRANSOBJ:   // nonfixed size
 #endif
-#ifdef MULTICORE_GC
-  case GCLOBJINFO:
-#endif
-  {  // nonfixed size
-    if(size > 1) {
-      msglength = msgdata[(msgdataindex+1)&(BAMBOO_MSG_BUF_MASK)];
-    } else {
-      return -1;
-    }
-    break;
-  }
-
-  default:
-  {
-    BAMBOO_EXIT(0xe001);
-    break;
-  }
-  }
-  return msglength;
+  return msgsizearray[type];
 }
 
-INLINE void processmsg_transobj_I() {
-  MSG_INDEXINC_I();
+#ifdef TASK
+INLINE void processmsg_transobj_I(int msglength) {
   struct transObjInfo * transObj=RUNMALLOC_I(sizeof(struct transObjInfo));
-  int k = 0;
-  if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
-    BAMBOO_EXIT(0xe201);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE <= NUMCORESACTIVE - 1);
+
   // store the object and its corresponding queue info, enqueue it later
   transObj->objptr = (void *)msgdata[msgdataindex]; 
   MSG_INDEXINC_I();
-  transObj->length = (msglength - 3) / 2;
+  transObj->length = (msglength - 2) / 2;
   transObj->queues = RUNMALLOC_I(sizeof(int)*(msglength - 3));
-  for(k = 0; k < transObj->length; ++k) {
+  for(int k = 0; k < transObj->length; k++) {
     transObj->queues[2*k] = msgdata[msgdataindex];  
     MSG_INDEXINC_I();
     transObj->queues[2*k+1] = msgdata[msgdataindex]; 
     MSG_INDEXINC_I();
   }
   // check if there is an existing duplicate item
-  {
-    struct QueueItem * qitem = getHead(&objqueue);
-    struct QueueItem * prev = NULL;
-    while(qitem != NULL) {
-      struct transObjInfo * tmpinfo =
-        (struct transObjInfo *)(qitem->objectptr);
-      if(tmpinfo->objptr == transObj->objptr) {
-               // the same object, remove outdate one
-               RUNFREE_I(tmpinfo->queues);
-               RUNFREE_I(tmpinfo);
-               removeItem(&objqueue, qitem);
-               //break;
-      } else {
-               prev = qitem;
-      }
-      if(prev == NULL) {
-               qitem = getHead(&objqueue);
-      } else {
-               qitem = getNextQueueItem(prev);
-      }
+  struct QueueItem * prev = NULL;
+  for(struct QueueItem * qitem = getHead(&objqueue);qitem != NULL;qitem=(prev==NULL)?getHead(&objqueue):getNextQueueItem(prev)) {
+    struct transObjInfo * tmpinfo = (struct transObjInfo *)(qitem->objectptr);
+    if(tmpinfo->objptr == transObj->objptr) {
+      // the same object, remove outdate one
+      RUNFREE_I(tmpinfo->queues);
+      RUNFREE_I(tmpinfo);
+      removeItem(&objqueue, qitem);
+      //break;
+    } else {
+      prev = qitem;
     }
-    addNewItem_I(&objqueue, (void *)transObj);
   }
-  ++(self_numreceiveobjs);
+  addNewItem_I(&objqueue, (void *)transObj);
+  
+  self_numreceiveobjs++;
 #ifdef MULTICORE_GC
-  if(gcprocessing) {
+  if(gc_status_info.gcprocessing) {
     if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
       // set the gcprecheck to enable checking again
       gcprecheck = true;
     } else {
       // send a update pregc information msg to the master core
       if(BAMBOO_CHECK_SEND_MODE()) {
-        cache_msg_4(STARTUPCORE, GCFINISHPRE, BAMBOO_NUM_OF_CORE, 
-            self_numsendobjs, self_numreceiveobjs);
+        cache_msg_4_I(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
       } else {
-        send_msg_4(STARTUPCORE, GCFINISHPRE, BAMBOO_NUM_OF_CORE, 
-            self_numsendobjs, self_numreceiveobjs, true);
+        send_msg_4_I(STARTUPCORE,GCFINISHPRE,BAMBOO_NUM_OF_CORE,self_numsendobjs, self_numreceiveobjs);
       }
     }
   }
 #endif 
 }
+#endif
 
 INLINE void processmsg_transtall_I() {
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive stall msg
-    BAMBOO_EXIT(0xe002);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+  
   int num_core = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   int data2 = msgdata[msgdataindex]; 
@@ -212,40 +160,31 @@ INLINE void processmsg_lockrequest_I() {
   MSG_INDEXINC_I();
   // -1: redirected, 0: approved, 1: denied
   int deny=processlockrequest(locktype, data3, data2, data4, data4, true);
-  if(deny == -1) {
-    // this lock request is redirected
-    return;
-  } else {
+  if(deny != -1) {
     // send response msg
     // for 32 bit machine, the size is always 4 words, cache the msg first
     int tmp = deny==1 ? LOCKDENY : LOCKGROUNT;
     if(BAMBOO_CHECK_SEND_MODE()) {
-         cache_msg_4(data4, tmp, locktype, data2, data3);
+      cache_msg_4_I(data4,tmp,locktype,data2,data3);
     } else {
-         send_msg_4(data4, tmp, locktype, data2, data3, true);
+      send_msg_4_I(data4,tmp,locktype,data2,data3);
     }
   }
 }
 
 INLINE void processmsg_lockgrount_I() {
   MSG_INDEXINC_I();
-  if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
-    BAMBOO_EXIT(0xe202);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE <= NUMCORESACTIVE - 1);
   int data2 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   int data3 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
-  if((lockobj == data2) && (lock2require == data3)) {
-    lockresult = 1;
-    lockflag = true;
+  BAMBOO_ASSERT((lockobj == data2) && (lock2require == data3));
+  lockresult = 1;
+  lockflag = true;
 #ifndef INTERRUPT
-    reside = false;
+  reside = false;
 #endif
-  } else {
-    // conflicts on lockresults
-    BAMBOO_EXIT(0xe203);
-  }
 }
 
 INLINE void processmsg_lockdeny_I() {
@@ -254,19 +193,13 @@ INLINE void processmsg_lockdeny_I() {
   MSG_INDEXINC_I();
   int data3 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
-  if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
-    BAMBOO_EXIT(0xe204);
-  }
-  if((lockobj == data2) && (lock2require == data3)) {
-    lockresult = 0;
-    lockflag = true;
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE <= NUMCORESACTIVE - 1);
+  BAMBOO_ASSERT((lockobj == data2) && (lock2require == data3));
+  lockresult = 0;
+  lockflag = true;
 #ifndef INTERRUPT
-    reside = false;
+  reside = false;
 #endif
-  } else {
-    // conflicts on lockresults
-    BAMBOO_EXIT(0xe205);
-  }
 }
 
 INLINE void processmsg_lockrelease_I() {
@@ -292,19 +225,14 @@ INLINE void processmsg_redirectlock_I() {
   MSG_INDEXINC_I();    // root request core
   int data5 = msgdata[msgdataindex];
   MSG_INDEXINC_I();    // request core
-  int deny = processlockrequest(data1, data3, data2, data5, data4, true);
-  if(deny == -1) {
-    // this lock request is redirected
-    return;
-  } else {
+  int deny = processlockrequest_I(data1, data3, data2, data5, data4, true);
+  if(deny != -1) {
     // send response msg
     // for 32 bit machine, the size is always 4 words, cache the msg first
     if(BAMBOO_CHECK_SEND_MODE()) {
-      cache_msg_4(data4, deny==1 ? REDIRECTDENY : REDIRECTGROUNT,
-          data1, data2, data3);
+      cache_msg_4_I(data4,deny==1?REDIRECTDENY:REDIRECTGROUNT,data1,data2,data3);
     } else {
-      send_msg_4(data4, deny==1?REDIRECTDENY:REDIRECTGROUNT,
-          data1, data2, data3, true);
+      send_msg_4_I(data4,deny==1?REDIRECTDENY:REDIRECTGROUNT,data1,data2,data3);
     }
   }
 }
@@ -313,22 +241,16 @@ INLINE void processmsg_redirectgrount_I() {
   MSG_INDEXINC_I();
   int data2 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
-  if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
-    BAMBOO_EXIT(0xe206);
-  }
-  if(lockobj == data2) {
-    int data3 = msgdata[msgdataindex];
-    MSG_INDEXINC_I();
-    lockresult = 1;
-    lockflag = true;
-    RuntimeHashadd_I(objRedirectLockTbl, lockobj, data3);
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE <= NUMCORESACTIVE - 1);
+  BAMBOO_ASSERT(lockobj == data2, 0xe207);
+  int data3 = msgdata[msgdataindex];
+  MSG_INDEXINC_I();
+  lockresult = 1;
+  lockflag = true;
+  RuntimeHashadd_I(objRedirectLockTbl, lockobj, data3);
 #ifndef INTERRUPT
-    reside = false;
+  reside = false;
 #endif
-  } else {
-    // conflicts on lockresults
-    BAMBOO_EXIT(0xe207);
-  }
 }
 
 INLINE void processmsg_redirectdeny_I() {
@@ -337,19 +259,13 @@ INLINE void processmsg_redirectdeny_I() {
   MSG_INDEXINC_I();
   int data3 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
-  if(BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1) {
-    BAMBOO_EXIT(0xe208);
-  }
-  if(lockobj == data2) {
-    lockresult = 0;
-    lockflag = true;
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE <= NUMCORESACTIVE - 1);
+  BAMBOO_ASSERT(lockobj == data2);
+  lockresult = 0;
+  lockflag = true;
 #ifndef INTERRUPT
-    reside = false;
+  reside = false;
 #endif
-  } else {
-    // conflicts on lockresults
-    BAMBOO_EXIT(0xe209);
-  }
 }
 
 INLINE void processmsg_redirectrelease_I() {
@@ -359,16 +275,13 @@ INLINE void processmsg_redirectrelease_I() {
   MSG_INDEXINC_I();
   int data3 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
-  processlockrelease(data1, data2, data3, true);
+  processlockrelease_I(data1, data2, data3, true);
 }
 #endif // #ifndef MULTICORE_GC
 
 #ifdef PROFILE
 INLINE void processmsg_profileoutput_I() {
-  if(BAMBOO_NUM_OF_CORE == STARTUPCORE) {
-    // startup core can not receive profile output finish msg
-    BAMBOO_EXIT(0xe20a);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE != STARTUPCORE);
   stall = true;
   totalexetime = msgdata[msgdataindex];
   MSG_INDEXINC_I();
@@ -378,38 +291,26 @@ INLINE void processmsg_profileoutput_I() {
 #endif
   // cache the msg first
   if(BAMBOO_CHECK_SEND_MODE()) {
-    cache_msg_2(STARTUPCORE, PROFILEFINISH, BAMBOO_NUM_OF_CORE);
+    cache_msg_1_I(STARTUPCORE,PROFILEFINISH);
   } else {
-    send_msg_2(STARTUPCORE, PROFILEFINISH, BAMBOO_NUM_OF_CORE, true);
+    send_msg_1_I(STARTUPCORE,PROFILEFINISH);
   }
 }
 
 INLINE void processmsg_profilefinish_I() {
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive profile output finish msg
-    BAMBOO_EXIT(0xe20b);
-  }
-  int data1 = msgdata[msgdataindex];
-  MSG_INDEXINC_I();
-  profilestatus[data1] = 0;
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+  numconfirm--;
 }
 #endif // PROFILE
 
 INLINE void processmsg_statusconfirm_I() {
-  if((BAMBOO_NUM_OF_CORE == STARTUPCORE)
-     || (BAMBOO_NUM_OF_CORE > NUMCORESACTIVE - 1)) {
-    // wrong core to receive such msg
-    BAMBOO_EXIT(0xe003);
+  BAMBOO_ASSERT(((BAMBOO_NUM_OF_CORE != STARTUPCORE) && (BAMBOO_NUM_OF_CORE <= NUMCORESACTIVE - 1)));
+  // send response msg
+  // cache the msg first
+  if(BAMBOO_CHECK_SEND_MODE()) {
+    cache_msg_5_I(STARTUPCORE,STATUSREPORT,busystatus?1:0,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
   } else {
-    // send response msg
-    // cache the msg first
-    if(BAMBOO_CHECK_SEND_MODE()) {
-      cache_msg_5(STARTUPCORE,STATUSREPORT,busystatus?1:0,BAMBOO_NUM_OF_CORE,
-          self_numsendobjs, self_numreceiveobjs);
-    } else {
-      send_msg_5(STARTUPCORE,STATUSREPORT,busystatus?1:0,BAMBOO_NUM_OF_CORE,
-          self_numsendobjs,self_numreceiveobjs, true);
-    }
+    send_msg_5_I(STARTUPCORE,STATUSREPORT,busystatus?1:0,BAMBOO_NUM_OF_CORE,self_numsendobjs,self_numreceiveobjs);
   }
 }
 
@@ -423,17 +324,13 @@ INLINE void processmsg_statusreport_I() {
   int data4 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // receive a status confirm info
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // wrong core to receive such msg
-    BAMBOO_EXIT(0xe004);
-  } else {
-    if(waitconfirm) {
-      numconfirm--;
-    }
-    corestatus[data2] = data1;
-    numsendobjs[data2] = data3;
-    numreceiveobjs[data2] = data4;
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+  if(waitconfirm) {
+    numconfirm--;
   }
+  corestatus[data2] = data1;
+  numsendobjs[data2] = data3;
+  numreceiveobjs[data2] = data4;
 }
 
 INLINE void processmsg_terminate_I() {
@@ -452,33 +349,27 @@ INLINE void processmsg_memrequest_I() {
   int data2 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // receive a shared memory request msg
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // wrong core to receive such msg
-    BAMBOO_EXIT(0xe005);
-  } else {
-    int allocsize = 0;
-    void * mem = NULL;
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+  int allocsize = 0;
+  void * mem = NULL;
 #ifdef MULTICORE_GC
-    if(gcprocessing && gcflag) {
-      // is currently doing GC and the master core did not decide to stop GC
-    } else {
-      // either not doing GC or the master core has decided to stop GC but 
-      // // still sending msgs to other cores to inform them to stop the GC
+  if(!gc_status_info.gcprocessing || !gcflag) {
+    // either not doing GC or the master core has decided to stop GC but 
+    // // still sending msgs to other cores to inform them to stop the GC
 #endif
-      mem = smemalloc_I(data2, data1, &allocsize);
-      if(mem != NULL) {
-        // send the start_va to request core, cache the msg first
-        if(BAMBOO_CHECK_SEND_MODE()) {
-          cache_msg_3(data2, MEMRESPONSE, mem, allocsize);
-        } else {
-          send_msg_3(data2, MEMRESPONSE, mem, allocsize, true);
-        }
-      } //else if mem == NULL, the gcflag of the startup core has been set
-        // and all the other cores have been informed to start gc
+    mem = smemalloc_I(data2, data1, &allocsize);
+    if(mem != NULL) {
+      // send the start_va to request core, cache the msg first
+      if(BAMBOO_CHECK_SEND_MODE()) {
+        cache_msg_3_I(data2,MEMRESPONSE,mem,allocsize);
+      } else {
+        send_msg_3_I(data2,MEMRESPONSE,mem,allocsize);
+      }
+    } //else if mem == NULL, the gcflag of the startup core has been set
+    // and all the other cores have been informed to start gc
 #ifdef MULTICORE_GC
-    }
-#endif
   }
+#endif
 }
 
 INLINE void processmsg_memresponse_I() {
@@ -489,7 +380,7 @@ INLINE void processmsg_memresponse_I() {
   // receive a shared memory response msg
 #ifdef MULTICORE_GC
   // if is currently doing gc, dump this msg
-  if(!gcprocessing) {
+  if(!gc_status_info.gcprocessing) {
 #endif
   if(data2 == 0) {
 #ifdef MULTICORE_GC
@@ -521,39 +412,38 @@ INLINE void processmsg_memresponse_I() {
 
 #ifdef MULTICORE_GC
 INLINE void processmsg_gcstartpre_I() {
-       // the first time to be informed to start gc
-  tprintf("pre msg \n");
-       gcflag = true;
-       if(!smemflag) {
+  // the first time to be informed to start gc
+  gcflag = true;
+  if(!smemflag) {
     // Zero out the remaining memory here because for the GC_CACHE_ADAPT 
     // version, we need to make sure during the gcinit phase the shared heap 
     // is not touched. Otherwise, there would be problem when adapt the cache 
     // strategy.
     BAMBOO_CLOSE_CUR_MSP();
-         bamboo_smem_size = 0;
-         bamboo_cur_msp = NULL;
-         smemflag = true;
-         bamboo_smem_zero_top = NULL;
-       }
+    bamboo_smem_size = 0;
+    bamboo_cur_msp = NULL;
+    smemflag = true;
+    bamboo_smem_zero_top = NULL;
+  }
 }
 
 INLINE void processmsg_gcstartinit_I() {
-  gcphase = INITPHASE;
+  gc_status_info.gcphase = INITPHASE;
 }
 
 INLINE void processmsg_gcstart_I() {
   // set the GC flag
-  gcphase = MARKPHASE;
+  gc_status_info.gcphase = MARKPHASE;
 }
 
 INLINE void processmsg_gcstartcompact_I() {
   gcblock2fill = msgdata[msgdataindex];
   MSG_INDEXINC_I();  
-  gcphase = COMPACTPHASE;
+  gc_status_info.gcphase = COMPACTPHASE;
 }
 
 INLINE void processmsg_gcstartflush_I() {
-  gcphase = FLUSHPHASE;
+  gc_status_info.gcphase = FLUSHPHASE;
 }
 
 INLINE void processmsg_gcfinishpre_I() {
@@ -564,14 +454,10 @@ INLINE void processmsg_gcfinishpre_I() {
   int data3 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // received a init phase finish msg
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive this msg
-    BAMBOO_EXIT(0xe006);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+
   // All cores should do init GC
-  if(!gcprecheck) {
-    gcprecheck = true;
-  }
+  gcprecheck = true;
   gccorestatus[data1] = 0;
   gcnumsendobjs[0][data1] = data2;
   gcnumreceiveobjs[0][data1] = data3;
@@ -581,10 +467,8 @@ INLINE void processmsg_gcfinishinit_I() {
   int data1 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // received a init phase finish msg
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive this msg
-    BAMBOO_EXIT(0xe007);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+
   // All cores should do init GC
   if(data1 < NUMCORESACTIVE) {
     gccorestatus[data1] = 0;
@@ -599,10 +483,8 @@ INLINE void processmsg_gcfinishmark_I() {
   int data3 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // received a mark phase finish msg
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive this msg
-    BAMBOO_EXIT(0xe008);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+
   // all cores should do mark
   if(data1 < NUMCORESACTIVE) {
     gccorestatus[data1] = 0;
@@ -620,13 +502,12 @@ INLINE void processmsg_gcfinishmark_I() {
 }
 
 INLINE void processmsg_gcfinishcompact_I() {
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive this msg
-    // return -1
-    BAMBOO_EXIT(0xe009);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+
   int cnum = msgdata[msgdataindex];
-  MSG_INDEXINC_I();      
+  MSG_INDEXINC_I();  
+  bool loadbalancemove = msgdata[msgdataindex];
+  MSG_INDEXINC_I();
   int filledblocks = msgdata[msgdataindex];
   MSG_INDEXINC_I();    
   int heaptop = msgdata[msgdataindex];
@@ -635,7 +516,7 @@ INLINE void processmsg_gcfinishcompact_I() {
   MSG_INDEXINC_I(); 
   // only gc cores need to do compact
   if(cnum < NUMCORES4GC) {
-    if(COMPACTPHASE == gcphase) {
+    if(!loadbalancemove && (COMPACTPHASE == gc_status_info.gcphase)) {
       gcfilledblocks[cnum] = filledblocks;
       gcloads[cnum] = heaptop;
     }
@@ -647,9 +528,9 @@ INLINE void processmsg_gcfinishcompact_I() {
       if(gcfindSpareMem_I(&startaddr, &tomove, &dstcore, data4, cnum)) {
         // cache the msg first
         if(BAMBOO_CHECK_SEND_MODE()) {
-          cache_msg_4(cnum, GCMOVESTART, dstcore, startaddr, tomove);
+          cache_msg_4_I(cnum,GCMOVESTART,dstcore,startaddr,tomove);
         } else {
-          send_msg_4(cnum, GCMOVESTART, dstcore, startaddr, tomove, true);
+          send_msg_4_I(cnum,GCMOVESTART,dstcore,startaddr,tomove);
         }
       }
     } else {
@@ -662,10 +543,8 @@ INLINE void processmsg_gcfinishflush_I() {
   int data1 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // received a flush phase finish msg
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive this msg
-    BAMBOO_EXIT(0xe00a);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+
   // all cores should do flush
   if(data1 < NUMCORESACTIVE) {
     gccorestatus[data1] = 0;
@@ -674,24 +553,18 @@ INLINE void processmsg_gcfinishflush_I() {
 
 INLINE void processmsg_gcfinish_I() {
   // received a GC finish msg
-  gcphase = FINISHPHASE;
-  gcprocessing = false;
+  gc_status_info.gcphase = FINISHPHASE;
+  gc_status_info.gcprocessing = false;
 }
 
 INLINE void processmsg_gcmarkconfirm_I() {
-  if((BAMBOO_NUM_OF_CORE==STARTUPCORE)||(BAMBOO_NUM_OF_CORE>NUMCORESACTIVE-1)){
-    // wrong core to receive such msg
-    BAMBOO_EXIT(0xe00b);
+  BAMBOO_ASSERT(((BAMBOO_NUM_OF_CORE!=STARTUPCORE)&&(BAMBOO_NUM_OF_CORE<=NUMCORESACTIVE-1)));
+  gc_status_info.gcbusystatus = gc_moreItems2_I();
+  // send response msg, cahce the msg first
+  if(BAMBOO_CHECK_SEND_MODE()) {
+    cache_msg_5_I(STARTUPCORE,GCMARKREPORT,BAMBOO_NUM_OF_CORE,gc_status_info.gcbusystatus,gcself_numsendobjs,gcself_numreceiveobjs);
   } else {
-       gcbusystatus = gc_moreItems2_I();
-    // send response msg, cahce the msg first
-    if(BAMBOO_CHECK_SEND_MODE()) {
-      cache_msg_5(STARTUPCORE,GCMARKREPORT,BAMBOO_NUM_OF_CORE,gcbusystatus,
-          gcself_numsendobjs,gcself_numreceiveobjs);
-    } else {
-      send_msg_5(STARTUPCORE,GCMARKREPORT,BAMBOO_NUM_OF_CORE,gcbusystatus,
-          gcself_numsendobjs,gcself_numreceiveobjs, true);
-    }
+    send_msg_5_I(STARTUPCORE,GCMARKREPORT,BAMBOO_NUM_OF_CORE,gc_status_info.gcbusystatus,gcself_numsendobjs,gcself_numreceiveobjs);
   }
 }
 
@@ -705,31 +578,24 @@ INLINE void processmsg_gcmarkreport_I() {
   int data4 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // received a marked phase finish confirm response msg
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // wrong core to receive such msg
-    BAMBOO_EXIT(0xe00c);
-  } else {
-       int entry_index = 0;
-    if(waitconfirm) {
-      // phse 2
-      numconfirm--;
-      entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
-    } else {
-      // can never reach here
-      BAMBOO_EXIT(0xe00d);
-    }
-    gccorestatus[data1] = data2;
-    gcnumsendobjs[entry_index][data1] = data3;
-    gcnumreceiveobjs[entry_index][data1] = data4;
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+  int entry_index = 0;
+  BAMBOO_ASSERT(waitconfirm);
+
+  // phase 2
+  numconfirm--;
+  entry_index = (gcnumsrobjs_index == 0) ? 1 : 0;
+  gccorestatus[data1] = data2;
+  gcnumsendobjs[entry_index][data1] = data3;
+  gcnumreceiveobjs[entry_index][data1] = data4;
+
 }
 
 INLINE void processmsg_gcmarkedobj_I() {
   int data1 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
-  if(!ISSHAREDOBJ(data1)) {
-    BAMBOO_EXIT(0xa0000000+(int)data1);
-  }
+  BAMBOO_ASSERT(ISSHAREDOBJ(data1));
+
   // received a markedObj msg
   if(((struct ___Object___ *)data1)->marked == INIT) {
     // this is the first time that this object is discovered,
@@ -738,7 +604,7 @@ INLINE void processmsg_gcmarkedobj_I() {
     gc_enqueue_I(data1);
   }
   gcself_numreceiveobjs++;
-  gcbusystatus = true;
+  gc_status_info.gcbusystatus = true;
 }
 
 INLINE void processmsg_gcmovestart_I() {
@@ -751,16 +617,12 @@ INLINE void processmsg_gcmovestart_I() {
   MSG_INDEXINC_I();     
 }
 
-INLINE void processmsg_gclobjinfo_I() {
+INLINE void processmsg_gclobjinfo_I(unsigned int data1) {
   numconfirm--;
-
-  int data1 = msgdata[msgdataindex];
-  MSG_INDEXINC_I();
   int data2 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
-  if(BAMBOO_NUM_OF_CORE > NUMCORES4GC - 1) {
-    BAMBOO_EXIT(0xe00e);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE <= NUMCORES4GC - 1);
+
   // store the mark result info
   int cnum = data2;
   gcloads[cnum] = msgdata[msgdataindex];
@@ -771,7 +633,7 @@ INLINE void processmsg_gclobjinfo_I() {
     gcheaptop = data4;
   }
   // large obj info here
-  for(int k = 5; k < data1; k+=2) {
+  for(int k = 4; k < data1; k+=2) {
     int lobj = msgdata[msgdataindex];
     MSG_INDEXINC_I();  
     int length = msgdata[msgdataindex];
@@ -792,9 +654,9 @@ INLINE void processmsg_gcprofiles_I() {
 #ifdef MGC_SPEC
   if(gc_profile_flag) {
 #endif
-  gc_num_obj += data1;
-  gc_num_liveobj += data2;
-  gc_num_forwardobj += data3;
+    gc_num_obj += data1;
+    gc_num_liveobj += data2;
+    gc_num_forwardobj += data3;
 #ifdef MGC_SPEC
   }
 #endif
@@ -803,18 +665,31 @@ INLINE void processmsg_gcprofiles_I() {
 #endif // GC_PROFILE
 
 #ifdef GC_CACHE_ADAPT
+INLINE void processmsg_gcstartcachepolicy_I() {
+  gc_status_info.gcphase = CACHEPOLICYPHASE;
+}
+
+INLINE void processmsg_gcfinishcachepolicy_I() {
+  int data1 = msgdata[msgdataindex];
+  MSG_INDEXINC_I();
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+
+  // all cores should do flush
+  if(data1 < NUMCORESACTIVE) {
+    gccorestatus[data1] = 0;
+  }
+}
+
 INLINE void processmsg_gcstartpref_I() {
-  gcphase = PREFINISHPHASE;
+  gc_status_info.gcphase = PREFINISHPHASE;
 }
 
 INLINE void processmsg_gcfinishpref_I() {
   int data1 = msgdata[msgdataindex];
   MSG_INDEXINC_I();
   // received a flush phase finish msg
-  if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
-    // non startup core can not receive this msg
-    BAMBOO_EXIT(0xe00f);
-  }
+  BAMBOO_ASSERT(BAMBOO_NUM_OF_CORE == STARTUPCORE);
+
   // all cores should do flush
   if(data1 < NUMCORESACTIVE) {
     gccorestatus[data1] = 0;
@@ -837,18 +712,23 @@ INLINE void processmsg_gcfinishpref_I() {
 //               3--received a lock Msg
 //               RAW version: -1 -- received nothing
 //                            otherwise -- received msg type
-int receiveObject(int send_port_pending) {
+int receiveObject_I() {
   PROFILE_INTERRUPT_START(); 
 msg:
   // get the incoming msgs
-  if(receiveMsg(send_port_pending) == -1) {
+  receiveMsg_I();
+  if((msgdataindex == msgdatalast) && (!msgdatafull)) {
     return -1;
   }
+  if(BAMBOO_CHECK_SEND_MODE()) {
+    // during send, don't process the msg now
+    return -3; 
+  }
 processmsg:
   // processing received msgs
   int size = 0;
   MSG_REMAINSIZE_I(&size);
-  if((size == 0) || (checkMsgLength_I(size) == -1)) {
+  if(size == 0) {
     // not a whole msg
     // have new coming msg
     if((BAMBOO_MSG_AVAIL() != 0) && !msgdatafull) {
@@ -858,21 +738,28 @@ processmsg:
     }
   }
 
+  //we only ever read the first word
+  unsigned int realtype = msgdata[msgdataindex];
+  unsigned int msglength = checkMsgLength_I(realtype);
+
+#if (defined(TASK)||defined(MULTICORE_GC))
+  unsigned int type = realtype & 0xff;
+#else
+  unsigned int type = realtype;
+#endif
+
   if(msglength <= size) {
     // have some whole msg
-    MSGTYPE type;
-    type = msgdata[msgdataindex]; //[0]
     MSG_INDEXINC_I();
     msgdatafull = false;
     switch(type) {
 #ifdef TASK
     case TRANSOBJ: {
       // receive a object transfer msg
-      processmsg_transobj_I();
+      processmsg_transobj_I(msglength);
       break;
     }  
 #endif 
-
     case TRANSTALL: {
       // receive a stall msg
       processmsg_transtall_I();
@@ -887,19 +774,16 @@ processmsg:
       processmsg_lockrequest_I();
       break;
     }   
-
     case LOCKGROUNT: {
       // receive lock grount msg
       processmsg_lockgrount_I();
       break;
     } 
-
     case LOCKDENY: {
       // receive lock deny msg
       processmsg_lockdeny_I();
       break;
     }  
-
     case LOCKRELEASE: {
       processmsg_lockrelease_I();
       break;
@@ -912,7 +796,6 @@ processmsg:
       processmsg_profileoutput_I();
       break;
     }   
-
     case PROFILEFINISH: {
       // receive a profile output finish msg
       processmsg_profilefinish_I();
@@ -1067,7 +950,7 @@ processmsg:
 
     case GCLOBJINFO: {
       // received a large objs info response msg
-      processmsg_gclobjinfo_I();
+      processmsg_gclobjinfo_I(msglength);
       break;
     } 
 
@@ -1080,6 +963,18 @@ processmsg:
 #endif // GC_PROFILE
 
 #ifdef GC_CACHE_ADAPT
+    case GCSTARTCACHEPOLICY: {
+      // received a gcstartcachepolicy msg
+      processmsg_gcstartcachepolicy_I();
+      break;
+    }
+
+    case GCFINISHCACHEPOLICY: {
+      // received a gcfinishcachepolicy msg
+      processmsg_gcfinishcachepolicy_I();
+      break;
+    }
+
     case GCSTARTPREF: {
       // received a gcstartpref msg
       processmsg_gcstartpref_I();
@@ -1097,7 +992,6 @@ processmsg:
     default:
       break;
     }
-    msglength = BAMBOO_MSG_BUF_LENGTH;
 
     if((msgdataindex != msgdatalast) || (msgdatafull)) {
       // still have available msg