Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Abstract the process and a snapshot types with a address_space superclass
[simgrid.git] / src / mc / mc_snapshot.c
index bc1b553..0a4f4f5 100644 (file)
@@ -9,6 +9,7 @@
 #include "internal_config.h"
 #include "smpi/private.h"
 
+#include "mc_snapshot.h"
 #include "mc_private.h"
 #include "mc_mmu.h"
 #include "mc_page_store.h"
  *  @param Snapshot region in the snapshot this pointer belongs to
  *         (or NULL if it does not belong to any snapshot region)
  * */
-mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int process_index)
+mc_mem_region_t mc_get_snapshot_region(const void* addr, mc_snapshot_t snapshot, int process_index)
 {
-#ifdef HAVE_SMPI
-  if (snapshot->privatization_regions) {
-
-    if (process_index < 0) {
+  size_t n = snapshot->snapshot_regions_count;
+  for (size_t i = 0; i != n; ++i) {
+    mc_mem_region_t region = snapshot->snapshot_regions[i];
+    if (!(region && mc_region_contain(region, addr)))
+      continue;
 
-      mc_mem_region_t region = snapshot->privatization_regions[0];
-      if( mc_region_contain(region, addr) ) {
+    if (region->storage_type == MC_REGION_STORAGE_TYPE_PRIVATIZED) {
+#ifdef HAVE_SMPI
+      // Use the current process index of the snapshot:
+      if (process_index == MC_PROCESS_INDEX_DISABLED) {
+        process_index = snapshot->privatization_index;
+      }
+      if (process_index < 0) {
         xbt_die("Missing process index");
       }
-
-    } else {
-      if (process_index >= smpi_process_count()) {
+      if (process_index >= region->privatized.regions_count) {
         xbt_die("Invalid process index");
       }
-
-      mc_mem_region_t region = snapshot->privatization_regions[process_index];
-      if( mc_region_contain(region, addr) ) {
-        return region;
-      }
-
-    }
-  }
+      mc_mem_region_t priv_region = region->privatized.regions[process_index];
+      xbt_assert(mc_region_contain(priv_region, addr));
+      return priv_region;
+#else
+      xbt_die("Privatized region in a non SMPI build (this should not happen)");
 #endif
-
-  for (size_t i = 0; i != NB_REGIONS; ++i) {
-    mc_mem_region_t region = snapshot->regions[i];
-    if ( region && mc_region_contain(region, addr) ) {
-      return region;
     }
+
+    return region;
   }
 
   return NULL;
@@ -64,7 +63,7 @@ mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int p
  *  @param size    Size of the data to read in bytes
  *  @return Pointer where the data is located (target buffer of original location)
  */
-void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
+const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size)
 {
   // Last byte of the memory area:
   void* end = (char*) addr + size - 1;
@@ -104,14 +103,15 @@ void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* targ
  *  @param size     Size of the data to read in bytes
  *  @return Pointer where the data is located (target buffer or original location)
  */
-void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size)
+const void* MC_snapshot_read(
+  mc_snapshot_t snapshot, e_adress_space_read_flags_t flags,
+  void* target, const void* addr, size_t size, int process_index)
 {
-  if (snapshot) {
-    mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
-    return mc_snapshot_read_region(addr, region, target, size);
-  } else {
-    return addr;
-  }
+  mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot, process_index);
+  if (region)
+    return MC_region_read(region, target, addr, size);
+  else
+    return MC_process_read(snapshot->process, flags, target, addr, size, process_index);
 }
 
 /** Compare memory between snapshots (with known regions)
@@ -122,18 +122,20 @@ void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, vo
  * @param snapshot2 Region of the address in the second snapshot
  * @return same as memcmp
  * */
-int mc_snapshot_region_memcmp(
-  void* addr1, mc_mem_region_t region1,
-  void* addr2, mc_mem_region_t region2,
+int MC_snapshot_region_memcmp(
+  const void* addr1, mc_mem_region_t region1,
+  const void* addr2, mc_mem_region_t region2,
   size_t size)
 {
   // Using alloca() for large allocations may trigger stack overflow:
   // use malloc if the buffer is too big.
   bool stack_alloc = size < 64;
-  void* buffer1a = (region1==NULL || region1->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
-  void* buffer2a = (region2==NULL || region2->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
-  void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer1a, size);
-  void* buffer2 = mc_snapshot_read_region(addr2, region2, buffer2a, size);
+  const bool region1_need_buffer = region1==NULL || region1->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
+  const bool region2_need_buffer = region2==NULL || region2->storage_type==MC_REGION_STORAGE_TYPE_FLAT;
+  void* buffer1a = region1_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
+  void* buffer2a = region2_need_buffer ? NULL : stack_alloc ? alloca(size) : malloc(size);
+  const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
+  const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
   int res;
   if (buffer1 == buffer2) {
     res = 0;
@@ -155,13 +157,13 @@ int mc_snapshot_region_memcmp(
  * @param snapshot2 Second snapshot
  * @return same as memcmp
  * */
-int mc_snapshot_memcmp(
-  void* addr1, mc_snapshot_t snapshot1,
-  void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
+int MC_snapshot_memcmp(
+  const void* addr1, mc_snapshot_t snapshot1,
+  const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
 {
   mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1, process_index);
   mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2, process_index);
-  return mc_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
+  return MC_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
 }
 
 #ifdef SIMGRID_TEST
@@ -172,6 +174,8 @@ int mc_snapshot_memcmp(
 #include <sys/mman.h>
 
 #include "mc/mc_private.h"
+#include "mc/mc_snapshot.h"
+#include "mc/mc_mmu.h"
 
 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
 
@@ -216,56 +220,56 @@ static void test_snapshot(bool sparse_checkpoint) {
 
     // Init memory and take snapshots:
     init_memory(source, byte_size);
-    mc_mem_region_t region0 = mc_region_new_sparse(0, source, source, byte_size, NULL);
+    mc_mem_region_t region0 = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
     for(int i=0; i<n; i+=2) {
       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
     }
-    mc_mem_region_t region = mc_region_new_sparse(0, source, source, byte_size, NULL);
+    mc_mem_region_t region = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
 
     void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
 
     xbt_test_add("Reading whole region data for %i page(s)", n);
-    void* read = mc_snapshot_read_region(source, region, destination, byte_size);
-    xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in mc_snapshot_read_region()");
+    const void* read = MC_region_read(region, source, destination, byte_size);
+    xbt_test_assert(!memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
 
     xbt_test_add("Reading parts of region data for %i page(s)", n);
     for(int j=0; j!=100; ++j) {
       size_t offset = rand() % byte_size;
       size_t size = rand() % (byte_size - offset);
-      void* read = mc_snapshot_read_region((char*) source+offset, region, destination, size);
+      const void* read = MC_region_read(region, destination, (const char*) source+offset, size);
       xbt_test_assert(!memcmp((char*) source+offset, read, size),
-        "Mismatch in mc_snapshot_read_region()");
+        "Mismatch in MC_region_read()");
     }
 
     xbt_test_add("Compare whole region data for %i page(s)", n);
-    xbt_test_assert(!mc_snapshot_region_memcmp(source, NULL, source, region, byte_size),
-      "Mismatch in mc_snapshot_region_memcmp() for the whole region");
-    xbt_test_assert(mc_snapshot_region_memcmp(source, region0, source, region, byte_size),
-      "Unexpected match in mc_snapshot_region_memcmp() with previous snapshot");
+    xbt_test_assert(!MC_snapshot_region_memcmp(source, NULL, source, region, byte_size),
+      "Mismatch in MC_snapshot_region_memcmp() for the whole region");
+    xbt_test_assert(MC_snapshot_region_memcmp(source, region0, source, region, byte_size),
+      "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
 
     xbt_test_add("Compare parts of region data for %i page(s) with current value", n);
     for(int j=0; j!=100; ++j) {
       size_t offset = rand() % byte_size;
       size_t size = rand() % (byte_size - offset);
-      xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
-        "Mismatch in mc_snapshot_region_memcmp()");
+      xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
+        "Mismatch in MC_snapshot_region_memcmp()");
     }
 
     xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
     for(int j=0; j!=100; ++j) {
       size_t offset = rand() % byte_size;
       size_t size = rand() % (byte_size - offset);
-      xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
-        "Mismatch in mc_snapshot_region_memcmp()");
+      xbt_test_assert(!MC_snapshot_region_memcmp((char*) source+offset, region, (char*) source+offset, region, size),
+        "Mismatch in MC_snapshot_region_memcmp()");
     }
 
     if (n==1) {
       xbt_test_add("Read pointer for %i page(s)", n);
       memcpy(source, &mc_model_checker, sizeof(void*));
-      mc_mem_region_t region2 = mc_region_new_sparse(0, source, source, byte_size, NULL);
-      xbt_test_assert(mc_snapshot_read_pointer_region(source, region2) == mc_model_checker,
-        "Mismtach in mc_snapshot_read_pointer_region()");
+      mc_mem_region_t region2 = mc_region_new_sparse(MC_REGION_TYPE_UNKNOWN, source, source, byte_size, NULL);
+      xbt_test_assert(MC_region_read_pointer(region2, source) == mc_model_checker,
+        "Mismtach in MC_region_read_pointer()");
       MC_region_destroy(region2);
     }
 
@@ -281,4 +285,3 @@ static void test_snapshot(bool sparse_checkpoint) {
 }
 
 #endif /* SIMGRID_TEST */
-