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"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
11 "Logging specific to mc_checkpoint");
13 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
14 static void MC_region_restore(mc_mem_region_t reg);
15 static void MC_region_destroy(mc_mem_region_t reg);
17 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
19 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
21 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
23 new_reg->start_addr = start_addr;
25 new_reg->data = xbt_malloc0(size);
26 XBT_DEBUG("New reg data %p, start_addr %p, size %zu", new_reg->data, start_addr, size);
27 memcpy(new_reg->data, start_addr, size);
32 static void MC_region_restore(mc_mem_region_t reg)
34 /*FIXME: check if start_addr is still mapped, if it is not, then map it
35 before copying the data */
37 memory_map_t maps = get_memory_map();
41 while(i < maps->mapsize){
43 if (maps->regions[i].pathname != NULL){
44 if (!memcmp(maps->regions[i].pathname, "[stack]", 7)){
45 size_t diff = (char*)reg->start_addr - (char*)r.start_addr;
46 void *segment = malloc(reg->size + diff);
47 XBT_DEBUG("Size of segment : %zu", sizeof(segment));
48 memcpy((char *)segment + diff, reg->data, reg->size);
49 memcpy(r.start_addr, segment, sizeof(segment));
50 XBT_DEBUG("Memcpy region ok");
57 XBT_DEBUG("Memcpy : dest %p, src %p, size %zu", reg->start_addr, reg->data, reg->size);
58 memcpy(reg->start_addr, reg->data, reg->size);
64 static void MC_region_destroy(mc_mem_region_t reg)
70 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
74 XBT_DEBUG("New region heap (%zu)", size);
77 XBT_DEBUG("New region libsimgrid (%zu)", size);
80 XBT_DEBUG("New region program data (%zu)", size);
83 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
84 snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
85 snapshot->regions[snapshot->num_reg] = new_reg;
90 void MC_take_snapshot(mc_snapshot_t snapshot)
94 memory_map_t maps = get_memory_map();
96 /* Save the std heap and the writable mapped pages of libsimgrid */
97 while (i < maps->mapsize) {
98 reg = maps->regions[i];
99 if ((reg.prot & PROT_WRITE)){
100 if (maps->regions[i].pathname == NULL){
101 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
102 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
105 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
106 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
113 /* FIXME: free the memory map */
116 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
120 memory_map_t maps = get_memory_map();
122 for(i=0; i< snapshot->num_reg; i++){
123 MC_region_destroy(snapshot->regions[i]);
126 snapshot->num_reg = 0;
130 /* Save the std heap and the writable mapped pages of libsimgrid */
131 while (i < maps->mapsize) {
132 reg = maps->regions[i];
133 if ((reg.prot & PROT_WRITE)){
134 if (maps->regions[i].pathname == NULL){
135 if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
136 MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
139 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
140 MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
142 if (!memcmp(basename(maps->regions[i].pathname), basename(prog_name), strlen(basename(prog_name)))){
143 MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
151 /* FIXME: free the memory map */
154 void MC_restore_snapshot(mc_snapshot_t snapshot)
157 for(i=0; i < snapshot->num_reg; i++){
158 MC_region_restore(snapshot->regions[i]);
159 switch(snapshot->regions[i]->type){
161 XBT_DEBUG("heap restored");
164 XBT_DEBUG("libsimgrid (data) restored");
167 XBT_DEBUG("data program restored");
175 void MC_free_snapshot(mc_snapshot_t snapshot)
178 for(i=0; i < snapshot->num_reg; i++)
179 MC_region_destroy(snapshot->regions[i]);