Logo AND Algorithmique Numérique Distribuée

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