Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move visitedState as a field of SafetyChecker
[simgrid.git] / src / mc / RegionSnapshot.cpp
index a045b2f..8fe9a98 100644 (file)
 #include "src/mc/ChunkedData.hpp"
 #include "src/mc/RegionSnapshot.hpp"
 
-extern "C" {
-
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
                                 "Logging specific to region snapshots");
 
-}
-
 namespace simgrid {
 namespace mc {
 
@@ -39,14 +35,14 @@ const char* to_cstr(RegionType region)
   }
 }
 
-buffer::buffer(std::size_t size, Type type) : size_(size), type_(type)
+Buffer::Buffer(std::size_t size, Type type) : size_(size), type_(type)
 {
   switch(type_) {
   case Type::Malloc:
-    data_ = malloc(size_);
+    data_ = ::malloc(size_);
     break;
   case Type::Mmap:
-    data_ = mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
+    data_ = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
     if (data_ == MAP_FAILED) {
       data_ = nullptr;
       size_ = 0;
@@ -59,7 +55,7 @@ buffer::buffer(std::size_t size, Type type) : size_(size), type_(type)
   }
 }
 
-void buffer::clear() noexcept
+void Buffer::clear() noexcept
 {
   switch(type_) {
   case Type::Malloc:
@@ -81,16 +77,14 @@ RegionSnapshot dense_region(
   RegionType region_type,
   void *start_addr, void* permanent_addr, size_t size)
 {
-  simgrid::mc::buffer::Type buffer_type;
+  // When KSM support is enables, we allocate memory using mmap:
+  // * we don't want to madvise bits of the heap;
+  // * mmap gives data aligned on page boundaries which is merge friendly.
+  simgrid::mc::Buffer data;
   if (_sg_mc_ksm)
-    // We use mmap to allocate the memory in order to madvise it.
-    // We don't want to madvise the main heap.
-    // Moreover we get aligned pgaes which is merge-friendly.
-    buffer_type = simgrid::mc::buffer::Type::Mmap;
+    data = Buffer::mmap(size);
   else
-    buffer_type = simgrid::mc::buffer::Type::Malloc;
-
-  simgrid::mc::buffer data(size, buffer_type);
+    data = Buffer::malloc(size);
 
   mc_model_checker->process().read_bytes(data.get(), size,
     remote(permanent_addr),
@@ -98,7 +92,7 @@ RegionSnapshot dense_region(
 
   if (_sg_mc_ksm)
     // Mark the region as mergeable *after* we have written into it.
-    // There no point to let KSM do the hard work before that.
+    // Trying to merge them before is useless/counterproductive.
     madvise(data.get(), size, MADV_MERGEABLE);
 
   simgrid::mc::RegionSnapshot region(
@@ -107,7 +101,7 @@ RegionSnapshot dense_region(
 
   XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
             to_cstr(region_type), region.flat_data().get(), permanent_addr, size);
-  return std::move(region);
+  return region;
 }
 
 /** @brief Take a snapshot of a given region
@@ -121,11 +115,10 @@ RegionSnapshot region(
   RegionType type, void *start_addr, void* permanent_addr, size_t size,
   RegionSnapshot const* ref_region)
 {
-  if (_sg_mc_sparse_checkpoint) {
+  if (_sg_mc_sparse_checkpoint)
     return sparse_region(type, start_addr, permanent_addr, size, ref_region);
-  } else  {
+  else
     return dense_region(type, start_addr, permanent_addr, size);
-  }
 }
 
 RegionSnapshot sparse_region(RegionType region_type,
@@ -143,14 +136,14 @@ RegionSnapshot sparse_region(RegionType region_type,
     "Not at the beginning of a page");
   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
     "Not at the beginning of a page");
-  size_t page_count = mc_page_count(size);
+  size_t page_count = simgrid::mc::mmu::chunkCount(size);
 
   std::vector<std::uint64_t> pagemap;
   const size_t* ref_page_numbers = nullptr;
   if (use_soft_dirty) {
     pagemap.resize(page_count);
     process->read_pagemap(pagemap.data(),
-      mc_page_number(nullptr, permanent_addr), page_count);
+      simgrid::mc::mmu::split((std::size_t) permanent_addr).first, page_count);
     ref_page_numbers = ref_region->page_data().pagenos();
   }
 
@@ -162,7 +155,7 @@ RegionSnapshot sparse_region(RegionType region_type,
   simgrid::mc::RegionSnapshot region(
     region_type, start_addr, permanent_addr, size);
   region.page_data(std::move(page_data));
-  return std::move(region);
+  return region;
 }
   
 }