Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4e8499e45361cd0822f871de433332e62f8b539b
[simgrid.git] / src / mc / ChunkedData.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 <cstddef>
8 #include <cstdint>
9
10 #include <vector>
11
12 #include <xbt/misc.h> // xbt_pagesize and friends
13 #include <xbt/asserts.h>
14
15 #include "src/mc/AddressSpace.hpp"
16 #include "src/mc/ChunkedData.hpp"
17
18 #define SOFT_DIRTY_BIT_NUMBER 55
19 #define SOFT_DIRTY (((uint64_t)1) << SOFT_DIRTY_BIT_NUMBER)
20
21 namespace simgrid {
22 namespace mc {
23
24 /** Take a per-page snapshot of a region
25  *
26  *  @param data            The start of the region (must be at the beginning of a page)
27  *  @param pag_count       Number of pages of the region
28  *  @return                Snapshot page numbers of this new snapshot
29  */
30 ChunkedData::ChunkedData(PageStore& store, AddressSpace& as,
31     RemotePtr<void> addr, std::size_t page_count,
32     const std::size_t* ref_page_numbers, const std::uint64_t* pagemap)
33 {
34   store_ = &store;
35   this->pagenos_.resize(page_count);
36   std::vector<char> buffer(xbt_pagesize);
37
38   for (size_t i = 0; i != page_count; ++i) {
39
40     // We don't have to compare soft-clean pages:
41     if (ref_page_numbers && pagemap && !(pagemap[i] & SOFT_DIRTY)) {
42       pagenos_[i] = ref_page_numbers[i];
43       store_->ref_page(ref_page_numbers[i]);
44       continue;
45     }
46
47       RemotePtr<void> page = remote((void*)
48         simgrid::mc::mmu::join(i, addr.address()));
49       xbt_assert(simgrid::mc::mmu::split(page.address()).second == 0,
50         "Not at the beginning of a page");
51
52         /* Adding another copy (and a syscall) will probably slow things a lot.
53            TODO, optimize this somehow (at least by grouping the syscalls)
54            if needed. Either:
55             - reduce the number of syscalls;
56             - let the application snapshot itself;
57             - move the segments in shared memory (this will break `fork` however).
58         */
59
60         as.read_bytes(
61           buffer.data(), xbt_pagesize, page,
62           simgrid::mc::ProcessIndexDisabled);
63
64       pagenos_[i] = store_->store_page(buffer.data());
65
66   }
67 }
68
69 }
70 }