Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add AddressSpace::process() method
[simgrid.git] / src / mc / mc_page_snapshot.cpp
1 /* MC interface: definitions that non-MC modules must see, but not the user */
2
3 /* Copyright (c) 2014-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <unistd.h> // pread, pwrite
10
11 #include "PageStore.hpp"
12 #include "mc_mmu.h"
13 #include "mc_private.h"
14 #include "mc_snapshot.h"
15
16 #include <xbt/mmalloc.h>
17
18 #define SOFT_DIRTY_BIT_NUMBER 55
19 #define SOFT_DIRTY (((uint64_t)1) << SOFT_DIRTY_BIT_NUMBER)
20
21 using simgrid::mc::remote;
22
23 namespace simgrid {
24 namespace mc {
25
26 /** @brief Take a per-page snapshot of a region
27  *
28  *  @param data            The start of the region (must be at the beginning of a page)
29  *  @param pag_count       Number of pages of the region
30  *  @return                Snapshot page numbers of this new snapshot
31  */
32 PerPageCopy::PerPageCopy(PageStore& store, AddressSpace& as,
33     remote_ptr<void> addr, std::size_t page_count,
34     const size_t* ref_page_numbers, const std::uint64_t* pagemap)
35 {
36   store_ = &store;
37   this->pagenos_.resize(page_count);
38   std::vector<char> buffer(xbt_pagesize);
39
40   for (size_t i = 0; i != page_count; ++i) {
41
42     // We don't have to compare soft-clean pages:
43     if (ref_page_numbers && pagemap && !(pagemap[i] & SOFT_DIRTY)) {
44       pagenos_[i] = ref_page_numbers[i];
45       store_->ref_page(ref_page_numbers[i]);
46       continue;
47     }
48
49       remote_ptr<void> page = remote(addr.address() + (i << xbt_pagebits));
50       xbt_assert(mc_page_offset((void*)page.address())==0,
51         "Not at the beginning of a page");
52
53         /* Adding another copy (and a syscall) will probably slow things a lot.
54            TODO, optimize this somehow (at least by grouping the syscalls)
55            if needed. Either:
56             - reduce the number of syscalls;
57             - let the application snapshot itself;
58             - move the segments in shared memory (this will break `fork` however).
59         */
60
61         as.read_bytes(
62           buffer.data(), xbt_pagesize, page,
63           simgrid::mc::ProcessIndexDisabled);
64
65       pagenos_[i] = store_->store_page(buffer.data());
66
67   }
68 }
69
70 }
71 }
72
73 extern "C" {
74
75 /** @brief Restore a snapshot of a region
76  *
77  *  If possible, the restoration will be incremental
78  *  (the modified pages will not be touched).
79  *
80  *  @param start_addr
81  *  @param page_count       Number of pages of the region
82  *  @param pagenos
83  */
84 void mc_restore_page_snapshot_region(simgrid::mc::Process* process,
85   void* start_addr, simgrid::mc::PerPageCopy const& pages_copy)
86 {
87   for (size_t i = 0; i != pages_copy.page_count(); ++i) {
88     // Otherwise, copy the page:
89     void* target_page = mc_page_from_number(start_addr, i);
90     const void* source_page = pages_copy.page(i);
91     process->write_bytes(source_page, xbt_pagesize, remote(target_page));
92   }
93 }
94
95 // ***** High level API
96
97 void mc_region_restore_sparse(simgrid::mc::Process* process, mc_mem_region_t reg)
98 {
99   xbt_assert(((reg->permanent_address().address()) & (xbt_pagesize-1)) == 0,
100     "Not at the beginning of a page");
101   xbt_assert(mc_page_count(reg->size()) == reg->page_data().page_count());
102   mc_restore_page_snapshot_region(process,
103     (void*) reg->permanent_address().address(), reg->page_data());
104 }
105
106 }