Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master'
[simgrid.git] / src / mc / mc_snapshot.c
index 2e7fc1e..97c4017 100644 (file)
@@ -6,18 +6,50 @@
 
 #include <stdbool.h>
 
+#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"
 
-mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot)
+/** @brief Find the snapshoted region from a pointer
+ *
+ *  @param addr     Pointer
+ *  @param snapshot Snapshot
+ *  @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)
 {
+#ifdef HAVE_SMPI
+  if (snapshot->privatization_regions) {
+
+    if (process_index < 0) {
+
+      mc_mem_region_t region = snapshot->privatization_regions[0];
+      if( mc_region_contain(region, addr) ) {
+        xbt_die("Missing process index");
+      }
+
+    } else {
+      if (process_index >= smpi_process_count()) {
+        xbt_die("Invalid process index");
+      }
+
+      mc_mem_region_t region = snapshot->privatization_regions[process_index];
+      if( mc_region_contain(region, addr) ) {
+        return region;
+      }
+
+    }
+  }
+#endif
+
   for (size_t i = 0; i != NB_REGIONS; ++i) {
     mc_mem_region_t region = snapshot->regions[i];
-    void* start = region->start_addr;
-    void* end = (char*) start + region->size;
-
-    if (addr >= start && addr < end) {
+    if ( region && mc_region_contain(region, addr) ) {
       return region;
     }
   }
@@ -35,8 +67,12 @@ mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot)
  */
 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
 {
+  // Last byte of the memory area:
   void* end = (char*) addr + size - 1;
+
+  // Page of the last byte of the memory area:
   size_t page_end = mc_page_number(NULL, end);
+
   void* dest = target;
 
   if (dest==NULL) {
@@ -67,12 +103,12 @@ void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* targ
  *  @param snapshot Snapshot (or NULL is no snapshot)
  *  @param target   Buffer to store the value
  *  @param size     Size of the data to read in bytes
- *  @return Pointer where the data is located (target buffer of original location)
+ *  @return Pointer where the data is located (target buffer or original location)
  */
-void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size)
+void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size)
 {
   if (snapshot) {
-    mc_mem_region_t region = mc_get_snapshot_region(addr, 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;
@@ -89,7 +125,8 @@ void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t
  * */
 int mc_snapshot_region_memcmp(
   void* addr1, mc_mem_region_t region1,
-  void* addr2, mc_mem_region_t region2, size_t size)
+  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.
@@ -119,12 +156,12 @@ int mc_snapshot_region_memcmp(
  * @param snapshot2 Second snapshot
  * @return same as memcmp
  * */
-int mc_snapshot_memcp(
+int mc_snapshot_memcmp(
   void* addr1, mc_snapshot_t snapshot1,
-  void* addr2, mc_snapshot_t snapshot2, size_t size)
+  void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size)
 {
-  mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1);
-  mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2);
+  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);
 }
 
@@ -136,6 +173,8 @@ int mc_snapshot_memcp(
 #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");
 
@@ -171,7 +210,7 @@ static void test_snapshot(bool sparse_checkpoint) {
   mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
   mc_model_checker->pages = mc_pages_store_new();
 
-  for(int n=1; n!=10; ++n) {
+  for(int n=1; n!=256; ++n) {
 
     // Store region page(s):
     size_t byte_size = n * xbt_pagesize;
@@ -180,11 +219,11 @@ 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, byte_size, NULL);
+    mc_mem_region_t region0 = mc_region_new_sparse(0, 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, byte_size, NULL);
+    mc_mem_region_t region = mc_region_new_sparse(0, 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");
@@ -208,7 +247,7 @@ static void test_snapshot(bool sparse_checkpoint) {
     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)", n);
+    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);
@@ -216,10 +255,18 @@ static void test_snapshot(bool sparse_checkpoint) {
         "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()");
+    }
+
     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, byte_size, NULL);
+      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_region_destroy(region2);
@@ -237,4 +284,3 @@ static void test_snapshot(bool sparse_checkpoint) {
 }
 
 #endif /* SIMGRID_TEST */
-