towards fixing bugs...
[IRC.git] / Robust / src / Runtime / bamboo / multicorecache.c
index 2de873f0192a9e1b4163313250bd72e2166b1faf..377bc698a11271e5b41eecf1dd11a9972d1d9aeb 100644 (file)
 #include "multicoremsg.h"
 #include "multicoregcprofile.h"
 
-gc_cache_revise_info_t gc_cache_revise_information;
-
-/* This function initialize the gc_cache_revise_information. It should be 
- * invoked before we start compaction.
- */
-void samplingDataReviseInit(struct moveHelper * orig,struct moveHelper * to) {
-  // initialize the destination page info
-  gc_cache_revise_information.to_page_start_va=to->ptr;
-  unsigned int toindex=(unsigned INTPTR)(to->base-gcbaseva)>>BAMBOO_PAGE_SIZE_BITS;
-  gc_cache_revise_information.to_page_end_va=gcbaseva+BAMBOO_PAGE_SIZE*(toindex+1);
-  gc_cache_revise_information.to_page_index=toindex;
-  // initilaize the original page info
-  unsigned int origindex=((unsigned INTPTR)(orig->base-gcbaseva))>>BAMBOO_PAGE_SIZE_BITS;
-  gc_cache_revise_information.orig_page_start_va=orig->ptr;
-  gc_cache_revise_information.orig_page_end_va=gcbaseva+BAMBOO_PAGE_SIZE*(origindex+1);
-  gc_cache_revise_information.orig_page_index=origindex;
-}
+void cacheadapt_finish_src_page(void *srcptr, void *tostart, void *tofinish) {
+  unsigned int srcpage=(srcptr-gcbaseva)>>BAMBOO_PAGE_SIZE_BITS;
+  unsigned int dstpage=(tostart-gcbase)>>BAMBOO_PAGE_SIZE_BITS;
+  unsigned int numbytes=tofinish-tostart;
+  
+  unsigned int * oldtable=&gccachesamplingtbl[srcpage*NUMCORESACTIVE];
+  unsigned int * newtable=&gccachesamplingtbl_r[dstpage*NUMCORESACTIVE];
+  
+  unsigned int page64th=numbytes>>(BAMBOO_PAGE_SIZE_BITS-6);
 
-/* This function computes the revised profiling data of the first closed destination 
- * page of an object that acrosses multiple pages
- */
-void firstPageConvert(bool origclosefirst, unsigned INTPTR main_factor, unsigned INTPTR delta_factor) {
-  unsigned INTPTR topage=gc_cache_revise_information.to_page_index*NUMCORESACTIVE;
-  unsigned INTPTR oldpage=gc_cache_revise_information.orig_page_index*NUMCORESACTIVE;
-  int * newtable=&gccachesamplingtbl_r[topage];
-  int * oldtable=&gccachesamplingtbl[oldpage];
-  // compute the revised profiling info for the start destination page
-  if(origclosefirst) {
-    // the start original page closes first, now compute the revised profiling
-    // info for the start destination page.
-    // The start destination page = the rest of the start original page + 
-    //                              delta_fator from the next original page
-    int * oldtable_next=&gccachesamplingtbl[oldpage+NUMCORESACTIVE];
-    for(int tt = 0; tt < NUMCORESACTIVE; tt++) {
-      (*newtable)=(*newtable)+((*oldtable)*main_factor+(*oldtable_next)*delta_factor)>>BAMBOO_PAGE_SIZE_BITS;
-      newtable++;
-      oldtable++;
-      oldtable_next++;
-    }
-    // close the start original page 
-    gc_cache_revise_information.orig_page_start_va+=main_factor+delta_factor;
-    gc_cache_revise_information.orig_page_end_va+=BAMBOO_PAGE_SIZE;
-    gc_cache_revise_information.orig_page_index++;
-  } else {
-    // the start destination page closes first, now compute the revised 
-    // profiling info for it.
-    for(int tt = 0; tt < NUMCORESACTIVE; tt++) {
-      (*newtable)=(*newtable)+((*oldtable)*main_factor)>>BAMBOO_PAGE_SIZE_BITS;
-      newtable++;
-      oldtable++;
-    }
-    // record the new start of the original page
-    gc_cache_revise_information.orig_page_start_va+=main_factor;
-  }
-  // close the start original page and destination page
-  gc_cache_revise_information.to_page_start_va=gc_cache_revise_information.to_page_end_va;
-  gc_cache_revise_information.to_page_end_va+=BAMBOO_PAGE_SIZE;
-  gc_cache_revise_information.to_page_index++;
+  for(int core = 0; core < NUMCORESACTIVE; core++) {
+    (*newtable)+=page64th*(*oldtable);
+    newtable++;
+    oldtable++;
+  }  
 }
 
-/* This function computes the revised profiling info for closed destination 
- * pages that are occupied by one object that acrosses multiple pages.
- * the destination page = main_factor from the first unclosed original page 
- *                       + delta_factor from the next unclosed original page
- */
-void restClosedPageConvert(void * current_ptr, unsigned INTPTR main_factor, unsigned INTPTR delta_factor) {
-  while(gc_cache_revise_information.to_page_end_va<=current_ptr) {
-    unsigned INTPTR topage=gc_cache_revise_information.to_page_index*NUMCORESACTIVE;
-    unsigned INTPTR oldpage=gc_cache_revise_information.orig_page_index*NUMCORESACTIVE;
-    int *newtable=&gccachesamplingtbl_r[topage];
-    int *oldtable=&gccachesamplingtbl[oldpage];
-    int *oldtable_next=&gccachesamplingtbl[oldpage+NUMCORESACTIVE];
-
-    for(int tt = 0; tt < NUMCORESACTIVE; tt++) {
-      (*newtable)=(*newtable)+((*oldtable)*main_factor+(*oldtable_next)*delta_factor)>>BAMBOO_PAGE_SIZE_BITS;
-      newtable++;
-      oldtable++;
-      oldtable_next++;
-    }
+void cacheadapt_finish_dst_page(void *origptr, void *tostart, void *toptr, unsigned int bytesneeded) {
+  unsigned int numbytes=toptr-tostart;
 
-    // close the original page and the destination page
-    gc_cache_revise_information.orig_page_start_va+=BAMBOO_PAGE_SIZE;
-    gc_cache_revise_information.orig_page_end_va+=BAMBOO_PAGE_SIZE;
-    gc_cache_revise_information.orig_page_index++;
-    gc_cache_revise_information.to_page_start_va=gc_cache_revise_information.to_page_end_va;
-    gc_cache_revise_information.to_page_end_va+=BAMBOO_PAGE_SIZE;
-    gc_cache_revise_information.to_page_index++;
-  }
-}
+  void *tobound=(tostart&~(BAMBOO_PAGE_SIZE-1))+BAMBOO_PAGE_SIZE;
+  void *origbound=(origstart&~(BAMBOO_PAGE_SIZE-1))+BAMBOO_PAGE_SIZE;
+  
+  unsigned int topage=(tostart-gcbase)>>BAMBOO_PAGE_SIZE_BITS; 
+  unsigned int origpage=(origptr-gcbaseva)>>BAMBOO_PAGE_SIZE_BITS;
 
-/* This function computes the revised profiling info for the last
- * destination page of an object that acrosses multiple pages.
- */
-void lastPageConvert(void * current_ptr) {
-  unsigned INTPTR to_factor=current_ptr-gc_cache_revise_information.to_page_start_va;
-  unsigned INTPTR topage=gc_cache_revise_information.to_page_index*NUMCORESACTIVE;
-  unsigned INTPTR oldpage=gc_cache_revise_information.orig_page_index*NUMCORESACTIVE;
-  int *newtable=&gccachesamplingtbl_r[topage];
-  int *oldtable=&gccachesamplingtbl[oldpage];
+  unsigned int * totable=&gccachesamplingtbl_r[topage*NUMCORESACTIVE];
+  unsigned int * origtable=&gccachesamplingtbl[origpage*NUMCORESACTIVE];
 
-  for(int tt = 0; tt < NUMCORESACTIVE; tt++) {
-    (*newtable)=(*newtable)+((*oldtable)*to_factor)>>BAMBOO_PAGE_SIZE_BITS;
-    newtable++;
-    oldtable++;
-  }
-  // do not need to set gc_cache_revise_information here for the last 
-  // original/destination page as it will be set in completePageConvert()
-}
-
-/* This function converts multiple original pages profiling data to multiple 
- * destination pages' profiling data
- */
-void samplingDataConvertMultiple(void * current_ptr) {
-  // first decide which page close first: original or destination?
-  unsigned INTPTR to_factor=(unsigned INTPTR)(gc_cache_revise_information.to_page_end_va-gc_cache_revise_information.to_page_start_va);
-  unsigned INTPTR orig_factor=(unsigned INTPTR)(gc_cache_revise_information.orig_page_end_va-gc_cache_revise_information.orig_page_start_va);
-  bool origclosefirst=to_factor>orig_factor;
-  unsigned INTPTR delta_factor=(origclosefirst)?(to_factor-orig_factor):(orig_factor-to_factor);
-  unsigned INTPTR main_factor=(origclosefirst)?orig_factor:to_factor;
+  unsigned int remaintobytes=tobound-toptr;
+  unsigned int remainorigbytes=origbound-origptr;
 
-  // compute the revised profiling info for the start destination page
-  firstPageConvert(origclosefirst, main_factor, delta_factor);
-  // update main_factor/delta_factor
-  if(origclosefirst) {
-    // for the following destination pages that are fully used:
-    // the destination page = (page_size-delta_factor) from the 
-    //                        first unclosed original page + delta_factor 
-    //                        from the next unclosed original page
-    // we always use main_factor to represent the factor from the first 
-    // unclosed original page
-    main_factor=BAMBOO_PAGE_SIZE-delta_factor;
-  } else {
-    // for the following destination pages that are fully used:
-    // the destination page = delta_factor from the first unclosed original    
-    //                        page + (page_size-delta_factor) from the next 
-    //                        unclosed original page
-    // we always use main_factor to represent the factor from the first
-    // unclosed original page
-    main_factor=delta_factor;
-    delta_factor=BAMBOO_PAGE_SIZE-delta_factor;
-  }
+  do {
+    //round source bytes down....don't want to close out page if not necessary
+    remainorigbytes=(remainorigbytes>bytesneeded)?bytesneeded:remainorigbytes;
 
-  // compute the revised profiling info for the following closed destination
-  // pages
-  restClosedPageConvert(current_ptr, main_factor, delta_factor);
+    if (remaintobytes<=remainorigbytes) {
+      //Need to close out to page
 
-  // compute the revised profiling info for the last destination page if needed
-  lastPageConvert(current_ptr);
-}
+      numbytes+=remaintobytes;
+      unsigned int page64th=numbytes>>(BAMBOO_PAGE_SIZE_BITS-6);
 
-/* This function converts originial pages' profiling data to destination pages'
- * profiling data.
- * The parameter current_ptr indicates the current position in the destination 
- * pages.
- * Note that there could be objects that across pages. In such cases, there are 
- * multiple orig/to pages are closed and all these to pages' profiling data 
- * should be properly updated.
- */
-void samplingDataConvert(void * current_ptr) {
-  if(gc_cache_revise_information.to_page_end_va<current_ptr) {
-    // multiple pages are closed
-    samplingDataConvertMultiple(current_ptr);
-  } else {
-    unsigned INTPTR tmp_factor=(unsigned INTPTR)(current_ptr-gc_cache_revise_information.to_page_start_va);
-    if(tmp_factor) {
-      unsigned INTPTR topage=gc_cache_revise_information.to_page_index*NUMCORESACTIVE;
-      unsigned INTPTR oldpage=gc_cache_revise_information.orig_page_index*NUMCORESACTIVE;
-      int * newtable=&gccachesamplingtbl_r[topage];
-      int * oldtable=&gccachesamplingtbl[oldpage];
-  
-      for(int tt = 0; tt < NUMCORESACTIVE; tt++) {
-        (*newtable)=(*newtable)+((*oldtable)*tmp_factor)>>BAMBOO_PAGE_SIZE_BITS;
-        newtable++;
-        oldtable++;
+      for(int core = 0; core < NUMCORESACTIVE; core++) {
+       (*totable)=(*totable+page64th*(*origtable))>>6;
+       totable++;
+       origtable++;
       }
+      toptr+=remaintobytes;
+      origptr+=remaintobytes;
+      bytesneeded-=remaintobytes;
+      topage++;//to page is definitely done
+      tobound+=BAMBOO_PAGE_SIZE;
+      origpage=(origptr-gcbaseva)>>BAMBOO_PAGE_SIZE_BITS;//handle exact match case
+      origbound=(origptr&~(BAMBOO_PAGE_SIZE-1))+BAMBOO_PAGE_SIZE;
+    } else {
+      //Finishing off orig page
+
+      numbytes+=remainorigbytes;
+      unsigned int page64th=numbytes>>(BAMBOO_PAGE_SIZE_BITS-6);
+      
+      for(int core = 0; core < NUMCORESACTIVE; core++) {
+       (*totable)+=page64th*(*origtable);
+       totable++;
+       origtable++;
+      }
+      toptr+=remainorigbytes;
+      origptr+=remainorigbytes;
+      bytesneeded-=remainorigbytes;
+      origpage++;//just orig page is done
+      origbound+=BAMBOO_PAGE_SIZE;
     }
-  }
-} 
-
-/* This function computes the impact of an original page on a destination page
- * in terms of profiling data. It can only be invoked when there is an original 
- * page that is closed or a destination page that is closed. When finished 
- * computing the revised profiling info of the current destination page, it 
- * sets up the gc_cache_revise_information to the latest position in the 
- * original page and the destination page.
- */
-void completePageConvert(void * origptr, void * toptr, void * current_ptr) {
-  bool closeToPage=(unsigned int)(toptr)>=(unsigned int)(gc_cache_revise_information.to_page_end_va);
-  bool closeOrigPage=(unsigned int)(origptr)>=(unsigned int)(gc_cache_revise_information.orig_page_end_va);
-  if(closeToPage||closeOrigPage) {
-    // end of one or more orig/to page
-    // compute the impact of the original page(s) for the desitination page(s)
-    samplingDataConvert(current_ptr);
-    // prepare for an new orig page
-    unsigned INTPTR tmp_index=((unsigned INTPTR)(origptr-gcbaseva))>>BAMBOO_PAGE_SIZE_BITS;
-    gc_cache_revise_information.orig_page_start_va=origptr;
-    gc_cache_revise_information.orig_page_end_va=gcbaseva+BAMBOO_PAGE_SIZE*(tmp_index+1);
-    gc_cache_revise_information.orig_page_index=tmp_index;
-    gc_cache_revise_information.to_page_start_va=toptr;
-    if(closeToPage) {
-      unsigned INTPTR to_index=((unsigned INTPTR)(toptr-gcbaseva))>>BAMBOO_PAGE_SIZE_BITS;
-      gc_cache_revise_information.to_page_end_va=gcbaseva+BAMBOO_PAGE_SIZE*(to_index+1);
-      gc_cache_revise_information.to_page_index=to_index;
-    }
-  }
+    totable=&gccachesamplingtbl_r[topage*NUMCORESACTIVE];
+    origtable=&gccachesamplingtbl[origpage*NUMCORESACTIVE];
+    
+    remaintobytes=tobound-toptr;
+    remainorigbytes=origbound-origptr;
+    
+    numbytes=0;
+  } while(bytesneeded!=0);
 }
 
 // prepare for cache adaption:
@@ -234,7 +108,7 @@ void cacheAdapt_gc(bool isgccachestage) {
 // find the core that accesses the page #page_index most
 #define CACHEADAPT_FIND_HOTTEST_CORE(page_index,hottestcore,hotfreq) \
   { \
-    int *local_tbl=&gccachesamplingtbl_r[page_index*NUMCORESACTIVE]; \
+    unsigned int *local_tbl=&gccachesamplingtbl_r[page_index*NUMCORESACTIVE];  \
     for(int i = 0; i < NUMCORESACTIVE; i++) { \
       int freq = *local_tbl; \
       local_tbl++; \
@@ -248,7 +122,7 @@ void cacheAdapt_gc(bool isgccachestage) {
 // access time of the page at the same time
 #define CACHEADAPT_FIND_HOTTEST_CORE_W_TOTALFREQ(page_index,hottestcore,hotfreq,totalfreq) \
   { \
-    int *local_tbl=&gccachesamplingtbl_r[page_index*NUMCORESACTIVE]; \
+    unsigned int *local_tbl=&gccachesamplingtbl_r[page_index*NUMCORESACTIVE];  \
     for(int i = 0; i < NUMCORESACTIVE; i++) { \
       int freq = *local_tbl; \
       local_tbl++; \
@@ -280,7 +154,7 @@ void cacheAdapt_policy_h4h(int coren){
   unsigned int page_index=page_gap*coren;
   unsigned int page_index_end=(coren==NUMCORESACTIVE-1)?page_num:(page_index+page_gap);
   VA page_sva = gcbaseva+(BAMBOO_PAGE_SIZE)*page_index;
-  int * tmp_p = gccachepolicytbl;
+  unsigned int * tmp_p = gccachepolicytbl;
   for(; page_index < page_index_end; page_index++) {
     bamboo_cache_policy_t policy = {0};
     policy.cache_mode = BAMBOO_CACHE_MODE_HASH;
@@ -296,7 +170,7 @@ void cacheAdapt_policy_local(int coren){
   unsigned int page_index=page_gap*coren;
   unsigned int page_index_end=(coren==NUMCORESACTIVE-1)?page_num:(page_index+page_gap);
   VA page_sva = gcbaseva+(BAMBOO_PAGE_SIZE)*page_index;
-  int * tmp_p = gccachepolicytbl;
+  unsigned int * tmp_p = gccachepolicytbl;
   for(; page_index < page_index_end; page_index++) {
     bamboo_cache_policy_t policy = {0};
     unsigned int block = 0;
@@ -314,7 +188,7 @@ void cacheAdapt_policy_hottest(int coren){
   unsigned int page_index=page_gap*coren;
   unsigned int page_index_end=(coren==NUMCORESACTIVE-1)?page_num:(page_index+page_gap);
   VA page_sva = gcbaseva+(BAMBOO_PAGE_SIZE)*page_index;
-  int * tmp_p = gccachepolicytbl;
+  unsigned int * tmp_p = gccachepolicytbl;
   for(; page_index < page_index_end; page_index++) {
     bamboo_cache_policy_t policy = {0};
     unsigned int hottestcore = 0;
@@ -345,7 +219,7 @@ void cacheAdapt_policy_dominate(int coren){
   unsigned int page_index=page_gap*coren;
   unsigned int page_index_end=(coren==NUMCORESACTIVE-1)?page_num:(page_index+page_gap);
   VA page_sva = gcbaseva+(BAMBOO_PAGE_SIZE)*page_index;
-  int * tmp_p = gccachepolicytbl;
+  unsigned int * tmp_p = gccachepolicytbl;
   for(; page_index < page_index_end; page_index++) {
     bamboo_cache_policy_t policy = {0};
     unsigned int hottestcore = 0;
@@ -394,7 +268,7 @@ unsigned int cacheAdapt_decision(int coren) {
 void cacheAdapt_mutator() {
   BAMBOO_CACHE_MF();
   // check the changes and adapt them
-  int * tmp_p = gccachepolicytbl;
+  unsigned int * tmp_p = gccachepolicytbl;
   unsigned int page_sva = gcbaseva;
   for(; page_sva<gctopva; page_sva+=BAMBOO_PAGE_SIZE) {
     // read out the policy
@@ -481,16 +355,28 @@ void gc_output_cache_sampling() {
     unsigned int block = 0;
     BLOCKINDEX(block, (void *) page_sva);
     unsigned int coren = gc_block2core[block%(NUMCORES4GC*2)];
-    printf("%x,  %d,  %d,  ",(int)page_sva,page_index,coren);
-    int * local_tbl = &gccachesamplingtbl[page_index*NUMCORESACTIVE];
+    //printf("%x,  %d,  %d,  ",(int)page_sva,page_index,coren);
+    unsigned int * local_tbl = &gccachesamplingtbl[page_index*NUMCORESACTIVE];
+    int accesscore = 0;
     for(int i = 0; i < NUMCORESACTIVE; i++) {
       int freq = *local_tbl;
       local_tbl++;
-      //if(freq != 0) {
+      if(freq != 0) {
+        accesscore++;
+        //printf("%d,  ", freq);
+      }
+    }
+    if(accesscore!=0) {
+      printf("%x,  %d,  %d,  ",(int)page_sva,page_index,coren);
+      unsigned int * local_tbl = &gccachesamplingtbl[page_index*NUMCORESACTIVE];
+      for(int i = 0; i < NUMCORESACTIVE; i++) {
+        int freq = *local_tbl;
+        local_tbl++;
         printf("%d,  ", freq);
-      //}
+      }
+      printf("\n");
     }
-    printf("\n");
+    //printf("\n");
   }
   printf("=================\n");
 } 
@@ -515,27 +401,29 @@ void gc_output_cache_sampling_r() {
     unsigned int block = 0;
     BLOCKINDEX(block, (void *)page_sva);
     unsigned int coren = gc_block2core[block%(NUMCORES4GC*2)];
-    printf(" %x,  %d,  %d,  ",(int)page_sva,page_index,coren);
+    //printf("%x,  %d,  %d,  ",(int)page_sva,page_index,coren);
     int accesscore = 0; // TODO
-    int * local_tbl = &gccachesamplingtbl_r[page_index*NUMCORESACTIVE];
+    unsigned int * local_tbl = &gccachesamplingtbl_r[page_index*NUMCORESACTIVE];
     for(int i = 0; i < NUMCORESACTIVE; i++) {
       int freq = *local_tbl; 
-      printf("%d,  ", freq);
+      //printf("%d,  ", freq);
       if(freq != 0) {
         accesscore++;// TODO
       }
       local_tbl++;
     }
     if(accesscore!=0) {
-      int * local_tbl = &gccachesamplingtbl_r[page_index*NUMCORESACTIVE];
+      printf("%x,  %d,  %d,  ",(int)page_sva,page_index,coren);
+      unsigned int * local_tbl = &gccachesamplingtbl_r[page_index*NUMCORESACTIVE];
       for(int i = 0; i < NUMCORESACTIVE; i++) {
         int freq = *local_tbl;
+        printf("%d,  ", freq);
         sumdata[accesscore-1][i]+=freq;
         local_tbl++;
       }
-    }
-  
-    printf("\n");
+      printf("\n");
+    }  
+    //printf("\n");
   }
   printf("+++++\n");
   // TODO printout the summary data