Logo AND Algorithmique Numérique Distribuée

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