1 /* Copyright (c) 2007-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include <sys/types.h> // off_t
8 #include <stdint.h> // size_t
10 #include <xbt/asserts.h>
11 #include <xbt/dynar.h>
12 #include <simgrid_config.h>
14 #include "mc_forward.h"
15 #include "mc_page_store.h"
16 #include "mc_model_checker.h"
17 #include "mc_mmalloc.h"
24 void mc_softdirty_reset();
26 // ***** Snapshot region
28 #define NB_REGIONS 3 /* binary data (data + BSS) (type = 2), libsimgrid data (data + BSS) (type = 1), std_heap (type = 0)*/
30 /** @brief Copy/snapshot of a given memory region
32 * Two types of region snapshots exist:
34 * <li>flat/dense snapshots are a simple copy of the region;</li>
35 * <li>sparse/per-page snapshots are snaapshots which shared
36 * identical pages.</li>
39 typedef struct s_mc_mem_region{
40 /** @brief Virtual address of the region in the simulated process */
43 /** @brief Permanent virtual address of the region
45 * This is usually the same address as the simuilated process address.
46 * However, when using SMPI privatization of global variables,
47 * each SMPI process has its own set of global variables stored
48 * at a different virtual address. The scheduler maps those region
49 * on the region of the global variables.
54 /** @brief Copy of the snapshot for flat snapshots regions (NULL otherwise) */
57 /** @brief Size of the data region in bytes */
60 /** @brief Pages indices in the page store for per-page snapshots (NULL otherwise) */
63 } s_mc_mem_region_t, *mc_mem_region_t;
65 mc_mem_region_t mc_region_new_sparse(int type, void *start_addr, void* data_addr, size_t size, mc_mem_region_t ref_reg);
66 void MC_region_destroy(mc_mem_region_t reg);
67 void mc_region_restore_sparse(mc_mem_region_t reg, mc_mem_region_t ref_reg);
69 static inline __attribute__ ((always_inline))
70 bool mc_region_contain(mc_mem_region_t region, void* p)
72 return p >= region->start_addr &&
73 p < (void*)((char*) region->start_addr + region->size);
76 static inline __attribute__((always_inline))
77 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region)
79 size_t pageno = mc_page_number(region->start_addr, (void*) addr);
80 size_t snapshot_pageno = region->page_numbers[pageno];
81 const void* snapshot_page = mc_page_store_get_page(mc_model_checker->pages, snapshot_pageno);
82 return (char*) snapshot_page + mc_page_offset((void*) addr);
85 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int process_index);
87 /** \brief Translate a pointer from process address space to snapshot address space
89 * The address space contains snapshot of the main/application memory:
90 * this function finds the address in a given snaphot for a given
91 * real/application address.
93 * For read only memory regions and other regions which are not int the
94 * snapshot, the address is not changed.
96 * \param addr Application address
97 * \param snapshot The snapshot of interest (if NULL no translation is done)
98 * \return Translated address in the snapshot address space
100 static inline __attribute__((always_inline))
101 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot, int process_index)
104 // If not in a process state/clone:
106 return (uintptr_t *) addr;
109 mc_mem_region_t region = mc_get_snapshot_region((void*) addr, snapshot, process_index);
111 xbt_assert(mc_region_contain(region, (void*) addr), "Trying to read out of the region boundary.");
114 return (void *) addr;
118 else if (region->data) {
119 uintptr_t offset = addr - (uintptr_t) region->start_addr;
120 return (void *) ((uintptr_t) region->data + offset);
123 // Per-page snapshot:
124 else if (region->page_numbers) {
125 return mc_translate_address_region(addr, region);
129 xbt_die("No data for this memory region");
137 * Some parts of the snapshot are ignored by zeroing them out: the real
138 * values is stored here.
140 typedef struct s_mc_snapshot_ignored_data {
144 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
146 typedef struct s_fd_infos{
149 off_t current_position;
151 }s_fd_infos_t, *fd_infos_t;
153 typedef struct s_mc_snapshot{
154 size_t heap_bytes_used;
155 mc_mem_region_t regions[NB_REGIONS];
156 xbt_dynar_t enabled_processes;
157 mc_mem_region_t* privatization_regions;
158 int privatization_index;
161 xbt_dynar_t to_ignore;
163 xbt_dynar_t ignored_data;
165 fd_infos_t *current_fd;
168 /** @brief Process index used when no process is available
170 * The expected behaviour is that if a process index is needed it will fail.
172 #define MC_NO_PROCESS_INDEX -1
174 /** @brief Process index when any process is suitable
176 * We could use a special negative value in the future.
178 #define MC_ANY_PROCESS_INDEX 0
180 static inline __attribute__ ((always_inline))
181 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
183 if (mc_region_contain(region, addr))
186 return mc_get_snapshot_region(addr, snapshot, process_index);
189 /** Information about a given stack frame
192 typedef struct s_mc_stack_frame {
193 /** Instruction pointer */
197 unw_word_t frame_base;
200 unw_cursor_t unw_cursor;
201 } s_mc_stack_frame_t, *mc_stack_frame_t;
203 typedef struct s_mc_snapshot_stack{
204 xbt_dynar_t local_variables;
205 xbt_dynar_t stack_frames; // mc_stack_frame_t
207 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
209 typedef struct s_mc_global_t{
210 mc_snapshot_t snapshot;
214 int initial_communications_pattern_done;
215 int comm_deterministic;
216 int send_deterministic;
217 }s_mc_global_t, *mc_global_t;
219 typedef struct s_mc_checkpoint_ignore_region{
222 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
224 static void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
226 mc_snapshot_t MC_take_snapshot(int num_state);
227 void MC_restore_snapshot(mc_snapshot_t);
228 void MC_free_snapshot(mc_snapshot_t);
230 int mc_important_snapshot(mc_snapshot_t snapshot);
232 size_t* mc_take_page_snapshot_region(void* data, size_t page_count, uint64_t* pagemap, size_t* reference_pages);
233 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
234 void mc_restore_page_snapshot_region(void* start_addr, size_t page_count, size_t* pagenos, uint64_t* pagemap, size_t* reference_pagenos);
236 static inline __attribute__((always_inline))
237 bool mc_snapshot_region_linear(mc_mem_region_t region) {
238 return !region || !region->data;
241 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size);
243 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size);
244 int mc_snapshot_region_memcmp(
245 void* addr1, mc_mem_region_t region1,
246 void* addr2, mc_mem_region_t region2, size_t size);
247 int mc_snapshot_memcmp(
248 void* addr1, mc_snapshot_t snapshot1,
249 void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
251 static void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot, int process_index);
253 static inline __attribute__ ((always_inline))
254 void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot, int process_index)
257 return *(void**) mc_snapshot_read(addr, snapshot, process_index, &res, sizeof(void*));
260 static inline __attribute__ ((always_inline))
261 void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot) {
263 xbt_die("snapshot is NULL");
264 void** addr = &(std_heap->breakval);
265 return mc_snapshot_read_pointer(addr, snapshot, MC_ANY_PROCESS_INDEX);
268 /** @brief Read memory from a snapshot region
270 * @param addr Process (non-snapshot) address of the data
271 * @param region Snapshot memory region where the data is located
272 * @param target Buffer to store the value
273 * @param size Size of the data to read in bytes
274 * @return Pointer where the data is located (target buffer of original location)
276 static inline __attribute__((always_inline))
277 void* mc_snapshot_read_region(void* addr, mc_mem_region_t region, void* target, size_t size)
282 uintptr_t offset = (char*) addr - (char*) region->start_addr;
284 xbt_assert(mc_region_contain(region, addr),
285 "Trying to read out of the region boundary.");
287 // Linear memory region:
289 return (char*) region->data + offset;
292 // Fragmented memory region:
293 else if (region->page_numbers) {
294 // Last byte of the region:
295 void* end = (char*) addr + size - 1;
296 if( mc_same_page(addr, end) ) {
297 // The memory is contained in a single page:
298 return mc_translate_address_region((uintptr_t) addr, region);
300 // The memory spans several pages:
301 return mc_snapshot_read_fragmented(addr, region, target, size);
306 xbt_die("No data available for this region");
310 static inline __attribute__ ((always_inline))
311 void* mc_snapshot_read_pointer_region(void* addr, mc_mem_region_t region)
314 return *(void**) mc_snapshot_read_region(addr, region, &res, sizeof(void*));