Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move extended hash to xbt/utility.
[simgrid.git] / src / mc / compare.cpp
index c86de8c..e2adf2d 100644 (file)
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
 
+using simgrid::mc::remote;
+
 namespace simgrid {
 namespace mc {
 
-struct HeapLocation;
-typedef std::array<HeapLocation, 2> HeapLocationPair;
-typedef std::set<HeapLocationPair> HeapLocationPairs;
-struct HeapArea;
-struct ProcessComparisonState;
-struct StateComparator;
-
-static int compare_heap_area(StateComparator& state, const void* area1, const void* area2, Snapshot* snapshot1,
-                             Snapshot* snapshot2, HeapLocationPairs* previous, Type* type, int pointer_level);
-}
-}
-
-using simgrid::mc::remote;
-
 /*********************************** Heap comparison ***********************************/
 /***************************************************************************************/
 
-namespace simgrid {
-namespace mc {
-
 class HeapLocation {
 public:
   int block_    = 0;
@@ -53,13 +38,16 @@ public:
   }
 };
 
+typedef std::array<HeapLocation, 2> HeapLocationPair;
+typedef std::set<HeapLocationPair> HeapLocationPairs;
+
+struct ProcessComparisonState;
+struct StateComparator;
+
 static inline
 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
 {
-  return simgrid::mc::HeapLocationPair{{
-    simgrid::mc::HeapLocation(block1, fragment1),
-    simgrid::mc::HeapLocation(block2, fragment2)
-  }};
+  return HeapLocationPair{{HeapLocation(block1, fragment1), HeapLocation(block2, fragment2)}};
 }
 
 class HeapArea : public HeapLocation {
@@ -84,26 +72,8 @@ public:
   void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
 };
 
-namespace {
-
-/** A hash which works with more stuff
- *
- *  It can hash pairs: the standard hash currently doesn't include this.
- */
-template <class X> class hash : public std::hash<X> {
-};
-
-template <class X, class Y> class hash<std::pair<X, Y>> {
-public:
-  std::size_t operator()(std::pair<X,Y>const& x) const
-  {
-    hash<X> h1;
-    hash<X> h2;
-    return h1(x.first) ^ h2(x.second);
-  }
-};
-
-}
+static int compare_heap_area(StateComparator& state, const void* area1, const void* area2, Snapshot* snapshot1,
+                             Snapshot* snapshot2, HeapLocationPairs* previous, Type* type, int pointer_level);
 
 class StateComparator {
 public:
@@ -111,7 +81,7 @@ public:
   std::size_t heaplimit;
   std::array<ProcessComparisonState, 2> processStates;
 
-  std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
+  std::unordered_set<std::pair<void*, void*>, simgrid::xbt::hash<std::pair<void*, void*>>> compared_pointers;
 
   void clear()
   {
@@ -281,11 +251,6 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
 {
   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
 
-  /* Start comparison */
-  int nb_diff1 = 0;
-  int nb_diff2 = 0;
-  bool equal;
-
   /* Check busy blocks */
   size_t i1 = 1;
 
@@ -341,7 +306,7 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
       }
 
       size_t i2 = 1;
-      equal     = false;
+      bool equal = false;
 
       /* Try first to associate to same block in the other heap */
       if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
@@ -395,8 +360,7 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
 
       if (not equal) {
         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
-        i1 = state.heaplimit + 1;
-        nb_diff1++;
+        return false;
       }
 
     } else {                    /* Fragmented block */
@@ -412,7 +376,7 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
         void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
 
         size_t i2 = 1;
-        equal     = false;
+        bool equal = false;
 
         /* Try first to associate to same fragment_ in the other heap */
         if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
@@ -456,7 +420,7 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
             void* addr_frag2  = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
 
             int res_compare =
-                compare_heap_area(state, addr_frag1, addr_frag2, snapshot2, snapshot2, nullptr, nullptr, 0);
+                compare_heap_area(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0);
             if (res_compare != 1) {
               equal = true;
               break;
@@ -469,9 +433,7 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
         if (not equal) {
           XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
                     heapinfo1->busy_frag.frag_size[j1], addr_frag1);
-          i1 = state.heaplimit + 1;
-          nb_diff1++;
-          break;
+          return false;
         }
       }
 
@@ -487,7 +449,7 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
         not state.equals_to1_(i, 0).valid_) {
       XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
-      nb_diff1++;
+      return false;
     }
 
     if (heapinfo1->type <= 0)
@@ -495,13 +457,10 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
       if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
-        nb_diff1++;
+        return false;
       }
   }
 
-  if (i1 == state.heaplimit)
-    XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
-
   for (size_t i = 1; i < state.heaplimit; i++) {
     const malloc_info* heapinfo2 =
         (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
@@ -509,7 +468,7 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
         not state.equals_to2_(i, 0).valid_) {
       XBT_DEBUG("Block %zu not found (size used = %zu)", i,
                 heapinfo2->busy_block.busy_size);
-      nb_diff2++;
+      return false;
     }
 
     if (heapinfo2->type <= 0)
@@ -519,15 +478,11 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
       if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
           i, j, heapinfo2->busy_frag.frag_size[j]);
-        nb_diff2++;
+        return false;
       }
-
   }
 
-  if (i1 == state.heaplimit)
-    XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
-
-  return nb_diff1 == 0 && nb_diff2 == 0;
+  return true;
 }
 
 /**
@@ -541,10 +496,10 @@ static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc:
  * @param size
  * @param check_ignore
  */
-static int compare_heap_area_without_type(simgrid::mc::StateComparator& state, const void* real_area1,
-                                          const void* real_area2, simgrid::mc::Snapshot* snapshot1,
-                                          simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous, int size,
-                                          int check_ignore)
+static bool heap_area_equal_without_type(simgrid::mc::StateComparator& state, const void* real_area1,
+                                         const void* real_area2, simgrid::mc::Snapshot* snapshot1,
+                                         simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous, int size,
+                                         int check_ignore)
 {
   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
   simgrid::mc::Region* heap_region1  = MC_get_heap_region(snapshot1);
@@ -553,15 +508,13 @@ static int compare_heap_area_without_type(simgrid::mc::StateComparator& state, c
   for (int i = 0; i < size; ) {
 
     if (check_ignore > 0) {
-      ssize_t ignore1 = heap_comparison_ignore_size(
-        state.processStates[0].to_ignore, (char *) real_area1 + i);
+      ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, (char*)real_area1 + i);
       if (ignore1 != -1) {
-        ssize_t ignore2 = heap_comparison_ignore_size(
-          state.processStates[1].to_ignore, (char *) real_area2 + i);
+        ssize_t ignore2 = heap_comparison_ignore_size(state.processStates[1].to_ignore, (char*)real_area2 + i);
         if (ignore2 == ignore1) {
           if (ignore1 == 0) {
             check_ignore--;
-            return 0;
+            return true;
           } else {
             i = i + ignore2;
             check_ignore--;
@@ -591,18 +544,18 @@ static int compare_heap_area_without_type(simgrid::mc::StateComparator& state, c
         int res_compare =
             compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, nullptr, 0);
         if (res_compare == 1)
-          return res_compare;
+          return false;
         i = pointer_align + sizeof(void *);
         continue;
       }
 
-      return 1;
+      return false;
     }
 
     i++;
   }
 
-  return 0;
+  return true;
 }
 
 /**
@@ -783,8 +736,8 @@ static int compare_heap_area_with_type(simgrid::mc::StateComparator& state, cons
         return 0;
 
     case DW_TAG_union_type:
-      return compare_heap_area_without_type(state, real_area1, real_area2, snapshot1, snapshot2, previous,
-                                            type->byte_size, check_ignore);
+      return not heap_area_equal_without_type(state, real_area1, real_area2, snapshot1, snapshot2, previous,
+                                              type->byte_size, check_ignore);
   }
   return 0;
 }
@@ -1124,7 +1077,7 @@ static int compare_heap_area(simgrid::mc::StateComparator& state, const void* ar
                                               check_ignore, pointer_level);
   else
     res_compare =
-        compare_heap_area_without_type(state, area1, area2, snapshot1, snapshot2, previous, size, check_ignore);
+        not heap_area_equal_without_type(state, area1, area2, snapshot1, snapshot2, previous, size, check_ignore);
 
   if (res_compare == 1)
     return res_compare;