Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++11 <random> instead of rand().
[simgrid.git] / src / mc / sosp / RegionSnapshot.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstdlib>
7
8 #include <sys/mman.h>
9 #ifdef __FreeBSD__
10 #define MAP_POPULATE MAP_PREFAULT_READ
11 #endif
12
13 #include "mc/mc.h"
14 #include "src/mc/mc_config.hpp"
15 #include "src/mc/sosp/mc_snapshot.hpp"
16
17 #include "src/mc/sosp/ChunkedData.hpp"
18 #include "src/mc/sosp/RegionSnapshot.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc, "Logging specific to region snapshots");
21
22 namespace simgrid {
23 namespace mc {
24
25 static inline const char* to_cstr(RegionType region)
26 {
27   switch (region) {
28     case RegionType::Unknown:
29       return "unknown";
30     case RegionType::Heap:
31       return "Heap";
32     case RegionType::Data:
33       return "Data";
34     default:
35       return "?";
36   }
37 }
38
39 Buffer::Buffer(std::size_t size, Type type) : size_(size), type_(type)
40 {
41   switch (type_) {
42     case Type::Malloc:
43       data_ = ::operator new(size_);
44       break;
45     case Type::Mmap:
46       data_ = ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
47       if (data_ == MAP_FAILED) {
48         data_ = nullptr;
49         size_ = 0;
50         type_ = Type::Malloc;
51         throw std::bad_alloc();
52       }
53       break;
54     default:
55       abort();
56   }
57 }
58
59 void Buffer::clear() noexcept
60 {
61   switch (type_) {
62     case Type::Malloc:
63       ::operator delete(data_);
64       break;
65     case Type::Mmap:
66       if (munmap(data_, size_) != 0)
67         abort();
68       break;
69     default:
70       abort();
71   }
72   data_ = nullptr;
73   size_ = 0;
74   type_ = Type::Malloc;
75 }
76
77 RegionSnapshot dense_region(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
78 {
79   // When KSM support is enables, we allocate memory using mmap:
80   // * we don't want to advise bits of the heap as mergable
81   // * mmap gives data aligned on page boundaries which is merge friendly
82   simgrid::mc::Buffer data;
83   if (_sg_mc_ksm)
84     data = Buffer::mmap(size);
85   else
86     data = Buffer::malloc(size);
87
88   mc_model_checker->process().read_bytes(data.get(), size, remote(permanent_addr), simgrid::mc::ProcessIndexDisabled);
89
90 #ifdef __linux__
91   if (_sg_mc_ksm)
92     // Mark the region as mergeable *after* we have written into it.
93     // Trying to merge them before is useless/counterproductive.
94     madvise(data.get(), size, MADV_MERGEABLE);
95 #endif
96
97   simgrid::mc::RegionSnapshot region(region_type, start_addr, permanent_addr, size);
98   region.flat_data(std::move(data));
99
100   XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu", to_cstr(region_type),
101             region.flat_data().get(), permanent_addr, size);
102   return region;
103 }
104
105 /** @brief Take a snapshot of a given region
106  *
107  * @param type
108  * @param start_addr   Address of the region in the simulated process
109  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the
110  * privatized mapping)
111  * @param size         Size of the data*
112  */
113 RegionSnapshot region(RegionType type, void* start_addr, void* permanent_addr, size_t size)
114 {
115   if (_sg_mc_sparse_checkpoint)
116     return sparse_region(type, start_addr, permanent_addr, size);
117   else
118     return dense_region(type, start_addr, permanent_addr, size);
119 }
120
121 RegionSnapshot sparse_region(RegionType region_type, void* start_addr, void* permanent_addr, size_t size)
122 {
123   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
124   assert(process != nullptr);
125
126   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
127   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
128   size_t page_count = simgrid::mc::mmu::chunkCount(size);
129
130   simgrid::mc::ChunkedData page_data(mc_model_checker->page_store(), *process, RemotePtr<void>(permanent_addr),
131                                      page_count);
132
133   simgrid::mc::RegionSnapshot region(region_type, start_addr, permanent_addr, size);
134   region.page_data(std::move(page_data));
135   return region;
136 }
137
138 } // namespace mc
139 } // namespace simgrid