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. */
10 #include <sys/types.h> // off_t
11 #include <stdint.h> // size_t
13 #include <simgrid_config.h>
14 #include "../xbt/mmalloc/mmprivate.h"
15 #include <xbt/asserts.h>
16 #include <xbt/dynar.h>
18 #include "mc_forward.h"
19 #include "mc_model_checker.h"
20 #include "mc_page_store.h"
21 #include "mc_mmalloc.h"
25 void mc_softdirty_reset(void);
27 // ***** Snapshot region
29 #define NB_REGIONS 3 /* binary data (data + BSS) (type = 2), libsimgrid data (data + BSS) (type = 1), std_heap (type = 0)*/
31 /** @brief Copy/snapshot of a given memory region
33 * Two types of region snapshots exist:
35 * <li>flat/dense snapshots are a simple copy of the region;</li>
36 * <li>sparse/per-page snapshots are snaapshots which shared
37 * identical pages.</li>
40 typedef struct s_mc_mem_region{
41 /** @brief Virtual address of the region in the simulated process */
44 /** @brief Permanent virtual address of the region
46 * This is usually the same address as the simuilated process address.
47 * However, when using SMPI privatization of global variables,
48 * each SMPI process has its own set of global variables stored
49 * at a different virtual address. The scheduler maps those region
50 * on the region of the global variables.
55 /** @brief Copy of the snapshot for flat snapshots regions (NULL otherwise) */
58 /** @brief Size of the data region in bytes */
61 /** @brief Pages indices in the page store for per-page snapshots (NULL otherwise) */
64 } s_mc_mem_region_t, *mc_mem_region_t;
66 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);
67 void MC_region_destroy(mc_mem_region_t reg);
68 void mc_region_restore_sparse(mc_mem_region_t reg, mc_mem_region_t ref_reg);
70 static inline __attribute__ ((always_inline))
71 bool mc_region_contain(mc_mem_region_t region, void* p)
73 return p >= region->start_addr &&
74 p < (void*)((char*) region->start_addr + region->size);
77 static inline __attribute__((always_inline))
78 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region)
80 size_t pageno = mc_page_number(region->start_addr, (void*) addr);
81 size_t snapshot_pageno = region->page_numbers[pageno];
82 const void* snapshot_page = mc_page_store_get_page(mc_model_checker->pages, snapshot_pageno);
83 return (char*) snapshot_page + mc_page_offset((void*) addr);
86 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int process_index);
88 /** \brief Translate a pointer from process address space to snapshot address space
90 * The address space contains snapshot of the main/application memory:
91 * this function finds the address in a given snaphot for a given
92 * real/application address.
94 * For read only memory regions and other regions which are not int the
95 * snapshot, the address is not changed.
97 * \param addr Application address
98 * \param snapshot The snapshot of interest (if NULL no translation is done)
99 * \return Translated address in the snapshot address space
101 static inline __attribute__((always_inline))
102 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot, int process_index)
105 // If not in a process state/clone:
107 return (uintptr_t *) addr;
110 mc_mem_region_t region = mc_get_snapshot_region((void*) addr, snapshot, process_index);
112 xbt_assert(mc_region_contain(region, (void*) addr), "Trying to read out of the region boundary.");
115 return (void *) addr;
119 else if (region->data) {
120 uintptr_t offset = addr - (uintptr_t) region->start_addr;
121 return (void *) ((uintptr_t) region->data + offset);
124 // Per-page snapshot:
125 else if (region->page_numbers) {
126 return mc_translate_address_region(addr, region);
130 xbt_die("No data for this memory region");
138 * Some parts of the snapshot are ignored by zeroing them out: the real
139 * values is stored here.
141 typedef struct s_mc_snapshot_ignored_data {
145 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
147 typedef struct s_fd_infos{
150 off_t current_position;
152 }s_fd_infos_t, *fd_infos_t;
154 struct s_mc_snapshot{
155 size_t heap_bytes_used;
156 mc_mem_region_t regions[NB_REGIONS];
157 xbt_dynar_t enabled_processes;
158 mc_mem_region_t* privatization_regions;
159 int privatization_index;
162 xbt_dynar_t to_ignore;
164 xbt_dynar_t ignored_data;
166 fd_infos_t *current_fd;
169 /** @brief Process index used when no process is available
171 * The expected behaviour is that if a process index is needed it will fail.
173 #define MC_NO_PROCESS_INDEX -1
175 /** @brief Process index when any process is suitable
177 * We could use a special negative value in the future.
179 #define MC_ANY_PROCESS_INDEX 0
181 static inline __attribute__ ((always_inline))
182 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
184 if (mc_region_contain(region, addr))
187 return mc_get_snapshot_region(addr, snapshot, process_index);
190 /** Information about a given stack frame
193 typedef struct s_mc_stack_frame {
194 /** Instruction pointer */
198 unw_word_t frame_base;
201 unw_cursor_t unw_cursor;
202 } s_mc_stack_frame_t, *mc_stack_frame_t;
204 typedef struct s_mc_snapshot_stack{
205 xbt_dynar_t local_variables;
206 xbt_dynar_t stack_frames; // mc_stack_frame_t
208 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
210 typedef struct s_mc_global_t{
211 mc_snapshot_t snapshot;
215 int initial_communications_pattern_done;
216 int comm_deterministic;
217 int send_deterministic;
218 }s_mc_global_t, *mc_global_t;
220 typedef struct s_mc_checkpoint_ignore_region{
223 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
225 static void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
227 mc_snapshot_t MC_take_snapshot(int num_state);
228 void MC_restore_snapshot(mc_snapshot_t);
229 void MC_free_snapshot(mc_snapshot_t);
231 int mc_important_snapshot(mc_snapshot_t snapshot);
233 size_t* mc_take_page_snapshot_region(void* data, size_t page_count, uint64_t* pagemap, size_t* reference_pages);
234 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
235 void mc_restore_page_snapshot_region(void* start_addr, size_t page_count, size_t* pagenos, uint64_t* pagemap, size_t* reference_pagenos);
237 static inline __attribute__((always_inline))
238 bool mc_snapshot_region_linear(mc_mem_region_t region) {
239 return !region || !region->data;
242 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size);
244 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size);
245 int mc_snapshot_region_memcmp(
246 void* addr1, mc_mem_region_t region1,
247 void* addr2, mc_mem_region_t region2, size_t size);
248 int mc_snapshot_memcmp(
249 void* addr1, mc_snapshot_t snapshot1,
250 void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
252 static void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot, int process_index);
254 static inline __attribute__ ((always_inline))
255 void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot, int process_index)
258 return *(void**) mc_snapshot_read(addr, snapshot, process_index, &res, sizeof(void*));
261 static inline __attribute__ ((always_inline))
262 void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot) {
264 xbt_die("snapshot is NULL");
265 void** addr = &(std_heap->breakval);
266 return mc_snapshot_read_pointer(addr, snapshot, MC_ANY_PROCESS_INDEX);
269 /** @brief Read memory from a snapshot region
271 * @param addr Process (non-snapshot) address of the data
272 * @param region Snapshot memory region where the data is located
273 * @param target Buffer to store the value
274 * @param size Size of the data to read in bytes
275 * @return Pointer where the data is located (target buffer of original location)
277 static inline __attribute__((always_inline))
278 void* mc_snapshot_read_region(void* addr, mc_mem_region_t region, void* target, size_t size)
283 uintptr_t offset = (char*) addr - (char*) region->start_addr;
285 xbt_assert(mc_region_contain(region, addr),
286 "Trying to read out of the region boundary.");
288 // Linear memory region:
290 return (char*) region->data + offset;
293 // Fragmented memory region:
294 else if (region->page_numbers) {
295 // Last byte of the region:
296 void* end = (char*) addr + size - 1;
297 if( mc_same_page(addr, end) ) {
298 // The memory is contained in a single page:
299 return mc_translate_address_region((uintptr_t) addr, region);
301 // The memory spans several pages:
302 return mc_snapshot_read_fragmented(addr, region, target, size);
307 xbt_die("No data available for this region");
311 static inline __attribute__ ((always_inline))
312 void* mc_snapshot_read_pointer_region(void* addr, mc_mem_region_t region)
315 return *(void**) mc_snapshot_read_region(addr, region, &res, sizeof(void*));