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 "ModelChecker.hpp"
20 #include "mc_page_store.h"
21 #include "mc_mmalloc.h"
22 #include "mc_address_space.h"
27 // ***** Snapshot region
29 typedef enum e_mc_region_type_t {
30 MC_REGION_TYPE_UNKNOWN = 0,
31 MC_REGION_TYPE_HEAP = 1,
32 MC_REGION_TYPE_DATA = 2
35 // TODO, use OO instead of this
36 typedef enum e_mc_region_storeage_type_t {
37 MC_REGION_STORAGE_TYPE_NONE = 0,
38 MC_REGION_STORAGE_TYPE_FLAT = 1,
39 MC_REGION_STORAGE_TYPE_CHUNKED = 2,
40 MC_REGION_STORAGE_TYPE_PRIVATIZED = 3
41 } mc_region_storage_type_t;
43 /** @brief Copy/snapshot of a given memory region
45 * Different types of region snapshot storage types exist:
47 * <li>flat/dense snapshots are a simple copy of the region;</li>
48 * <li>sparse/per-page snapshots are snaapshots which shared
49 * identical pages.</li>
50 * <li>privatized (SMPI global variable privatisation).
53 * This is handled with a variant based approch:
55 * * `storage_type` identified the type of storage;
56 * * an anonymous enum is used to distinguish the relevant types for
59 typedef struct s_mc_mem_region s_mc_mem_region_t, *mc_mem_region_t;
61 struct s_mc_mem_region {
62 mc_region_type_t region_type;
63 mc_region_storage_type_t storage_type;
64 mc_object_info_t object_info;
66 /** @brief Virtual address of the region in the simulated process */
69 /** @brief Size of the data region in bytes */
72 /** @brief Permanent virtual address of the region
74 * This is usually the same address as the simuilated process address.
75 * However, when using SMPI privatization of global variables,
76 * each SMPI process has its own set of global variables stored
77 * at a different virtual address. The scheduler maps those region
78 * on the region of the global variables.
85 /** @brief Copy of the snapshot for flat snapshots regions (NULL otherwise) */
89 /** @brief Pages indices in the page store for per-page snapshots (NULL otherwise) */
94 mc_mem_region_t* regions;
100 mc_mem_region_t mc_region_new_sparse(
101 mc_region_type_t type, void *start_addr, void* data_addr, size_t size);
102 void MC_region_destroy(mc_mem_region_t reg);
103 void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
105 static inline __attribute__ ((always_inline))
106 bool mc_region_contain(mc_mem_region_t region, const void* p)
108 return p >= region->start_addr &&
109 p < (void*)((char*) region->start_addr + region->size);
112 static inline __attribute__((always_inline))
113 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region)
115 size_t pageno = mc_page_number(region->start_addr, (void*) addr);
116 size_t snapshot_pageno = region->chunked.page_numbers[pageno];
117 const void* snapshot_page = mc_page_store_get_page(
118 &mc_model_checker->page_store(), snapshot_pageno);
119 return (char*) snapshot_page + mc_page_offset((void*) addr);
122 mc_mem_region_t mc_get_snapshot_region(const void* addr, mc_snapshot_t snapshot, int process_index);
124 /** \brief Translate a pointer from process address space to snapshot address space
126 * The address space contains snapshot of the main/application memory:
127 * this function finds the address in a given snaphot for a given
128 * real/application address.
130 * For read only memory regions and other regions which are not int the
131 * snapshot, the address is not changed.
133 * \param addr Application address
134 * \param snapshot The snapshot of interest (if NULL no translation is done)
135 * \return Translated address in the snapshot address space
137 static inline __attribute__((always_inline))
138 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot, int process_index)
141 // If not in a process state/clone:
143 return (uintptr_t *) addr;
146 mc_mem_region_t region = mc_get_snapshot_region((void*) addr, snapshot, process_index);
148 xbt_assert(mc_region_contain(region, (void*) addr), "Trying to read out of the region boundary.");
151 return (void *) addr;
154 switch (region->storage_type) {
155 case MC_REGION_STORAGE_TYPE_NONE:
157 xbt_die("Storage type not supported");
159 case MC_REGION_STORAGE_TYPE_FLAT:
161 uintptr_t offset = addr - (uintptr_t) region->start_addr;
162 return (void *) ((uintptr_t) region->flat.data + offset);
165 case MC_REGION_STORAGE_TYPE_CHUNKED:
166 return mc_translate_address_region(addr, region);
168 case MC_REGION_STORAGE_TYPE_PRIVATIZED:
170 xbt_assert(process_index >=0,
171 "Missing process index for privatized region");
172 xbt_assert((size_t) process_index < region->privatized.regions_count,
173 "Out of range process index");
174 mc_mem_region_t subregion = region->privatized.regions[process_index];
175 xbt_assert(subregion, "Missing memory region for process %i", process_index);
176 return mc_translate_address(addr, snapshot, process_index);
185 * Some parts of the snapshot are ignored by zeroing them out: the real
186 * values is stored here.
188 typedef struct s_mc_snapshot_ignored_data {
192 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
194 typedef struct s_fd_infos{
197 off_t current_position;
199 }s_fd_infos_t, *fd_infos_t;
201 struct s_mc_snapshot {
202 mc_process_t process;
204 s_mc_address_space_t address_space;
205 size_t heap_bytes_used;
206 mc_mem_region_t* snapshot_regions;
207 size_t snapshot_regions_count;
208 xbt_dynar_t enabled_processes;
209 int privatization_index;
212 xbt_dynar_t to_ignore;
214 xbt_dynar_t ignored_data;
216 fd_infos_t *current_fd;
219 static inline __attribute__ ((always_inline))
220 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
222 if (mc_region_contain(region, addr))
225 return mc_get_snapshot_region(addr, snapshot, process_index);
228 /** Information about a given stack frame
231 typedef struct s_mc_stack_frame {
232 /** Instruction pointer */
236 unw_word_t frame_base;
239 unw_cursor_t unw_cursor;
240 } s_mc_stack_frame_t, *mc_stack_frame_t;
242 typedef struct s_mc_snapshot_stack{
243 xbt_dynar_t local_variables;
244 mc_unw_context_t context;
245 xbt_dynar_t stack_frames; // mc_stack_frame_t
247 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
249 typedef struct s_mc_global_t {
250 mc_snapshot_t snapshot;
254 int initial_communications_pattern_done;
255 int recv_deterministic;
256 int send_deterministic;
259 }s_mc_global_t, *mc_global_t;
261 typedef struct s_mc_checkpoint_ignore_region{
264 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
266 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
268 mc_snapshot_t MC_take_snapshot(int num_state);
269 void MC_restore_snapshot(mc_snapshot_t);
270 void MC_free_snapshot(mc_snapshot_t);
272 size_t* mc_take_page_snapshot_region(mc_process_t process,
273 void* data, size_t page_count);
274 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
275 void mc_restore_page_snapshot_region(
276 mc_process_t process,
277 void* start_addr, size_t page_count, size_t* pagenos);
279 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, size_t size);
281 const void* MC_snapshot_read(mc_snapshot_t snapshot, adress_space_read_flags_t flags,
282 void* target, const void* addr, size_t size, int process_index);
283 int MC_snapshot_region_memcmp(
284 const void* addr1, mc_mem_region_t region1,
285 const void* addr2, mc_mem_region_t region2, size_t size);
286 int MC_snapshot_memcmp(
287 const void* addr1, mc_snapshot_t snapshot1,
288 const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
290 static inline __attribute__ ((always_inline))
291 const void* MC_snapshot_read_pointer(mc_snapshot_t snapshot, const void* addr, int process_index)
294 return *(const void**) MC_snapshot_read(snapshot, MC_ADDRESS_SPACE_READ_FLAGS_LAZY,
295 &res, addr, sizeof(void*), process_index);
298 static inline __attribute__ ((always_inline))
299 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
302 xbt_die("snapshot is NULL");
303 return MC_process_get_heap(&mc_model_checker->process())->breakval;
306 /** @brief Read memory from a snapshot region
308 * @param addr Process (non-snapshot) address of the data
309 * @param region Snapshot memory region where the data is located
310 * @param target Buffer to store the value
311 * @param size Size of the data to read in bytes
312 * @return Pointer where the data is located (target buffer of original location)
314 static inline __attribute__((always_inline))
315 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
318 // Should be deprecated:
321 uintptr_t offset = (char*) addr - (char*) region->start_addr;
323 xbt_assert(mc_region_contain(region, addr),
324 "Trying to read out of the region boundary.");
326 switch (region->storage_type) {
327 case MC_REGION_STORAGE_TYPE_NONE:
329 xbt_die("Storage type not supported");
331 case MC_REGION_STORAGE_TYPE_FLAT:
332 return (char*) region->flat.data + offset;
334 case MC_REGION_STORAGE_TYPE_CHUNKED:
336 // Last byte of the region:
337 void* end = (char*) addr + size - 1;
338 if (mc_same_page(addr, end) ) {
339 // The memory is contained in a single page:
340 return mc_translate_address_region((uintptr_t) addr, region);
342 // The memory spans several pages:
343 return MC_region_read_fragmented(region, target, addr, size);
347 // We currently do not pass the process_index to this function so we assume
348 // that the privatized region has been resolved in the callers:
349 case MC_REGION_STORAGE_TYPE_PRIVATIZED:
350 xbt_die("Storage type not supported");
354 static inline __attribute__ ((always_inline))
355 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
358 return *(void**) MC_region_read(region, &res, addr, sizeof(void*));