1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "mc_private.h"
8 #include "xbt/module.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
12 "Logging specific to mc_checkpoint");
14 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
15 static void MC_region_restore(mc_mem_region_t reg);
16 static void MC_region_destroy(mc_mem_region_t reg);
18 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
20 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
22 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
24 new_reg->start_addr = start_addr;
26 new_reg->data = xbt_malloc0(size);
27 XBT_DEBUG("New reg data %p, start_addr %p, size %zu", new_reg->data, start_addr, size);
28 memcpy(new_reg->data, start_addr, size);
33 static void MC_region_restore(mc_mem_region_t reg)
35 /*FIXME: check if start_addr is still mapped, if it is not, then map it
36 before copying the data */
38 memory_map_t maps = get_memory_map();
42 while(i < maps->mapsize){
44 if (maps->regions[i].pathname != NULL){
45 if (!memcmp(maps->regions[i].pathname, "[stack]", 7)){
46 size_t diff = (char*)reg->start_addr - (char*)r.start_addr;
47 void *segment = malloc(reg->size + diff);
48 XBT_DEBUG("Size of segment : %zu", sizeof(segment));
49 memcpy((char *)segment + diff, reg->data, reg->size);
50 memcpy(r.start_addr, segment, sizeof(segment));
51 XBT_DEBUG("Memcpy region ok");
58 XBT_DEBUG("Memcpy : dest %p, src %p, size %zu", reg->start_addr, reg->data, reg->size);
59 memcpy(reg->start_addr, reg->data, reg->size);
65 static void MC_region_destroy(mc_mem_region_t reg)
71 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
75 XBT_DEBUG("New region heap (%zu)", size);
78 XBT_DEBUG("New region libsimgrid (%zu)", size);
81 XBT_DEBUG("New region program data (%zu)", size);
84 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
85 snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
86 snapshot->regions[snapshot->num_reg] = new_reg;
91 void MC_take_snapshot(mc_snapshot_t snapshot)
95 memory_map_t maps = get_memory_map();
97 /* Save the std heap and the writable mapped pages of libsimgrid */
98 while (i < maps->mapsize) {
99 reg = maps->regions[i];
100 if ((reg.prot & PROT_WRITE)){
101 if (maps->regions[i].pathname == NULL){
102 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
103 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
106 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
107 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
114 /* FIXME: free the memory map */
117 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
121 memory_map_t maps = get_memory_map();
123 for(i=0; i< snapshot->num_reg; i++){
124 MC_region_destroy(snapshot->regions[i]);
127 snapshot->num_reg = 0;
131 /* Save the std heap and the writable mapped pages of libsimgrid */
132 while (i < maps->mapsize) {
133 reg = maps->regions[i];
134 if ((reg.prot & PROT_WRITE)){
135 if (maps->regions[i].pathname == NULL){
136 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
137 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
140 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
141 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
143 if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
144 MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
152 /* FIXME: free the memory map */
155 void MC_restore_snapshot(mc_snapshot_t snapshot)
158 for(i=0; i < snapshot->num_reg; i++){
159 MC_region_restore(snapshot->regions[i]);
160 switch(snapshot->regions[i]->type){
162 XBT_DEBUG("heap restored");
165 XBT_DEBUG("libsimgrid (data) restored");
168 XBT_DEBUG("data program restored");
176 void MC_free_snapshot(mc_snapshot_t snapshot)
179 for(i=0; i < snapshot->num_reg; i++)
180 MC_region_destroy(snapshot->regions[i]);