1 /* MC interface: definitions that non-MC modules must see, but not the user */
3 /* Copyright (c) 2014-2015. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include <unistd.h> // pread, pwrite
10 #include "PageStore.hpp"
12 #include "mc_private.h"
13 #include "mc_snapshot.h"
15 #include <xbt/mmalloc.h>
17 using simgrid::mc::remote;
22 /** @brief Take a per-page snapshot of a region
24 * @param data The start of the region (must be at the beginning of a page)
25 * @param pag_count Number of pages of the region
26 * @return Snapshot page numbers of this new snapshot
28 PerPageCopy::PerPageCopy(PageStore& store, AddressSpace& as,
29 remote_ptr<void> addr, std::size_t page_count)
32 this->pagenos_.resize(page_count);
33 std::vector<char> buffer(xbt_pagesize);
35 for (size_t i = 0; i != page_count; ++i) {
37 remote_ptr<void> page = remote(addr.address() + (i << xbt_pagebits));
38 xbt_assert(mc_page_offset((void*)page.address())==0,
39 "Not at the beginning of a page");
41 /* Adding another copy (and a syscall) will probably slow things a lot.
42 TODO, optimize this somehow (at least by grouping the syscalls)
44 - reduce the number of syscalls;
45 - let the application snapshot itself;
46 - move the segments in shared memory (this will break `fork` however).
50 buffer.data(), xbt_pagesize, page,
51 simgrid::mc::ProcessIndexDisabled);
53 pagenos_[i] = store_->store_page(buffer.data());
63 /** @brief Restore a snapshot of a region
65 * If possible, the restoration will be incremental
66 * (the modified pages will not be touched).
69 * @param page_count Number of pages of the region
72 void mc_restore_page_snapshot_region(simgrid::mc::Process* process,
73 void* start_addr, simgrid::mc::PerPageCopy const& pages_copy)
75 for (size_t i = 0; i != pages_copy.page_count(); ++i) {
76 // Otherwise, copy the page:
77 void* target_page = mc_page_from_number(start_addr, i);
78 const void* source_page = pages_copy.page(i);
79 process->write_bytes(source_page, xbt_pagesize, remote(target_page));
83 // ***** High level API
85 void mc_region_restore_sparse(simgrid::mc::Process* process, mc_mem_region_t reg)
87 xbt_assert(((reg->permanent_address().address()) & (xbt_pagesize-1)) == 0,
88 "Not at the beginning of a page");
89 xbt_assert(mc_page_count(reg->size()) == reg->page_data().page_count());
90 mc_restore_page_snapshot_region(process,
91 (void*) reg->permanent_address().address(), reg->page_data());