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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_RegionSnaphot, mc,
18                                 "Logging specific to region snapshots");
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 Buffer::Buffer(std::size_t size, Type type) : size_(size), type_(type)
39 {
40   switch(type_) {
41   case Type::Malloc:
42     data_ = ::malloc(size_);
43     break;
44   case Type::Mmap:
45     data_ = ::mmap(nullptr, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0);
46     if (data_ == MAP_FAILED) {
47       data_ = nullptr;
48       size_ = 0;
49       type_ = Type::Malloc;
50       throw std::bad_alloc();
51     }
52     break;
53   default:
54     abort();
55   }
56 }
57
58 void Buffer::clear() noexcept
59 {
60   switch(type_) {
61   case Type::Malloc:
62     std::free(data_);
63     break;
64   case Type::Mmap:
65     if (munmap(data_, size_) != 0)
66       abort();
67     break;
68   default:
69     abort();
70   }
71   data_ = nullptr;
72   size_ = 0;
73   type_ = Type::Malloc;
74 }
75
76 RegionSnapshot dense_region(
77   RegionType region_type,
78   void *start_addr, void* permanent_addr, size_t size)
79 {
80   // When KSM support is enables, we allocate memory using mmap:
81   // * we don't want to advise bits of the heap as mergable;
82   // * mmap gives data aligned on page boundaries which is merge friendly.
83   simgrid::mc::Buffer data;
84   if (_sg_mc_ksm)
85     data = Buffer::mmap(size);
86   else
87     data = Buffer::malloc(size);
88
89   mc_model_checker->process().read_bytes(data.get(), size,
90     remote(permanent_addr),
91     simgrid::mc::ProcessIndexDisabled);
92
93   if (_sg_mc_ksm)
94     // Mark the region as mergeable *after* we have written into it.
95     // Trying to merge them before is useless/counterproductive.
96     madvise(data.get(), size, MADV_MERGEABLE);
97
98   simgrid::mc::RegionSnapshot region(
99     region_type, start_addr, permanent_addr, size);
100   region.flat_data(std::move(data));
101
102   XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
103             to_cstr(region_type), region.flat_data().get(), permanent_addr, size);
104   return region;
105 }
106
107 /** @brief Take a snapshot of a given region
108  *
109  * @param type
110  * @param start_addr   Address of the region in the simulated process
111  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
112  * @param size         Size of the data*
113  */
114 RegionSnapshot region(
115   RegionType type, void *start_addr, void* permanent_addr, size_t size)
116 {
117   if (_sg_mc_sparse_checkpoint)
118     return sparse_region(type, start_addr, permanent_addr, size);
119   else
120     return dense_region(type, start_addr, permanent_addr, size);
121 }
122
123 RegionSnapshot sparse_region(RegionType region_type,
124   void *start_addr, void* permanent_addr, size_t size)
125 {
126   simgrid::mc::Process* process = &mc_model_checker->process();
127   assert(process != nullptr);
128
129   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
130     "Not at the beginning of a page");
131   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
132     "Not at the beginning of a page");
133   size_t page_count = simgrid::mc::mmu::chunkCount(size);
134
135   simgrid::mc::ChunkedData page_data(
136     mc_model_checker->page_store(), *process, permanent_addr, page_count);
137
138   simgrid::mc::RegionSnapshot region(
139     region_type, start_addr, permanent_addr, size);
140   region.page_data(std::move(page_data));
141   return region;
142 }
143   
144 }
145 }