Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] C++ class PageStore
[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 extern "C" {
18
19 // ***** Region management:
20
21 /** @brief Take a per-page snapshot of a region
22  *
23  *  @param data            The start of the region (must be at the beginning of a page)
24  *  @param pag_count       Number of pages of the region
25  *  @return                Snapshot page numbers of this new snapshot
26  */
27 size_t* mc_take_page_snapshot_region(mc_process_t process,
28   void* data, size_t page_count)
29 {
30   size_t* pagenos = (size_t*) malloc(page_count * sizeof(size_t));
31
32   const bool is_self = MC_process_is_self(process);
33
34   void* temp = NULL;
35   if (!is_self)
36     temp = malloc(xbt_pagesize);
37
38   for (size_t i=0; i!=page_count; ++i) {
39
40       // Otherwise, we need to store the page the hard way
41       // (by reading its content):
42       void* page = (char*) data + (i << xbt_pagebits);
43       xbt_assert(mc_page_offset(page)==0, "Not at the beginning of a page");
44       void* page_data;
45       if (is_self) {
46         page_data = page;
47       } else {
48         /* Adding another copy (and a syscall) will probably slow things a lot.
49            TODO, optimize this somehow (at least by grouping the syscalls)
50            if needed. Either:
51             - reduce the number of syscalls;
52             - let the application snapshot itself;
53             - move the segments in shared memory (this will break `fork` however).
54         */
55         page_data = temp;
56         MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
57           temp, page, xbt_pagesize, MC_PROCESS_INDEX_DISABLED);
58       }
59       pagenos[i] = mc_model_checker->page_store().store_page(page_data);
60
61   }
62
63   free(temp);
64   return pagenos;
65 }
66
67 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count)
68 {
69   for (size_t i=0; i!=page_count; ++i) {
70     mc_model_checker->page_store().unref_page(pagenos[i]);
71   }
72 }
73
74 /** @brief Restore a snapshot of a region
75  *
76  *  If possible, the restoration will be incremental
77  *  (the modified pages will not be touched).
78  *
79  *  @param start_addr
80  *  @param page_count       Number of pages of the region
81  *  @param pagenos
82  */
83 void mc_restore_page_snapshot_region(mc_process_t process,
84   void* start_addr, size_t page_count, size_t* pagenos)
85 {
86   for (size_t i=0; i!=page_count; ++i) {
87     // Otherwise, copy the page:
88     void* target_page = mc_page_from_number(start_addr, i);
89     const void* source_page = mc_model_checker->page_store().get_page(pagenos[i]);
90     MC_process_write(process, source_page, target_page, xbt_pagesize);
91   }
92 }
93
94 // ***** High level API
95
96 mc_mem_region_t mc_region_new_sparse(mc_region_type_t region_type,
97   void *start_addr, void* permanent_addr, size_t size)
98 {
99   mc_process_t process = &mc_model_checker->process();
100
101   mc_mem_region_t region = xbt_new(s_mc_mem_region_t, 1);
102   region->region_type = region_type;
103   region->storage_type = MC_REGION_STORAGE_TYPE_CHUNKED;
104   region->start_addr = start_addr;
105   region->permanent_addr = permanent_addr;
106   region->size = size;
107
108   xbt_assert((((uintptr_t)start_addr) & (xbt_pagesize-1)) == 0,
109     "Not at the beginning of a page");
110   xbt_assert((((uintptr_t)permanent_addr) & (xbt_pagesize-1)) == 0,
111     "Not at the beginning of a page");
112   size_t page_count = mc_page_count(size);
113
114   // Take incremental snapshot:
115   region->chunked.page_numbers = mc_take_page_snapshot_region(process,
116     permanent_addr, page_count);
117
118   return region;
119 }
120
121 void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg)
122 {
123   xbt_assert((((uintptr_t)reg->permanent_addr) & (xbt_pagesize-1)) == 0,
124     "Not at the beginning of a page");
125   size_t page_count = mc_page_count(reg->size);
126
127   mc_restore_page_snapshot_region(process,
128     reg->permanent_addr, page_count, reg->chunked.page_numbers);
129 }
130
131 }