Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into S4U
[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 using simgrid::mc::remote;
19
20 namespace simgrid {
21 namespace mc {
22
23 /** @brief Take a per-page snapshot of a region
24  *
25  *  @param data            The start of the region (must be at the beginning of a page)
26  *  @param pag_count       Number of pages of the region
27  *  @return                Snapshot page numbers of this new snapshot
28  */
29 PerPageCopy::PerPageCopy(PageStore& store, AddressSpace& as,
30     remote_ptr<void> addr, std::size_t page_count)
31 {
32   store_ = &store;
33   this->pagenos_.resize(page_count);
34   std::vector<char> buffer(xbt_pagesize);
35
36   for (size_t i = 0; i != page_count; ++i) {
37
38       remote_ptr<void> page = remote(addr.address() + (i << xbt_pagebits));
39       xbt_assert(mc_page_offset((void*)page.address())==0,
40         "Not at the beginning of a page");
41
42         /* Adding another copy (and a syscall) will probably slow things a lot.
43            TODO, optimize this somehow (at least by grouping the syscalls)
44            if needed. Either:
45             - reduce the number of syscalls;
46             - let the application snapshot itself;
47             - move the segments in shared memory (this will break `fork` however).
48         */
49
50         as.read_bytes(
51           buffer.data(), xbt_pagesize, page,
52           simgrid::mc::ProcessIndexDisabled);
53
54       pagenos_[i] = store_->store_page(buffer.data());
55
56   }
57 }
58
59 }
60 }
61
62 extern "C" {
63
64 /** @brief Restore a snapshot of a region
65  *
66  *  If possible, the restoration will be incremental
67  *  (the modified pages will not be touched).
68  *
69  *  @param start_addr
70  *  @param page_count       Number of pages of the region
71  *  @param pagenos
72  */
73 void mc_restore_page_snapshot_region(simgrid::mc::Process* process,
74   void* start_addr, simgrid::mc::PerPageCopy const& pages_copy)
75 {
76   for (size_t i = 0; i != pages_copy.page_count(); ++i) {
77     // Otherwise, copy the page:
78     void* target_page = mc_page_from_number(start_addr, i);
79     const void* source_page = pages_copy.page(i);
80     process->write_bytes(source_page, xbt_pagesize, remote(target_page));
81   }
82 }
83
84 // ***** High level API
85
86 void mc_region_restore_sparse(simgrid::mc::Process* process, mc_mem_region_t reg)
87 {
88   xbt_assert(((reg->permanent_address().address()) & (xbt_pagesize-1)) == 0,
89     "Not at the beginning of a page");
90   xbt_assert(mc_page_count(reg->size()) == reg->page_data().page_count());
91   mc_restore_page_snapshot_region(process,
92     (void*) reg->permanent_address().address(), reg->page_data());
93 }
94
95 }