Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c59f1909fc62ec2d1a60c70714f034c30cba8a85
[simgrid.git] / src / mc / RegionSnapshot.cpp
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <sys/mman.h>
8
9 #include "mc/mc.h"
10 #include "mc_snapshot.h"
11 #include "RegionSnapshot.hpp"
12
13 extern "C" {
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
16                                 "Logging specific to region snapshots");
17
18 }
19
20 namespace simgrid {
21 namespace mc {
22
23 static inline
24 const char* to_cstr(RegionType region)
25 {
26   switch (region) {
27   case RegionType::Unknown:
28     return "unknown";
29   case RegionType::Heap:
30     return "Heap";
31   case RegionType::Data:
32     return "Data";
33   default:
34     return "?";
35   }
36 }
37
38 void data_deleter::operator()(void* p) const
39 {
40   switch(type_) {
41   case Free:
42     free(p);
43     break;
44   case Munmap:
45     munmap(p, size_);
46     break;
47   }
48 }
49
50 RegionSnapshot dense_region(
51   RegionType region_type,
52   void *start_addr, void* permanent_addr, size_t size)
53 {
54   simgrid::mc::RegionSnapshot::flat_data_ptr data;
55   if (!_sg_mc_ksm)
56     data = simgrid::mc::RegionSnapshot::flat_data_ptr((char*) malloc(size));
57   else {
58     char* ptr = (char*) mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
59     if (ptr == MAP_FAILED)
60       throw std::bad_alloc();
61     simgrid::mc::data_deleter deleter(
62       simgrid::mc::data_deleter::Munmap, size);
63     data = simgrid::mc::RegionSnapshot::flat_data_ptr(ptr, deleter);
64   }
65   mc_model_checker->process().read_bytes(data.get(), size,
66     remote(permanent_addr),
67     simgrid::mc::ProcessIndexDisabled);
68   if (_sg_mc_ksm)
69     // Mark the region as mergeable *after* we have written into it.
70     // There no point to let KSM do the hard work before that.
71     madvise(data.get(), size, MADV_MERGEABLE);
72
73   simgrid::mc::RegionSnapshot region(
74     region_type, start_addr, permanent_addr, size);
75   region.flat_data(std::move(data));
76
77   XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
78             to_cstr(region_type), region.flat_data(), permanent_addr, size);
79   return std::move(region);
80 }
81
82 /** @brief Take a snapshot of a given region
83  *
84  * @param type
85  * @param start_addr   Address of the region in the simulated process
86  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
87  * @param size         Size of the data*
88  */
89 RegionSnapshot region(
90   RegionType type, void *start_addr, void* permanent_addr, size_t size)
91 {
92   if (_sg_mc_sparse_checkpoint) {
93     return sparse_region(type, start_addr, permanent_addr, size);
94   } else  {
95     return dense_region(type, start_addr, permanent_addr, size);
96   }
97 }
98
99 RegionSnapshot sparse_region(RegionType region_type,
100   void *start_addr, void* permanent_addr, size_t size)
101 {
102   simgrid::mc::Process* process = &mc_model_checker->process();
103
104   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
105     "Not at the beginning of a page");
106   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
107     "Not at the beginning of a page");
108   size_t page_count = mc_page_count(size);
109
110   simgrid::mc::PerPageCopy page_data(mc_model_checker->page_store(), *process,
111       permanent_addr, page_count);
112
113   simgrid::mc::RegionSnapshot region(
114     region_type, start_addr, permanent_addr, size);
115   region.page_data(std::move(page_data));
116   return std::move(region);
117 }
118   
119 }
120 }