Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] RegionSnapshot class
[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.  All rights reserved.         */
4
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. */
7
8 #include <unistd.h> // pread, pwrite
9
10 #include "PageStore.hpp"
11 #include "mc_mmu.h"
12 #include "mc_private.h"
13 #include "mc_snapshot.h"
14
15 #include <xbt/mmalloc.h>
16
17 using simgrid::mc::remote;
18
19 namespace simgrid {
20 namespace mc {
21
22 /** @brief Take a per-page snapshot of a region
23  *
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
27  */
28 PerPageCopy::PerPageCopy(PageStore& store, AddressSpace& as,
29     remote_ptr<void> addr, std::size_t page_count)
30 {
31   store_ = &store;
32   this->pagenos_.resize(page_count);
33   std::vector<char> buffer(xbt_pagesize);
34
35   for (size_t i = 0; i != page_count; ++i) {
36
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");
40
41         /* Adding another copy (and a syscall) will probably slow things a lot.
42            TODO, optimize this somehow (at least by grouping the syscalls)
43            if needed. Either:
44             - reduce the number of syscalls;
45             - let the application snapshot itself;
46             - move the segments in shared memory (this will break `fork` however).
47         */
48
49         as.read_bytes(
50           buffer.data(), xbt_pagesize, page,
51           simgrid::mc::ProcessIndexDisabled);
52
53       pagenos_[i] = store_->store_page(buffer.data());
54
55   }
56 }
57
58 }
59 }
60
61 extern "C" {
62
63 /** @brief Restore a snapshot of a region
64  *
65  *  If possible, the restoration will be incremental
66  *  (the modified pages will not be touched).
67  *
68  *  @param start_addr
69  *  @param page_count       Number of pages of the region
70  *  @param pagenos
71  */
72 void mc_restore_page_snapshot_region(mc_process_t process,
73   void* start_addr, simgrid::mc::PerPageCopy const& pages_copy)
74 {
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));
80   }
81 }
82
83 // ***** High level API
84
85 mc_mem_region_t mc_region_new_sparse(mc_region_type_t region_type,
86   void *start_addr, void* permanent_addr, size_t size)
87 {
88   mc_process_t process = &mc_model_checker->process();
89
90   mc_mem_region_t region = new simgrid::mc::RegionSnapshot();
91   region->region_type = region_type;
92   region->storage_type = MC_REGION_STORAGE_TYPE_CHUNKED;
93   region->start_addr = start_addr;
94   region->permanent_addr = permanent_addr;
95   region->size = size;
96
97   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
98     "Not at the beginning of a page");
99   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
100     "Not at the beginning of a page");
101   size_t page_count = mc_page_count(size);
102
103   // Take incremental snapshot:
104   region->page_numbers_ = simgrid::mc::PerPageCopy(mc_model_checker->page_store(), *process,
105     permanent_addr, page_count);
106
107   return region;
108 }
109
110 void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg)
111 {
112   xbt_assert((((uintptr_t)reg->permanent_addr) & (xbt_pagesize-1)) == 0,
113     "Not at the beginning of a page");
114   xbt_assert(mc_page_count(reg->size) == reg->page_numbers_.page_count());
115   mc_restore_page_snapshot_region(process,
116     reg->permanent_addr, reg->page_numbers_);
117 }
118
119 }