Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 <cstdlib>
8
9 #include <sys/mman.h>
10
11 #include "mc/mc.h"
12 #include "src/mc/mc_snapshot.h"
13
14 #include "src/mc/ChunkedData.hpp"
15 #include "src/mc/RegionSnapshot.hpp"
16
17 extern "C" {
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
20                                 "Logging specific to region snapshots");
21
22 }
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_ = malloc(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     std::free(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   simgrid::mc::buffer::Type buffer_type;
85   if (_sg_mc_ksm)
86     // We use mmap to allocate the memory in order to madvise it.
87     // We don't want to madvise the main heap.
88     // Moreover we get aligned pgaes which is merge-friendly.
89     buffer_type = simgrid::mc::buffer::Type::Mmap;
90   else
91     buffer_type = simgrid::mc::buffer::Type::Malloc;
92
93   simgrid::mc::buffer data(size, buffer_type);
94
95   mc_model_checker->process().read_bytes(data.get(), size,
96     remote(permanent_addr),
97     simgrid::mc::ProcessIndexDisabled);
98
99   if (_sg_mc_ksm)
100     // Mark the region as mergeable *after* we have written into it.
101     // There no point to let KSM do the hard work before that.
102     madvise(data.get(), size, MADV_MERGEABLE);
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 std::move(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   RegionSnapshot const* ref_region)
123 {
124   if (_sg_mc_sparse_checkpoint) {
125     return sparse_region(type, start_addr, permanent_addr, size, ref_region);
126   } else  {
127     return dense_region(type, start_addr, permanent_addr, size);
128   }
129 }
130
131 RegionSnapshot sparse_region(RegionType region_type,
132   void *start_addr, void* permanent_addr, size_t size,
133   RegionSnapshot const* ref_region)
134 {
135   simgrid::mc::Process* process = &mc_model_checker->process();
136   assert(process != nullptr);
137
138   bool use_soft_dirty = _sg_mc_sparse_checkpoint && _sg_mc_soft_dirty
139     && ref_region != nullptr
140     && ref_region->storage_type() == simgrid::mc::StorageType::Chunked;
141
142   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
143     "Not at the beginning of a page");
144   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
145     "Not at the beginning of a page");
146   size_t page_count = mc_page_count(size);
147
148   std::vector<std::uint64_t> pagemap;
149   const size_t* ref_page_numbers = nullptr;
150   if (use_soft_dirty) {
151     pagemap.resize(page_count);
152     process->read_pagemap(pagemap.data(),
153       mc_page_number(nullptr, permanent_addr), page_count);
154     ref_page_numbers = ref_region->page_data().pagenos();
155   }
156
157   simgrid::mc::ChunkedData page_data(
158     mc_model_checker->page_store(), *process, permanent_addr, page_count,
159     ref_page_numbers,
160     use_soft_dirty ? pagemap.data() : nullptr);
161
162   simgrid::mc::RegionSnapshot region(
163     region_type, start_addr, permanent_addr, size);
164   region.page_data(std::move(page_data));
165   return std::move(region);
166 }
167   
168 }
169 }