Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Support for mmap-able snapthots
[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((char*) malloc(size));
55   mc_model_checker->process().read_bytes(data.get(), size,
56     remote(permanent_addr),
57     simgrid::mc::ProcessIndexDisabled);
58
59   simgrid::mc::RegionSnapshot region(
60     region_type, start_addr, permanent_addr, size);
61   region.flat_data(std::move(data));
62
63   XBT_DEBUG("New region : type : %s, data : %p (real addr %p), size : %zu",
64             to_cstr(region_type), region.flat_data(), permanent_addr, size);
65   return std::move(region);
66 }
67
68 /** @brief Take a snapshot of a given region
69  *
70  * @param type
71  * @param start_addr   Address of the region in the simulated process
72  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
73  * @param size         Size of the data*
74  */
75 RegionSnapshot region(
76   RegionType type, void *start_addr, void* permanent_addr, size_t size)
77 {
78   if (_sg_mc_sparse_checkpoint) {
79     return sparse_region(type, start_addr, permanent_addr, size);
80   } else  {
81     return dense_region(type, start_addr, permanent_addr, size);
82   }
83 }
84
85 RegionSnapshot sparse_region(RegionType region_type,
86   void *start_addr, void* permanent_addr, size_t size)
87 {
88   simgrid::mc::Process* process = &mc_model_checker->process();
89
90   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
91     "Not at the beginning of a page");
92   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
93     "Not at the beginning of a page");
94   size_t page_count = mc_page_count(size);
95
96   simgrid::mc::PerPageCopy page_data(mc_model_checker->page_store(), *process,
97       permanent_addr, page_count);
98
99   simgrid::mc::RegionSnapshot region(
100     region_type, start_addr, permanent_addr, size);
101   region.page_data(std::move(page_data));
102   return std::move(region);
103 }
104   
105 }
106 }